diff --git a/scheduler.module b/scheduler.module index 2cc2b28..e8abc55 100644 --- a/scheduler.module +++ b/scheduler.module @@ -248,17 +248,21 @@ function scheduler_form_node_type_form_alter(&$form, $form_state) { '#title' => t('Advanced options'), '#collapsible' => TRUE, '#collapsed' => TRUE, + '#states' => array( + 'visible' => array( + ':input[name="scheduler_publish_enable"]' => array('checked' => TRUE), + ), + ), ); $form['scheduler']['publish']['advanced']['scheduler_publish_past_date'] = array( '#type' => 'radios', - '#title' => t('Publication dates in the past'), + '#title' => t('Action to be taken for publication dates in the past'), '#default_value' => variable_get('scheduler_publish_past_date_' . $form['#node_type']->type, 'error'), '#options' => array( - 'error' => t('Show an error message that the publication date should be in the future.'), - 'publish' => t('Publish the content immediately.'), - 'schedule' => t('Schedule the content for publication on the next cron run.'), + 'error' => t('Display an error message - do not allow dates in the past'), + 'publish' => t('Publish the content immediately after saving'), + 'schedule' => t('Schedule the content for publication on the next cron run'), ), - '#description' => t('What should be done if a user enters a publication date that is in the past.'), ); $form['scheduler']['unpublish'] = array( '#type' => 'fieldset', @@ -688,17 +692,16 @@ function scheduler_node_presave($node) { } elseif (!is_numeric($node->$key)) { $node->$key = _scheduler_strtotime($node->$key); - - // Publish the node immediately if the publication date is in the past. - if ($key == 'publish_on' && variable_get('scheduler_publish_past_date_' . $node->type, 'error') == 'publish' && !empty($node->$key) && $node->$key < REQUEST_TIME) { - $node->$key = 0; - $node->status = 1; - } } } - // Right before we save the node, we need to check if a "publish on" value has been set. - // If it has been set, we want to make sure the node is unpublished since it will be published at a later date - if (!empty($node->publish_on) && is_numeric($node->publish_on) && ($node->publish_on > REQUEST_TIME || variable_get('scheduler_publish_past_date_' . $node->type, 'error' == 'schedule'))) { + // Publish the node immediately if the publication date is in the past. + if (variable_get('scheduler_publish_past_date_' . $node->type, 'error') == 'publish' && is_numeric($node->publish_on) && $node->publish_on < REQUEST_TIME) { + $node->publish_on = 0; + $node->status = 1; + } + // Otherwise, if a pubishing date has been set, make sure the node is + // unpublished since it will be published by cron later. + elseif (is_numeric($node->publish_on) && $node->publish_on > 0) { $node->status = 0; $date_format = variable_get('scheduler_date_format', SCHEDULER_DATE_FORMAT); drupal_set_message(t('This post is unpublished and will be published @publish_time.', array('@publish_time' => format_date($node->publish_on, 'custom', $date_format))), 'status', FALSE); diff --git a/scheduler.test b/scheduler.test index 916d281..fef3f30 100644 --- a/scheduler.test +++ b/scheduler.test @@ -71,42 +71,44 @@ class SchedulerTestCase extends DrupalWebTestCase { // Log in. $this->drupalLogin($this->web_user); - // Create an unpublished test node. + // 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 lies in the past. + // enters a publication date that is in the past. $edit = array( 'title' => $this->randomName(), 'publish_on' => format_date(strtotime('-1 day'), 'custom', 'Y-m-d H:i:s'), ); $this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save')); - $this->assertRaw(t("The 'publish on' date must be in the future"), 'An error message is shown when the publication date lies in the past and the "error" behavior is chosen.'); + $this->assertRaw(t("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->assertNoRaw(t("The 'publish on' date must be in the future"), 'No error message is shown when the publication date lies in the past and the "publish" behavior is chosen.'); - $this->assertRaw(t('@type %title has been updated.', array('@type' => t('Basic page'), '%title' => check_plain($edit['title']))), 'The node is saved successfully when the publication date lies in the past and the "publish" behavior is chosen.'); + $this->assertNoRaw(t("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->assertRaw(t('@type %title has been updated.', array('@type' => t('Basic page'), '%title' => check_plain($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 lies in the past and the "publish" behavior is chosen.'); + $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->assertNoRaw(t("The 'publish on' date must be in the future"), 'No error message is shown when the publication date lies in the past and the "schedule" behavior is chosen.'); - $this->assertRaw(t('@type %title has been updated.', array('@type' => t('Basic page'), '%title' => check_plain($edit['title']))), 'The node is saved successfully when the publication date lies in the past and the "schedule" behavior is chosen.'); + $this->assertNoRaw(t("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->assertRaw(t('@type %title has been updated.', array('@type' => t('Basic page'), '%title' => check_plain($edit['title']))), 'The node is saved successfully when the publication date is in the past and the "schedule" behavior is chosen.'); + $this->assertRaw(t('This post is unpublished and will be published @publish_time.', array('@publish_time' => $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 changed node and check that it is unpublished. + // 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 lies in the past and the "schedule" behavior is chosen.'); + $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 unpublished node with publication date in the past and the "schedule" behavior has been published on the next cron run.'); + $this->assertTrue($node->status, 'The node with publication date in the past and the "schedule" behavior has now been published by cron.'); } }