<?php

/**
 * @file
 * Test timezone handling.
 */

/**
 * Test timezone handling.
 */
class DateTimezoneTestCase extends DateFieldTestBase {

  /**
   * Timezone & Granularity.
   */
  public static function getInfo() {
    return array(
      'name' => 'Timezone & Granularity',
      'description' => 'Test combinations of date field timezone handling and granularity.',
      'group' => 'date',
    );
  }

  /**
   * {@inheritdoc}
   */
  public function setUp(array $modules = array()) {
    parent::setUp($modules);

    // Set the timezone explicitly. Otherwise the site's default timezone is
    // used, which defaults to the server timezone when installing Drupal. This
    // depends on the environment and is therefore uncertain.
    // The Australia/Sydney timezone is chosen so all tests are run using an
    // edge case scenario (UTC+10 and DST).
    variable_set('date_default_timezone', 'Australia/Sydney');
  }

  /**
   * Create a date fields, combining various timezones and granularity.
   */
  public function testTimezone() {
    foreach (array('date', 'datestamp', 'datetime') as $field_type) {
      foreach (array('site', 'none', 'date', 'user', 'utc', 'Europe/Dublin') as $tz_handling) {
        foreach (array('year', 'month', 'day', 'hour', 'minute', 'second') as $max_granularity) {
          // Skip invalid combinations.
          if (in_array($max_granularity, array('year', 'month', 'day')) && $tz_handling !== 'none') {
            continue;
          }
          $field_name = "field_test";
          $label = 'Test';
          $granularity = date_granularity_array_from_precision($max_granularity);
          $options = array(
            'label' => $label,
            'widget_type' => 'date_text',
            'field_name' => $field_name,
            'field_type' => $field_type,
            'input_format' => 'custom',
            'input_format_custom' => 'm/d/Y - H:i:s',
            'tz_handling' => $tz_handling,
            'granularity' => $granularity,
          );
          $this->createDateField($options);
          $this->buildDateForm($field_name, $field_type, $max_granularity, $tz_handling);
          $this->deleteDateField($label);
        }
      }
    }
  }

  /**
   * Test timezone handling validation on the field settings form.
   */
  public function testFieldWidgetSettings() {
    $label = 'Test';
    $options = array(
      'label' => $label,
      'field_type' => 'date',
      'widget_type' => 'date_select',
      'granularity' => array('year', 'month', 'day'),
    );
    $this->createDateField($options);
    $this->drupalGet('admin/structure/types/manage/story/fields/field_' . strtolower($label));
    $this->assertResponse(200);
    $edit = array(
      'field[settings][granularity][hour]' => FALSE,
    );
    $this->drupalPost('admin/structure/types/manage/story/fields/field_' . strtolower($label), $edit, 'Save settings');
    $this->assertText("Dates without hours granularity must not use any timezone handling.", "Dates without hours granularity required to use timezone handling of 'none.'");
    $this->deleteDateField($label);
  }

  /**
   * Verify field timezones work properly when conversion is disabled.
   */
  public function testFieldConversionDisabled() {
    // @todo Delete all nodes if any exist. Is this necessary?

    // Add a date field to the Story content type.
    $options = array(
      // Just use the simple text field.
      'widget_type' => 'date_text',

      // Disable timezone handling.
      'tz_handling' => 'none',
    );
    $this->createDateField($options);

    // Enable system timezone conversion.
    $this->drupalGet('admin/config/regional/settings');
    $this->assertResponse(200);
    $edit = array(
      'configurable_timezones' => TRUE,
    );
    $this->drupalPost(NULL, $edit, 'Save configuration');
    $this->assertText('The configuration options have been saved.');

    // Set the user's timezone to something other than the site's.
    $this->drupalGet('user/' . $this->loggedInUser->uid . '/edit');
    $this->assertResponse(200);
    $edit = array(
      'timezone' => 'America/New_York',
    );
    $this->drupalPost(NULL, $edit, 'Save');
    $this->assertResponse(200);
    $this->assertText('The changes have been saved.');

    // Create a node with a date value.
    $this->drupalGet('node/add/story');
    $this->assertResponse(200);
    $edit = array(
      'title' => 'Testing how dates work with timezone conversion disabled',
      'field_test[und][0][value][date]' => '12/31/2021 - 00:00',
    );
    $this->drupalPost(NULL, $edit, 'Save');

    // Confirm the node was saved correctly.
    $this->assertResponse(200);
    $this->assertNoText('The value input for field Test Start date is invalid');
    $this->assertText('Story Testing how dates work with timezone conversion disabled has been created');

    // Load the node data.
    $node = node_load(1);
    $this->verbose($node);

    // Confirm the data is stored correctly.
    $this->assertEqual($node->field_test[LANGUAGE_NONE][0]['value'], '2021-12-31 00:00:00');

    // Load the node page.
    $this->drupalGet('node/1');
    $this->assertResponse(200);
    $this->assertText('Testing how dates work with timezone conversion disabled');

    // Confirm the date value displays correctly.
    $this->assertText('Friday, December 31, 2021 - 00:00');

    // Change the user's timezone to something else.
    $this->drupalGet('user/' . $this->loggedInUser->uid . '/edit');
    $this->assertResponse(200);
    $edit = array(
      'timezone' => 'Europe/Berlin',
    );
    $this->drupalPost(NULL, $edit, 'Save');
    $this->assertResponse(200);
    $this->assertText('The changes have been saved.');

    // Load the node page again.
    $this->drupalGet('node/1');
    $this->assertResponse(200);
    $this->assertText('Testing how dates work with timezone conversion disabled');

    // Confirm the date value still displays correctly.
    $this->assertText('Friday, December 31, 2021 - 00:00');
  }

  /**
   * Verify field timezones work properly when site-conversion is enabled.
   */
  public function testFieldConversionSite() {
    // @todo Create a date format "l, jS F, Y, g:i:s a, e I" that would look
    // like: Thursday, 21st April, 2022, 4:52:15 pm, UTC 0.
    // Add a date field to the Story content type.
    $options = array(
      // Just use the simple text field.
      'widget_type' => 'date_text',

      // Disable timezone handling.
      'tz_handling' => 'site',
    );
    $this->createDateField($options);

    // Enable system timezone conversion.
    $this->drupalGet('admin/config/regional/settings');
    $this->assertResponse(200);
    $edit = array(
      'configurable_timezones' => TRUE,
    );
    $this->drupalPost(NULL, $edit, 'Save configuration');
    $this->assertText('The configuration options have been saved.');

    // Set the user's timezone to something other than the site's.
    $this->drupalGet('user/' . $this->loggedInUser->uid . '/edit');
    $this->assertResponse(200);
    $edit = array(
      'timezone' => 'America/New_York',
    );
    $this->drupalPost(NULL, $edit, 'Save');
    $this->assertResponse(200);
    $this->assertText('The changes have been saved.');

    // Create a node with a date value.
    $this->drupalGet('node/add/story');
    $this->assertResponse(200);
    $edit = array(
      'title' => 'Testing how dates work with timezone conversion enabled',
      'field_test[und][0][value][date]' => '12/31/2021 - 10:00',
    );
    $this->drupalPost(NULL, $edit, 'Save');

    // Confirm the node was saved correctly.
    $this->assertResponse(200);
    $this->assertNoText('The value input for field Test Start date is invalid');
    $this->assertText('Story Testing how dates work with timezone conversion enabled has been created');

    // Load the node data.
    $node = node_load(1);
    $this->verbose($node);

    // Confirm the data is stored correctly - the site has its timezone set to
    // Australia/Sydney so an EST value will be stored with a very different
    // value.
    $this->assertEqual($node->field_test[LANGUAGE_NONE][0]['value'], '2021-12-30 23:00:00');
    $this->assertEqual($node->field_test[LANGUAGE_NONE][0]['timezone_db'], 'UTC');

    // Load the node page.
    $this->drupalGet('node/1');
    $this->assertResponse(200);
    $this->assertText('Testing how dates work with timezone conversion enabled');

    // Confirm the date value displays correctly.
    $this->assertText('Friday, December 31, 2021 - 10:00');

    // Change the user's timezone to something else.
    $this->drupalGet('user/' . $this->loggedInUser->uid . '/edit');
    $this->assertResponse(200);
    $edit = array(
      'timezone' => 'Europe/Berlin',
    );
    $this->drupalPost(NULL, $edit, 'Save');
    $this->assertResponse(200);
    $this->assertText('The changes have been saved.');

    // Load the node page again.
    $this->drupalGet('node/1');
    $this->assertResponse(200);
    $this->assertText('Testing how dates work with timezone conversion enabled');

    // @todo This takes daylights savings time into account so it might stop
    // working when DST is no longer active.
    // @todo Is this correct?
    $this->assertText('Friday, December 31, 2021 - 10:00');

    // Document how the date looks when logged out.
    $this->drupalLogout();
    $this->drupalGet('node/1');
    $this->assertResponse(200);
    $this->assertText('Testing how dates work with timezone conversion enabled');
    // @todo Is this correct?
    $this->assertText('Friday, December 31, 2021 - 10:00');
  }

  /**
   * Verify field timezones work properly when date-conversion is enabled.
   *
   * The time values used in this test are based upon example data from an issue
   * listed on d.o.
   *
   * @see https://www.drupal.org/project/drupalorg/issues/3164818
   */
  public function testFieldConversionDate() {
    // @todo Create a date format "l, jS F, Y, g:i:s a, e I" that would look
    // like: Thursday, 21st April, 2022, 4:52:15 pm, UTC 0.
    // Add a date field to the Story content type.
    $options = array(
      // Just use the simple text field.
      'widget_type' => 'date_text',

      // Disable timezone handling.
      'tz_handling' => 'date',
    );
    $this->createDateField($options);

    // Enable system timezone conversion.
    $this->drupalGet('admin/config/regional/settings');
    $this->assertResponse(200);
    $edit = array(
      'configurable_timezones' => TRUE,
    );
    $this->drupalPost(NULL, $edit, 'Save configuration');
    $this->assertText('The configuration options have been saved.');

    // Set the user's timezone to something other than the site's.
    $this->drupalGet('user/' . $this->loggedInUser->uid . '/edit');
    $this->assertResponse(200);
    $edit = array(
      'timezone' => 'America/Chicago',
    );
    $this->drupalPost(NULL, $edit, 'Save');
    $this->assertResponse(200);
    $this->assertText('The changes have been saved.');

    // Create a node with a date value.
    $this->drupalGet('node/add/story');
    $this->assertResponse(200);
    $edit = array(
      'title' => 'Testing how dates work with timezone conversion enabled',
      'field_test[und][0][value][date]' => '07/21/2021 - 18:30',
    );
    $this->drupalPost(NULL, $edit, 'Save');

    // Confirm the node was saved correctly.
    $this->assertResponse(200);
    $this->assertNoText('The value input for field Test Start date is invalid');
    $this->assertText('Story Testing how dates work with timezone conversion enabled has been created');

    // Load the node data.
    $node = node_load(1);
    $this->verbose($node);

    // Confirm the data is stored correctly.
    $this->assertEqual($node->field_test[LANGUAGE_NONE][0]['value'], '2021-07-21 18:30:00');
    $this->assertEqual($node->field_test[LANGUAGE_NONE][0]['timezone'], 'America/Chicago');
    $this->assertEqual($node->field_test[LANGUAGE_NONE][0]['timezone_db'], 'America/Chicago');

    // Load the node page.
    $this->drupalGet('node/1');
    $this->assertResponse(200);
    $this->assertText('Testing how dates work with timezone conversion enabled');

    // Confirm the date value displays correctly.
    $this->assertText('Wednesday, July 21, 2021 - 18:30');

    // Change the user's timezone to something else.
    $this->drupalGet('user/' . $this->loggedInUser->uid . '/edit');
    $this->assertResponse(200);
    $edit = array(
      'timezone' => 'America/New_York',
    );
    $this->drupalPost(NULL, $edit, 'Save');
    $this->assertResponse(200);
    $this->assertText('The changes have been saved.');

    // Load the node page again.
    $this->drupalGet('node/1');
    $this->assertResponse(200);
    $this->assertText('Testing how dates work with timezone conversion enabled');

    // @todo This takes daylights savings time into account so it might stop
    // working when DST is no longer active. Maybe. I'm not currently sure.
    // @todo Is this correct?
    // The "date" conversion logic does not change the time display based upon
    // the viewer's configured timezone, therefore the item should have the same
    // 6:30pm display as it did previously.
    // @code
    // $this->assertText('Wednesday, July 21, 2021 - 18:30');
    // @endcode
    // @todo This is incorrect, it should actually show 18:30.
    // @see https://www.drupal.org/project/date/issues/998076
    $this->assertText('Wednesday, July 21, 2021 - 17:30');
  }

  /**
   * Verify field timezones work properly when user-conversion is enabled.
   */
  public function testFieldConversionUser() {
    // @todo Create a date format "l, jS F, Y, g:i:s a, e I" that would look
    // like: Thursday, 21st April, 2022, 4:52:15 pm, UTC 0.
    // Add a date field to the Story content type.
    $options = array(
      // Just use the simple text field.
      'widget_type' => 'date_text',

      // Disable timezone handling.
      'tz_handling' => 'user',
    );
    $this->createDateField($options);

    // Enable system timezone conversion.
    $this->drupalGet('admin/config/regional/settings');
    $this->assertResponse(200);
    $edit = array(
      'configurable_timezones' => TRUE,
    );
    $this->drupalPost(NULL, $edit, 'Save configuration');
    $this->assertText('The configuration options have been saved.');

    // Set the user's timezone to something other than the site's.
    $this->drupalGet('user/' . $this->loggedInUser->uid . '/edit');
    $this->assertResponse(200);
    $edit = array(
      'timezone' => 'America/New_York',
    );
    $this->drupalPost(NULL, $edit, 'Save');
    $this->assertResponse(200);
    $this->assertText('The changes have been saved.');

    // Create a node with a date value.
    $this->drupalGet('node/add/story');
    $this->assertResponse(200);
    $edit = array(
      'title' => 'Testing how dates work with timezone conversion enabled',
      'field_test[und][0][value][date]' => '12/31/2021 - 10:00',
    );
    $this->drupalPost(NULL, $edit, 'Save');

    // Confirm the node was saved correctly.
    $this->assertResponse(200);
    $this->assertNoText('The value input for field Test Start date is invalid');
    $this->assertText('Story Testing how dates work with timezone conversion enabled has been created');

    // Load the node data.
    $node = node_load(1);
    $this->verbose($node);

    // Confirm the data is stored correctly - it will be stored with respect to
    // UTC, so 10am EST gets changed to 3pm UTC.
    $this->assertEqual($node->field_test[LANGUAGE_NONE][0]['value'], '2021-12-31 15:00:00');
    $this->assertEqual($node->field_test[LANGUAGE_NONE][0]['timezone_db'], 'UTC');

    // Load the node page.
    $this->drupalGet('node/1');
    $this->assertResponse(200);
    $this->assertText('Testing how dates work with timezone conversion enabled');

    // Confirm the date value displays correctly.
    $this->assertText('Friday, December 31, 2021 - 10:00');

    // Change the user's timezone to something else.
    $this->drupalGet('user/' . $this->loggedInUser->uid . '/edit');
    $this->assertResponse(200);
    $edit = array(
      'timezone' => 'Europe/Berlin',
    );
    $this->drupalPost(NULL, $edit, 'Save');
    $this->assertResponse(200);
    $this->assertText('The changes have been saved.');

    // Load the node page again.
    $this->drupalGet('node/1');
    $this->assertResponse(200);
    $this->assertText('Testing how dates work with timezone conversion enabled');

    // Confirm the date value shows as 4pm Berlin time.
    // @todo This takes daylights savings time into account so it might stop
    // working when DST is no longer active.
    $this->assertText('Friday, December 31, 2021 - 16:00');

    // Document how the date looks when logged out.
    $this->drupalLogout();
    $this->drupalGet('node/1');
    $this->assertResponse(200);
    $this->assertText('Testing how dates work with timezone conversion enabled');
    // @todo Is this correct?
    $this->assertText('Saturday, January 1, 2022 - 02:00');
  }

  /**
   * Verify field timezones work properly when UTC-conversion is enabled.
   */
  public function testFieldConversionUtc() {
    // @todo Create a date format "l, jS F, Y, g:i:s a, e I" that would look
    // like: Thursday, 21st April, 2022, 4:52:15 pm, UTC 0.
    // Add a date field to the Story content type.
    $options = array(
      // Just use the simple text field.
      'widget_type' => 'date_text',

      // Disable timezone handling.
      'tz_handling' => 'utc',
    );
    $this->createDateField($options);

    // Enable system timezone conversion.
    $this->drupalGet('admin/config/regional/settings');
    $this->assertResponse(200);
    $edit = array(
      'configurable_timezones' => TRUE,
    );
    $this->drupalPost(NULL, $edit, 'Save configuration');
    $this->assertText('The configuration options have been saved.');

    // Set the user's timezone to something other than the site's.
    $this->drupalGet('user/' . $this->loggedInUser->uid . '/edit');
    $this->assertResponse(200);
    $edit = array(
      'timezone' => 'America/New_York',
    );
    $this->drupalPost(NULL, $edit, 'Save');
    $this->assertResponse(200);
    $this->assertText('The changes have been saved.');

    // Create a node with a date value.
    $this->drupalGet('node/add/story');
    $this->assertResponse(200);
    $edit = array(
      'title' => 'Testing how dates work with timezone conversion enabled',
      'field_test[und][0][value][date]' => '12/31/2021 - 10:00',
    );
    $this->drupalPost(NULL, $edit, 'Save');

    // Confirm the node was saved correctly.
    $this->assertResponse(200);
    $this->assertNoText('The value input for field Test Start date is invalid');
    $this->assertText('Story Testing how dates work with timezone conversion enabled has been created');

    // Load the node data.
    $node = node_load(1);
    $this->verbose($node);

    // Confirm the data is stored correctly - the value is stored as-is.
    // @todo Is this correct?
    $this->assertEqual($node->field_test[LANGUAGE_NONE][0]['value'], '2021-12-31 10:00:00');
    $this->assertEqual($node->field_test[LANGUAGE_NONE][0]['timezone_db'], 'UTC');

    // Load the node page.
    $this->drupalGet('node/1');
    $this->assertResponse(200);
    $this->assertText('Testing how dates work with timezone conversion enabled');

    // Confirm the date value displays correctly.
    $this->assertText('Friday, December 31, 2021 - 05:00');

    // Change the user's timezone to something else.
    $this->drupalGet('user/' . $this->loggedInUser->uid . '/edit');
    $this->assertResponse(200);
    $edit = array(
      'timezone' => 'Europe/Berlin',
    );
    $this->drupalPost(NULL, $edit, 'Save');
    $this->assertResponse(200);
    $this->assertText('The changes have been saved.');

    // Load the node page again.
    $this->drupalGet('node/1');
    $this->assertResponse(200);
    $this->assertText('Testing how dates work with timezone conversion enabled');

    // Confirm the date value shows as 4pm Berlin time.
    // @todo This takes daylights savings time into account so it might stop
    // working when DST is no longer active.
    // @todo Is this correct?
    $this->assertText('Friday, December 31, 2021 - 11:00');

    // Document how the date looks when logged out.
    $this->drupalLogout();
    $this->drupalGet('node/1');
    $this->assertResponse(200);
    $this->assertText('Testing how dates work with timezone conversion enabled');
    // @todo Is this correct?
    $this->assertText('Friday, December 31, 2021 - 21:00');
  }

  /**
   * Validates timezone handling with a multi-value field w hour granularity.
   */
  public function testMultiValueTimezoneHour() {
    // Create date fields with combinations of various types and granularity
    // using the "Date's Timezone" strategy.
    $field_type = 'datetime';
    $tz_handling = 'date';
    $granularity = 'hour';

    // Create date field.
    $field_name = "field_test";
    $label = 'Test';
    $options = array(
      'label' => $label,
      'widget_type' => 'date_text',
      'field_name' => $field_name,
      'field_type' => $field_type,
      'input_format' => 'custom',
      'input_format_custom' => 'm/d/Y - H:i:s T',
      'cardinality' => 3,
      'tz_handling' => $tz_handling,
      'granularity' => array(
        'year',
        'month',
        'day',
        'hour',
      ),
    );
    $this->createMultiDateField($options);

    // Submit a date field form with multiple values.
    $this->dateMultiValueForm($field_name, $field_type, $granularity, $tz_handling);

    $this->deleteDateField($label);
  }

  /**
   * Validates timezone handling with a multi-value field w minute granularity.
   */
  public function testMultiValueTimezoneMinute() {
    // Create date fields with combinations of various types and granularity
    // using the "Date's Timezone" strategy.
    $field_type = 'datetime';
    $tz_handling = 'date';
    $granularity = 'minute';

    // Create date field.
    $field_name = "field_test";
    $label = 'Test';
    $options = array(
      'label' => $label,
      'widget_type' => 'date_text',
      'field_name' => $field_name,
      'field_type' => $field_type,
      'input_format' => 'custom',
      'input_format_custom' => 'm/d/Y - H:i:s T',
      'cardinality' => 3,
      'tz_handling' => $tz_handling,
      'granularity' => array(
        'year',
        'month',
        'day',
        'hour',
        'minute',
      ),
    );
    $this->createMultiDateField($options);

    // Submit a date field form with multiple values.
    $this->dateMultiValueForm($field_name, $field_type, $granularity, $tz_handling);

    $this->deleteDateField($label);
  }

  /**
   * Validates timezone handling with a multi-value field w second granularity.
   */
  public function testMultiValueTimezoneSecond() {
    // Create date fields with combinations of various types and granularity
    // using the "Date's Timezone" strategy.
    $field_type = 'datetime';
    $tz_handling = 'date';
    $granularity = 'second';

    // Create date field.
    $field_name = "field_test";
    $label = 'Test';
    $options = array(
      'label' => $label,
      'widget_type' => 'date_text',
      'field_name' => $field_name,
      'field_type' => $field_type,
      'input_format' => 'custom',
      'input_format_custom' => 'm/d/Y - H:i:s T',
      'cardinality' => 3,
      'tz_handling' => $tz_handling,
      'granularity' => array(
        'year',
        'month',
        'day',
        'hour',
        'minute',
        'second',
      ),
    );
    $this->createMultiDateField($options);

    // Submit a date field form with multiple values.
    $this->dateMultiValueForm($field_name, $field_type, $granularity, $tz_handling);

    $this->deleteDateField($label);
  }

  /**
   * Tests the submission of a date field's widget with unlimited cardinality.
   */
  protected function dateMultiValueForm($field_name, $field_type, $granularity, $tz_handling) {
    variable_set('date_format_long', 'D, m/d/Y - H:i:s T');

    $edit = array();
    $should_be = array();
    $edit['title'] = self::randomName(8);
    $timezones = array(
      'America/Chicago', 'America/Los_Angeles', 'America/New_York',
    );

    switch ($granularity) {
      case 'hour':
        $edit[$field_name . '[und][0][value][date]'] = '10/07/2010 - 10';
        $edit[$field_name . '[und][0][timezone][timezone]'] = 'America/Chicago';
        $should_be[0] = 'Thu, 10/07/2010 - 10 CDT';

        $edit[$field_name . '[und][1][value][date]'] = '10/07/2010 - 10';
        $edit[$field_name . '[und][1][timezone][timezone]'] = 'America/Los_Angeles';
        $should_be[1] = 'Thu, 10/07/2010 - 10 PDT';

        $edit[$field_name . '[und][2][value][date]'] = '10/07/2010 - 10';
        $edit[$field_name . '[und][2][timezone][timezone]'] = 'America/New_York';
        $should_be[2] = 'Thu, 10/07/2010 - 10 EDT';
        break;

      case 'minute':
        $edit[$field_name . '[und][0][value][date]'] = '10/07/2010 - 10:30';
        $edit[$field_name . '[und][0][timezone][timezone]'] = 'America/Chicago';
        $should_be[0] = 'Thu, 10/07/2010 - 10:30 CDT';

        $edit[$field_name . '[und][1][value][date]'] = '10/07/2010 - 10:30';
        $edit[$field_name . '[und][1][timezone][timezone]'] = 'America/Los_Angeles';
        $should_be[1] = 'Thu, 10/07/2010 - 10:30 PDT';

        $edit[$field_name . '[und][2][value][date]'] = '10/07/2010 - 10:30';
        $edit[$field_name . '[und][2][timezone][timezone]'] = 'America/New_York';
        $should_be[2] = 'Thu, 10/07/2010 - 10:30 EDT';
        break;

      case 'second':
        $edit[$field_name . '[und][0][value][date]'] = '10/07/2010 - 10:30:30';
        $edit[$field_name . '[und][0][timezone][timezone]'] = 'America/Chicago';
        $should_be[0] = 'Thu, 10/07/2010 - 10:30:30 CDT';

        $edit[$field_name . '[und][1][value][date]'] = '10/07/2010 - 10:30:30';
        $edit[$field_name . '[und][1][timezone][timezone]'] = 'America/Los_Angeles';
        $should_be[1] = 'Thu, 10/07/2010 - 10:30:30 PDT';

        $edit[$field_name . '[und][2][value][date]'] = '10/07/2010 - 10:30:30';
        $edit[$field_name . '[und][2][timezone][timezone]'] = 'America/New_York';
        $should_be[2] = 'Thu, 10/07/2010 - 10:30:30 EDT';
        break;

      default:
        throw new \Exception("Oh dear! Unknown granularity '$granularity'.");
    }

    $this->drupalPost('node/add/story', $edit, 'Save');
    $this->assertText($edit['title'], "Node has been created");
    $this->assertNoText('does not match the expected format.');

    foreach ($should_be as $assertion) {
      $this->assertText($assertion, "Found the correct date for a $field_type field using $granularity granularity with $tz_handling timezone handling.");
    }

    // Goto the edit page and save the node again.
    $node = $this->drupalGetNodeByTitle($edit['title']);
    $this->assertNotNull($node);
    if (!empty($node)) {
      $this->verbose($node);
      $this->drupalGet('node/' . $node->nid . '/edit');

      // Re-assert the proper date timezones.
      foreach ($timezones as $key => $timezone) {
        $this->assertOptionSelected('edit-field-test-und-' . $key . '-timezone-timezone', $timezone, "Found the correct timezone $timezone for a $field_type field using $granularity granularity with $tz_handling timezone handling.");
      }
    }
  }

  /**
   * Replacement for dateForm().
   */
  protected function buildDateForm($field_name, $field_type, $granularity, $tz_handling) {
    variable_set('date_format_long', 'D, m/d/Y - H:i:s');
    $edit = array();
    $edit['title'] = self::randomName(8);
    $edit[$field_name . '[und][0][show_todate]'] = '1';
    switch ($granularity) {
      case 'year':
        $edit[$field_name . '[und][0][value][date]'] = '2010';
        $edit[$field_name . '[und][0][value2][date]'] = '2011';
        $should_be = '2010 to 2011';
        break;

      case 'month':
        $edit[$field_name . '[und][0][value][date]'] = '07/2010';
        $edit[$field_name . '[und][0][value2][date]'] = '08/2010';
        $should_be = '07/2010 to 08/2010';
        break;

      case 'day':
        $edit[$field_name . '[und][0][value][date]'] = '10/07/2010';
        $edit[$field_name . '[und][0][value2][date]'] = '10/08/2010';
        $should_be = 'Thu, 10/07/2010 to Fri, 10/08/2010';
        break;

      case 'hour':
        $edit[$field_name . '[und][0][value][date]'] = '10/07/2010 - 10';
        $edit[$field_name . '[und][0][value2][date]'] = '10/07/2010 - 11';
        if ($tz_handling === 'utc') {
          $should_be = 'Thu, 10/07/2010 - 21 to Thu, 10/07/2010 - 22';
        }
        else {
          $should_be = 'Thu, 10/07/2010 - 10 to Thu, 10/07/2010 - 11';
        }
        break;

      case 'minute':
        $edit[$field_name . '[und][0][value][date]'] = '10/07/2010 - 10:30';
        $edit[$field_name . '[und][0][value2][date]'] = '10/07/2010 - 11:30';
        if ($tz_handling === 'utc') {
          $should_be = 'Thu, 10/07/2010 - 21:30 to 22:30';
        }
        else {
          $should_be = 'Thu, 10/07/2010 - 10:30 to 11:30';
        }
        break;

      case 'second':
        $edit[$field_name . '[und][0][value][date]'] = '10/07/2010 - 10:30:30';
        $edit[$field_name . '[und][0][value2][date]'] = '10/07/2010 - 11:30:30';
        if ($tz_handling === 'utc') {
          $should_be = 'Thu, 10/07/2010 - 21:30:30 to 22:30:30';
        }
        else {
          $should_be = 'Thu, 10/07/2010 - 10:30:30 to 11:30:30';
        }
        break;

      default:
        throw new \Exception("Oh dear! Unknown granularity '$granularity'.");
    }
    $this->drupalPost('node/add/story', $edit, 'Save');
    $this->assertText($edit['title'], "Node has been created");
    $this->assertText($should_be, "Found the correct date for a $field_type field using $granularity granularity with $tz_handling timezone handling.");
  }

}
