Compare commits
7 Commits
9862e65ea9
...
f9654bcd78
Author | SHA1 | Date | |
---|---|---|---|
|
f9654bcd78 | ||
4852de71c7 | |||
|
fdf00d5c95 | ||
a4aa8444e3 | |||
a228ea5554 | |||
c29a480401 | |||
56bc7b83dd |
@ -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'));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -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';
|
||||||
}
|
}
|
||||||
|
@ -14,11 +14,11 @@ final class TimeBasedFieldUpdater {
|
|||||||
private string $bundle;
|
private string $bundle;
|
||||||
|
|
||||||
final public function __construct(
|
final public function __construct(
|
||||||
EntityTypeManagerInterface $entityTypeManager,
|
EntityTypeManagerInterface $entityTypeManager,
|
||||||
Utils $utils,
|
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->utils = $utils;
|
||||||
$this->date_field = $date_field;
|
$this->date_field = $date_field;
|
||||||
@ -31,16 +31,19 @@ final class TimeBasedFieldUpdater {
|
|||||||
$query = $this->entityTypeManager->getStorage($this->entity_type)->getQuery();
|
$query = $this->entityTypeManager->getStorage($this->entity_type)->getQuery();
|
||||||
$conditions[] = [$this->date_field, date($this->date_format, strtotime('-'.$time_elapsed)), "<"];
|
$conditions[] = [$this->date_field, date($this->date_format, strtotime('-'.$time_elapsed)), "<"];
|
||||||
$conditions[] = ['type', $this->bundle, '='];
|
$conditions[] = ['type', $this->bundle, '='];
|
||||||
$this->utils->addConditionsToQuery($query, $conditions);
|
|
||||||
|
foreach ($conditions as $condition) {
|
||||||
|
$query->condition($condition[0], $condition[1], $condition[2] ?? "=");
|
||||||
|
}
|
||||||
foreach($query->execute() as $id) {
|
foreach($query->execute() as $id) {
|
||||||
$this->updateEntity($id, $new_values);
|
$this->updateEntity($id, $new_values);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
private function updateEntity(int $entity_id, array $new_values): void {
|
private function updateEntity(int $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;
|
||||||
}
|
}
|
||||||
$entity->save();
|
$entity->save();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,10 +3,8 @@
|
|||||||
namespace Drupal\opencase;
|
namespace Drupal\opencase;
|
||||||
|
|
||||||
use \Drupal;
|
use \Drupal;
|
||||||
use Drupal\Core\Entity\EntityTypeManager;
|
|
||||||
use Drupal\Core\Entity\Query\QueryInterface;
|
use Drupal\Core\Entity\Query\QueryInterface;
|
||||||
use Drupal\Core\Entity\EntityTypeManagerInterface;
|
use Drupal\Core\Entity\EntityTypeManagerInterface;
|
||||||
use RuntimeException;
|
|
||||||
|
|
||||||
class Utils {
|
class Utils {
|
||||||
|
|
||||||
@ -19,15 +17,6 @@ class Utils {
|
|||||||
$this->entityTypeManager = $entityTypeManager;
|
$this->entityTypeManager = $entityTypeManager;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function addConditionsToQuery(QueryInterface $query, array $conditions): void {
|
|
||||||
foreach($conditions as $condition) {
|
|
||||||
$field = $condition[0];
|
|
||||||
$value = $condition[1];
|
|
||||||
$operator = isset($condition[2]) ? $condition[2] : "=";
|
|
||||||
$query->condition($field, $value, $operator);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Utility: find term by name and vid.
|
* Utility: find term by name and vid.
|
||||||
*
|
*
|
||||||
@ -40,7 +29,7 @@ class Utils {
|
|||||||
*/
|
*/
|
||||||
public function getTidByName(string $name, string $vid):int {
|
public function getTidByName(string $name, string $vid):int {
|
||||||
if (empty($name) || empty($vid)) {
|
if (empty($name) || empty($vid)) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
$properties = [
|
$properties = [
|
||||||
'name' => $name,
|
'name' => $name,
|
||||||
|
40
tests/src/Unit/OCActorRevisionRevertFormTest.php
Normal file
40
tests/src/Unit/OCActorRevisionRevertFormTest.php
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
<?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())));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
40
tests/src/Unit/OCOrganisationRevisionRevertFormTest.php
Normal file
40
tests/src/Unit/OCOrganisationRevisionRevertFormTest.php
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
<?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())));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -22,7 +22,7 @@ class TimeBasedFieldUpdaterTest extends UnitTestCase{
|
|||||||
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);
|
||||||
}
|
}
|
||||||
@ -30,16 +30,16 @@ class TimeBasedFieldUpdaterTest extends UnitTestCase{
|
|||||||
$this->query->method('execute')->willReturn([1, 2]);
|
$this->query->method('execute')->willReturn([1, 2]);
|
||||||
$this->entity = $this->getMockBuilder('\\Drupal\\Core\\Entity\\EntityBase')->disableOriginalConstructor()->getMock();
|
$this->entity = $this->getMockBuilder('\\Drupal\\Core\\Entity\\EntityBase')->disableOriginalConstructor()->getMock();
|
||||||
$this->entity2 = $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]]);
|
$this->storage->method('load')->willReturnMap([[1, $this->entity], [2, $this-> 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($this->entity->dummy_field, 4);
|
||||||
$this->assertEquals($this->entity2->dummy_field, 4);
|
$this->assertEquals($this->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();
|
$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, '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($this->entity->dummy_field, 4);
|
||||||
$this->assertEquals($this->entity->dummy_field_2, 5);
|
$this->assertEquals($this->entity->dummy_field_2, 5);
|
||||||
@ -48,13 +48,13 @@ class TimeBasedFieldUpdaterTest extends UnitTestCase{
|
|||||||
|
|
||||||
function testBundleAndDateAndExtraConditionsAreAllAddedAsQueryConditions(): void {
|
function testBundleAndDateAndExtraConditionsAreAllAddedAsQueryConditions(): void {
|
||||||
$this->query->method('execute')->willReturn([]);
|
$this->query->method('execute')->willReturn([]);
|
||||||
$expected_conditions= [
|
|
||||||
['dummy_field', 'dummy_value', '<'],
|
$this->query->expects($this->exactly(4))->method('condition')->withConsecutive(
|
||||||
['dummy_field_2', 'dummy_value_2', '='],
|
['dummy_field', 'dummy_value', '<'],
|
||||||
['dummy_date_field', date('Y-m-d', strtotime('- 3 months')), "<"],
|
['dummy_field_2', 'dummy_value_2', '='],
|
||||||
['type', 'dummy_bundle', '=']
|
['dummy_date_field', date('Y-m-d', strtotime('- 3 months')), "<"],
|
||||||
];
|
['type', 'dummy_bundle', '=']);
|
||||||
$this->utils->expects($this->once())->method('addConditionsToQuery')->with($this->query, $expected_conditions);
|
|
||||||
$this->updater->update([['dummy_field', 'dummy_value', '<'], ['dummy_field_2', 'dummy_value_2', '='] ], '3 months', ['dummy_field' => 4]);
|
$this->updater->update([['dummy_field', 'dummy_value', '<'], ['dummy_field_2', 'dummy_value_2', '='] ], '3 months', ['dummy_field' => 4]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -12,24 +12,14 @@ class UtilsTest extends UnitTestCase{
|
|||||||
$this->utils = new Utils($this->entityTypeManager);
|
$this->utils = new Utils($this->entityTypeManager);
|
||||||
$this->storage = $this->getMockBuilder('\\Drupal\\Core\\Entity\\EntityStorageInterface')->getMock();
|
$this->storage = $this->getMockBuilder('\\Drupal\\Core\\Entity\\EntityStorageInterface')->getMock();
|
||||||
$this->query = $this->getMockBuilder('\\Drupal\\Core\\Entity\\Query\\QueryInterface')->getMock();
|
$this->query = $this->getMockBuilder('\\Drupal\\Core\\Entity\\Query\\QueryInterface')->getMock();
|
||||||
$this->entityTypeManager->method('getStorage')->willReturn($this->storage);
|
$this->entityTypeManager->method('getStorage')->willReturn($this->storage);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testGetTidByNameGetsTid():void {
|
public function testGetTidByNameGetsTid():void {
|
||||||
$this->entityTypeManager->method('getStorage')->willReturn($this->storage);
|
|
||||||
$term_entity = $this->getMockBuilder('\\Drupal\\Core\\Entity\\EntityBase')->disableOriginalConstructor()->getMock();
|
$term_entity = $this->getMockBuilder('\\Drupal\\Core\\Entity\\EntityBase')->disableOriginalConstructor()->getMock();
|
||||||
$term_entity->expects($this->once())->method('id')->willReturn('3');
|
$term_entity->expects($this->once())->method('id')->willReturn('3');
|
||||||
$this->storage->expects($this->once())->method('loadByProperties')->with(['name' => 'foo', 'vid' => 'bar'])
|
$this->storage->expects($this->once())->method('loadByProperties')->with(['name' => 'foo', 'vid' => 'bar'])
|
||||||
->willReturn([$term_entity]);
|
->willReturn([$term_entity]);
|
||||||
$this->assertEquals($this->utils->getTidByName('foo', 'bar'), 3);
|
$this->assertEquals($this->utils->getTidByName('foo', 'bar'), 3);
|
||||||
}
|
}
|
||||||
public function testAddConditionToQueryAddsEqualsIfNoOperatorProvided():void {
|
}
|
||||||
$this->query->expects($this->exactly(1))->method('condition')->with("1", "2", "=");
|
|
||||||
$this->utils->addConditionsToQuery($this->query, [["1","2"]]);
|
|
||||||
|
|
||||||
}
|
|
||||||
public function testAddConditionToQueryAddsTheRightAmountOfConditions():void {
|
|
||||||
$this->query->expects($this->exactly(4))->method('condition');
|
|
||||||
$this->utils->addConditionsToQuery($this->query, [["1","2","3"], ["lk", "n", "kk"], ['sfd', 'ds', 'fds'], ["1","2","3"]]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
Reference in New Issue
Block a user