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([]); $this->query->expects($this->exactly(4))->method('condition')->withConsecutive( ['dummy_field', 'dummy_value', '<'], ['dummy_field_2', 'dummy_value_2', '='], ['dummy_date_field', date('Y-m-d', strtotime('- 3 months')), "<"], ['type', 'dummy_bundle', '=']); $this->updater->update([['dummy_field', 'dummy_value', '<'], ['dummy_field_2', 'dummy_value_2', '='] ], '3 months', ['dummy_field' => 4]); } }