Compare commits
4 Commits
ffb56ab09f
...
49bf43e5b4
Author | SHA1 | Date | |
---|---|---|---|
49bf43e5b4 | |||
2c65aff7e1 | |||
44b9680e0c | |||
b70697995a |
@ -84,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);
|
||||
@ -92,6 +96,26 @@ class OCCase extends RevisionableContentEntityBase implements OCCaseInterface
|
||||
];
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
public function deleteCaseProvisions(): void {
|
||||
$this->deleteChildren('oc_case_provision');
|
||||
}
|
||||
@ -99,8 +123,7 @@ class OCCase extends RevisionableContentEntityBase implements OCCaseInterface
|
||||
$this->deleteChildren('oc_activity');
|
||||
}
|
||||
|
||||
|
||||
public function deleteChildren($child_entity_type):void {
|
||||
private function deleteChildren($child_entity_type):void {
|
||||
$query = \Drupal::entityQuery($child_entity_type)
|
||||
->condition('oc_case.target_id', $this->id());
|
||||
$ids = $query->execute();
|
||||
|
125
tests/src/Unit/OCCaseTest.php
Normal file
125
tests/src/Unit/OCCaseTest.php
Normal file
@ -0,0 +1,125 @@
|
||||
<?php declare(strict_types = 1);
|
||||
|
||||
namespace Drupal\Tests\opencase\Unit;
|
||||
|
||||
use Drupal\Tests\UnitTestCase;
|
||||
|
||||
class OCCaseTest extends UnitTestCase{
|
||||
|
||||
use EntityTrait;
|
||||
|
||||
private $case;
|
||||
|
||||
public function setUp(): void {
|
||||
$this->etm = $this->getEntityTypeManager();
|
||||
$this->getContainer([
|
||||
'entity_type.manager' => $this->etm
|
||||
]);
|
||||
$this->case = $this->getMockBuilder('\\Drupal\\opencase_cases\\Entity\\OCCase')->disableOriginalConstructor()
|
||||
->onlyMethods(['id'])
|
||||
->getMock();
|
||||
}
|
||||
|
||||
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')->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 == []);
|
||||
}
|
||||
}
|
@ -17,24 +17,24 @@ class TimeBasedFieldUpdaterTest extends UnitTestCase{
|
||||
|
||||
}
|
||||
function testFieldIsUpdatedOnEntityReturnedByQuery():void {
|
||||
$this->query->method('execute')->willReturn([1]);
|
||||
$this->query->method('execute')->willReturn(['1']);
|
||||
$this->entity = $this->getMockBuilder('\\Drupal\\Core\\Entity\\EntityBase')->disableOriginalConstructor()->getMock();
|
||||
$this->storage->expects($this->once())->method('load')->with(1)->willReturn($this->entity);
|
||||
$this->updater->update([], '3 months', ['dummy_field' => 4]);
|
||||
$this->assertEquals($this->entity->dummy_field, 4);
|
||||
}
|
||||
function testFieldIsUpdatedOnAllEntitiesReturnedByQuery():void {
|
||||
$this->query->method('execute')->willReturn([1, 2]);
|
||||
$this->query->method('execute')->willReturn(['1', '2']);
|
||||
$entity = $this->getEntity();
|
||||
$entity2 = $this->getEntity();
|
||||
$this->storage->method('load')->willReturnMap([[1, $entity], [2, $entity2]]);
|
||||
$this->storage->method('load')->willReturnMap([['1', $entity], ['2', $entity2]]);
|
||||
$this->updater->update([], '3 months', ['dummy_field' => 4]);
|
||||
$this->assertEquals($entity->dummy_field, 4);
|
||||
$this->assertEquals($entity2->dummy_field, 4);
|
||||
}
|
||||
|
||||
function testMultipleFieldsAreUpdated(): void {
|
||||
$this->query->method('execute')->willReturn([1]);
|
||||
$this->query->method('execute')->willReturn(['1']);
|
||||
$entity = $this->getEntity();
|
||||
$this->storage->expects($this->once())->method('load')->with(1)->willReturn($entity);
|
||||
$this->updater->update([], '3 months', ['dummy_field' => 4, 'dummy_field_2' => 5]);
|
||||
|
Reference in New Issue
Block a user