Merge branch 'master' of ssh://git.autonomic.zone:2222/autonomic-cooperative/opencase

This commit is contained in:
naomi 2022-06-05 16:04:49 +01:00
commit f48dae3371
9 changed files with 143 additions and 182 deletions

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;

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

@ -1,40 +0,0 @@
<?php declare(strict_types = 1);
namespace Drupal\Tests\opencase\Unit;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Form\FormState;
use Drupal\opencase_entities\Form\OCActivityRevisionRevertForm;
use Drupal\Tests\UnitTestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
class OCActivityRevisionRevertFormTest extends UnitTestCase{
public function setUp(): void {
$container = new ContainerBuilder();
$entityTypeManager = $this->getMockBuilder('\\Drupal\\Core\\Entity\\EntityTypeManager')->disableOriginalConstructor()->getMock();
$dateFormatter = $this->getMockBuilder('\\Drupal\\Core\\Datetime\\DateFormatterInterface')->disableOriginalConstructor()->getMock();
$storage = $this->getMockBuilder('\\Drupal\\Core\\Entity\\EntityStorageInterface')->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');
$container->set('entity_type.manager', $entityTypeManager);
$container->set('date.formatter', $dateFormatter);
$entityTypeManager->method('getStorage')->willReturn($storage);
$storage->method('loadRevision')->willReturn($revision);
$container->set('string_translation', self::getStringTranslationStub());
$container->set('request_stack', $requestStack);
\Drupal::setContainer($container);
$this->reverter = OCActivityRevisionRevertForm::create($container);
}
public function testBuildForm():void {
$form = [];
$this->assertTrue(is_array($this->reverter->buildForm($form, new FormState())));
}
}

View File

@ -1,40 +0,0 @@
<?php declare(strict_types = 1);
namespace Drupal\Tests\opencase\Unit;
use Drupal\opencase_entities\Form\OCActorRevisionRevertForm;
use Drupal\Tests\UnitTestCase;
use Drupal\Core\Form\FormState;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Drupal\Core\DependencyInjection\ContainerBuilder;
class OCActorRevisionRevertFormTest extends UnitTestCase{
public function setUp(): void {
$container = new ContainerBuilder();
$entityTypeManager = $this->getMockBuilder('\\Drupal\\Core\\Entity\\EntityTypeManager')->disableOriginalConstructor()->getMock();
$dateFormatter = $this->getMockBuilder('\\Drupal\\Core\\Datetime\\DateFormatterInterface')->disableOriginalConstructor()->getMock();
$storage = $this->getMockBuilder('\\Drupal\\Core\\Entity\\EntityStorageInterface')->disableOriginalConstructor()->getMock();
$revision = $this->getMockBuilder('\\Drupal\\opencase_entities\\Entity\OCActor')->disableOriginalConstructor()->getMock();
$request = new Request([], [], [], [], [], [], [], json_encode([
'foo' => 'bar'
]));
$requestStack = new RequestStack();
$requestStack->push($request);
$dateFormatter->method('format');
$container->set('entity_type.manager', $entityTypeManager);
$container->set('date.formatter', $dateFormatter);
$entityTypeManager->method('getStorage')->willReturn($storage);
$storage->method('loadRevision')->willReturn($revision);
$container->set('string_translation', self::getStringTranslationStub());
$container->set('request_stack', $requestStack);
\Drupal::setContainer($container);
$this->reverter = OCActorRevisionRevertForm::create($container);
}
public function testBuildForm():void {
$form = [];
$this->assertTrue(is_array($this->reverter->buildForm($form, new FormState())));
}
}

View File

@ -0,0 +1,30 @@
<?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 testGetCountOfCaseProvisions(): 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);
}
}

View File

@ -1,40 +0,0 @@
<?php declare(strict_types = 1);
namespace Drupal\Tests\opencase\Unit;
use Drupal\opencase_cases\Form\OCCaseRevisionRevertForm;
use Drupal\Tests\UnitTestCase;
use Drupal\Core\Form\FormState;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Drupal\Core\DependencyInjection\ContainerBuilder;
class OCCaseRevisionRevertFormTest extends UnitTestCase{
public function setUp(): void {
$container = new ContainerBuilder();
$entityTypeManager = $this->getMockBuilder('\\Drupal\\Core\\Entity\\EntityTypeManager')->disableOriginalConstructor()->getMock();
$dateFormatter = $this->getMockBuilder('\\Drupal\\Core\\Datetime\\DateFormatterInterface')->disableOriginalConstructor()->getMock();
$storage = $this->getMockBuilder('\\Drupal\\Core\\Entity\\EntityStorageInterface')->disableOriginalConstructor()->getMock();
$revision = $this->getMockBuilder('\\Drupal\\opencase_cases\\Entity\OCCase')->disableOriginalConstructor()->getMock();
$request = new Request([], [], [], [], [], [], [], json_encode([
'foo' => 'bar'
]));
$requestStack = new RequestStack();
$requestStack->push($request);
$dateFormatter->method('format');
$container->set('entity_type.manager', $entityTypeManager);
$container->set('date.formatter', $dateFormatter);
$entityTypeManager->method('getStorage')->willReturn($storage);
$storage->method('loadRevision')->willReturn($revision);
$container->set('string_translation', self::getStringTranslationStub());
$container->set('request_stack', $requestStack);
\Drupal::setContainer($container);
$this->reverter = OCCaseRevisionRevertForm::create($container);
}
public function testBuildForm():void {
$form = [];
$this->assertTrue(is_array($this->reverter->buildForm($form, new FormState())));
}
}

View File

@ -1,40 +0,0 @@
<?php declare(strict_types = 1);
namespace Drupal\Tests\opencase\Unit;
use Drupal\Tests\UnitTestCase;
use Drupal\Core\Form\FormState;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\opencase_entities\Form\OCOrganisationRevisionRevertForm;
class OCOrganisationRevisionRevertFormTest extends UnitTestCase{
public function setUp(): void {
$container = new ContainerBuilder();
$entityTypeManager = $this->getMockBuilder('\\Drupal\\Core\\Entity\\EntityTypeManager')->disableOriginalConstructor()->getMock();
$dateFormatter = $this->getMockBuilder('\\Drupal\\Core\\Datetime\\DateFormatterInterface')->disableOriginalConstructor()->getMock();
$storage = $this->getMockBuilder('\\Drupal\\Core\\Entity\\EntityStorageInterface')->disableOriginalConstructor()->getMock();
$revision = $this->getMockBuilder('\\Drupal\\opencase_entities\\Entity\OCOrganisation')->disableOriginalConstructor()->getMock();
$request = new Request([], [], [], [], [], [], [], json_encode([
'foo' => 'bar'
]));
$requestStack = new RequestStack();
$requestStack->push($request);
$dateFormatter->method('format');
$container->set('entity_type.manager', $entityTypeManager);
$container->set('date.formatter', $dateFormatter);
$entityTypeManager->method('getStorage')->willReturn($storage);
$storage->method('loadRevision')->willReturn($revision);
$container->set('string_translation', self::getStringTranslationStub());
$container->set('request_stack', $requestStack);
\Drupal::setContainer($container);
$this->reverter = OCOrganisationRevisionRevertForm::create($container);
}
public function testBuildForm():void {
$form = [];
$this->assertTrue(is_array($this->reverter->buildForm($form, new FormState())));
}
}

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,16 +7,13 @@ 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 {
@ -28,21 +25,21 @@ class TimeBasedFieldUpdaterTest extends UnitTestCase{
} }
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);
} }