Added function to cases for getting case provider ids

This commit is contained in:
naomi 2022-06-14 13:55:25 +01:00
parent 2c65aff7e1
commit 49bf43e5b4
2 changed files with 117 additions and 8 deletions

View File

@ -9,7 +9,6 @@ use Drupal\Core\Entity\RevisionableContentEntityBase;
use Drupal\Core\Entity\RevisionableInterface;
use Drupal\Core\Entity\EntityChangedTrait;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\EntityTypeManager;
use Drupal\user\UserInterface;
/**
@ -85,6 +84,10 @@ class OCCase extends RevisionableContentEntityBase implements OCCaseInterface
/**
* {@inheritdoc}
*/
public static function loadFromUrlQueryParameter(string $param) {
return self::load(\Drupal::request()->query->get($param));
}
public static function preCreate(EntityStorageInterface $storage_controller, array &$values)
{
parent::preCreate($storage_controller, $values);
@ -93,7 +96,21 @@ class OCCase extends RevisionableContentEntityBase implements OCCaseInterface
];
}
public function getCaseProvisionIds(EntityTypeManager $etm): array {
public function getCaseProviderIds(int $role_id = null): array {
$case_provision_ids = $this->getCaseProvisionIds();
$provider_ids = [];
foreach($case_provision_ids as $id) {
$provision = \Drupal::entityTypeManager()->getStorage('oc_case_provision')->load($id);
if ($provision instanceOf OCCaseProvision) {
if (is_null($role_id) || $role_id == $provision->get('oc_case_provider_role')->target_id) {
$provider_ids[] = $provision->get('oc_provider')->target_id;
}
}
}
return $provider_ids;
}
private function getCaseProvisionIds(): array {
$query = \Drupal::entityTypeManager()->getStorage('oc_case_provision')->getQuery();
$query->condition('oc_case.target_id', $this->id());
return $query->execute();

View File

@ -20,14 +20,106 @@ class OCCaseTest extends UnitTestCase{
->getMock();
}
public function testGetCaseProvisionIds(): void{
public function testGetCaseProviderIds_SingleCaseProvision(): void{
// It will first get the id of the case provision that references this case
$storage = $this->getStorage($this->etm, 'oc_case_provision');
$query = $this->getQuery($storage);
$this->case->expects($this->once())->method('id')->willReturn(5);
$query->expects($this->once())->method('condition')->withConsecutive(
['oc_case.target_id', 5]);
$query->expects($this->once())->method('execute')->willReturn([1,2,3,4]);
$ids = $this->case->getCaseProvisionIds($this->etm);
$this->assertTrue($ids == [1,2,3,4]);
$query->expects($this->once())->method('condition')->with('oc_case.target_id', 5);
$query->expects($this->once())->method('execute')->willReturn([1]);
// Then it will load the provision
$provision = $this->getMockBuilder('\\Drupal\\opencase_cases\\Entity\\OCCaseProvision')->disableOriginalConstructor()->getMock();
$storage->expects($this->once())->method('load')->with(1)->willReturn($provision);
// Then it will get the target id of the provider field from the provision and return it as an array
$providerField = $this->getMockBuilder('\\Drupal\\COre\\Field\\FieldItemListInterface')->disableOriginalConstructor()->getMock();
$providerField->target_id = '45';
$provision->expects($this->once())->method('get')->with('oc_provider')->willReturn($providerField);
$ids = $this->case->getCaseProviderIds();
$this->assertTrue($ids == [45]);
}
public function testGetCaseProviderIds_MultipleCaseProvisions(): void{
// It will first get the id of the case provisions that reference this case
$storage = $this->getStorage($this->etm, 'oc_case_provision');
$query = $this->getQuery($storage);
$this->case->expects($this->once())->method('id')->willReturn(5);
$query->expects($this->once())->method('condition')->with('oc_case.target_id', 5);
$query->expects($this->once())->method('execute')->willReturn([1, 2, 3]);
// Then it will load the provisions
$provision1 = $this->getMockBuilder('\\Drupal\\opencase_cases\\Entity\\OCCaseProvision')->disableOriginalConstructor()->getMock();
$provision2 = $this->getMockBuilder('\\Drupal\\opencase_cases\\Entity\\OCCaseProvision')->disableOriginalConstructor()->getMock();
$provision3 = $this->getMockBuilder('\\Drupal\\opencase_cases\\Entity\\OCCaseProvision')->disableOriginalConstructor()->getMock();
$storage->method('load')->willReturnMap([[1, $provision1], [2, $provision2], [3, $provision3]]);
// Then it will get the target id of the provider field from each provision and return them as an array
$providerField1 = $this->getMockBuilder('\\Drupal\\COre\\Field\\FieldItemListInterface')->disableOriginalConstructor()->getMock();
$providerField1->target_id = '45';
$provision1->expects($this->once())->method('get')->with('oc_provider')->willReturn($providerField1);
$providerField2 = $this->getMockBuilder('\\Drupal\\COre\\Field\\FieldItemListInterface')->disableOriginalConstructor()->getMock();
$providerField2->target_id = '55';
$provision2->expects($this->once())->method('get')->with('oc_provider')->willReturn($providerField2);
$providerField3 = $this->getMockBuilder('\\Drupal\\COre\\Field\\FieldItemListInterface')->disableOriginalConstructor()->getMock();
$providerField3->target_id = '65';
$provision3->expects($this->once())->method('get')->with('oc_provider')->willReturn($providerField3);
$ids = $this->case->getCaseProviderIds();
$this->assertTrue($ids == [45, 55, 65]);
}
public function testGetCaseProviderIds_SingleCaseProvision_RoleSpecifiedAndMatched(): void{
// It will first get the id of the case provision that references this case
$storage = $this->getStorage($this->etm, 'oc_case_provision');
$query = $this->getQuery($storage);
$this->case->expects($this->once())->method('id')->willReturn(5);
$query->expects($this->once())->method('condition')->with('oc_case.target_id', 5);
$query->expects($this->once())->method('execute')->willReturn([1]);
// Then it will load the provision
$provision = $this->getMockBuilder('\\Drupal\\opencase_cases\\Entity\\OCCaseProvision')->disableOriginalConstructor()->getMock();
$storage->expects($this->once())->method('load')->with(1)->willReturn($provision);
// Then it will check the role field, and find that the role is the one specified
// so it will get the target id of the provider field from the provision and return it as an array
$roleField = $this->getMockBuilder('\\Drupal\\COre\\Field\\FieldItemListInterface')->disableOriginalConstructor()->getMock();
$roleField->target_id = '7';
$providerField = $this->getMockBuilder('\\Drupal\\COre\\Field\\FieldItemListInterface')->disableOriginalConstructor()->getMock();
$providerField->target_id = '45';
$provision->expects($this->any())->method('get')->withConsecutive(['oc_case_provider_role'], ['oc_provider'])->willReturnMap(
[['oc_provider', $providerField], ['oc_case_provider_role', $roleField]]
);
$ids = $this->case->getCaseProviderIds(7);
$this->assertTrue($ids == [45]);
}
public function testGetCaseProviderIds_SingleCaseProvision_RoleSpecifiedAndNotMatched(): void{
// It will first get the id of the case provision that references this case
$storage = $this->getStorage($this->etm, 'oc_case_provision');
$query = $this->getQuery($storage);
$this->case->expects($this->once())->method('id')->willReturn(5);
$query->expects($this->once())->method('condition')->with('oc_case.target_id', 5);
$query->expects($this->once())->method('execute')->willReturn([1]);
// Then it will load the provision
$provision = $this->getMockBuilder('\\Drupal\\opencase_cases\\Entity\\OCCaseProvision')->disableOriginalConstructor()->getMock();
$storage->expects($this->once())->method('load')->with(1)->willReturn($provision);
// Then it will check the role field, and find that the role is *not* the one specified
// so it will return nothing
$roleField = $this->getMockBuilder('\\Drupal\\COre\\Field\\FieldItemListInterface')->disableOriginalConstructor()->getMock();
$roleField->target_id = '7';
$providerField = $this->getMockBuilder('\\Drupal\\COre\\Field\\FieldItemListInterface')->disableOriginalConstructor()->getMock();
$providerField->target_id = '45';
$provision->expects($this->any())->method('get')->withConsecutive(['oc_case_provider_role'], ['oc_provider'])->willReturnMap(
[['oc_provider', $providerField], ['oc_case_provider_role', $roleField]]
);
$ids = $this->case->getCaseProviderIds(8);
$this->assertTrue($ids == []);
}
}