You are here

public function SchedulerHooksTest::testPublishUnpublishProcess in Scheduler 2.x

Tests when other modules execute the 'publish' and 'unpublish' processes.

This test covers: hook_scheduler_publish_process() hook_scheduler_unpublish_process() hook_scheduler_{type}_publish_process() hook_scheduler_{type}_unpublish_process()

@dataProvider dataStandardEntityTypes()

File

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

Class

SchedulerHooksTest
Tests the API hook functions of the Scheduler module.

Namespace

Drupal\Tests\scheduler\Functional

Code

public function testPublishUnpublishProcess($entityTypeId, $bundle) {

  // $this->drupalLogin($this->schedulerUser);
  $storage = $this
    ->entityStorageObject($entityTypeId);

  // Create test entities.
  $entity1 = $this
    ->createEntity($entityTypeId, $bundle, [
    'status' => FALSE,
    'title' => "Red {$entityTypeId} will cause a failure on publishing",
    'publish_on' => strtotime('-1 day'),
  ]);
  $entity2 = $this
    ->createEntity($entityTypeId, $bundle, [
    'status' => TRUE,
    'title' => "Orange {$entityTypeId} will be unpublished by the API test module not Scheduler",
    'unpublish_on' => strtotime('-1 day'),
  ]);
  $entity3 = $this
    ->createEntity($entityTypeId, $bundle, [
    'status' => FALSE,
    'title' => "Yellow {$entityTypeId} will be published by the API test module not Scheduler",
    'publish_on' => strtotime('-1 day'),
  ]);

  // 'Green' will have both fields hidden so is harder to test manually.
  // Therefore introduce a different colour - Blue.
  $entity4 = $this
    ->createEntity($entityTypeId, $bundle, [
    'status' => TRUE,
    'title' => "Blue {$entityTypeId} will cause a failure on unpublishing",
    'unpublish_on' => strtotime('-1 day'),
  ]);

  // Simulate a cron run.
  scheduler_cron();

  // Check red.
  $storage
    ->resetCache([
    $entity1
      ->id(),
  ]);
  $entity1 = $storage
    ->load($entity1
    ->id());
  $this
    ->assertFalse($entity1
    ->isPublished(), 'Red should remain unpublished.');
  $this
    ->assertNotEmpty($entity1->publish_on->value, 'Red should still have a publish-on date.');

  // Check orange.
  $storage
    ->resetCache([
    $entity2
      ->id(),
  ]);
  $entity2 = $storage
    ->load($entity2
    ->id());
  $this
    ->assertFalse($entity2
    ->isPublished(), 'Orange should be unpublished.');
  $this
    ->assertStringContainsString('unpublishing processed by API test module', $entity2
    ->label(), 'Orange should be processed by the API test module.');
  $this
    ->assertEmpty($entity2->unpublish_on->value, 'Orange should not have an unpublish-on date.');

  // Check yellow.
  $storage
    ->resetCache([
    $entity3
      ->id(),
  ]);
  $entity3 = $storage
    ->load($entity3
    ->id());
  $this
    ->assertTrue($entity3
    ->isPublished(), 'Yellow should be published.');
  $this
    ->assertStringContainsString('publishing processed by API test module', $entity3
    ->label(), 'Yellow should be processed by the API test module.');
  $this
    ->assertEmpty($entity3->publish_on->value, 'Yellow should not have a publish-on date.');

  // Check blue.
  $storage
    ->resetCache([
    $entity4
      ->id(),
  ]);
  $entity4 = $storage
    ->load($entity4
    ->id());
  $this
    ->assertTrue($entity4
    ->isPublished(), 'Blue should remain published.');
  $this
    ->assertNotEmpty($entity4->unpublish_on->value, 'Blue should still have an unpublish-on date.');
}