From 0d15888a25b6d60c5187c8864307c255c16466b6 Mon Sep 17 00:00:00 2001 From: Andy Newton Date: Mon, 9 Sep 2024 07:34:18 +0100 Subject: [PATCH] # This is a combination of 2 commits. # This is the 1st commit message: fix: Operator Admins no longer get operator-tc Role assigned when being created. # This is the commit message #2: chore: phpunit test config --- .idea/php.xml | 2 +- app/api/module/Api/src/Entity/User/User.php | 30 ++++++++--- .../Api/src/Entity/User/UserEntityTest.php | 50 +++++++++++++++---- 3 files changed, 66 insertions(+), 16 deletions(-) diff --git a/.idea/php.xml b/.idea/php.xml index 925aae17c1..a57ae1ebdc 100644 --- a/.idea/php.xml +++ b/.idea/php.xml @@ -591,4 +591,4 @@ - \ No newline at end of file + diff --git a/app/api/module/Api/src/Entity/User/User.php b/app/api/module/Api/src/Entity/User/User.php index fb5bdd1d79..824a16087d 100644 --- a/app/api/module/Api/src/Entity/User/User.php +++ b/app/api/module/Api/src/Entity/User/User.php @@ -3,14 +3,13 @@ namespace Dvsa\Olcs\Api\Entity\User; use Doctrine\Common\Collections\ArrayCollection; -use Doctrine\ORM\Mapping as ORM; use Dvsa\Olcs\Api\Domain\Exception\ValidationException; +use Dvsa\Olcs\Api\Entity\Application\Application as ApplicationEntity; +use Dvsa\Olcs\Api\Entity\Licence\Licence as LicenceEntity; use Dvsa\Olcs\Api\Entity\Organisation\Organisation; use Dvsa\Olcs\Api\Entity\Organisation\OrganisationUser as OrganisationUserEntity; use Dvsa\Olcs\Api\Entity\OrganisationProviderInterface; -use Dvsa\Olcs\Api\Entity\Licence\Licence as LicenceEntity; use Dvsa\Olcs\Api\Entity\User\Role as RoleEntity; -use Dvsa\Olcs\Api\Entity\Application\Application as ApplicationEntity; use Dvsa\Olcs\Api\Rbac\IdentityProviderInterface; /** @@ -39,6 +38,7 @@ class User extends AbstractUser implements OrganisationProviderInterface public const USER_TYPE_ANON = 'anon'; public const USER_TYPE_LOCAL_AUTHORITY = 'local-authority'; public const USER_TYPE_OPERATOR = 'operator'; + public const USER_TYPE_TC = 'operator-tc'; public const USER_TYPE_PARTNER = 'partner'; public const USER_TYPE_TRANSPORT_MANAGER = 'transport-manager'; @@ -57,14 +57,15 @@ class User extends AbstractUser implements OrganisationProviderInterface RoleEntity::ROLE_LOCAL_AUTHORITY_ADMIN, RoleEntity::ROLE_LOCAL_AUTHORITY_USER, ], - self::USER_TYPE_OPERATOR => [ + self::USER_TYPE_TC => [ RoleEntity::ROLE_OPERATOR_TC, + ], + self::USER_TYPE_OPERATOR => [ RoleEntity::ROLE_OPERATOR_ADMIN, RoleEntity::ROLE_OPERATOR_USER, RoleEntity::ROLE_OPERATOR_TM, ], self::USER_TYPE_TRANSPORT_MANAGER => [ - RoleEntity::ROLE_OPERATOR_TC, RoleEntity::ROLE_OPERATOR_ADMIN, RoleEntity::ROLE_OPERATOR_USER, RoleEntity::ROLE_OPERATOR_TM, @@ -96,8 +97,11 @@ class User extends AbstractUser implements OrganisationProviderInterface self::PERMISSION_ADMIN => [RoleEntity::ROLE_LOCAL_AUTHORITY_ADMIN], self::PERMISSION_USER => [RoleEntity::ROLE_LOCAL_AUTHORITY_USER], ], + self::USER_TYPE_TC => [ + self::PERMISSION_ADMIN => [RoleEntity::ROLE_OPERATOR_TC], + ], self::USER_TYPE_OPERATOR => [ - self::PERMISSION_ADMIN => [RoleEntity::ROLE_OPERATOR_ADMIN, RoleEntity::ROLE_OPERATOR_TC], + self::PERMISSION_ADMIN => [RoleEntity::ROLE_OPERATOR_ADMIN], self::PERMISSION_USER => [RoleEntity::ROLE_OPERATOR_USER], self::PERMISSION_TM => [RoleEntity::ROLE_OPERATOR_TM], ], @@ -415,6 +419,8 @@ public function getUserType() $this->userType = self::USER_TYPE_TRANSPORT_MANAGER; } elseif (isset($this->partnerContactDetails)) { $this->userType = self::USER_TYPE_PARTNER; + } elseif ($this->isTransportConsultant()) { + $this->userType = self::USER_TYPE_TC; } else { $this->userType = self::USER_TYPE_OPERATOR; } @@ -515,6 +521,18 @@ public function isInternal() return (self::USER_TYPE_INTERNAL === $this->getUserType()); } + /** + * Checks whether the user is of internal type + * + * @return bool + */ + public function isTransportConsultant() + { + return $this->hasRoles([RoleEntity::ROLE_OPERATOR_TC]); + } + + + /** * Get permission * diff --git a/app/api/test/module/Api/src/Entity/User/UserEntityTest.php b/app/api/test/module/Api/src/Entity/User/UserEntityTest.php index 7ae0272ca0..5a2f485a1d 100644 --- a/app/api/test/module/Api/src/Entity/User/UserEntityTest.php +++ b/app/api/test/module/Api/src/Entity/User/UserEntityTest.php @@ -5,20 +5,20 @@ namespace Dvsa\OlcsTest\Api\Entity\User; use Doctrine\Common\Collections\ArrayCollection; -use Dvsa\Olcs\Api\Entity\Organisation\OrganisationUser; -use Dvsa\Olcs\Api\Entity\User\User as UserEntity; -use Dvsa\Olcs\Api\Rbac\IdentityProviderInterface; -use Dvsa\OlcsTest\Api\Entity\Abstracts\EntityTester; +use Dvsa\Olcs\Api\Entity\Application\Application as ApplicationEntity; use Dvsa\Olcs\Api\Entity\Bus\LocalAuthority as LocalAuthorityEntity; use Dvsa\Olcs\Api\Entity\ContactDetails\ContactDetails as ContactDetailsEntity; +use Dvsa\Olcs\Api\Entity\Licence\Licence as LicenceEntity; use Dvsa\Olcs\Api\Entity\Organisation\Organisation as OrganisationEntity; +use Dvsa\Olcs\Api\Entity\Organisation\OrganisationUser; use Dvsa\Olcs\Api\Entity\Organisation\OrganisationUser as OrganisationUserEntity; -use Dvsa\Olcs\Api\Entity\Licence\Licence as LicenceEntity; -use Dvsa\Olcs\Api\Entity\Application\Application as ApplicationEntity; use Dvsa\Olcs\Api\Entity\Tm\TransportManager as TransportManagerEntity; use Dvsa\Olcs\Api\Entity\User\Role as RoleEntity; -use Dvsa\Olcs\Api\Entity\User\User as Entity; use Dvsa\Olcs\Api\Entity\User\Team as TeamEntity; +use Dvsa\Olcs\Api\Entity\User\User as Entity; +use Dvsa\Olcs\Api\Entity\User\User as UserEntity; +use Dvsa\Olcs\Api\Rbac\IdentityProviderInterface; +use Dvsa\OlcsTest\Api\Entity\Abstracts\EntityTester; use Mockery as m; use ReflectionClass; @@ -1405,8 +1405,40 @@ public function dpCanResetPassword(): array public function dpOperatorAdminRoles(): array { return [ - [RoleEntity::ROLE_OPERATOR_ADMIN], - [RoleEntity::ROLE_OPERATOR_TC], + [RoleEntity::ROLE_OPERATOR_ADMIN] ]; } + + public function testIsTransportConsultant() + { + $roleTC = m::mock(RoleEntity::class)->makePartial(); + $roleTC->setRole(RoleEntity::ROLE_OPERATOR_TC); + + $roleOther = m::mock(RoleEntity::class)->makePartial(); + $roleOther->setRole('some_other_role'); + + $userTC = new UserEntity('pid', UserEntity::USER_TYPE_OPERATOR); + $userTC->setRoles(new ArrayCollection([$roleTC])); + $this->assertTrue($userTC->isTransportConsultant(), 'User should be identified as a transport consultant'); + + $userNonTC = new UserEntity('pid', UserEntity::USER_TYPE_OPERATOR); + $userNonTC->setRoles(new ArrayCollection([$roleOther])); + $this->assertFalse($userNonTC->isTransportConsultant(), 'User should not be identified as a transport consultant'); + } + + public function testGetUserTypeForTransportConsultant() + { + $roleTC = m::mock(RoleEntity::class)->makePartial(); + $roleTC->setRole(RoleEntity::ROLE_OPERATOR_TC); + + $user = new UserEntity('pid', UserEntity::USER_TYPE_OPERATOR); + $user->setRoles(new ArrayCollection([$roleTC])); + + $reflectionClass = new ReflectionClass(UserEntity::class); + $property = $reflectionClass->getProperty('userType'); + $property->setAccessible(true); + $property->setValue($user, null); + + $this->assertEquals(UserEntity::USER_TYPE_TC, $user->getUserType(), 'User type should be set to transport consultant'); + } }