Compare commits
19 Commits
multiplefu
...
6857bb8d4c
Author | SHA1 | Date | |
---|---|---|---|
6857bb8d4c | |||
f48dae3371 | |||
183537db39 | |||
fb9737ca04 | |||
737104d7bd | |||
66fa591566 | |||
c318c451dc | |||
d81e1b3711 | |||
eb9335b250 | |||
809dbfc837 | |||
156434fecc | |||
57795b6393 | |||
457b4ad694 | |||
780f144b52 | |||
33aec90a78 | |||
f9654bcd78 | |||
4852de71c7 | |||
c29a480401 | |||
56bc7b83dd |
@ -230,7 +230,8 @@ class OCCaseFee extends EditorialContentEntityBase implements OCCaseFeeInterface
|
||||
->setLabel(t('Visible'))
|
||||
->setDescription(t('A boolean indicating whether the Case Fee is published.'))
|
||||
->setDisplayConfigurable('form', TRUE)
|
||||
->setDisplayConfigurable('view', TRUE);
|
||||
->setDisplayConfigurable('view', TRUE)
|
||||
->setDefaultValue(TRUE);
|
||||
|
||||
$fields['created'] = BaseFieldDefinition::create('created')
|
||||
->setLabel(t('Created'))
|
||||
@ -268,7 +269,7 @@ class OCCaseFee extends EditorialContentEntityBase implements OCCaseFeeInterface
|
||||
->setDisplayConfigurable("view", true)
|
||||
->setDisplayConfigurable("form", true)
|
||||
->setRequired(FALSE);
|
||||
|
||||
|
||||
$fields['description'] = BaseFieldDefinition::create('string_long')
|
||||
->setRevisionable(TRUE)
|
||||
->setLabel(t('Description'))
|
||||
|
@ -56,10 +56,10 @@ class OCCaseRevisionRevertForm extends ConfirmFormBase {
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function create(ContainerInterface $container) {
|
||||
return new static(
|
||||
$container->get('entity.manager')->getStorage('oc_case'),
|
||||
$container->get('date.formatter')
|
||||
);
|
||||
return new static($container
|
||||
->get('entity_type.manager')
|
||||
->getStorage('oc_case'), $container
|
||||
->get('date.formatter'));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -211,6 +211,9 @@ class OCActor extends RevisionableContentEntityBase implements OCActorInterface
|
||||
public function getCountOfCaseProvisions(array $conditionsToApplyToCaseProvisionQuery = []): int {
|
||||
$query = \Drupal::entityQuery('oc_case_provision');
|
||||
$query->condition('oc_provider', $this->id());
|
||||
foreach($conditionsToApplyToCaseProvisionQuery as $condition) {
|
||||
$query->condition($condition[0], $condition[1], $condition[2] ?? '=');
|
||||
}
|
||||
return count($query->execute());
|
||||
}
|
||||
|
||||
|
@ -57,7 +57,7 @@ class OCActivityRevisionRevertForm extends ConfirmFormBase {
|
||||
*/
|
||||
public static function create(ContainerInterface $container) {
|
||||
return new static(
|
||||
$container->get('entity.manager')->getStorage('oc_activity'),
|
||||
$container->get('entity_type.manager')->getStorage('oc_activity'),
|
||||
$container->get('date.formatter')
|
||||
);
|
||||
}
|
||||
|
@ -56,10 +56,10 @@ class OCActorRevisionRevertForm extends ConfirmFormBase {
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function create(ContainerInterface $container) {
|
||||
return new static(
|
||||
$container->get('entity.manager')->getStorage('oc_actor'),
|
||||
$container->get('date.formatter')
|
||||
);
|
||||
return new static($container
|
||||
->get('entity_type.manager')
|
||||
->getStorage('oc_actor'), $container
|
||||
->get('date.formatter'));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -41,14 +41,14 @@ class OCOrganisationRevisionRevertForm extends ConfirmFormBase {
|
||||
*/
|
||||
public static function create(ContainerInterface $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');
|
||||
return $instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
*/
|
||||
public function getFormId() {
|
||||
return 'oc_organisation_revision_revert_confirm';
|
||||
}
|
||||
|
@ -2,8 +2,14 @@
|
||||
|
||||
namespace Drupal\opencase;
|
||||
|
||||
|
||||
class Pluraliser {
|
||||
const NO_CHANGE = ['Staff'];
|
||||
public static function pluralise($text) {
|
||||
return $text . "s";
|
||||
if (in_array($text, self::NO_CHANGE)) {
|
||||
return $text;
|
||||
} else {
|
||||
return $text . "s";
|
||||
}
|
||||
}
|
||||
}
|
@ -2,25 +2,21 @@
|
||||
|
||||
namespace Drupal\opencase;
|
||||
use Drupal\Core\Entity\EntityTypeManagerInterface;
|
||||
use Drupal\opencase\Utils;
|
||||
|
||||
final class TimeBasedFieldUpdater {
|
||||
|
||||
private EntityTypeManagerInterface $entityTypeManager;
|
||||
private string $date_field;
|
||||
private Utils $utils;
|
||||
private string $entity_type;
|
||||
private string $date_format;
|
||||
private string $bundle;
|
||||
|
||||
final public function __construct(
|
||||
EntityTypeManagerInterface $entityTypeManager,
|
||||
Utils $utils,
|
||||
string $entity_type, string $bundle, string $date_field, string $date_format = 'Y-m-d'
|
||||
)
|
||||
{
|
||||
$this->entityTypeManager = $entityTypeManager;
|
||||
$this->utils = $utils;
|
||||
$this->date_field = $date_field;
|
||||
$this->date_format = $date_format;
|
||||
$this->entity_type = $entity_type;
|
||||
|
38
tests/src/Unit/EntityTrait.php
Normal file
38
tests/src/Unit/EntityTrait.php
Normal 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;
|
||||
}
|
||||
}
|
61
tests/src/Unit/OCActorTest.php
Normal file
61
tests/src/Unit/OCActorTest.php
Normal 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);
|
||||
}
|
||||
}
|
60
tests/src/Unit/RevisionRevertFormTest.php
Normal file
60
tests/src/Unit/RevisionRevertFormTest.php
Normal 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())));
|
||||
}
|
||||
}
|
@ -7,16 +7,13 @@ use Drupal\opencase\TimeBasedFieldUpdater;
|
||||
|
||||
class TimeBasedFieldUpdaterTest extends UnitTestCase{
|
||||
|
||||
use EntityTrait;
|
||||
|
||||
function setUp():void {
|
||||
/** @var \Drupal\opencase\Utils&\PHPUnit\Framework\MockObject\MockObject $utils */
|
||||
$this->utils = $this->getMockBuilder('\\Drupal\\opencase\\Utils')->disableOriginalConstructor()->getMock();
|
||||
/** @var \Drupal\core\Entity\EntityTypeManagerInterface&\PHPUnit\Framework\MockObject\MockObject $entityTypeManager */
|
||||
$this->entityTypeManager = $this->getMockBuilder('\\Drupal\\Core\\Entity\\EntityTypeManager')->disableOriginalConstructor()->getMock();
|
||||
$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');
|
||||
$this->etm = $this->getEntityTypeManager();
|
||||
$this->storage = $this->getStorage($this->etm);
|
||||
$this->query = $this->getQuery($this->storage);
|
||||
$this->updater = new TimeBasedFieldUpdater($this->etm, 'dummy_entity_type', 'dummy_bundle', 'dummy_date_field');
|
||||
|
||||
}
|
||||
function testFieldIsUpdatedOnEntityReturnedByQuery():void {
|
||||
@ -28,21 +25,21 @@ class TimeBasedFieldUpdaterTest extends UnitTestCase{
|
||||
}
|
||||
function testFieldIsUpdatedOnAllEntitiesReturnedByQuery():void {
|
||||
$this->query->method('execute')->willReturn([1, 2]);
|
||||
$this->entity = $this->getMockBuilder('\\Drupal\\Core\\Entity\\EntityBase')->disableOriginalConstructor()->getMock();
|
||||
$this->entity2 = $this->getMockBuilder('\\Drupal\\Core\\Entity\\EntityBase')->disableOriginalConstructor()->getMock();
|
||||
$this->storage->method('load')->willReturnMap([[1, $this->entity], [2, $this-> entity2]]);
|
||||
$entity = $this->getEntity();
|
||||
$entity2 = $this->getEntity();
|
||||
$this->storage->method('load')->willReturnMap([[1, $entity], [2, $entity2]]);
|
||||
$this->updater->update([], '3 months', ['dummy_field' => 4]);
|
||||
$this->assertEquals($this->entity->dummy_field, 4);
|
||||
$this->assertEquals($this->entity2->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->entity = $this->getMockBuilder('\\Drupal\\Core\\Entity\\EntityBase')->disableOriginalConstructor()->getMock();
|
||||
$this->storage->expects($this->once())->method('load')->with(1)->willReturn($this->entity);
|
||||
$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]);
|
||||
$this->assertEquals($this->entity->dummy_field, 4);
|
||||
$this->assertEquals($this->entity->dummy_field_2, 5);
|
||||
$this->assertEquals($entity->dummy_field, 4);
|
||||
$this->assertEquals($entity->dummy_field_2, 5);
|
||||
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user