Skip to content

Commit

Permalink
Refactor FundingCaseContactsLoader
Browse files Browse the repository at this point in the history
Replace method `getContactsWithPermission()` with
`getContactsWithAnyPermission()`.
  • Loading branch information
Dominic Tubach committed Nov 21, 2024
1 parent a8376c4 commit cc94d86
Show file tree
Hide file tree
Showing 14 changed files with 47 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ private function getReviewerContactOptions(string $permission) {
return TRUE;
}

$contacts = $this->contactsLoader->getContactsWithPermission($fundingCase, $permission);
$contacts = $this->contactsLoader->getContactsWithAnyPermission($fundingCase, [$permission]);

/** @phpstan-ignore-next-line */
return array_map(fn (array $contact) => ContactUtil::getDisplayName($contact), $contacts);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use Civi\Funding\Api4\Util\ContactUtil;
use Civi\Funding\ApplicationProcess\ApplicationProcessManager;
use Civi\Funding\ClearingProcess\ClearingProcessManager;
use Civi\Funding\ClearingProcess\ClearingProcessPermissions;
use Civi\Funding\Entity\FundingCaseEntity;
use Civi\Funding\FundingCase\FundingCaseManager;
use Civi\Funding\Permission\FundingCase\FundingCaseContactsLoaderInterface;
Expand Down Expand Up @@ -68,10 +69,10 @@ protected function getRecords(): array {
$fields = parent::getRecords();
foreach ($fields as &$field) {
if ('reviewer_calc_contact_id' === $field['name']) {
$field['options'] = $this->getReviewerContactOptions('review_clearing_calculative');
$field['options'] = $this->getReviewerContactOptions(ClearingProcessPermissions::REVIEW_CALCULATIVE);
}
elseif ('reviewer_cont_contact_id' === $field['name']) {
$field['options'] = $this->getReviewerContactOptions('review_clearing_content');
$field['options'] = $this->getReviewerContactOptions(ClearingProcessPermissions::REVIEW_CONTENT);
}
}

Expand Down Expand Up @@ -204,7 +205,7 @@ private function getReviewerContactOptions(string $permission) {
return TRUE;
}

$contacts = $this->contactsLoader->getContactsWithPermission($fundingCase, $permission);
$contacts = $this->contactsLoader->getContactsWithAnyPermission($fundingCase, [$permission]);

/** @phpstan-ignore-next-line */
return array_map(fn (array $contact) => ContactUtil::getDisplayName($contact), $contacts);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@ private function assertPermission(FundingCaseEntity $fundingCase): void {

private function validateContactId(int $contactId, FundingCaseEntity $fundingCase): EntityValidationResult {
// @todo Make permission depend on funding case type.
$possibleReviewers = $this->fundingCaseContactsLoader->getContactsWithPermission(
$possibleReviewers = $this->fundingCaseContactsLoader->getContactsWithAnyPermission(
$fundingCase,
'review_calculative',
['review_calculative'],
);
if (isset($possibleReviewers[$contactId])) {
return EntityValidationResult::new();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,10 @@ private function assertPermission(FundingCaseEntity $fundingCase): void {

private function validateContactId(int $contactId, FundingCaseEntity $fundingCase): EntityValidationResult {
// @todo Make permission depend on funding case type.
$possibleReviewers = $this->fundingCaseContactsLoader->getContactsWithPermission($fundingCase, 'review_content');
$possibleReviewers = $this->fundingCaseContactsLoader->getContactsWithAnyPermission(
$fundingCase,
['review_content']
);
if (isset($possibleReviewers[$contactId])) {
return EntityValidationResult::new();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ public function setCalculativeReviewer(SetCalculativeReviewerAction $action): ar
}

private function validateContactId(int $contactId, FundingCaseEntity $fundingCase): void {
$possibleReviewers = $this->fundingCaseContactsLoader->getContactsWithPermission(
$possibleReviewers = $this->fundingCaseContactsLoader->getContactsWithAnyPermission(
$fundingCase,
ClearingProcessPermissions::REVIEW_CALCULATIVE,
[ClearingProcessPermissions::REVIEW_CALCULATIVE],
);

if (!isset($possibleReviewers[$contactId])) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ public function setContentReviewer(SetContentReviewerAction $action): array {
}

private function validateContactId(int $contactId, FundingCaseEntity $fundingCase): void {
$possibleReviewers = $this->fundingCaseContactsLoader->getContactsWithPermission(
$possibleReviewers = $this->fundingCaseContactsLoader->getContactsWithAnyPermission(
$fundingCase,
ClearingProcessPermissions::REVIEW_CONTENT,
[ClearingProcessPermissions::REVIEW_CONTENT],
);

if (!isset($possibleReviewers[$contactId])) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,15 @@ public function __construct(ContactRelationLoaderInterface $contactRelationLoade

/**
* @phpstan-param array<\Civi\Funding\Entity\FundingCaseContactRelationEntity> $contactRelations
* @phpstan-param non-empty-list<string> $permissions
*
* @phpstan-return array<int, array<string, mixed>>
* Contacts indexed by id.
*/
public function getContactsWithPermission(array $contactRelations, string $permission): array {
public function getContactsWithAnyPermission(array $contactRelations, array $permissions): array {
$contacts = [];
foreach ($contactRelations as $contactRelation) {
if (in_array($permission, $contactRelation->getPermissions(), TRUE)) {
if ([] !== array_intersect($permissions, $contactRelation->getPermissions())) {
$contacts += $this->contactRelationLoader->getContacts(
$contactRelation->getType(),
$contactRelation->getProperties()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ public function __construct(Api4Interface $api4, ContactsWithPermissionLoader $c
$this->contactsLoader = $contactsLoader;
}

public function getContactsWithPermission(FundingCaseEntity $fundingCase, string $permission): array {
public function getContactsWithAnyPermission(FundingCaseEntity $fundingCase, array $permissions): array {
$action = $this->api4->createGetAction(FundingCaseContactRelation::getEntityName())
->setCheckPermissions(FALSE)
->addWhere('funding_case_id', '=', $fundingCase->getId());
$result = $this->api4->executeAction($action);
$contactRelations = FundingCaseContactRelationEntity::allFromApiResult($result);

return $this->contactsLoader->getContactsWithPermission($contactRelations, $permission);
return $this->contactsLoader->getContactsWithAnyPermission($contactRelations, $permissions);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ public function __construct(iterable $contactsLoader) {
/**
* @inheritDoc
*/
public function getContactsWithPermission(FundingCaseEntity $fundingCase, string $permission): array {
public function getContactsWithAnyPermission(FundingCaseEntity $fundingCase, array $permissions): array {
$contacts = [];
foreach ($this->contactsLoader as $contactLoader) {
$contacts += $contactLoader->getContactsWithPermission($fundingCase, $permission);
$contacts += $contactLoader->getContactsWithAnyPermission($fundingCase, $permissions);
}

return $contacts;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@
interface FundingCaseContactsLoaderInterface {

/**
* @phpstan-param non-empty-list<string> $permissions
*
* @phpstan-return array<int, array<string, mixed>>
* Contacts indexed by id.
* Contacts that have any of the given permissions indexed by id.
*/
public function getContactsWithPermission(FundingCaseEntity $fundingCase, string $permission): array;
public function getContactsWithAnyPermission(FundingCaseEntity $fundingCase, array $permissions): array;

}
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ public function testValidateWithPermission(): void {
'reviewer_calc_contact_id' => 1,
]);

$this->fundingCaseContactsLoaderMock->method('getContactsWithPermission')
->with($this->fundingCase, 'review_calculative')
$this->fundingCaseContactsLoaderMock->method('getContactsWithAnyPermission')
->with($this->fundingCase, ['review_calculative'])
->willReturn([2 => 'foo', 3 => 'bar']);
$this->fundingCase->setValues(['permissions' => ['review_calculative']] + $this->fundingCase->toArray());
static::assertTrue($this->validator->validate($new, $current)->isValid());
Expand All @@ -115,8 +115,8 @@ public function testValidateContactNotAllowed(): void {
'reviewer_calc_contact_id' => 1,
]);

$this->fundingCaseContactsLoaderMock->method('getContactsWithPermission')
->with($this->fundingCase, 'review_calculative')
$this->fundingCaseContactsLoaderMock->method('getContactsWithAnyPermission')
->with($this->fundingCase, ['review_calculative'])
->willReturn([3 => 'bar']);
$this->fundingCase->setValues(['permissions' => ['review_calculative']] + $this->fundingCase->toArray());
$result = $this->validator->validate($new, $current);
Expand Down Expand Up @@ -159,8 +159,8 @@ public function testValidateNewWithPermission(): void {
'reviewer_calc_contact_id' => 1,
]);

$this->fundingCaseContactsLoaderMock->method('getContactsWithPermission')
->with($this->fundingCase, 'review_calculative')
$this->fundingCaseContactsLoaderMock->method('getContactsWithAnyPermission')
->with($this->fundingCase, ['review_calculative'])
->willReturn([1 => 'foo']);
$this->fundingCase->setValues(['permissions' => ['review_calculative']] + $this->fundingCase->toArray());
static::assertTrue($this->validator->validateNew($new)->isValid());
Expand All @@ -173,8 +173,8 @@ public function testValidateNewContactNotAllowed(): void {
'reviewer_calc_contact_id' => 1,
]);

$this->fundingCaseContactsLoaderMock->method('getContactsWithPermission')
->with($this->fundingCase, 'review_calculative')
$this->fundingCaseContactsLoaderMock->method('getContactsWithAnyPermission')
->with($this->fundingCase, ['review_calculative'])
->willReturn([2 => 'foo']);
$this->fundingCase->setValues(['permissions' => ['review_calculative']] + $this->fundingCase->toArray());
$result = $this->validator->validateNew($new);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ public function testValidateWithPermission(): void {
'reviewer_cont_contact_id' => 1,
]);

$this->fundingCaseContactsLoaderMock->method('getContactsWithPermission')
->with($this->fundingCase, 'review_content')
$this->fundingCaseContactsLoaderMock->method('getContactsWithAnyPermission')
->with($this->fundingCase, ['review_content'])
->willReturn([2 => 'foo', 3 => 'bar']);
$this->fundingCase->setValues(['permissions' => ['review_content']] + $this->fundingCase->toArray());
static::assertTrue($this->validator->validate($new, $current)->isValid());
Expand All @@ -115,8 +115,8 @@ public function testValidateContactNotAllowed(): void {
'reviewer_cont_contact_id' => 1,
]);

$this->fundingCaseContactsLoaderMock->method('getContactsWithPermission')
->with($this->fundingCase, 'review_content')
$this->fundingCaseContactsLoaderMock->method('getContactsWithAnyPermission')
->with($this->fundingCase, ['review_content'])
->willReturn([3 => 'bar']);
$this->fundingCase->setValues(['permissions' => ['review_content']] + $this->fundingCase->toArray());
$result = $this->validator->validate($new, $current);
Expand Down Expand Up @@ -159,8 +159,8 @@ public function testValidateNewWithPermission(): void {
'reviewer_cont_contact_id' => 1,
]);

$this->fundingCaseContactsLoaderMock->method('getContactsWithPermission')
->with($this->fundingCase, 'review_content')
$this->fundingCaseContactsLoaderMock->method('getContactsWithAnyPermission')
->with($this->fundingCase, ['review_content'])
->willReturn([1 => 'foo']);
$this->fundingCase->setValues(['permissions' => ['review_content']] + $this->fundingCase->toArray());
static::assertTrue($this->validator->validateNew($new)->isValid());
Expand All @@ -173,8 +173,8 @@ public function testValidateNewContactNotAllowed(): void {
'reviewer_cont_contact_id' => 1,
]);

$this->fundingCaseContactsLoaderMock->method('getContactsWithPermission')
->with($this->fundingCase, 'review_content')
$this->fundingCaseContactsLoaderMock->method('getContactsWithAnyPermission')
->with($this->fundingCase, ['review_content'])
->willReturn([2 => 'foo']);
$this->fundingCase->setValues(['permissions' => ['review_content']] + $this->fundingCase->toArray());
$result = $this->validator->validateNew($new);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public function testGetContactsWithPermission(): void {
2 => ['id' => 2, 'display_name' => 'Contact2_1'],
3 => ['id' => 3, 'display_name' => 'Contact3'],
];
static::assertEquals($expectedContacts, $this->loader->getContactsWithPermission($contactRelations, 'foo'));
static::assertEquals($expectedContacts, $this->loader->getContactsWithAnyPermission($contactRelations, ['foo']));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ protected function setUp(): void {
);
}

public function testGetContactsWithPermission(): void {
public function testGetContactsWithAnyPermission(): void {
$actionMock = new BasicGetAction(FundingCaseContactRelation::getEntityName(), 'get');
$this->api4Mock->method('createGetAction')
->with(FundingCaseContactRelation::getEntityName())
Expand Down Expand Up @@ -93,12 +93,12 @@ public function testGetContactsWithPermission(): void {
1 => ['id' => 1, 'name' => 'Contact1'],
2 => ['id' => 2, 'name' => 'Contact2'],
];
$this->contactsWithPermissionLoaderMock->method('getContactsWithPermission')
->with($contactRelations, 'test')
$this->contactsWithPermissionLoaderMock->method('getContactsWithAnyPermission')
->with($contactRelations, ['test'])
->willReturn($contacts);

$fundingCase = FundingCaseFactory::createFundingCase();
static::assertSame($contacts, $this->contactsLoader->getContactsWithPermission($fundingCase, 'test'));
static::assertSame($contacts, $this->contactsLoader->getContactsWithAnyPermission($fundingCase, ['test']));
}

}

0 comments on commit cc94d86

Please sign in to comment.