You are here

public function SchedulerFunctionalTest::testPastDates in Scheduler 7

Test the different options for past publication dates.

File

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

Class

SchedulerFunctionalTest
Tests the scheduler interface.

Code

public function testPastDates() {

  // Log in.
  $this
    ->drupalLogin($this->adminUser);

  // Create an unpublished page node.
  $node = $this
    ->drupalCreateNode(array(
    'type' => 'page',
    'status' => FALSE,
  ));

  // Test the default behavior: an error message should be shown when the user
  // enters a publication date that is in the past.
  $edit = array(
    'title' => $this
      ->randomName(),
    'publish_on' => format_date(strtotime('-1 day', REQUEST_TIME), 'custom', 'Y-m-d H:i:s'),
  );
  $this
    ->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
  $this
    ->assertText("The 'publish on' date must be in the future", 'An error message is shown when the publication date is in the past and the "error" behavior is chosen.');

  // Test the 'publish' behavior: the node should be published immediately.
  variable_set('scheduler_publish_past_date_page', 'publish');
  $this
    ->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
  $this
    ->assertNoText("The 'publish on' date must be in the future", 'No error message is shown when the publication date is in the past and the "publish" behavior is chosen.');
  $this
    ->assertText(sprintf('%s %s has been updated.', 'Basic page', $edit['title']), 'The node is saved successfully when the publication date is in the past and the "publish" behavior is chosen.');

  // Reload the changed node and check that it is published.
  $node = node_load($node->nid, NULL, TRUE);
  $this
    ->assertTrue($node->status, 'The node has been published immediately when the publication date is in the past and the "publish" behavior is chosen.');

  // Test the 'schedule' behavior: the node should be unpublished and become
  // published on the next cron run.
  variable_set('scheduler_publish_past_date_page', 'schedule');
  $this
    ->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
  $this
    ->assertNoText("The 'publish on' date must be in the future", 'No error message is shown when the publication date is in the past and the "schedule" behavior is chosen.');
  $this
    ->assertText(sprintf('%s %s has been updated.', 'Basic page', check_plain($edit['title'])), 'The node is saved successfully when the publication date is in the past and the "schedule" behavior is chosen.');
  $this
    ->assertText(sprintf('This post is unpublished and will be published %s.', $edit['publish_on']), 'The node is scheduled to be published when the publication date is in the past and the "schedule" behavior is chosen.');

  // Reload the node and check that it is unpublished but scheduled correctly.
  $node = node_load($node->nid, NULL, TRUE);
  $this
    ->assertFalse($node->status, 'The node has been unpublished when the publication date is in the past and the "schedule" behavior is chosen.');
  $this
    ->assertEqual(format_date($node->publish_on, 'custom', 'Y-m-d H:i:s'), $edit['publish_on'], 'The node is scheduled for the required date');

  // Simulate a cron run and check that the node is published.
  scheduler_cron();
  $node = node_load($node->nid, NULL, TRUE);
  $this
    ->assertTrue($node->status, 'The node with publication date in the past and the "schedule" behavior has now been published by cron.');
}