You are here

public function SchedulerApiTest::testHookPublishUnpublishAction in Scheduler 8

Test when other modules process the publish and unpublish actions.

This covers hook_scheduler_publish_action and hook_scheduler_unpublish_action.

File

tests/src/Functional/SchedulerApiTest.php, line 437

Class

SchedulerApiTest
Tests the API of the Scheduler module.

Namespace

Drupal\Tests\scheduler\Functional

Code

public function testHookPublishUnpublishAction() {
  $this
    ->drupalLogin($this->schedulerUser);

  // Create test nodes.
  $node1 = $this
    ->drupalCreateNode([
    'type' => $this->type,
    'status' => FALSE,
    'title' => 'Red will cause a failure on publishing',
    'publish_on' => strtotime('-1 day'),
  ]);
  $node2 = $this
    ->drupalCreateNode([
    'type' => $this->type,
    'status' => TRUE,
    'title' => 'Orange will be unpublished by the API test module not Scheduler',
    'unpublish_on' => strtotime('-1 day'),
  ]);
  $node3 = $this
    ->drupalCreateNode([
    'type' => $this->type,
    'status' => FALSE,
    'title' => 'Yellow will be published by the API test module not Scheduler',
    'publish_on' => strtotime('-1 day'),
  ]);

  // 'green' nodes will have both fields hidden so is harder to test manually.
  // Therefore introduce a different colour.
  $node4 = $this
    ->drupalCreateNode([
    'type' => $this->type,
    'status' => TRUE,
    'title' => 'Blue will cause a failure on unpublishing',
    'unpublish_on' => strtotime('-1 day'),
  ]);

  // Simulate a cron run.
  scheduler_cron();

  // Check the red node.
  $this->nodeStorage
    ->resetCache([
    $node1
      ->id(),
  ]);
  $node1 = $this->nodeStorage
    ->load($node1
    ->id());
  $this
    ->assertFalse($node1
    ->isPublished(), 'The red node is still unpublished.');
  $this
    ->assertNotEmpty($node1->publish_on->value, 'The red node still has a publish-on date.');

  // Check the orange node.
  $this->nodeStorage
    ->resetCache([
    $node2
      ->id(),
  ]);
  $node2 = $this->nodeStorage
    ->load($node2
    ->id());
  $this
    ->assertFalse($node2
    ->isPublished(), 'The orange node was unpublished by the API test module.');
  $this
    ->assertNotEmpty(stristr($node2->title->value, 'unpublishing processed by API test module'), 'The orange node was processed by the API test module.');
  $this
    ->assertEmpty($node2->unpublish_on->value, 'The orange node no longer has an unpublish-on date.');

  // Check the yellow node.
  $this->nodeStorage
    ->resetCache([
    $node3
      ->id(),
  ]);
  $node3 = $this->nodeStorage
    ->load($node3
    ->id());
  $this
    ->assertTrue($node3
    ->isPublished(), 'The yellow node was published by the API test module.');
  $this
    ->assertNotEmpty(stristr($node3->title->value, 'publishing processed by API test module'), 'The yellow node was processed by the API test module.');
  $this
    ->assertEmpty($node3->publish_on->value, 'The yellow node no longer has a publish-on date.');

  // Check the blue node.
  $this->nodeStorage
    ->resetCache([
    $node4
      ->id(),
  ]);
  $node4 = $this->nodeStorage
    ->load($node4
    ->id());
  $this
    ->assertTrue($node4
    ->isPublished(), 'The green node is still published.');
  $this
    ->assertNotEmpty($node4->unpublish_on->value, 'The green node still has an unpublish-on date.');
}