Files
myep-team/tests/Security/UserCheckerTest.php
T
2026-08-11 12:26:13 +02:00

70 lines
2.0 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Tests\Security;
use App\Entity\User;
use App\Security\UserChecker;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Security\Core\Exception\CustomUserMessageAccountStatusException;
class UserCheckerTest extends TestCase
{
private UserChecker $userChecker;
protected function setUp(): void
{
$this->userChecker = new UserChecker();
}
public function testDeletedUserIsRefused(): void
{
$user = (new User())->setRoles(['ROLE_TEAMER']);
$user->setDeleted();
$this->expectException(CustomUserMessageAccountStatusException::class);
$this->expectExceptionMessageMatches('/gelöscht/');
$this->userChecker->checkPreAuth($user);
}
/**
* A deletion is the stronger statement, so its message has to win over the block
* message when an account carries both.
*/
public function testDeletedAndDisabledUserIsRefusedWithTheDeletionMessage(): void
{
$user = (new User())->setRoles(['ROLE_TEAMER']);
$user->setDisabled(true);
$user->setDisabledReason('Disziplinarisch gesperrt');
$user->setDeleted();
$this->expectException(CustomUserMessageAccountStatusException::class);
$this->expectExceptionMessageMatches('/gelöscht/');
$this->userChecker->checkPreAuth($user);
}
public function testDisabledUserStillGetsTheBlockMessage(): void
{
$user = (new User())->setRoles(['ROLE_TEAMER']);
$user->setDisabled(true);
$user->setDisabledReason('Disziplinarisch gesperrt');
$this->expectException(CustomUserMessageAccountStatusException::class);
$this->expectExceptionMessage('Dein Account wurde gesperrt: Disziplinarisch gesperrt');
$this->userChecker->checkPreAuth($user);
}
public function testActiveUserWithValidRolePasses(): void
{
$user = (new User())->setRoles(['ROLE_TEAMER']);
$this->userChecker->checkPreAuth($user);
$this->assertFalse($user->isBlocked());
}
}