2022-05-15 15:13:53 +00:00
|
|
|
<?php declare(strict_types = 1);
|
|
|
|
|
|
|
|
namespace Drupal\Tests\opencase\Unit;
|
|
|
|
|
|
|
|
use Drupal\Tests\UnitTestCase;
|
|
|
|
use Drupal\opencase\TimeBasedFieldUpdater;
|
|
|
|
|
|
|
|
class TimeBasedFieldUpdaterTest extends UnitTestCase{
|
|
|
|
|
|
|
|
function setUp():void {
|
2022-05-15 17:49:02 +00:00
|
|
|
/** @var \Drupal\opencase\Utils&\PHPUnit\Framework\MockObject\MockObject $utils */
|
2022-05-15 18:22:30 +00:00
|
|
|
$this->utils = $this->getMockBuilder('\\Drupal\\opencase\\Utils')->disableOriginalConstructor()->getMock();
|
2022-05-15 17:49:02 +00:00
|
|
|
/** @var \Drupal\core\Entity\EntityTypeManagerInterface&\PHPUnit\Framework\MockObject\MockObject $entityTypeManager */
|
2022-05-15 15:13:53 +00:00
|
|
|
$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);
|
2022-05-15 17:49:02 +00:00
|
|
|
$this->updater = new TimeBasedFieldUpdater($this->entityTypeManager, $this->utils, 'dummy_entity_type', 'dummy_bundle', 'dummy_date_field');
|
|
|
|
|
2022-05-15 15:13:53 +00:00
|
|
|
}
|
|
|
|
function testFieldIsUpdatedOnEntityReturnedByQuery():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);
|
2022-05-15 17:49:02 +00:00
|
|
|
$this->updater->update([], '3 months', ['dummy_field' => 4]);
|
|
|
|
$this->assertEquals($this->entity->dummy_field, 4);
|
2022-05-15 15:13:53 +00:00
|
|
|
}
|
|
|
|
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]]);
|
2022-05-15 17:49:02 +00:00
|
|
|
$this->updater->update([], '3 months', ['dummy_field' => 4]);
|
|
|
|
$this->assertEquals($this->entity->dummy_field, 4);
|
|
|
|
$this->assertEquals($this->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);
|
|
|
|
$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);
|
|
|
|
|
2022-05-15 15:13:53 +00:00
|
|
|
}
|
2022-05-15 17:49:02 +00:00
|
|
|
|
|
|
|
function testBundleAndDateAndExtraConditionsAreAllAddedAsQueryConditions(): void {
|
|
|
|
$this->query->method('execute')->willReturn([]);
|
|
|
|
$expected_conditions= [
|
|
|
|
['dummy_field', 'dummy_value', '<'],
|
|
|
|
['dummy_field_2', 'dummy_value_2', '='],
|
|
|
|
['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]);
|
|
|
|
}
|
2022-05-15 15:13:53 +00:00
|
|
|
}
|