public function SchedulerFunctionalTest::testTokenReplacement in Scheduler 7
Tests Scheduler token support.
File
- tests/
scheduler.test, line 918 - Scheduler module test case file.
Class
- SchedulerFunctionalTest
- Tests the scheduler interface.
Code
public function testTokenReplacement() {
// Log in.
$this
->drupalLogin($this->adminUser);
// 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,
);
$node = $this
->drupalCreateNode($settings);
// 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',
),
);
foreach ($test_cases as $test_data) {
// Define a variable containing the template of tokens to be replaced.
// The template is not held in the node body, as that is confusing when
// viewing the test debug, becuase the tokens will not be replaced unless
// a text format for tokens is added. That is unnecessary for the tests.
$template = 'Publish on: [node:scheduler-publish' . $test_data['token_format'] . ']. Unpublish on: [node:scheduler-unpublish' . $test_data['token_format'] . '].';
// With each of the test cases, test using both numeric and string input.
foreach (array(
'numeric',
'string',
) as $test_data['input_type']) {
if ($test_data['input_type'] == 'numeric') {
// Set the node fields to numeric timestanps, as they will be in the
// final stored node, after hook_node_presave() has been executed.
$node->publish_on = $publish_on_timestamp;
$node->unpublish_on = $unpublish_on_timestamp;
}
else {
// Replicate the scheduler fields as if just input by a user during
// edit, before hook_node_presave() has been executed.
// @see https://www.drupal.org/node/2750467
$node->publish_on = format_date($publish_on_timestamp, 'custom', 'Y-m-d H:i:s');
$node->unpublish_on = format_date($unpublish_on_timestamp, 'custom', 'Y-m-d H:i:s');
}
// Get the output value after tokens have been replaced.
$token_output = token_replace($template, array(
'node' => $node,
));
// Create the expected text.
$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 with ' . $test_data['input_type'] . ' input data.');
}
// Remove the scheduled dates and check that token replacment still works.
unset($node->publish_on);
unset($node->unpublish_on);
$token_output = token_replace($template, array(
'node' => $node,
));
$expected_output = 'Publish on: [node:scheduler-publish' . $test_data['token_format'] . ']. Unpublish on: [node:scheduler-unpublish' . $test_data['token_format'] . '].';
$this
->assertEqual($token_output, $expected_output, 'Scheduler tokens replaced correctly for ' . $tested_format . ' format with no scheduled dates.');
}
}