Projet

Général

Profil

Paste
Télécharger (9,67 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / date / tests / DateTimezoneTestCase.test @ 599a39cd

1
<?php
2

    
3
/**
4
 * @file
5
 * Test timezone handling.
6
 */
7

    
8
/**
9
 * Test timezone handling.
10
 */
11
class DateTimezoneTestCase extends DateFieldTestBase {
12

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

    
24
  /**
25
   * {@inheritdoc}
26
   */
27
  public function setUp(array $modules = array()) {
28
    parent::setUp($modules);
29

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

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

    
72
  /**
73
   * Test timezone handling validation on the field settings form.
74
  */
75
  public function testFieldWidgetSettings() {
76
    $label = 'Test';
77
    $options = array(
78
      'label' => $label,
79
      'field_type' => 'date',
80
      'widget_type' => 'date_select',
81
      'granularity' => array('year', 'month', 'day'),
82
    );
83
    $this->createDateField($options);
84
    $this->drupalGet('admin/structure/types/manage/story/fields/field_' . strtolower($label));
85
    $this->assertResponse(200);
86
    $edit = array(
87
      'field[settings][granularity][hour]' => FALSE,
88
    );
89
    $this->drupalPost('admin/structure/types/manage/story/fields/field_' . strtolower($label), $edit, t('Save settings'));
90
    $this->assertText("Dates without hours granularity must not use any timezone handling.", "Dates without hours granularity required to use timezone handling of 'none.'");
91
    $this->deleteDateField($label);
92
  }
93

    
94
  /**
95
   * Validates timezone handling with a multi-value date field.
96
   */
97
  public function testMultiUserTimezone() {
98
    // Create date fields with combinations of various types and granularity
99
    // using the "Date's Timezone" strategy.
100
    $field_type = 'datetime';
101
    $tz_handling = 'date';
102
    $max_granularity = 'minute';
103

    
104
    // Create date field.
105
    $field_name = "field_test";
106
    $label = 'Test';
107
    $options = array(
108
      'label' => $label,
109
      'widget_type' => 'date_text',
110
      'field_name' => $field_name,
111
      'field_type' => $field_type,
112
      'input_format' => 'custom',
113
      'input_format_custom' => 'm/d/Y - H:i:s T',
114
      'cardinality' => 3,
115
      'tz_handling' => $tz_handling,
116
    );
117
    $this->createMultiDateField($options);
118

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

    
122
    $this->deleteDateField($label);
123
  }
124

    
125
  /**
126
   * Tests the submission of a date field's widget with unlimited cardinality.
127
   */
128
  public function dateMultiValueForm($field_name, $field_type, $max_granularity, $tz_handling) {
129
    variable_set('date_format_long', 'D, m/d/Y - H:i:s T');
130

    
131
    $edit = array();
132
    $should_be = array();
133
    $edit['title'] = $this->randomName(8);
134
    $timezones = array('America/Chicago', 'America/Los_Angeles', 'America/New_York');
135

    
136
    switch ($max_granularity) {
137
      case 'hour':
138
        $edit[$field_name . '[und][0][value][date]'] = '10/07/2010 - 10:30';
139
        $edit[$field_name . '[und][0][timezone][timezone]'] = 'America/Chicago';
140
        $should_be[0] = 'Thu, 10/07/2010 - 10 CDT';
141

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

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

    
150
        break;
151

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

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

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

    
165
        break;
166

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

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

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

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

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

    
189
    // Goto the edit page and save the node again.
190
    $node = $this->drupalGetNodeByTitle($edit['title']);
191
    $this->drupalGet('node/' . $node->nid . '/edit');
192

    
193
    // Re-assert the proper date timezones.
194
    foreach ($timezones as $key => $timezone) {
195
      $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.");
196
    }
197
  }
198

    
199
  /**
200
   * Replacement for dateForm().
201
   */
202
  public function buildDateForm($field_name, $field_type, $max_granularity, $tz_handling) {
203
    variable_set('date_format_long', 'D, m/d/Y - H:i:s');
204
    $edit = array();
205
    $edit['title'] = $this->randomName(8);
206
    $edit[$field_name . '[und][0][show_todate]'] = '1';
207
    switch ($max_granularity) {
208
      case 'year':
209
        $edit[$field_name . '[und][0][value][date]'] = '2010';
210
        $edit[$field_name . '[und][0][value2][date]'] = '2011';
211
        $should_be = '2010 to 2011';
212
        break;
213

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

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

    
226
      case 'hour':
227
        $edit[$field_name . '[und][0][value][date]'] = '10/07/2010 - 10';
228
        $edit[$field_name . '[und][0][value2][date]'] = '10/07/2010 - 11';
229
        if ($tz_handling == 'utc') {
230
          $should_be = 'Thu, 10/07/2010 - 21 to Thu, 10/07/2010 - 22';
231
        }
232
        else {
233
          $should_be = 'Thu, 10/07/2010 - 10 to Thu, 10/07/2010 - 11';
234
        }
235
        break;
236

    
237
      case 'minute':
238
        $edit[$field_name . '[und][0][value][date]'] = '10/07/2010 - 10:30';
239
        $edit[$field_name . '[und][0][value2][date]'] = '10/07/2010 - 11:30';
240
        if ($tz_handling == 'utc') {
241
          $should_be = 'Thu, 10/07/2010 - 21:30 to 22:30';
242
        }
243
        else {
244
          $should_be = 'Thu, 10/07/2010 - 10:30 to 11:30';
245
        }
246
        break;
247

    
248
      case 'second':
249
        $edit[$field_name . '[und][0][value][date]'] = '10/07/2010 - 10:30:30';
250
        $edit[$field_name . '[und][0][value2][date]'] = '10/07/2010 - 11:30:30';
251
        if ($tz_handling == 'utc') {
252
          $should_be = 'Thu, 10/07/2010 - 21:30:30 to 22:30:30';
253
        }
254
        else {
255
          $should_be = 'Thu, 10/07/2010 - 10:30:30 to 11:30:30';
256
        }
257
        break;
258
    }
259
    $this->drupalPost('node/add/story', $edit, t('Save'));
260
    $this->assertText($edit['title'], "Node has been created");
261
    $this->assertText($should_be, "Found the correct date for a $field_type field using $max_granularity granularity with $tz_handling timezone handling.");
262
  }
263

    
264
}