You are here

public function SchedulerDateModuleTest::testDefaultTime in Scheduler 7

Test the default time functionality.

File

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

Class

SchedulerDateModuleTest
Tests the components of the scheduler interface which use the date module.

Code

public function testDefaultTime() {
  $this
    ->drupalLogin($this->adminUser);

  // Perform the checks twice, first using plain text entry then with date
  // popup calendars.
  foreach (array(
    'textfield',
    'date_popup',
  ) as $field_type) {

    // Define the form fields and date formats we will test according to
    // whether date calendar popups will be used or not.
    $using_popup = $field_type == 'date_popup';
    $publish_date_field = $using_popup ? 'publish_on[date]' : 'publish_on';
    $unpublish_date_field = $using_popup ? 'unpublish_on[date]' : 'unpublish_on';
    $publish_time_field = $using_popup ? 'publish_on[time]' : 'publish_on';
    $unpublish_time_field = $using_popup ? 'unpublish_on[time]' : 'unpublish_on';
    $time_format = $using_popup ? 'H:i:s' : 'Y-m-d H:i:s';

    // We cannot easily test the exact validation messages as they contain the
    // REQUEST_TIME of the POST request, which can be one or more seconds in
    // the past. Best we can do is check the fixed part of the message.
    $publish_validation_message = $using_popup ? 'The value input for field Publish on is invalid:' : "The 'publish on' value does not match the expected format of";
    $unpublish_validation_message = $using_popup ? 'The value input for field Unpublish on is invalid:' : "The 'unpublish on' value does not match the expected format of";

    // For testing we use an offset of 6 hours 30 minutes (23400 seconds).
    // First test with the "date only" functionality disabled.
    $settings = array(
      'scheduler_date_format' => 'Y-m-d H:i:s',
      'scheduler_default_time' => '6:30',
      'scheduler_field_type' => $field_type,
      'scheduler_allow_date_only' => FALSE,
    );
    $this
      ->drupalPost('admin/config/content/scheduler', $settings, t('Save configuration'));

    // Verify that entering a time is required.
    $edit = array(
      'title' => $this
        ->randomName(),
      $publish_date_field => date('Y-m-d', strtotime('+1 day', REQUEST_TIME)),
      $unpublish_date_field => date('Y-m-d', strtotime('+2 day', REQUEST_TIME)),
    );
    $this
      ->drupalPost('node/add/page', $edit, t('Save'));
    $this
      ->assertText($publish_validation_message, 'Validation error message is shown correctly. By default it is required to enter a time when scheduling content for publication.');
    $this
      ->assertText($unpublish_validation_message, 'Validation error message is shown correctly. By default it is required to enter a time when scheduling content for unpublication.');

    // Allow the user to enter only the date.
    $settings = array(
      'scheduler_allow_date_only' => TRUE,
    );
    $this
      ->drupalPost('admin/config/content/scheduler', $settings, t('Save configuration'));

    // Check that the correct default time is added to the scheduled date.
    $this
      ->drupalPost('node/add/page', $edit, t('Save'));
    $this
      ->assertNoText("The 'publish on' value does not match the expected format of", 'If the default time option is enabled the user can skip the time when scheduling content for publication.');
    $this
      ->assertNoText("The 'unpublish on' value does not match the expected format of", 'If the default time option is enabled the user can skip the time when scheduling content for unpublication.');
    $publish_time = date('Y-m-d H:i:s', strtotime('tomorrow', REQUEST_TIME) + 23400);
    $this
      ->assertText(sprintf('This post is unpublished and will be published %s.', $publish_time), 'The user is informed that the content will be published on the requested date, on the default time.');

    // Check that the default time has been added to the form fields on edit.
    $this
      ->clickLink(t('Edit'));
    $this
      ->assertFieldByName($publish_time_field, date($time_format, strtotime('tomorrow', REQUEST_TIME) + 23400), 'The default time offset has been added to the date field when scheduling content for publication.');
    $this
      ->assertFieldByName($unpublish_time_field, date($time_format, strtotime('tomorrow +1 day', REQUEST_TIME) + 23400), 'The default time offset has been added to the date field when scheduling content for unpublication.');

    // Check that it is not possible for the admin to enter a date format
    // without a time if the 'date only' option is not enabled.
    $edit = array(
      'scheduler_date_format' => 'Y-m-d',
      'scheduler_allow_date_only' => FALSE,
    );
    $this
      ->drupalPost('admin/config/content/scheduler', $edit, t('Save configuration'));
    $this
      ->assertText('You must either include a time within the date format or enable the date-only option.', format_string('It is not possible to enter a date format without a time if the "date only" option is not enabled and the field type is set to %field_type.', array(
      '%field_type' => $field_type,
    )));
  }
}