Compare commits

...

26 Commits

Author SHA1 Message Date
49bf43e5b4 Added function to cases for getting case provider ids 2022-06-14 13:59:51 +01:00
2c65aff7e1 added method to case to get case provisions 2022-06-07 14:26:51 +01:00
44b9680e0c fixed type error in test for timebasefieldupdater 2022-06-07 13:59:18 +01:00
b70697995a Started adding function to case, to get case providers 2022-06-07 13:54:19 +01:00
ffb56ab09f fixed breadcrumbs so they work for ciac 2022-06-07 13:07:18 +01:00
2f07a2b9aa Added breadcrumb stuff 2022-06-07 12:43:07 +01:00
20f2312245 fixed type error in timebasedfieldupdater 2022-06-07 12:39:22 +01:00
6857bb8d4c Merge branch 'master' of ssh://git.autonomic.zone:2222/autonomic-cooperative/opencase 2022-06-05 17:15:53 +01:00
f48dae3371 Merge branch 'master' of ssh://git.autonomic.zone:2222/autonomic-cooperative/opencase 2022-06-05 16:04:49 +01:00
183537db39 pluraliser ignores selected words e,g, "staff" 2022-06-05 15:49:27 +01:00
fb9737ca04 added extra conditions param to getCountOfCaseProvisions 2022-06-05 14:54:27 +01:00
737104d7bd Merge branch 'master' of ssh://git.autonomic.zone:2222/autonomic-cooperative/opencase 2022-06-05 13:48:15 +01:00
66fa591566 removed utils from timebasedfieldupdater 2022-06-05 13:47:21 +01:00
c318c451dc refactored tests, added EntityTrait 2022-06-05 13:15:33 +01:00
d81e1b3711 Added test for OCActor::getCountOfCaseProvisions 2022-06-05 12:25:28 +01:00
eb9335b250 Revert "commit project after oc case fee bug"
This reverts commit 809dbfc837.
2022-06-03 12:02:45 +02:00
809dbfc837 commit project after oc case fee bug
:wq
2022-06-03 11:53:40 +02:00
156434fecc Merge branch 'master' of ssh://git.autonomic.zone:2222/autonomic-cooperative/opencase 2022-06-03 11:51:45 +02:00
57795b6393 added defauilt value to OCCaseFee 2022-06-03 11:51:37 +02:00
457b4ad694 Merge branch 'revisionerrors' of ssh://git.autonomic.zone:2222/autonomic-cooperative/opencase into revisionerrors 2022-06-03 11:44:32 +02:00
780f144b52 fixed case revisions 2022-06-03 11:44:26 +02:00
33aec90a78 fixed revision reverting error for activities 2022-06-03 10:41:39 +01:00
f9654bcd78 fixed actor and added test for it 2022-05-27 12:09:45 +02:00
4852de71c7 Merge branch 'master' into revisionerrors 2022-05-27 10:25:36 +01:00
c29a480401 Added test for organisation revision 2022-05-27 09:34:37 +01:00
56bc7b83dd added tests for COrganisationRevisionRevertForm 2022-05-24 12:31:03 +01:00
16 changed files with 476 additions and 42 deletions

View File

@ -2,6 +2,7 @@
namespace Drupal\opencase_cases\Entity; namespace Drupal\opencase_cases\Entity;
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\EntityStorageInterface; use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Field\BaseFieldDefinition; use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Entity\RevisionableContentEntityBase; use Drupal\Core\Entity\RevisionableContentEntityBase;
@ -83,6 +84,10 @@ class OCCase extends RevisionableContentEntityBase implements OCCaseInterface
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public static function loadFromUrlQueryParameter(string $param) {
return self::load(\Drupal::request()->query->get($param));
}
public static function preCreate(EntityStorageInterface $storage_controller, array &$values) public static function preCreate(EntityStorageInterface $storage_controller, array &$values)
{ {
parent::preCreate($storage_controller, $values); parent::preCreate($storage_controller, $values);
@ -91,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 { public function deleteCaseProvisions(): void {
$this->deleteChildren('oc_case_provision'); $this->deleteChildren('oc_case_provision');
} }
@ -98,8 +123,7 @@ class OCCase extends RevisionableContentEntityBase implements OCCaseInterface
$this->deleteChildren('oc_activity'); $this->deleteChildren('oc_activity');
} }
private function deleteChildren($child_entity_type):void {
public function deleteChildren($child_entity_type):void {
$query = \Drupal::entityQuery($child_entity_type) $query = \Drupal::entityQuery($child_entity_type)
->condition('oc_case.target_id', $this->id()); ->condition('oc_case.target_id', $this->id());
$ids = $query->execute(); $ids = $query->execute();
@ -116,6 +140,16 @@ class OCCase extends RevisionableContentEntityBase implements OCCaseInterface
else return []; else return [];
} }
public function getTargetEntity(): ContentEntityBase {
if (!$this->oc_target->isEmpty()) {
return $this->oc_target->entity;
} elseif ($this->hasField('client') && !$this->client->isEmpty()) {
return $this->client->entity;
} else {
throw new \Exception('No target found on case');
}
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */

View File

@ -230,7 +230,8 @@ class OCCaseFee extends EditorialContentEntityBase implements OCCaseFeeInterface
->setLabel(t('Visible')) ->setLabel(t('Visible'))
->setDescription(t('A boolean indicating whether the Case Fee is published.')) ->setDescription(t('A boolean indicating whether the Case Fee is published.'))
->setDisplayConfigurable('form', TRUE) ->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE); ->setDisplayConfigurable('view', TRUE)
->setDefaultValue(TRUE);
$fields['created'] = BaseFieldDefinition::create('created') $fields['created'] = BaseFieldDefinition::create('created')
->setLabel(t('Created')) ->setLabel(t('Created'))
@ -268,7 +269,7 @@ class OCCaseFee extends EditorialContentEntityBase implements OCCaseFeeInterface
->setDisplayConfigurable("view", true) ->setDisplayConfigurable("view", true)
->setDisplayConfigurable("form", true) ->setDisplayConfigurable("form", true)
->setRequired(FALSE); ->setRequired(FALSE);
$fields['description'] = BaseFieldDefinition::create('string_long') $fields['description'] = BaseFieldDefinition::create('string_long')
->setRevisionable(TRUE) ->setRevisionable(TRUE)
->setLabel(t('Description')) ->setLabel(t('Description'))

View File

@ -56,10 +56,10 @@ class OCCaseRevisionRevertForm extends ConfirmFormBase {
* {@inheritdoc} * {@inheritdoc}
*/ */
public static function create(ContainerInterface $container) { public static function create(ContainerInterface $container) {
return new static( return new static($container
$container->get('entity.manager')->getStorage('oc_case'), ->get('entity_type.manager')
$container->get('date.formatter') ->getStorage('oc_case'), $container
); ->get('date.formatter'));
} }
/** /**

View File

@ -211,6 +211,9 @@ class OCActor extends RevisionableContentEntityBase implements OCActorInterface
public function getCountOfCaseProvisions(array $conditionsToApplyToCaseProvisionQuery = []): int { public function getCountOfCaseProvisions(array $conditionsToApplyToCaseProvisionQuery = []): int {
$query = \Drupal::entityQuery('oc_case_provision'); $query = \Drupal::entityQuery('oc_case_provision');
$query->condition('oc_provider', $this->id()); $query->condition('oc_provider', $this->id());
foreach($conditionsToApplyToCaseProvisionQuery as $condition) {
$query->condition($condition[0], $condition[1], $condition[2] ?? '=');
}
return count($query->execute()); return count($query->execute());
} }

View File

@ -57,7 +57,7 @@ class OCActivityRevisionRevertForm extends ConfirmFormBase {
*/ */
public static function create(ContainerInterface $container) { public static function create(ContainerInterface $container) {
return new static( return new static(
$container->get('entity.manager')->getStorage('oc_activity'), $container->get('entity_type.manager')->getStorage('oc_activity'),
$container->get('date.formatter') $container->get('date.formatter')
); );
} }

View File

@ -56,10 +56,10 @@ class OCActorRevisionRevertForm extends ConfirmFormBase {
* {@inheritdoc} * {@inheritdoc}
*/ */
public static function create(ContainerInterface $container) { public static function create(ContainerInterface $container) {
return new static( return new static($container
$container->get('entity.manager')->getStorage('oc_actor'), ->get('entity_type.manager')
$container->get('date.formatter') ->getStorage('oc_actor'), $container
); ->get('date.formatter'));
} }
/** /**

View File

@ -41,14 +41,14 @@ class OCOrganisationRevisionRevertForm extends ConfirmFormBase {
*/ */
public static function create(ContainerInterface $container) { public static function create(ContainerInterface $container) {
$instance = parent::create($container); $instance = parent::create($container);
$instance->oCOrganisationStorage = $container->get('entity_type.manager')->getStorage('oc_organisation'); $instance->OCOrganisationStorage = $container->get('entity_type.manager')->getStorage('oc_organisation');
$instance->dateFormatter = $container->get('date.formatter'); $instance->dateFormatter = $container->get('date.formatter');
return $instance; return $instance;
} }
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getFormId() { public function getFormId() {
return 'oc_organisation_revision_revert_confirm'; return 'oc_organisation_revision_revert_confirm';
} }

8
opencase.services.yml Normal file
View File

@ -0,0 +1,8 @@
services:
opencase.breadcrumb:
# The namespace + classname from your BreadcrumbBuilderInterface class
class: Drupal\opencase\Breadcrumb\BreadcrumbBuilder
# Priority determines the order in which Breadcrumb services run.
tags:
- { name: breadcrumb_builder, priority: 100 }

View File

@ -0,0 +1,105 @@
<?php declare(strict_types = 1);
namespace Drupal\opencase\Breadcrumb;
use Drupal\Core\Link ;
use Drupal\Core\Breadcrumb\Breadcrumb;
use Drupal\opencase_cases\Entity\OCCase;
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\opencase_entities\Entity\OCOrganisation;
use Drupal\Core\Breadcrumb\BreadcrumbBuilderInterface;
class BreadcrumbBuilder implements BreadcrumbBuilderInterface {
private string $title;
private function addTitle($route_match) {
$title = $this->title ?? $this->getPageTitle($route_match);
$this->breadcrumb->addLink(Link::createFromRoute($title, '<none>'));
}
/**
* {@inheritdoc}
*/
public function applies(RouteMatchInterface $route_match) {
return TRUE;
}
private function getPageTitle($route_match) {
$request = \Drupal::request();
return \Drupal::service('title_resolver')->getTitle($request, $route_match->getRouteObject());
}
private function addLinksForCaseTarget(ContentEntityBase $entity) {
if ($entity instanceof OCOrganisation) {
$this->addLinksForOrganisation($entity);
}
}
private function addLinksForOrganisation(ContentEntityBase $entity) {
$fields = ['field_umbrella_client'];
$this->addLinks($entity, $fields);
}
private function addLinksForCase(OCCase $case) {
$fields = ['client', 'field_project'];
$this->addLinksForCaseTarget($case->getTargetEntity());
$this->addLinks($case, $fields);
}
private function addLinks(ContentEntityBase $entity, array $fields) {
foreach ($fields as $field) {
if ($entity->hasField($field) && !$entity->get($field)->isEmpty()) {
$this->breadcrumb->addLink($entity->$field->entity->toLink());
}
}
}
/**
* {@inheritdoc}
*/
public function build(RouteMatchInterface $route_match) {
$this->breadcrumb = new Breadcrumb();
$this->breadcrumb->addLink(Link::createFromRoute('Home', '<front>'));
$params = $route_match->getParameters();
if ($params->has('oc_activity')) {
$activity = $params->get('oc_activity');
if (!$activity->get('oc_case')->isEmpty()){
$this->addLinksForCase($activity->get('oc_case')->entity);
}
$this->addLinks($activity, ['client', 'oc_case']);
$this->title = $activity->getName() ?? $activity->type->entity->label();
}
elseif ($params->has('oc_organisation')) {
$this->addLinksForOrganisation($params->get('oc_organisation'));
}
elseif ($params->has('oc_case')) {
$this->addLinksForCase($params->get('oc_case'));
}
elseif ($params->has('oc_case_fee')) {
$case_provision = $params->get('oc_case_fee');
$this->addLinksForCase($case_provision->get('oc_case')->entity);
$this->addLinks($case_provision, ['oc_case']);
$this->title = "Fee";
}
elseif ($params->has('oc_case_provision')) {
$case_provision = $params->get('oc_case_provision');
$this->addLinksForCase($case_provision->get('oc_case')->entity);
$this->addLinks($case_provision, ['oc_case']);
$this->title = $case_provision->type->entity->label();
}
$this->addTitle($route_match);
// Don't forget to add cache control by a route.
// Otherwise all pages will have the same breadcrumb.
$this->breadcrumb->addCacheContexts(['route']);
return $this->breadcrumb;
}
}

View File

@ -2,8 +2,14 @@
namespace Drupal\opencase; namespace Drupal\opencase;
class Pluraliser { class Pluraliser {
const NO_CHANGE = ['Staff'];
public static function pluralise($text) { public static function pluralise($text) {
return $text . "s"; if (in_array($text, self::NO_CHANGE)) {
return $text;
} else {
return $text . "s";
}
} }
} }

View File

@ -2,25 +2,21 @@
namespace Drupal\opencase; namespace Drupal\opencase;
use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\opencase\Utils;
final class TimeBasedFieldUpdater { final class TimeBasedFieldUpdater {
private EntityTypeManagerInterface $entityTypeManager; private EntityTypeManagerInterface $entityTypeManager;
private string $date_field; private string $date_field;
private Utils $utils;
private string $entity_type; private string $entity_type;
private string $date_format; private string $date_format;
private string $bundle; private string $bundle;
final public function __construct( final public function __construct(
EntityTypeManagerInterface $entityTypeManager, EntityTypeManagerInterface $entityTypeManager,
Utils $utils,
string $entity_type, string $bundle, string $date_field, string $date_format = 'Y-m-d' string $entity_type, string $bundle, string $date_field, string $date_format = 'Y-m-d'
) )
{ {
$this->entityTypeManager = $entityTypeManager; $this->entityTypeManager = $entityTypeManager;
$this->utils = $utils;
$this->date_field = $date_field; $this->date_field = $date_field;
$this->date_format = $date_format; $this->date_format = $date_format;
$this->entity_type = $entity_type; $this->entity_type = $entity_type;
@ -39,7 +35,7 @@ final class TimeBasedFieldUpdater {
$this->updateEntity($id, $new_values); $this->updateEntity($id, $new_values);
} }
} }
private function updateEntity(int $entity_id, array $new_values): void { private function updateEntity(string $entity_id, array $new_values): void {
$entity = $this->entityTypeManager->getStorage($this->entity_type)->load($entity_id); $entity = $this->entityTypeManager->getStorage($this->entity_type)->load($entity_id);
foreach($new_values as $new_field=>$new_value) { foreach($new_values as $new_field=>$new_value) {
$entity->$new_field = $new_value; $entity->$new_field = $new_value;

View File

@ -0,0 +1,38 @@
<?php
namespace Drupal\Tests\opencase\Unit;
use PHPUnit\Framework\MockObject\MockObject;
use Drupal\Core\DependencyInjection\ContainerBuilder;
trait EntityTrait {
public function getEntityTypeManager() {
return $this->getMockBuilder('\\Drupal\\Core\\Entity\\EntityTypeManager')->disableOriginalConstructor()->getMock();
}
public function getStorage(MockObject $entityTypeManager, string $entityTypeToExpect = ''): MockObject {
$storage = $this->getMockBuilder('\\Drupal\\Core\\Entity\\EntityStorageInterface')->disableOriginalConstructor()->getMock();
$entityTypeManager->method('getStorage')->willReturn($storage);
if ($entityTypeToExpect) {
$entityTypeManager->expects($this->any())->method('getStorage')->with($entityTypeToExpect)->willReturn($storage);
}
return $storage;
}
public function getQuery(MockObject $storage): MockObject {
$query = $this->getMockBuilder('\\Drupal\\Core\\Entity\\Query\\QueryInterface')->getMock();
$storage->method('getQuery')->willReturn($query);
return $query;
}
public function getEntity(): MockObject {
return $this->getMockBuilder('\\Drupal\\Core\\Entity\\EntityBase')->disableOriginalConstructor()->getMock();
}
public function getContainer(array $services): ContainerBuilder {
$container = new ContainerBuilder();
foreach ($services as $key => $mock) {
$container->set($key, $mock);
}
\Drupal::setContainer($container);
return $container;
}
}

View File

@ -0,0 +1,61 @@
<?php declare(strict_types = 1);
namespace Drupal\Tests\opencase\Unit;
use Drupal\Tests\UnitTestCase;
class OCActorTest extends UnitTestCase{
use EntityTrait;
public function setUp(): void {
$this->etm = $this->getEntityTypeManager();
$this->getContainer([
'entity_type.manager' => $this->etm
]);
}
public function testGetCountOfCaseProvisionsWithNoExtraConditions(): void{
$storage = $this->getStorage($this->etm, 'oc_case_provision');
$query = $this->getQuery($storage);
$actor = $this->getMockBuilder('\\Drupal\\opencase_entities\\Entity\\OCActor')->disableOriginalConstructor()
->onlyMethods(['id'])
->getMock();
$actor->expects($this->once())->method('id')->willReturn(5);
$query->expects($this->once())->method('condition')->with('oc_provider', 5);
$query->expects($this->once())->method('execute')->willReturn([1,2,3,4]);
$count = $actor->getCountOfCaseProvisions();
$this->assertTrue($count == 4);
}
public function testGetCountOfCaseProvisionsWithExtraConditions(): void{
$storage = $this->getStorage($this->etm, 'oc_case_provision');
$query = $this->getQuery($storage);
$actor = $this->getMockBuilder('\\Drupal\\opencase_entities\\Entity\\OCActor')->disableOriginalConstructor()
->onlyMethods(['id'])
->getMock();
$actor->expects($this->once())->method('id')->willReturn(5);
$query->expects($this->exactly(2))->method('condition')->withConsecutive(
['oc_provider', 5],
['some_date_field', '2022-01-01', '<']);
$query->expects($this->once())->method('execute')->willReturn([1,2,3,4]);
$count = $actor->getCountOfCaseProvisions([['some_date_field', '2022-01-01', '<']]);
$this->assertTrue($count == 4);
}
public function testGetCountOfCaseProvisionsWithExtraConditionsWithAssumedEqualityOperator(): void{
$storage = $this->getStorage($this->etm, 'oc_case_provision');
$query = $this->getQuery($storage);
$actor = $this->getMockBuilder('\\Drupal\\opencase_entities\\Entity\\OCActor')->disableOriginalConstructor()
->onlyMethods(['id'])
->getMock();
$actor->expects($this->once())->method('id')->willReturn(5);
$query->expects($this->exactly(2))->method('condition')->withConsecutive(
['oc_provider', 5],
['some_date_field', '2022-01-01', '=']);
$query->expects($this->once())->method('execute')->willReturn([1,2,3,4]);
$count = $actor->getCountOfCaseProvisions([['some_date_field', '2022-01-01']]);
$this->assertTrue($count == 4);
}
}

View 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 == []);
}
}

View File

@ -0,0 +1,60 @@
<?php declare(strict_types = 1);
namespace Drupal\Tests\opencase\Unit;
use Drupal\Tests\UnitTestCase;
use Drupal\Core\Form\FormState;
use Drupal\Tests\opencase\Unit\EntityTrait;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Drupal\opencase_cases\Form\OCCaseRevisionRevertForm;
use Drupal\opencase_entities\Form\OCActorRevisionRevertForm;
use Drupal\opencase_entities\Form\OCActivityRevisionRevertForm;
use Drupal\opencase_entities\Form\OCOrganisationRevisionRevertForm;
class RevisionRevertFormTest extends UnitTestCase{
use EntityTrait;
public function setUp(): void {
$entityTypeManager = $this->getEntityTypeManager();
$storage = $this->getStorage($entityTypeManager);
$dateFormatter = $this->getMockBuilder('\\Drupal\\Core\\Datetime\\DateFormatterInterface')->disableOriginalConstructor()->getMock();
$revision = $this->getMockBuilder('\\Drupal\\opencase_entities\\Entity\OCActivity')->disableOriginalConstructor()->getMock();
$request = new Request([], [], [], [], [], [], [], json_encode([
'foo' => 'bar'
]));
$requestStack = new RequestStack();
$requestStack->push($request);
$dateFormatter->method('format');
$storage->method('loadRevision')->willReturn($revision);
$this->container = $this->getContainer([
'entity_type.manager'=> $entityTypeManager,
'date.formatter' => $dateFormatter,
'string_translation'=> self::getStringTranslationStub(),
'request_stack'=> $requestStack
]);
}
public function testBuildFormForRevertingActivity():void {
$this->reverter = OCActivityRevisionRevertForm::create($this->container);
$form = [];
$this->assertTrue(is_array($this->reverter->buildForm($form, new FormState())));
}
public function testBuildFormForRevertingActor():void {
$this->reverter = OCActorRevisionRevertForm::create($this->container);
$form = [];
$this->assertTrue(is_array($this->reverter->buildForm($form, new FormState())));
}
public function testBuildFormForRevertingCase():void {
$this->reverter = OCCaseRevisionRevertForm::create($this->container);
$form = [];
$this->assertTrue(is_array($this->reverter->buildForm($form, new FormState())));
}
public function testBuildFormForRevertingOrganisation():void {
$this->reverter = OCOrganisationRevisionRevertForm::create($this->container);
$form = [];
$this->assertTrue(is_array($this->reverter->buildForm($form, new FormState())));
}
}

View File

@ -7,42 +7,39 @@ use Drupal\opencase\TimeBasedFieldUpdater;
class TimeBasedFieldUpdaterTest extends UnitTestCase{ class TimeBasedFieldUpdaterTest extends UnitTestCase{
use EntityTrait;
function setUp():void { function setUp():void {
/** @var \Drupal\opencase\Utils&\PHPUnit\Framework\MockObject\MockObject $utils */ $this->etm = $this->getEntityTypeManager();
$this->utils = $this->getMockBuilder('\\Drupal\\opencase\\Utils')->disableOriginalConstructor()->getMock(); $this->storage = $this->getStorage($this->etm);
/** @var \Drupal\core\Entity\EntityTypeManagerInterface&\PHPUnit\Framework\MockObject\MockObject $entityTypeManager */ $this->query = $this->getQuery($this->storage);
$this->entityTypeManager = $this->getMockBuilder('\\Drupal\\Core\\Entity\\EntityTypeManager')->disableOriginalConstructor()->getMock(); $this->updater = new TimeBasedFieldUpdater($this->etm, 'dummy_entity_type', 'dummy_bundle', 'dummy_date_field');
$this->storage = $this->getMockBuilder('\\Drupal\\Core\\Entity\\EntityStorageInterface')->getMock();
$this->query = $this->getMockBuilder('\\Drupal\\Core\\Entity\\Query\\QueryInterface')->getMock();
$this->entityTypeManager->method('getStorage')->willReturn($this->storage);
$this->storage->method('getQuery')->willReturn($this->query);
$this->updater = new TimeBasedFieldUpdater($this->entityTypeManager, $this->utils, 'dummy_entity_type', 'dummy_bundle', 'dummy_date_field');
} }
function testFieldIsUpdatedOnEntityReturnedByQuery():void { 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->entity = $this->getMockBuilder('\\Drupal\\Core\\Entity\\EntityBase')->disableOriginalConstructor()->getMock();
$this->storage->expects($this->once())->method('load')->with(1)->willReturn($this->entity); $this->storage->expects($this->once())->method('load')->with(1)->willReturn($this->entity);
$this->updater->update([], '3 months', ['dummy_field' => 4]); $this->updater->update([], '3 months', ['dummy_field' => 4]);
$this->assertEquals($this->entity->dummy_field, 4); $this->assertEquals($this->entity->dummy_field, 4);
} }
function testFieldIsUpdatedOnAllEntitiesReturnedByQuery():void { function testFieldIsUpdatedOnAllEntitiesReturnedByQuery():void {
$this->query->method('execute')->willReturn([1, 2]); $this->query->method('execute')->willReturn(['1', '2']);
$this->entity = $this->getMockBuilder('\\Drupal\\Core\\Entity\\EntityBase')->disableOriginalConstructor()->getMock(); $entity = $this->getEntity();
$this->entity2 = $this->getMockBuilder('\\Drupal\\Core\\Entity\\EntityBase')->disableOriginalConstructor()->getMock(); $entity2 = $this->getEntity();
$this->storage->method('load')->willReturnMap([[1, $this->entity], [2, $this-> entity2]]); $this->storage->method('load')->willReturnMap([['1', $entity], ['2', $entity2]]);
$this->updater->update([], '3 months', ['dummy_field' => 4]); $this->updater->update([], '3 months', ['dummy_field' => 4]);
$this->assertEquals($this->entity->dummy_field, 4); $this->assertEquals($entity->dummy_field, 4);
$this->assertEquals($this->entity2->dummy_field, 4); $this->assertEquals($entity2->dummy_field, 4);
} }
function testMultipleFieldsAreUpdated(): void { function testMultipleFieldsAreUpdated(): void {
$this->query->method('execute')->willReturn([1]); $this->query->method('execute')->willReturn(['1']);
$this->entity = $this->getMockBuilder('\\Drupal\\Core\\Entity\\EntityBase')->disableOriginalConstructor()->getMock(); $entity = $this->getEntity();
$this->storage->expects($this->once())->method('load')->with(1)->willReturn($this->entity); $this->storage->expects($this->once())->method('load')->with(1)->willReturn($entity);
$this->updater->update([], '3 months', ['dummy_field' => 4, 'dummy_field_2' => 5]); $this->updater->update([], '3 months', ['dummy_field' => 4, 'dummy_field_2' => 5]);
$this->assertEquals($this->entity->dummy_field, 4); $this->assertEquals($entity->dummy_field, 4);
$this->assertEquals($this->entity->dummy_field_2, 5); $this->assertEquals($entity->dummy_field_2, 5);
} }