130 lines
3.6 KiB
PHP
130 lines
3.6 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Tests\Entity;
|
|
|
|
use App\Entity\User;
|
|
use PHPUnit\Framework\TestCase;
|
|
use Symfony\Component\Validator\ConstraintViolationListInterface;
|
|
use Symfony\Component\Validator\Validation;
|
|
|
|
class UserTest extends TestCase
|
|
{
|
|
public function testHotelCodeMatching(): void
|
|
{
|
|
$user = new User();
|
|
$user->setHotelCodes(['ABC', 'DEF', 'GHI']);
|
|
|
|
$isMatch = $user->hasHotelCodeMatch('ABCXXX');
|
|
$this->assertTrue($isMatch);
|
|
|
|
$isMatch = $user->hasHotelCodeMatch('XXXGHI');
|
|
$this->assertTrue($isMatch);
|
|
|
|
$isMatch = $user->hasHotelCodeMatch('XXDEFXX');
|
|
$this->assertFalse($isMatch);
|
|
}
|
|
|
|
public function testPendingMarkersAreLabelledButNotAssignable(): void
|
|
{
|
|
$user = (new User())->setRoles([
|
|
User::PENDING_ROLES['ROLE_ADMIN'],
|
|
User::PENDING_ROLES['ROLE_HOUSE_MANAGER'],
|
|
'ROLE_TEAMER',
|
|
]);
|
|
|
|
$this->assertSame(
|
|
['Admin (nicht freigeschaltet)', 'Hausleitung (nicht freigeschaltet)', 'Teamer'],
|
|
$user->getRolesLabels(),
|
|
);
|
|
$this->assertSame(['ROLE_TEAMER'], $user->getAssignedRoles());
|
|
$this->assertSame(
|
|
[User::PENDING_ROLES['ROLE_ADMIN'], User::PENDING_ROLES['ROLE_HOUSE_MANAGER']],
|
|
$user->getPendingRoles(),
|
|
);
|
|
}
|
|
|
|
public function testSuperAdminRequiresRoleAdmin(): void
|
|
{
|
|
$user = (new User())
|
|
->setRoles(['ROLE_MANAGER'])
|
|
->setSuperAdmin(true)
|
|
;
|
|
|
|
$violations = $this->validate($user);
|
|
|
|
$this->assertCount(1, $violations);
|
|
$this->assertSame('superAdmin', $violations[0]->getPropertyPath());
|
|
}
|
|
|
|
public function testSuperAdminWithRoleAdminIsValid(): void
|
|
{
|
|
$user = (new User())
|
|
->setRoles(['ROLE_ADMIN'])
|
|
->setSuperAdmin(true)
|
|
;
|
|
|
|
$this->assertCount(0, $this->validate($user));
|
|
}
|
|
|
|
public function testNonSuperAdminWithoutRoleAdminIsValid(): void
|
|
{
|
|
$user = (new User())
|
|
->setRoles(['ROLE_TEAMER'])
|
|
;
|
|
|
|
$this->assertCount(0, $this->validate($user));
|
|
}
|
|
|
|
public function testSetDisabledBlocksAndUnblocksTheAccount(): void
|
|
{
|
|
$user = new User();
|
|
|
|
$user->setDisabled(true);
|
|
|
|
$this->assertTrue($user->isDisabled());
|
|
$this->assertNotNull($user->getDisabledAt());
|
|
|
|
$user
|
|
->setDisabledReason('Gesperrt.')
|
|
->setDisabledReasonInternal('Siehe Vorgang 4711.')
|
|
->setDisabled(false)
|
|
;
|
|
|
|
$this->assertFalse($user->isDisabled());
|
|
$this->assertNull($user->getDisabledAt());
|
|
$this->assertNull($user->getDisabledReason());
|
|
$this->assertNull($user->getDisabledReasonInternal());
|
|
}
|
|
|
|
public function testSetDisabledKeepsTheOriginalTimestampWhenTheStateIsUnchanged(): void
|
|
{
|
|
$disabledAt = new \DateTimeImmutable('2026-01-01 08:00:00');
|
|
|
|
$user = (new User())
|
|
->setDisabledAt($disabledAt)
|
|
->setDisabledReason('Gesperrt.')
|
|
;
|
|
|
|
$user->setDisabled(true);
|
|
|
|
$this->assertSame($disabledAt, $user->getDisabledAt());
|
|
$this->assertSame('Gesperrt.', $user->getDisabledReason());
|
|
|
|
$enabled = new User();
|
|
$enabled->setDisabled(false);
|
|
|
|
$this->assertNull($enabled->getDisabledAt());
|
|
}
|
|
|
|
private function validate(User $user): ConstraintViolationListInterface
|
|
{
|
|
return Validation::createValidatorBuilder()
|
|
->enableAttributeMapping()
|
|
->getValidator()
|
|
->validate($user)
|
|
;
|
|
}
|
|
}
|