Projet

Général

Profil

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

root / drupal7 / sites / all / modules / date / tests / date_field.test @ b720ea3e

1
<?php
2

    
3
/**
4
 * @file
5
 * Basic functions for Date tests.
6
 */
7
abstract class DateFieldBasic extends DrupalWebTestCase {
8
  protected $privileged_user;
9

    
10
  /**
11
   * @todo.
12
   */
13
  protected function setUp() {
14
    // Load the date_api module.
15
    parent::setUp('field', 'field_ui', 'date_api', 'date', 'date_popup', 'date_tools');
16

    
17
    // Create and log in our privileged user.
18
    $this->privileged_user = $this->drupalCreateUser(
19
      array('administer content types', 'administer nodes', 'bypass node access', 'administer date tools')
20
    );
21
    $this->drupalLogin($this->privileged_user);
22

    
23
    variable_set('date_popup_timepicker', 'none');
24

    
25
    module_load_include('inc', 'node', 'content_types');
26
    module_load_include('inc', 'node', 'node.pages');
27
    module_load_include('inc', 'field', 'field.crud');
28
    module_load_include('inc', 'date', 'date_admin');
29

    
30
    $edit = array();
31
    $edit['name'] = 'Story';
32
    $edit['type'] = 'story';
33
    $this->drupalPost('admin/structure/types/add', $edit, t('Save content type'));
34
    $this->assertText('The content type Story has been added.', 'Content type added.');
35

    
36
  }
37

    
38
  /**
39
   * Creates a date field from an array of settings values.
40
   *
41
   * All values have defaults, only need to specify values that need to be
42
   * different.
43
   */
44
  protected function createDateField($values = array()) {
45
    extract($values);
46

    
47
    $field_name = !empty($field_name) ? $field_name : 'field_test';
48
    $entity_type = !empty($entity_type) ? $entity_type : 'node';
49
    $bundle = !empty($bundle) ? $bundle : 'story';
50
    $label = !empty($label) ? $label : 'Test';
51
    $field_type = !empty($field_type) ? $field_type : 'datetime';
52
    $repeat = !empty($repeat) ? $repeat : 0;
53
    $todate = !empty($todate) ? $todate : 'optional';
54
    $widget_type = !empty($widget_type) ? $widget_type : 'date_select';
55
    $tz_handling = !empty($tz_handling) ? $tz_handling : 'site';
56
    $granularity = !empty($granularity) ? $granularity : array('year', 'month', 'day', 'hour', 'minute');
57
    $year_range = !empty($year_range) ? $year_range : '2010:+1';
58
    $input_format = !empty($input_format) ? $input_format : date_default_format($widget_type);
59
    $input_format_custom = !empty($input_format_custom) ? $input_format_custom : '';
60
    $text_parts = !empty($text_parts) ? $text_parts : array();
61
    $increment = !empty($increment) ? $increment : 15;
62
    $default_value = !empty($default_value) ? $default_value : 'now';
63
    $default_value2 = !empty($default_value2) ? $default_value2 : 'blank';
64
    $default_format = !empty($default_format) ? $default_format : 'long';
65
    $cache_enabled = !empty($cache_enabled);
66
    $cache_count = !empty($cache_count) ? $cache_count : 4;
67

    
68
    $field = array(
69
      'field_name' => $field_name,
70
      'type' => $field_type,
71
      'cardinality' => !empty($repeat) ? FIELD_CARDINALITY_UNLIMITED : 1,
72
      'settings' => array(
73
        'granularity' => $granularity,
74
        'tz_handling' => $tz_handling,
75
        'timezone_db' => date_get_timezone_db($tz_handling),
76
        'repeat' => $repeat,
77
        'todate' => $todate,
78
        'cache_enabled' => $cache_enabled,
79
        'cache_count' => $cache_count,
80
      ),
81
    );
82
    $instance = array(
83
      'entity_type' => $entity_type,
84
      'field_name' => $field_name,
85
      'label' => $label,
86
      'bundle' => $bundle,
87
      // Move the date right below the title.
88
      'weight' => -4,
89
      'widget' => array(
90
        'type' => $widget_type,
91
        // Increment for minutes and seconds, can be 1, 5, 10, 15, or 30.
92
        'settings' => array(
93
          'increment' => $increment,
94
          // The number of years to go back and forward in drop-down year
95
          // selectors.
96
          'year_range' => $year_range,
97
          'input_format' => $input_format,
98
          'input_format_custom' => $input_format_custom,
99
          'text_parts' => $text_parts,
100
          'label_position' => 'above',
101
          'repeat_collapsed' => 0,
102
        ),
103
        'weight' => -4,
104
      ),
105
      'settings' => array(
106
        'default_value' => $default_value,
107
        'default_value2' => $default_value2,
108
      ),
109
    );
110

    
111
    $instance['display'] = array(
112
      'default' => array(
113
        'label' => 'above',
114
        'type' => 'date_default',
115
        'settings' => array(
116
          'format_type' => $default_format,
117
          'show_repeat_rule' => 'show',
118
          'multiple_number' => '',
119
          'multiple_from' => '',
120
          'multiple_to' => '',
121
          'fromto' => 'both',
122
        ),
123
        'module' => 'date',
124
        'weight' => 0 ,
125
      ),
126
      'teaser' => array(
127
        'label' => 'above',
128
        'type' => 'date_default',
129
        'weight' => 0,
130
        'settings' => array(
131
          'format_type' => $default_format,
132
          'show_repeat_rule' => 'show',
133
          'multiple_number' => '',
134
          'multiple_from' => '',
135
          'multiple_to' => '',
136
          'fromto' => 'both',
137
        ),
138
        'module' => 'date',
139
      ),
140
    );
141

    
142
    $field = field_create_field($field);
143
    $instance = field_create_instance($instance);
144

    
145
    field_info_cache_clear(TRUE);
146
    field_cache_clear(TRUE);
147

    
148
    // Look at how the field got configured.
149
    $this->drupalGet("admin/structure/types/manage/$bundle/fields/$field_name");
150
    $this->drupalGet("admin/structure/types/manage/$bundle/display");
151

    
152
  }
153

    
154
  /**
155
   * Creates a date field from an array of settings values.
156
   *
157
   * All values have defaults, only need to specify values that need to be
158
   * different.
159
   */
160
  protected function createMultiDateField($values = array()) {
161
    extract($values);
162

    
163
    $field_name = !empty($field_name) ? $field_name : 'field_test';
164
    $entity_type = !empty($entity_type) ? $entity_type : 'node';
165
    $bundle = !empty($bundle) ? $bundle : 'story';
166
    $label = !empty($label) ? $label : 'Test';
167
    $field_type = !empty($field_type) ? $field_type : 'datetime';
168
    $repeat = !empty($repeat) ? $repeat : 0;
169
    $todate = !empty($todate) ? $todate : 'optional';
170
    $widget_type = !empty($widget_type) ? $widget_type : 'date_select';
171
    $this->verbose(!empty($tz_handling));
172
    $tz_handling = !empty($tz_handling) ? $tz_handling : 'site';
173
    $granularity = !empty($granularity) ? $granularity : array('year', 'month', 'day', 'hour', 'minute');
174
    $year_range = !empty($year_range) ? $year_range : '2010:+1';
175
    $input_format = !empty($input_format) ? $input_format : date_default_format($widget_type);
176
    $input_format_custom = !empty($input_format_custom) ? $input_format_custom : '';
177
    $text_parts = !empty($text_parts) ? $text_parts : array();
178
    $increment = !empty($increment) ? $increment : 15;
179
    $default_value = !empty($default_value) ? $default_value : 'now';
180
    $default_value2 = !empty($default_value2) ? $default_value2 : 'blank';
181
    $default_format = !empty($default_format) ? $default_format : 'long';
182
    $cache_enabled = !empty($cache_enabled);
183
    $cache_count = !empty($cache_count) ? $cache_count : 4;
184
    $cardinality = !empty($cardinality) ? $cardinality : 1;
185

    
186
    $field = array(
187
      'field_name' => $field_name,
188
      'type' => $field_type,
189
      'cardinality' => $cardinality,
190
      'settings' => array(
191
        'granularity' => $granularity,
192
        'tz_handling' => $tz_handling,
193
        'timezone_db' => date_get_timezone_db($tz_handling),
194
        'repeat' => $repeat,
195
        'todate' => $todate,
196
        'cache_enabled' => $cache_enabled,
197
        'cache_count' => $cache_count,
198
      ),
199
    );
200
    $instance = array(
201
      'entity_type' => $entity_type,
202
      'field_name' => $field_name,
203
      'label' => $label,
204
      'bundle' => $bundle,
205
      // Move the date right below the title.
206
      'weight' => -4,
207
      'widget' => array(
208
        'type' => $widget_type,
209
        // Increment for minutes and seconds, can be 1, 5, 10, 15, or 30.
210
        'settings' => array(
211
          'increment' => $increment,
212
          // The number of years to go back and forward in drop-down year
213
          // selectors.
214
          'year_range' => $year_range,
215
          'input_format' => $input_format,
216
          'input_format_custom' => $input_format_custom,
217
          'text_parts' => $text_parts,
218
          'label_position' => 'above',
219
          'repeat_collapsed' => 0,
220
        ),
221
        'weight' => -4,
222
      ),
223
      'settings' => array(
224
        'default_value' => $default_value,
225
        'default_value2' => $default_value2,
226
      ),
227
    );
228

    
229
    $instance['display'] = array(
230
      'default' => array(
231
        'label' => 'above',
232
        'type' => 'date_default',
233
        'settings' => array(
234
          'format_type' => $default_format,
235
          'show_repeat_rule' => 'show',
236
          'multiple_number' => '',
237
          'multiple_from' => '',
238
          'multiple_to' => '',
239
          'fromto' => 'both',
240
        ),
241
        'module' => 'date',
242
        'weight' => 0 ,
243
      ),
244
      'teaser' => array(
245
        'label' => 'above',
246
        'type' => 'date_default',
247
        'weight' => 0,
248
        'settings' => array(
249
          'format_type' => $default_format,
250
          'show_repeat_rule' => 'show',
251
          'multiple_number' => '',
252
          'multiple_from' => '',
253
          'multiple_to' => '',
254
          'fromto' => 'both',
255
        ),
256
        'module' => 'date',
257
      ),
258
    );
259

    
260
    $field = field_create_field($field);
261
    $instance = field_create_instance($instance);
262

    
263
    field_info_cache_clear(TRUE);
264
    field_cache_clear(TRUE);
265

    
266
    // Look at how the field got configured.
267
    $this->drupalGet("admin/structure/types/manage/$bundle/fields/$field_name");
268
    $this->drupalGet("admin/structure/types/manage/$bundle/display");
269
  }
270

    
271
  /**
272
   * @todo.
273
   */
274
  protected function deleteDateField($label, $bundle = 'story', $bundle_name = 'Story') {
275
    $this->drupalGet("admin/structure/types/manage/$bundle/fields");
276
    $this->clickLink('delete');
277
    $this->drupalPost(NULL, NULL, t('Delete'));
278
    $this->assertText("The field $label has been deleted from the $bundle_name content type.", 'Removed date field.');
279
  }
280

    
281
}
282

    
283
class DateFieldTestCase extends DateFieldBasic {
284

    
285
  /**
286
   * @todo.
287
   */
288
  public static function getInfo() {
289
    return array(
290
      'name' => 'Date Field',
291
      'description' => 'Test date field settings and Start/End date interaction.',
292
      'group' => 'Date',
293
    );
294
  }
295

    
296
  /**
297
   * @todo.
298
   */
299
  public function testField() {
300
    // Create a date fields with simple values.
301
    foreach (array('date', 'datestamp', 'datetime') as $field_type) {
302
      foreach (array('date_select', 'date_popup', 'date_text') as $widget_type) {
303
        $field_name = "field_test_$widget_type";
304
        $label = 'Test';
305
        $options = array(
306
          'label' => $label,
307
          'widget_type' => $widget_type,
308
          'field_name' => $field_name,
309
          'field_type' => $field_type,
310
          'input_format' => 'm/d/Y - H:i',
311
        );
312
        $this->createDateField($options);
313
        $this->dateForm($field_name, $field_type, $widget_type);
314
        $this->deleteDateField($label);
315
      }
316
    }
317
  }
318

    
319
  /**
320
   * @todo.
321
   */
322
  public function dateForm($field_name, $field_type, $widget_type, $todate = TRUE) {
323
    // Tests that date field functions properly.
324
    $edit = array();
325
    $edit['title'] = $this->randomName(8);
326
    if ($widget_type == 'date_select') {
327
      $edit[$field_name . '[und][0][value][year]'] = '2010';
328
      $edit[$field_name . '[und][0][value][month]'] = '10';
329
      $edit[$field_name . '[und][0][value][day]'] = '7';
330
      $edit[$field_name . '[und][0][value][hour]'] = '10';
331
      $edit[$field_name . '[und][0][value][minute]'] = '30';
332
      if ($todate) {
333
        $edit[$field_name . '[und][0][show_todate]'] = '1';
334
        $edit[$field_name . '[und][0][value2][year]'] = '2010';
335
        $edit[$field_name . '[und][0][value2][month]'] = '10';
336
        $edit[$field_name . '[und][0][value2][day]'] = '7';
337
        $edit[$field_name . '[und][0][value2][hour]'] = '11';
338
        $edit[$field_name . '[und][0][value2][minute]'] = '30';
339
      }
340
    }
341
    elseif ($widget_type == 'date_text') {
342
      $edit[$field_name . '[und][0][value][date]'] = '10/07/2010 - 10:30';
343
      if ($todate) {
344
        $edit[$field_name . '[und][0][show_todate]'] = '1';
345
        $edit[$field_name . '[und][0][value2][date]'] = '10/07/2010 - 11:30';
346
      }
347
    }
348
    elseif ($widget_type == 'date_popup') {
349
      $edit[$field_name . '[und][0][value][date]'] = '10/07/2010';
350
      $edit[$field_name . '[und][0][value][time]'] = '10:30';
351
      if ($todate) {
352
        $edit[$field_name . '[und][0][show_todate]'] = '1';
353
        $edit[$field_name . '[und][0][value2][date]'] = '10/07/2010';
354
        $edit[$field_name . '[und][0][value2][time]'] = '11:30';
355
      }
356
    }
357
    // Test that the date is displayed correctly using both the 'short' and
358
    // 'long' date types.
359
    //
360
    // For the short type, save an explicit format and assert that is the one
361
    // which is displayed.
362
    variable_set('date_format_short', 'l, m/d/Y - H:i:s');
363
    $instance = field_info_instance('node', $field_name, 'story');
364
    $instance['display']['default']['settings']['format_type'] = 'short';
365
    field_update_instance($instance);
366
    $this->drupalPost('node/add/story', $edit, t('Save'));
367
    $this->assertText($edit['title'], "Node has been created");
368
    $should_be = $todate ? 'Thursday, 10/07/2010 - 10:30 to 11:30' : 'Thursday, 10/07/2010 - 10:30';
369
    $this->assertText($should_be, "Found the correct date for a $field_type field using the $widget_type widget displayed using the short date format.");
370
    // For the long format, do not save anything, and assert that the displayed
371
    // date uses the expected default value of this format provided by Drupal
372
    // core ('l, F j, Y - H:i').
373
    $instance = field_info_instance('node', $field_name, 'story');
374
    $instance['display']['default']['settings']['format_type'] = 'long';
375
    field_update_instance($instance);
376
    $this->drupalPost('node/add/story', $edit, t('Save'));
377
    $this->assertText($edit['title'], "Node has been created");
378
    $should_be = $todate ? 'Thursday, October 7, 2010 - 10:30 to 11:30' : 'Thursday, October 7, 2010 - 10:30';
379
    $this->assertText($should_be, "Found the correct date for a $field_type field using the $widget_type widget displayed using the long date format.");
380
  }
381
}