Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: added check for organisations with no op admin VOL-5955 #503

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,21 +1,10 @@
<?php

/**
* Organisation
*
* @author Rob Caiger <[email protected]>
*/

namespace Dvsa\Olcs\Api\Domain\QueryHandler\Organisation;

use Dvsa\Olcs\Api\Domain\QueryHandler\AbstractQueryHandler;
use Dvsa\Olcs\Transfer\Query\QueryInterface;

/**
* Organisation
*
* @author Rob Caiger <[email protected]>
*/
class Organisation extends AbstractQueryHandler
{
protected $repoServiceName = 'Organisation';
Expand All @@ -37,6 +26,7 @@ public function handleQuery(QueryInterface $query)
'isDisqualified' => $organisation->getDisqualifications()->count() > 0,
'taValueOptions' => $this->getTrafficAreaValueOptions($allowedOperatorLocation),
'allowedOperatorLocation' => $allowedOperatorLocation,
'hasOperatorAdmin' => $organisation->hasOperatorAdmin()
]
);
}
Expand Down
14 changes: 14 additions & 0 deletions app/api/module/Api/src/Entity/Organisation/Organisation.php
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,20 @@ public function getAdministratorUsers()
return $this->organisationUsers->matching($criteria);
}

public function hasOperatorAdmin(): bool
{
$administratorUsers = $this->getAdministratorUsers();

/** @var OrganisationUserEntity $orgUser */
foreach ($administratorUsers as $orgUser) {
if ($orgUser->getUser()->isOperatorAdministrator()) {
return true;
}
}

return false;
}

/**
* can only delete operator admin if more than one exists
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
<?php

/**
* Organisation Test
*
* @author Rob Caiger <[email protected]>
*/
declare(strict_types=1);

namespace Dvsa\OlcsTest\Api\Domain\QueryHandler\Organisation;

Expand All @@ -14,13 +10,7 @@
use Dvsa\Olcs\Api\Domain\Repository\TrafficArea as TrafficAreaRepo;
use Dvsa\Olcs\Transfer\Query\Organisation\Organisation as Qry;
use Mockery as m;
use SAML2\Utilities\ArrayCollection;

/**
* Organisation Test
*
* @author Rob Caiger <[email protected]>
*/
class OrganisationTest extends QueryHandlerTestCase
{
public function setUp(): void
Expand All @@ -32,14 +22,15 @@ public function setUp(): void
parent::setUp();
}

public function testHandleQueryDisqualified()
public function testHandleQueryDisqualified(): void
{
$query = Qry::create(['id' => 111]);

$mockOrganisation = m::mock(\Dvsa\Olcs\Api\Entity\Organisation\Organisation::class)->makePartial();
$mockOrganisation->shouldReceive('serialize')->andReturn(['foo' => 'bar']);
$mockOrganisation->shouldReceive('getDisqualifications->count')->andReturn(2);
$mockOrganisation->shouldReceive('getAllowedOperatorLocation')->andReturn('GB')->once();
$mockOrganisation->expects('hasOperatorAdmin')->withNoArgs()->andReturnTrue();

$mockTa = m::mock()
->shouldReceive('getId')
Expand All @@ -64,20 +55,22 @@ public function testHandleQueryDisqualified()
'foo' => 'bar',
'isDisqualified' => true,
'allowedOperatorLocation' => 'GB',
'hasOperatorAdmin' => true,
'taValueOptions' => [1 => 'foo'],
];

$this->assertEquals($expected, $this->sut->handleQuery($query)->serialize());
}

public function testHandleQueryNotDisqualified()
public function testHandleQueryNotDisqualified(): void
{
$query = Qry::create(['id' => 111]);

$mockOrganisation = m::mock(\Dvsa\Olcs\Api\Entity\Organisation\Organisation::class)->makePartial();
$mockOrganisation->shouldReceive('serialize')->andReturn(['foo' => 'bar']);
$mockOrganisation->shouldReceive('getDisqualifications->count')->andReturn(0);
$mockOrganisation->shouldReceive('getAllowedOperatorLocation')->andReturn('GB')->once();
$mockOrganisation->expects('hasOperatorAdmin')->withNoArgs()->andReturnFalse();

$mockTa = m::mock()
->shouldReceive('getId')
Expand All @@ -102,6 +95,7 @@ public function testHandleQueryNotDisqualified()
'foo' => 'bar',
'isDisqualified' => false,
'allowedOperatorLocation' => 'GB',
'hasOperatorAdmin' => false,
'taValueOptions' => [1 => 'foo'],
];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,50 @@ public function testCanDeleteOperatorAdminFalse(): void
$this->assertFalse($entity->canDeleteOperatorAdmin());
}

/**
* first user not op admin, 2nd user is op admin, 3rd user doesn't need to be checked
*/
public function testHasOperatorAdminTrue(): void
{
$entity = new Entity();

$user1 = m::mock(OrganisationUser::class)->makePartial();
$user1->setIsAdministrator('Y');
$user1->expects('getUser->isOperatorAdministrator')->withNoArgs()->andReturnFalse();

$user2 = m::mock(OrganisationUser::class)->makePartial();
$user2->setIsAdministrator('Y');
$user2->expects('getUser->isOperatorAdministrator')->withNoArgs()->andReturnTrue();

$user3 = m::mock(OrganisationUser::class)->makePartial();
$user3->setIsAdministrator('Y');
$user3->expects('getUser->isOperatorAdministrator')->never();

$entity->setOrganisationUsers(new ArrayCollection([$user1, $user2, $user3]));

$this->assertTrue($entity->hasOperatorAdmin());
}

/**
* no operator admins are found
*/
public function testHasOperatorAdminFalse(): void
{
$entity = new Entity();

$user1 = m::mock(OrganisationUser::class)->makePartial();
$user1->setIsAdministrator('Y');
$user1->expects('getUser->isOperatorAdministrator')->withNoArgs()->andReturnFalse();

$user2 = m::mock(OrganisationUser::class)->makePartial();
$user2->setIsAdministrator('Y');
$user2->expects('getUser->isOperatorAdministrator')->withNoArgs()->andReturnFalse();

$entity->setOrganisationUsers(new ArrayCollection([$user1, $user2]));

$this->assertFalse($entity->hasOperatorAdmin());
}

/**
* test if org user not found due to soft delete
*/
Expand Down
Loading