You are here

public function SchedulerHooksTest::testListAlter in Scheduler 2.x

Covers hook_scheduler_list_alter() and hook_scheduler_{type}_list_alter()

These hook allows other modules to add or remove entity ids from the list to be processed.

@dataProvider dataStandardEntityTypes()

File

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

Class

SchedulerHooksTest
Tests the API hook functions of the Scheduler module.

Namespace

Drupal\Tests\scheduler\Functional

Code

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

  // Create test entities using the standard scheduler test entity types.
  // Entity 1 is set for scheduled publishing, but will be removed by the test
  // API generic hook_scheduler_list_alter() function. Entity 2 is similar but
  // is removed via the specifc hook_scheduler_{type}_list_alter() function.
  $entity1 = $this
    ->createEntity($entityTypeId, $bundle, [
    'status' => FALSE,
    'title' => "Pink {$entityTypeId} list_alter do not publish me",
    'publish_on' => strtotime('-1 day'),
  ]);
  $entity2 = $this
    ->createEntity($entityTypeId, $bundle, [
    'status' => FALSE,
    'title' => "Purple {$entityTypeId} list_alter do not publish me",
    'publish_on' => strtotime('-1 day'),
  ]);

  // Entity 3 is not published and has no publishing date set. The test module
  // generic hook_scheduler_list_alter() function will add a date and add the
  // id into the list to be published. Entity 4 is similar but the date and id
  // is added by the specifc hook_scheduler_{type}_list_alter() function.
  $entity3 = $this
    ->createEntity($entityTypeId, $bundle, [
    'status' => FALSE,
    'title' => "Pink {$entityTypeId} list_alter publish me",
  ]);
  $entity4 = $this
    ->createEntity($entityTypeId, $bundle, [
    'status' => FALSE,
    'title' => "Purple {$entityTypeId} list_alter publish me",
  ]);

  // Entity 5 is set for scheduled unpublishing, but will be removed by the
  // generic hook_scheduler_list_alter() function. Entity 6 is similar but is
  // removed by the specifc hook_scheduler_{type}_list_alter() function.
  $entity5 = $this
    ->createEntity($entityTypeId, $bundle, [
    'status' => TRUE,
    'title' => "Pink {$entityTypeId} list_alter do not unpublish me",
    'unpublish_on' => strtotime('-1 day'),
  ]);
  $entity6 = $this
    ->createEntity($entityTypeId, $bundle, [
    'status' => TRUE,
    'title' => "Purple {$entityTypeId} list_alter do not unpublish me",
    'unpublish_on' => strtotime('-1 day'),
  ]);

  // Entity 7 is published and has no unpublishing date set. The generic
  // hook_scheduler_list_alter() will add a date and add the id into the list
  // to be unpublished. Entity 8 is similar but the date and id will be added
  // by the specifc hook_scheduler_{type}_list_alter() function.
  $entity7 = $this
    ->createEntity($entityTypeId, $bundle, [
    'status' => TRUE,
    'title' => "Pink {$entityTypeId} list_alter unpublish me",
  ]);
  $entity8 = $this
    ->createEntity($entityTypeId, $bundle, [
    'status' => TRUE,
    'title' => "Purple {$entityTypeId} list_alter unpublish me",
  ]);

  // Before cron, check entities 1-4 are unpublished and 5-8 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
    ->assertFalse($entity3
    ->isPublished(), "Before cron, {$entityTypeId} 3 '{$entity3->label()}' should be unpublished.");
  $this
    ->assertFalse($entity4
    ->isPublished(), "Before cron, {$entityTypeId} 4 '{$entity4->label()}' should be unpublished.");
  $this
    ->assertTrue($entity5
    ->isPublished(), "Before cron, {$entityTypeId} 5 '{$entity5->label()}' should be published.");
  $this
    ->assertTrue($entity6
    ->isPublished(), "Before cron, {$entityTypeId} 6 '{$entity6->label()}' should be published.");
  $this
    ->assertTrue($entity7
    ->isPublished(), "Before cron, {$entityTypeId} 7 '{$entity7->label()}' should be published.");
  $this
    ->assertTrue($entity8
    ->isPublished(), "Before cron, {$entityTypeId} 8 '{$entity8->label()}' should be published.");

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

  // After cron, check that entities 1-2 remain unpublished, 3-4 have now
  // been published, 5-6 remain published and 7-8 have been unpublished.
  $this
    ->assertFalse($entity1
    ->isPublished(), "After cron, {$entityTypeId} 1 '{$entity1->label()}' should be unpublished.");
  $this
    ->assertFalse($entity2
    ->isPublished(), "After cron, {$entityTypeId} 2 '{$entity2->label()}' should be unpublished.");
  $this
    ->assertTrue($entity3
    ->isPublished(), "After cron, {$entityTypeId} 3 '{$entity3->label()}' should be published.");
  $this
    ->assertTrue($entity4
    ->isPublished(), "After cron, {$entityTypeId} 4 '{$entity4->label()}' should be published.");
  $this
    ->assertTrue($entity5
    ->isPublished(), "After cron, {$entityTypeId} 5 '{$entity5->label()}' should be published.");
  $this
    ->assertTrue($entity6
    ->isPublished(), "After cron, {$entityTypeId} 6 '{$entity6->label()}' should be published.");
  $this
    ->assertFalse($entity7
    ->isPublished(), "After cron, {$entityTypeId} 7 '{$entity7->label()}' should be unpublished.");
  $this
    ->assertFalse($entity8
    ->isPublished(), "After cron, {$entityTypeId} 8 '{$entity8->label()}' should be unpublished.");
}