You are here

public function SchedulerRulesEventsTest::testRulesEventsBoth in Scheduler 8

Same name and namespace in other branches
  1. 2.x tests/src/Functional/SchedulerRulesEventsTest.php \Drupal\Tests\scheduler\Functional\SchedulerRulesEventsTest::testRulesEventsBoth()

Tests all six events related to publishing and unpublishing a node.

File

tests/src/Functional/SchedulerRulesEventsTest.php, line 204

Class

SchedulerRulesEventsTest
Tests the six events that Scheduler provides for use in Rules module.

Namespace

Drupal\Tests\scheduler\Functional

Code

public function testRulesEventsBoth() {
  $assert = $this
    ->assertSession();

  // Create a node with both publish-on and unpublish-on dates, and check that
  // both event 1 and event 4 are triggered.
  $edit = [
    'title[0][value]' => 'Create node with both dates',
    'publish_on[0][value][date]' => date('Y-m-d', time() + 3),
    'publish_on[0][value][time]' => date('H:i:s', time() + 3),
    'unpublish_on[0][value][date]' => date('Y-m-d', time() + 4),
    'unpublish_on[0][value][time]' => date('H:i:s', time() + 4),
    'body[0][value]' => $this
      ->randomString(30),
  ];
  $this
    ->drupalPostForm('node/add/' . $this->type, $edit, 'Save');
  $node = $this
    ->drupalGetNodeByTitle($edit['title[0][value]']);
  $assert
    ->pageTextContains($this->message[1]);
  $assert
    ->pageTextNotContains($this->message[2]);
  $assert
    ->pageTextNotContains($this->message[3]);
  $assert
    ->pageTextContains($this->message[4]);
  $assert
    ->pageTextNotContains($this->message[5]);
  $assert
    ->pageTextNotContains($this->message[6]);

  // Edit this node and check that events 2 and 5 are triggered.
  $edit = [
    'title[0][value]' => 'Edit node with both dates',
    'body[0][value]' => $this
      ->randomString(30),
  ];
  $this
    ->drupalPostForm('node/' . $node
    ->id() . '/edit', $edit, 'Save');
  $node = $this
    ->drupalGetNodeByTitle($edit['title[0][value]']);
  $assert
    ->pageTextNotContains($this->message[1]);
  $assert
    ->pageTextContains($this->message[2]);
  $assert
    ->pageTextNotContains($this->message[3]);
  $assert
    ->pageTextNotContains($this->message[4]);
  $assert
    ->pageTextContains($this->message[5]);
  $assert
    ->pageTextNotContains($this->message[6]);

  // Delay to ensure that the dates are now in the past so that the node will
  // be processed during cron, and assert that events 3, 5 & 6 are triggered.
  sleep(6);
  $this
    ->cronRun();
  $this
    ->drupalGet('admin/reports/dblog');
  $assert
    ->pageTextNotContains($this->message[1]);
  $assert
    ->pageTextNotContains($this->message[2]);
  $assert
    ->pageTextContains($this->message[3]);
  $assert
    ->pageTextNotContains($this->message[4]);
  $assert
    ->pageTextContains($this->message[5]);
  $assert
    ->pageTextContains($this->message[6]);
}