Commit 30e19ed5 authored by Jonathan Smith's avatar Jonathan Smith Committed by Jonathan Smith
Browse files

Issue #2750467 by jonathan1055: Added tests for scheduler token replacement

parent d1d3afb0
Loading
Loading
Loading
Loading
+56 −0
Changes for scheduler.test: 56 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -733,6 +733,62 @@ class SchedulerFunctionalTest extends SchedulerTestBase {
    // @see https://www.drupal.org/node/2707411
    // "Conflict between 'required publishing' and not having scheduler permission"
  }

  /**
   * Tests Scheduler token support.
   */
  public function testTokenReplacement() {
    // Log in.
    $this->drupalLogin($this->admin_user);

    // Define timestamps for consistent use when repeated throughout this test.
    $publish_on_timestamp = REQUEST_TIME + 3600;
    $unpublish_on_timestamp = REQUEST_TIME + 7200;

    // Create an unpublished page with scheduled dates.
    $settings = array(
      'type' => 'page',
      'status' => FALSE,
      'publish_on' => $publish_on_timestamp,
      'unpublish_on' => $unpublish_on_timestamp,
    );
    $node = $this->drupalCreateNode($settings);
    // Show that the node is scheduled.
    $this->drupalGet('admin/config/content/scheduler/list');

    // Create array of test case data.
    $test_cases = array(
      array('token_format' => '', 'date_format' => 'medium', 'custom' => ''),
      array('token_format' => ':long', 'date_format' => 'long', 'custom' => ''),
      array('token_format' => ':raw', 'date_format' => 'custom', 'custom' => 'U'),
      array('token_format' => ':custom:jS F g:ia e O', 'date_format' => 'custom', 'custom' => 'jS F g:ia e O'),
    );

    $langcode = LANGUAGE_NONE;
    foreach ($test_cases as $test_data) {
      // Edit the node and set the body tokens to use the format being tested.
      // The tokens will not be replaced automatically when the node is views,
      // but using the body field is a convenient way to store the data.
      $edit = array(
        "body[$langcode][0][value]" => 'Publish on: [node:scheduler-publish' . $test_data['token_format'] . ']. Unpublish on: [node:scheduler-unpublish' . $test_data['token_format'] . '].',
      );
      $this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
      $this->drupalGet('node/' . $node->nid);

      // Refresh the node - 3rd param TRUE to reset cache.
      $node = node_load($node->nid, NULL, TRUE);
      $token_output = token_replace($node->body[$langcode][0]['value'], array('node' => $node));

      // Create the expected text for the body.
      $publish_on_date = format_date($publish_on_timestamp, $test_data['date_format'], $test_data['custom']);
      $unpublish_on_date = format_date($unpublish_on_timestamp, $test_data['date_format'], $test_data['custom']);
      $expected_output = 'Publish on: ' . $publish_on_date . '. Unpublish on: ' . $unpublish_on_date . '.';

      // Check that the actual text matches the expected value.
      $tested_format = $test_data['token_format'] ? '"' . $test_data['token_format'] . '"' : 'default';
      $this->assertEqual($token_output, $expected_output, 'Scheduler tokens replaced correctly for ' . $tested_format . ' format.');
    }
  }
}

/**