Projet

Général

Profil

Paste
Télécharger (8,65 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / date / tests / date_timezone.test @ 1f683914

1
<?php
2
/**
3
 * @file
4
 * Timezone tests.
5
 */
6
class DateTimezoneTestCase extends DateFieldBasic {
7

    
8
  /**
9
   * @todo.
10
   */
11
  public static function getInfo() {
12
    return array(
13
      'name' => 'Timezone & Granularity',
14
      'description' => 'Test combinations of date field timezone handling and granularity.',
15
      'group' => 'Date',
16
    );
17
  }
18

    
19
  public function setUp() {
20
    parent::setUp();
21
    // Set the timezone explicitly. Otherwise the site's default timezone is
22
    // used, which defaults to the server timezone when installing Drupal. This
23
    // depends on the environment and is therefore uncertain.
24
    // The Australia/Sydney timezone is chosen so all tests are run using an
25
    // edge case scenario (UTC+10 and DST).
26
    variable_set('date_default_timezone', 'Australia/Sydney');
27
  }
28

    
29
  /**
30
   * @todo.
31
   */
32
  public function testTimezone() {
33
    // Create a date fields with combinations of various timezone handling and
34
    // granularity.
35
    foreach (array('date', 'datestamp', 'datetime') as $field_type) {
36
      foreach (array('site', 'none', 'date', 'user', 'utc', 'Europe/Dublin') as $tz_handling) {
37
        foreach (array('year', 'month', 'day', 'hour', 'minute', 'second') as $max_granularity) {
38
          // Skip invalid combinations.
39
          if (in_array($max_granularity, array('year', 'month', 'day')) && $tz_handling != 'none') {
40
            continue;
41
          }
42
          $field_name = "field_test";
43
          $label = 'Test';
44
          $granularity = date_granularity_array_from_precision($max_granularity);
45
          $options = array(
46
            'label' => $label,
47
            'widget_type' => 'date_text',
48
            'field_name' => $field_name,
49
            'field_type' => $field_type,
50
            'input_format' => 'custom',
51
            'input_format_custom' => 'm/d/Y - H:i:s',
52
            'tz_handling' => $tz_handling,
53
            'granularity' => $granularity,
54
          );
55
          $this->createDateField($options);
56
          $this->dateForm($field_name, $field_type, $max_granularity, $tz_handling);
57
          $this->deleteDateField($label);
58
        }
59
      }
60
    }
61
  }
62

    
63
  /**
64
   * Validates timezone handling with a multi-value date field.
65
   */
66
  public function testMultiUserTimezone() {
67
    // Create date fields with combinations of various types and granularity
68
    // using the "Date's Timezone" strategy.
69
    $field_type = 'datetime';
70
    $tz_handling = 'date';
71
    $max_granularity = 'minute';
72

    
73
    // Create date field
74
    $field_name = "field_test";
75
    $label = 'Test';
76
    $options = array(
77
      'label' => $label,
78
      'widget_type' => 'date_text',
79
      'field_name' => $field_name,
80
      'field_type' => $field_type,
81
      'input_format' => 'custom',
82
      'input_format_custom' => 'm/d/Y - H:i:s T',
83
      'cardinality' => 3,
84
      'tz_handling' => $tz_handling,
85
    );
86
    $this->createMultiDateField($options);
87

    
88
    // Submit a date field form with multiple values
89
    $this->dateMultiValueForm($field_name, $field_type, $max_granularity, $tz_handling);
90

    
91

    
92
    $this->deleteDateField($label);
93
  }
94

    
95
  /**
96
   * Tests the submission of a date field's widget form when using unlimited
97
   * cardinality
98
   */
99
  public function dateMultiValueForm($field_name, $field_type, $max_granularity, $tz_handling) {
100
    variable_set('date_format_long', 'D, m/d/Y - H:i:s T');
101

    
102
    $edit = array();
103
    $should_be = array();
104
    $edit['title'] = $this->randomName(8);
105
    $timezones = array('America/Chicago', 'America/Los_Angeles', 'America/New_York');
106

    
107
    switch ($max_granularity) {
108
      case 'hour':
109
        $edit[$field_name . '[und][0][value][date]'] = '10/07/2010 - 10:30';
110
        $edit[$field_name . '[und][0][timezone][timezone]'] = 'America/Chicago';
111
        $should_be[0] = 'Thu, 10/07/2010 - 10 CDT';
112

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

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

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

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

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

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

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

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

    
151
    $this->drupalPost('node/add/story', $edit, t('Save'));
152
    $this->assertText($edit['title'], "Node has been created");
153

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

    
158
    // Goto the edit page and save the node again.
159
    $node = $this->drupalGetNodeByTitle($edit['title']);
160
    $this->drupalGet('node/' . $node->nid . '/edit');
161

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

    
168
  /**
169
   * @todo.
170
   */
171
  public function dateForm($field_name, $field_type, $max_granularity, $tz_handling) {
172
    variable_set('date_format_long', 'D, m/d/Y - H:i:s');
173
    $edit = array();
174
    $edit['title'] = $this->randomName(8);
175
    $edit[$field_name . '[und][0][show_todate]'] = '1';
176
    switch ($max_granularity) {
177
      case 'year':
178
        $edit[$field_name . '[und][0][value][date]'] = '2010';
179
        $edit[$field_name . '[und][0][value2][date]'] = '2011';
180
        $should_be = '2010 to 2011';
181
        break;
182
      case 'month':
183
        $edit[$field_name . '[und][0][value][date]'] = '07/2010';
184
        $edit[$field_name . '[und][0][value2][date]'] = '08/2010';
185
        $should_be = '07/2010 to 08/2010';
186
        break;
187
      case 'day':
188
        $edit[$field_name . '[und][0][value][date]'] = '10/07/2010';
189
        $edit[$field_name . '[und][0][value2][date]'] = '10/08/2010';
190
        $should_be = 'Thu, 10/07/2010 to Fri, 10/08/2010';
191
        break;
192
      case 'hour':
193
        $edit[$field_name . '[und][0][value][date]'] = '10/07/2010 - 10';
194
        $edit[$field_name . '[und][0][value2][date]'] = '10/07/2010 - 11';
195
        if ($tz_handling == 'utc') {
196
          $should_be = 'Thu, 10/07/2010 - 21 to Thu, 10/07/2010 - 22';
197
        }
198
        else {
199
          $should_be = 'Thu, 10/07/2010 - 10 to Thu, 10/07/2010 - 11';
200
        }
201
        break;
202
      case 'minute':
203
        $edit[$field_name . '[und][0][value][date]'] = '10/07/2010 - 10:30';
204
        $edit[$field_name . '[und][0][value2][date]'] = '10/07/2010 - 11:30';
205
        if ($tz_handling == 'utc') {
206
          $should_be = 'Thu, 10/07/2010 - 21:30 to 22:30';
207
        }
208
        else {
209
          $should_be = 'Thu, 10/07/2010 - 10:30 to 11:30';
210
        }
211
        break;
212
      case 'second':
213
        $edit[$field_name . '[und][0][value][date]'] = '10/07/2010 - 10:30:30';
214
        $edit[$field_name . '[und][0][value2][date]'] = '10/07/2010 - 11:30:30';
215
        if ($tz_handling == 'utc') {
216
          $should_be = 'Thu, 10/07/2010 - 21:30:30 to 22:30:30';
217
        }
218
        else {
219
          $should_be = 'Thu, 10/07/2010 - 10:30:30 to 11:30:30';
220
        }
221
        break;
222
    }
223
    $this->drupalPost('node/add/story', $edit, t('Save'));
224
    $this->assertText($edit['title'], "Node has been created");
225
    $this->assertText($should_be, "Found the correct date for a $field_type field using $max_granularity granularity with $tz_handling timezone handling.");
226
  }
227
}