You are here

public function SchedulerRulesTest::testRulesEvents in Scheduler 7

Tests the six events provided by Scheduler.

This class tests all six events provided by Scheduler, by creating six rules which are all active throughout the test. They are all checked in this one test class to make the tests stronger, as this will show not only that the correct events are triggered in the right places, but also that they are not triggered in the wrong places.

File

tests/scheduler.test, line 1578
Scheduler module test case file.

Class

SchedulerRulesTest
Tests Schedulers interaction with the Rules module.

Code

public function testRulesEvents() {

  // Create six reaction rules, one for each event that Scheduler triggers.
  // phpcs:set Drupal.Arrays.Array lineLimit 150
  $rule_data = array(
    1 => array(
      'scheduler_new_node_is_scheduled_for_publishing_event',
      'A new node is created and is scheduled for publishing.',
    ),
    2 => array(
      'scheduler_existing_node_is_scheduled_for_publishing_event',
      'An existing node is saved and is scheduled for publishing.',
    ),
    3 => array(
      'scheduler_node_has_been_published_event',
      'Scheduler has published this node during cron.',
    ),
    4 => array(
      'scheduler_new_node_is_scheduled_for_unpublishing_event',
      'A new node is created and is scheduled for unpublishing.',
    ),
    5 => array(
      'scheduler_existing_node_is_scheduled_for_unpublishing_event',
      'An existing node is saved and is scheduled for unpublishing.',
    ),
    6 => array(
      'scheduler_node_has_been_unpublished_event',
      'Scheduler has unpublished this node during cron.',
    ),
  );

  // phpcs:set Drupal.Arrays.Array lineLimit 120
  foreach ($rule_data as $i => $values) {
    list($event_name, $description) = $values;
    $rule = rules_reaction_rule();
    $this->eventMessage[$i] = 'RULE ' . $i . '. ' . $description;
    $rule
      ->event($event_name)
      ->action('drupal_message', array(
      'message' => $this->eventMessage[$i],
    ));
    $rule
      ->access();
    $rule
      ->integrityCheck();
    $rule
      ->save('rule_id_' . $i, $this->eventMessage[$i]);
  }
  $this
    ->drupalLogin($this->adminUser);

  // Create a node without any scheduled dates, using node/add/page not
  // drupalCreateNode(), and check that no events are triggered.
  $edit = array(
    'title' => 'Test for no events on creation',
    'body[' . LANGUAGE_NONE . '][0][value]' => $this
      ->randomString(30),
  );
  $this
    ->drupalPost('node/add/page', $edit, t('Save'));
  $node = $this
    ->drupalGetNodeByTitle($edit['title']);
  $this
    ->checkEventText();

  // Edit the node and check that no events are triggered.
  $edit = array(
    'title' => 'Test for no events on edit',
    'body[' . LANGUAGE_NONE . '][0][value]' => $this
      ->randomString(30),
  );
  $this
    ->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
  $this
    ->checkEventText();

  // Create a new node with a publish-on date, and check that only event 1 is
  // triggered. Use time() not REQUEST_TIME to guarantee the datetime is in
  // the future but only by a few seconds.
  $edit = array(
    'title' => 'Create node with publish-on date',
    'publish_on' => date('Y-m-d H:i:s', time() + 3),
    'body[' . LANGUAGE_NONE . '][0][value]' => $this
      ->randomString(30),
  );
  $this
    ->drupalPost('node/add/page', $edit, t('Save'));
  $node = $this
    ->drupalGetNodeByTitle($edit['title']);
  $this
    ->checkEventText(array(
    1,
  ));

  // Edit this node and check that only event 2 is triggered.
  $edit = array(
    'title' => 'Edit node with publish-on date',
    'body[' . LANGUAGE_NONE . '][0][value]' => $this
      ->randomString(30),
  );
  $this
    ->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
  $this
    ->checkEventText(array(
    2,
  ));

  // Delay before running cron to ensure that the date will be in the past, so
  // that the node gets processed. Then assert that only event 3 is triggered.
  sleep(5);
  $this
    ->cronRun();
  $this
    ->drupalGet('admin/reports/dblog');
  $this
    ->checkEventText(array(
    3,
  ));

  // Create a new node with an unpublish-on date, and check that only event 4
  // is triggered.
  $edit = array(
    'title' => 'Create node with unpublish-on date',
    'unpublish_on' => date('Y-m-d H:i:s', time() + 3),
    'body[' . LANGUAGE_NONE . '][0][value]' => $this
      ->randomString(30),
  );
  $this
    ->drupalPost('node/add/page', $edit, t('Save'));
  $node = $this
    ->drupalGetNodeByTitle($edit['title']);
  $this
    ->checkEventText(array(
    4,
  ));

  // Edit this node and check that only event 5 is triggered.
  $edit = array(
    'title' => 'Edit node with unpublish-on date',
    'body[' . LANGUAGE_NONE . '][0][value]' => $this
      ->randomString(30),
  );
  $this
    ->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
  $this
    ->checkEventText(array(
    5,
  ));

  // Delay before running cron to ensure that the date will be in the past, so
  // that the node gets processed. Then assert that event 6 is triggered.
  sleep(5);
  $this
    ->cronRun();
  $this
    ->drupalGet('admin/reports/dblog');
  $this
    ->checkEventText(array(
    6,
  ));

  // Create a new node with both publish-on and unpublish-on dates, and check
  // that events 1 and event 4 are both triggered.
  $edit = array(
    'title' => 'Create node with both dates',
    'publish_on' => date('Y-m-d H:i:s', time() + 3),
    'unpublish_on' => date('Y-m-d H:i:s', time() + 4),
    'body[' . LANGUAGE_NONE . '][0][value]' => $this
      ->randomString(30),
  );
  $this
    ->drupalPost('node/add/page', $edit, t('Save'));
  $node = $this
    ->drupalGetNodeByTitle($edit['title']);
  $this
    ->checkEventText(array(
    1,
    4,
  ));

  // Edit this node and check that events 2 and 5 are triggered.
  $edit = array(
    'title' => 'Edit node with both dates',
    'body[' . LANGUAGE_NONE . '][0][value]' => $this
      ->randomString(30),
  );
  $this
    ->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
  $this
    ->checkEventText(array(
    2,
    5,
  ));

  // Delay before running cron to ensure that the dates will be in the past.
  // Then assert that events 3, 5 & 6 are triggered.
  sleep(6);
  $this
    ->cronRun();
  $this
    ->drupalGet('admin/reports/dblog');
  $this
    ->checkEventText(array(
    3,
    5,
    6,
  ));
}