This repository has been archived on 2022-07-12. You can view files and clone it, but cannot push or open issues or pull requests.
opencase/tests/src/Unit/TimeBasedFieldUpdaterTest.php

60 lines
3.6 KiB
PHP
Raw Normal View History

<?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 {
/** @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');
}
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);
$this->updater->update([], '3 months', ['dummy_field' => 4]);
$this->assertEquals($this->entity->dummy_field, 4);
}
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]]);
$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);
}
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]);
}
}