diff --git a/modules/opencase_defaults/tests/src/Functional/LoadTest.php b/modules/opencase_defaults/tests/src/Functional/LoadTest.php deleted file mode 100644 index d76f7a3..0000000 --- a/modules/opencase_defaults/tests/src/Functional/LoadTest.php +++ /dev/null @@ -1,46 +0,0 @@ -user = $this->drupalCreateUser(['administer site configuration']); - $this->drupalLogin($this->user); - } - - /** - * Tests that the home page loads with a 200 response. - */ - public function testLoad() { - $this->drupalGet(Url::fromRoute('')); - $this->assertSession()->statusCodeEquals(200); - } - -} diff --git a/modules/opencase_entities/tests/src/Functional/LoadTest.php b/modules/opencase_entities/tests/src/Functional/LoadTest.php deleted file mode 100644 index 2669263..0000000 --- a/modules/opencase_entities/tests/src/Functional/LoadTest.php +++ /dev/null @@ -1,46 +0,0 @@ -user = $this->drupalCreateUser(['administer site configuration']); - $this->drupalLogin($this->user); - } - - /** - * Tests that the home page loads with a 200 response. - */ - public function testLoad() { - $this->drupalGet(Url::fromRoute('')); - $this->assertSession()->statusCodeEquals(200); - } - -} diff --git a/src/TimeBasedFieldUpdater.php b/src/TimeBasedFieldUpdater.php index 2195946..731ac37 100644 --- a/src/TimeBasedFieldUpdater.php +++ b/src/TimeBasedFieldUpdater.php @@ -2,23 +2,27 @@ namespace Drupal\opencase; use Drupal; +use Drupal\Core\Entity\EntityTypeManagerInterface; final class TimeBasedFieldUpdater { + + private EntityTypeManagerInterface $entityTypeManager; private string $date_field; private string $entity_type; private array $conditions; private string $date_format; - final public function __construct($entity_type, $date_field, $conditions = [], $date_format = 'Y-m-d') - { + final public function __construct(EntityTypeManagerInterface $entityTypeManager, string $entity_type, string $date_field, array $conditions = [], string $date_format = 'Y-m-d') + { + $this->entityTypeManager = $entityTypeManager; $this->date_field = $date_field; $this->conditions = $conditions; $this->date_format = $date_format; $this->entity_type = $entity_type; } - final public function update($time_elapsed, $old_values, $new_values): void { - $query = Drupal::entityQuery($this->entity_type); + final public function update(string $time_elapsed, array $old_values, array $new_values): void { + $query = $this->entityTypeManager->getStorage($this->entity_type)->getQuery(); foreach($this->conditions as $cond_field=>$cond_value) { $query->condition($cond_field, $cond_value); } @@ -27,9 +31,9 @@ final class TimeBasedFieldUpdater { } $query->condition($this->date_field, date($this->date_format, strtotime('-'.$time_elapsed)), "<"); foreach($query->execute() as $id) { - $entity = Drupal::entityTypeManager()->getStorage($this->entity_type)->load($id); + $entity = $this->entityTypeManager->getStorage($this->entity_type)->load($id); foreach($new_values as $new_field=>$new_value) { - $entity->set($new_field, $new_value); + $entity->$new_field = $new_value; } $entity->save(); } diff --git a/tests/src/Functional/LoadTest.php b/tests/src/Functional/LoadTest.php deleted file mode 100644 index 87ac8af..0000000 --- a/tests/src/Functional/LoadTest.php +++ /dev/null @@ -1,46 +0,0 @@ -user = $this->drupalCreateUser(['administer site configuration']); - $this->drupalLogin($this->user); - } - - /** - * Tests that the home page loads with a 200 response. - */ - public function testLoad() { - $this->drupalGet(Url::fromRoute('')); - $this->assertSession()->statusCodeEquals(200); - } - -} diff --git a/tests/src/Unit/TimeBasedFieldUpdaterTest.php b/tests/src/Unit/TimeBasedFieldUpdaterTest.php new file mode 100644 index 0000000..19d6c92 --- /dev/null +++ b/tests/src/Unit/TimeBasedFieldUpdaterTest.php @@ -0,0 +1,35 @@ +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); + } + 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); + $updater = new TimeBasedFieldUpdater($this->entityTypeManager, 'dummy_entity', 'dummy_date_field'); + $updater->update('3 months', ['dummy_status_field' => 3], ['dummy_status_field' => 4]); + $this->assertEquals($this->entity->dummy_status_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]]); + $updater = new TimeBasedFieldUpdater($this->entityTypeManager, 'dummy_entity', 'dummy_date_field'); + $updater->update('3 months', ['dummy_status_field' => 3], ['dummy_status_field' => 4]); + $this->assertEquals($this->entity->dummy_status_field, 4); + $this->assertEquals($this->entity2->dummy_status_field, 4); + } +} \ No newline at end of file