You are here

public function SchedulerApiTest::testApiNodeAction in Scheduler 8

Covers six events.

The events allow other modules to react to the Scheduler process being run. The API test implementations of the event listeners alter the nodes 'promote' and 'sticky' settings and changes the title.

File

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

Class

SchedulerApiTest
Tests the API of the Scheduler module.

Namespace

Drupal\Tests\scheduler\Functional

Code

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

  // Create a test node. Having the 'approved' fields here would complicate
  // the tests, so use the ordinary page type.
  $settings = [
    'publish_on' => strtotime('-1 day'),
    'type' => $this->type,
    'promote' => FALSE,
    'sticky' => FALSE,
    'title' => 'API TEST node action',
  ];
  $node = $this
    ->drupalCreateNode($settings);

  // Check that the 'sticky' and 'promote' fields are off for the new node.
  $this
    ->assertFalse($node
    ->isSticky(), 'The unpublished node is not sticky.');
  $this
    ->assertFalse($node
    ->isPromoted(), 'The unpublished node is not promoted.');

  // Run cron and check that hook_scheduler_api() has executed correctly, by
  // verifying that the node has become promoted and is sticky.
  scheduler_cron();
  $this->nodeStorage
    ->resetCache([
    $node
      ->id(),
  ]);
  $node = $this->nodeStorage
    ->load($node
    ->id());
  $this
    ->assertTrue($node
    ->isSticky(), 'API action "PRE_PUBLISH" has changed the node to sticky.');
  $this
    ->assertTrue($node
    ->isPromoted(), 'API action "PUBLISH" has changed the node to promoted.');

  // Now set a date for unpublishing the node. Ensure 'sticky' and 'promote'
  // are set, so that the assertions are not affected by any failures above.
  $node
    ->set('unpublish_on', strtotime('-1 day'))
    ->set('sticky', TRUE)
    ->set('promote', TRUE)
    ->save();

  // Run cron and check that hook_scheduler_api() has executed correctly, by
  // verifying that the node is not promoted and no longer sticky.
  scheduler_cron();
  $this->nodeStorage
    ->resetCache([
    $node
      ->id(),
  ]);
  $node = $this->nodeStorage
    ->load($node
    ->id());
  $this
    ->assertFalse($node
    ->isSticky(), 'API action "PRE_UNPUBLISH" has changed the node to not sticky.');
  $this
    ->assertFalse($node
    ->isPromoted(), 'API action "UNPUBLISH" has changed the node to not promoted.');

  // Turn on immediate publication when a publish date is in the past.
  $this->nodetype
    ->setThirdPartySetting('scheduler', 'publish_past_date', 'publish')
    ->save();

  // Ensure 'sticky' and 'promote' are not set, so that the assertions are not
  // affected by any failures above.
  $node
    ->set('sticky', FALSE)
    ->set('promote', FALSE)
    ->save();

  // Edit the node and set a publish-on date in the past.
  $edit = [
    'publish_on[0][value][date]' => date('Y-m-d', strtotime('-2 day', $this->requestTime)),
    'publish_on[0][value][time]' => date('H:i:s', strtotime('-2 day', $this->requestTime)),
  ];
  $this
    ->drupalPostForm('node/' . $node
    ->id() . '/edit', $edit, 'Save');

  // Verify that the values have been altered as expected.
  $this->nodeStorage
    ->resetCache([
    $node
      ->id(),
  ]);
  $node = $this->nodeStorage
    ->load($node
    ->id());
  $this
    ->assertTrue($node
    ->isSticky(), 'API action "PRE_PUBLISH_IMMEDIATELY" has changed the node to sticky.');
  $this
    ->assertTrue($node
    ->isPromoted(), 'API action "PUBLISH_IMMEDIATELY" has changed the node to promoted.');
  $this
    ->assertEquals('Published immediately', $node->title->value, 'API action "PUBLISH_IMMEDIATELY" has changed the node title correctly.');
}