diff --git a/scheduler.module b/scheduler.module index 1845e38..21925e5 100644 --- a/scheduler.module +++ b/scheduler.module @@ -811,7 +811,6 @@ function scheduler_cron() { */ function _scheduler_publish() { $result = FALSE; - $date_format = variable_get('scheduler_date_format', SCHEDULER_DATE_FORMAT); // If the time now is greater than the time to publish a node, publish it. $query = db_select('scheduler', 's'); @@ -843,7 +842,11 @@ function _scheduler_publish() { $create_publishing_revision = variable_get('scheduler_publish_revision_' . $n->type, 0) == 1; if ($create_publishing_revision) { $n->revision = TRUE; - $n->log = "Node published by scheduler module. Original creation date was " . format_date($old_creation_date, 'custom', $date_format) . "."; + // Use a core date format to guarantee a time is included. + $n->log = t('Node published by Scheduler on @now. Previous creation date was @date.', array( + '@now' => format_date(REQUEST_TIME, 'short'), + '@date' => format_date($old_creation_date, 'short'), + )); } // Unset publish_on so the node will not get rescheduled by subsequent calls // to node_save(). @@ -886,7 +889,6 @@ function _scheduler_publish() { */ function _scheduler_unpublish() { $result = FALSE; - $date_format = variable_get('scheduler_date_format', SCHEDULER_DATE_FORMAT); // If the time is greater than the time to unpublish a node, unpublish it. $query = db_select('scheduler', 's'); @@ -917,7 +919,11 @@ function _scheduler_unpublish() { $create_unpublishing_revision = variable_get('scheduler_unpublish_revision_' . $n->type, 0) == 1; if ($create_unpublishing_revision) { $n->revision = TRUE; - $n->log = "Node unpublished by scheduler module. Original change date was " . format_date($old_change_date, 'custom', $date_format) . "."; + // Use a core date format to guarantee a time is included. + $n->log = t('Node unpublished by Scheduler on @now. Previous change date was @date.', array( + '@now' => format_date(REQUEST_TIME, 'short'), + '@date' => format_date($old_change_date, 'short'), + )); } // Unset unpublish_on so the node will not get rescheduled by subsequent // calls to node_save(). diff --git a/scheduler.test b/scheduler.test index c12567b..0a24683 100644 --- a/scheduler.test +++ b/scheduler.test @@ -133,4 +133,119 @@ class SchedulerTestCase extends DrupalWebTestCase { $this->assertTrue($node->status, 'The node with publication date in the past and the "schedule" behavior has now been published by cron.'); } + /** + * Tests the creation of new revisions on scheduling. + */ + public function testRevisioning() { + // Create a scheduled node that is not automatically revisioned. + $created = strtotime('-2 day'); + $settings = array( + 'revision' => 0, + 'created' => $created, + ); + $node = $this->drupalCreateNode($settings); + + // First test scheduled publication with revisioning disabled. + $node = $this->schedule($node); + $this->assertRevisionCount($node->nid, 1, 'No new revision was created when a node was published with revisioning disabled.'); + + // Test scheduled unpublication. + $node = $this->schedule($node, 'unpublish'); + $this->assertRevisionCount($node->nid, 1, 'No new revision was created when a node was unpublished with revisioning disabled.'); + + // Enable revisioning. + variable_set('scheduler_publish_revision_page', 1); + variable_set('scheduler_unpublish_revision_page', 1); + + // Test scheduled publication with revisioning enabled. + $node = $this->schedule($node); + $this->assertRevisionCount($node->nid, 2, 'A new revision was created when revisioning is enabled.'); + $expected_message = t('Node published by Scheduler on @now. Previous creation date was @date.', array( + '@now' => format_date(REQUEST_TIME, 'short'), + '@date' => format_date($created, 'short'), + )); + $this->assertRevisionLogMessage($node->nid, $expected_message, 'The correct message was found in the node revision log after scheduled publishing.'); + + // Test scheduled unpublication with revisioning enabled. + $node = $this->schedule($node, 'unpublish'); + $this->assertRevisionCount($node->nid, 3, 'A new revision was created when a node was unpublished with revisioning enabled.'); + $expected_message = t('Node unpublished by Scheduler on @now. Previous change date was @date.', array( + '@now' => format_date(REQUEST_TIME, 'short'), + '@date' => format_date(REQUEST_TIME, 'short'), + )); + $this->assertRevisionLogMessage($node->nid, $expected_message, 'The correct message was found in the node revision log after scheduled unpublishing.'); + } + + /** + * Simulates the scheduled (un)publication of a node. + * + * @param object $node + * The node to schedule. + * @param string $action + * The action to perform: either 'publish' or 'unpublish'. Defaults to + * 'publish'. + * + * @return object + * The updated node, after scheduled (un)publication. + */ + function schedule($node, $action = 'publish') { + // Simulate scheduling by setting the (un)publication date in the past and + // running cron. + $node->{$action . '_on'} = strtotime('-1 day'); + node_save($node); + scheduler_cron(); + return node_load($node->nid, NULL, TRUE); + } + + /** + * Check if the latest revision log message of a node matches a given string. + * + * @param int $nid + * The node id of the node to check. + * @param string $value + * The value with which the log message will be compared. + * @param string $message + * The message to display along with the assertion. + * @param string $group + * The type of assertion - examples are "Browser", "PHP". + * + * @return + * TRUE if the assertion succeeded, FALSE otherwise. + */ + function assertRevisionLogMessage($nid, $value, $message = '', $group = 'Other') { + $log_message = db_select('node_revision', 'r') + ->fields('r', array('log')) + ->condition('nid', $nid) + ->orderBy('vid', 'DESC') + ->range(0, 1) + ->execute() + ->fetchColumn(); + return $this->assertEqual($log_message, $value, $message, $group); + } + + /** + * Check if the number of revisions for a node matches a given value. + * + * @param int $nid + * The node id of the node to check. + * @param string $value + * The value with which the number of revisions will be compared. + * @param string $message + * The message to display along with the assertion. + * @param string $group + * The type of assertion - examples are "Browser", "PHP". + * + * @return + * TRUE if the assertion succeeded, FALSE otherwise. + */ + function assertRevisionCount($nid, $value, $message = '', $group = 'Other') { + $count = db_select('node_revision', 'r') + ->fields('r', array('vid')) + ->condition('nid', $nid) + ->countQuery() + ->execute() + ->fetchColumn(); + return $this->assertEqual($count, $value, $message, $group); + } + }