You are here

public function SchedulerHooksTest::testList in Scheduler 2.x

Covers hook_scheduler_list() and hook_scheduler_{type}_list()

These hooks allow other modules to add more entity ids into the list being processed. In real scenarios, the third-party module would likely have more complex data structures and/or tables from which to identify the ids to add. In this test, to keep it simple, we identify entities simply by title.

@dataProvider dataStandardEntityTypes()

File

tests/src/Functional/SchedulerHooksTest.php, line 102

Class

SchedulerHooksTest
Tests the API hook functions of the Scheduler module.

Namespace

Drupal\Tests\scheduler\Functional

Code

public function testList($entityTypeId, $bundle) {
  $storage = $this
    ->entityStorageObject($entityTypeId);
  $this
    ->drupalLogin($this->schedulerUser);

  // Create test entities using the standard scheduler test entity types.
  // Entity 1 is not published and has no publishing date set. The test API
  // module will add this entity into the list to be published using an
  // implementation of general hook_scheduler_list() function. Entity 2 is
  // similar but will be added via the hook_scheduler_{type}_list() function.
  $entity1 = $this
    ->createEntity($entityTypeId, $bundle, [
    'status' => FALSE,
    'title' => "Pink {$entityTypeId} list publish me",
  ]);
  $entity2 = $this
    ->createEntity($entityTypeId, $bundle, [
    'status' => FALSE,
    'title' => "Purple {$entityTypeId} list publish me",
  ]);

  // Entity 3 is published and has no unpublishing date set. The test API
  // module will add this entity into the list to be unpublished.
  $entity3 = $this
    ->createEntity($entityTypeId, $bundle, [
    'status' => TRUE,
    'title' => "Pink {$entityTypeId} list unpublish me",
  ]);
  $entity4 = $this
    ->createEntity($entityTypeId, $bundle, [
    'status' => TRUE,
    'title' => "Purple {$entityTypeId} list unpublish me",
  ]);

  // Before cron, check that entity 1 and 2 are unpublished and entity 3 and 4
  // are published.
  $this
    ->assertFalse($entity1
    ->isPublished(), "Before cron, {$entityTypeId} 1 '{$entity1->label()}' should be unpublished.");
  $this
    ->assertFalse($entity2
    ->isPublished(), "Before cron, {$entityTypeId} 2 '{$entity2->label()}' should be unpublished.");
  $this
    ->assertTrue($entity3
    ->isPublished(), "Before cron, {$entityTypeId} 3 '{$entity3->label()}' should be published.");
  $this
    ->assertTrue($entity4
    ->isPublished(), "Before cron, {$entityTypeId} 4 '{$entity4->label()}' should be published.");

  // Run cron and refresh the entities.
  scheduler_cron();
  $storage
    ->resetCache();
  for ($i = 1; $i <= 4; $i++) {
    ${"entity{$i}"} = $storage
      ->load(${"entity{$i}"}
      ->id());
  }

  // Check tha entity 1 and 2 have been published.
  $this
    ->assertTrue($entity1
    ->isPublished(), "After cron, {$entityTypeId} 1 '{$entity1->label()}' should be published.");
  $this
    ->assertTrue($entity2
    ->isPublished(), "After cron, {$entityTypeId} 2 '{$entity2->label()}' should be published.");

  // Check that entity 3 and 4 have been unpublished.
  $this
    ->assertFalse($entity3
    ->isPublished(), "After cron, {$entityTypeId} 3 '{$entity3->label()}' should be unpublished.");
  $this
    ->assertFalse($entity4
    ->isPublished(), "After cron, {$entityTypeId} 4 '{$entity4->label()}' should be unpublished.");
}