Projet

Général

Profil

Paste
Télécharger (14,5 ko) Statistiques
| Branche: | Révision:

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

1
<?php
2

    
3
/**
4
 * @file
5
 * Shared test functionality.
6
 */
7

    
8
/**
9
 * Shared test functionality.
10
 */
11
abstract class DateFieldTestBase extends DrupalWebTestCase {
12
  protected $privileged_user;
13

    
14
  /**
15
   * {@inheritdoc}
16
   */
17
  public function setUp(array $modules = array()) {
18
    $modules[] = 'field';
19
    $modules[] = 'field_ui';
20
    $modules[] = 'date_api';
21
    $modules[] = 'date';
22
    $modules[] = 'date_tools';
23
    parent::setUp($modules);
24

    
25
    // Create and log in our privileged user.
26
    $perms = array(
27
      'administer content types',
28
      'administer nodes',
29
      'bypass node access',
30
      'administer date tools',
31
      'administer fields',
32
    );
33
    $this->privileged_user = $this->drupalCreateUser($perms);
34
    $this->drupalLogin($this->privileged_user);
35

    
36
    module_load_include('inc', 'node', 'content_types');
37
    module_load_include('inc', 'node', 'node.pages');
38
    module_load_include('inc', 'field', 'field.crud');
39
    module_load_include('inc', 'date', 'date_admin');
40

    
41
    $edit = array();
42
    $edit['name'] = 'Story';
43
    $edit['type'] = 'story';
44
    $this->drupalPost('admin/structure/types/add', $edit, t('Save content type'));
45
    $this->assertText('The content type Story has been added.', 'Content type added.');
46
  }
47

    
48
  /**
49
   * Creates a date field from an array of settings values.
50
   *
51
   * All values have defaults, only need to specify values that need to be
52
   * different.
53
   */
54
  protected function createDateField($values = array()) {
55
    $this->verbose($values);
56
    extract($values);
57

    
58
    $field_name = !empty($field_name) ? $field_name : 'field_test';
59
    $entity_type = !empty($entity_type) ? $entity_type : 'node';
60
    $bundle = !empty($bundle) ? $bundle : 'story';
61
    $label = !empty($label) ? $label : 'Test';
62
    $field_type = !empty($field_type) ? $field_type : 'datetime';
63
    $repeat = !empty($repeat) ? $repeat : 0;
64
    $todate = !empty($todate) ? $todate : 'optional';
65
    $widget_type = !empty($widget_type) ? $widget_type : 'date_select';
66
    $tz_handling = !empty($tz_handling) ? $tz_handling : 'site';
67
    $granularity = !empty($granularity) ? $granularity : array('year', 'month', 'day', 'hour', 'minute');
68
    $year_range = !empty($year_range) ? $year_range : '2010:+1';
69
    $input_format = !empty($input_format) ? $input_format : date_default_format($widget_type);
70
    $input_format_custom = !empty($input_format_custom) ? $input_format_custom : '';
71
    $text_parts = !empty($text_parts) ? $text_parts : array();
72
    $increment = !empty($increment) ? $increment : 15;
73
    $default_value = !empty($default_value) ? $default_value : 'now';
74
    $default_value_code = !empty($default_value_code) ? $default_value_code : NULL;
75
    $default_value2 = !empty($default_value2) ? $default_value2 : 'blank';
76
    $default_format = !empty($default_format) ? $default_format : 'long';
77
    $cache_enabled = !empty($cache_enabled);
78
    $cache_count = !empty($cache_count) ? $cache_count : 4;
79

    
80
    $field = array(
81
      'field_name' => $field_name,
82
      'type' => $field_type,
83
      'cardinality' => !empty($repeat) ? FIELD_CARDINALITY_UNLIMITED : 1,
84
      'settings' => array(
85
        'granularity' => $granularity,
86
        'tz_handling' => $tz_handling,
87
        'timezone_db' => date_get_timezone_db($tz_handling),
88
        'repeat' => $repeat,
89
        'todate' => $todate,
90
        'cache_enabled' => $cache_enabled,
91
        'cache_count' => $cache_count,
92
      ),
93
    );
94
    $instance = array(
95
      'entity_type' => $entity_type,
96
      'field_name' => $field_name,
97
      'label' => $label,
98
      'bundle' => $bundle,
99
      // Move the date to the top.
100
      'weight' => -100,
101
      'widget' => array(
102
        'type' => $widget_type,
103
        // Increment for minutes and seconds, can be 1, 5, 10, 15, or 30.
104
        'settings' => array(
105
          'increment' => $increment,
106
          // The number of years to go back and forward in drop-down year
107
          // selectors.
108
          'year_range' => $year_range,
109
          'input_format' => $input_format,
110
          'input_format_custom' => $input_format_custom,
111
          'text_parts' => $text_parts,
112
          'label_position' => 'above',
113
          'repeat_collapsed' => 0,
114
        ),
115
        'weight' => -100,
116
      ),
117
      'settings' => array(
118
        'default_value' => $default_value,
119
        'default_value_code' => $default_value_code,
120
        'default_value2' => $default_value2,
121
      ),
122
    );
123

    
124
    $instance['display'] = array(
125
      'default' => array(
126
        'label' => 'above',
127
        'type' => 'date_default',
128
        'settings' => array(
129
          'format_type' => $default_format,
130
          'show_repeat_rule' => 'show',
131
          'multiple_number' => '',
132
          'multiple_from' => '',
133
          'multiple_to' => '',
134
          'fromto' => 'both',
135
        ),
136
        'module' => 'date',
137
        'weight' => 0 ,
138
      ),
139
      'teaser' => array(
140
        'label' => 'above',
141
        'type' => 'date_default',
142
        'weight' => 0,
143
        'settings' => array(
144
          'format_type' => $default_format,
145
          'show_repeat_rule' => 'show',
146
          'multiple_number' => '',
147
          'multiple_from' => '',
148
          'multiple_to' => '',
149
          'fromto' => 'both',
150
        ),
151
        'module' => 'date',
152
      ),
153
    );
154

    
155
    $this->verbose($field);
156
    $field = field_create_field($field);
157

    
158
    $this->verbose($instance);
159
    $instance = field_create_instance($instance);
160

    
161
    field_info_cache_clear();
162
    field_cache_clear();
163

    
164
    // Look at how the field got configured.
165
    $this->drupalGet("admin/structure/types/manage/$bundle/fields/$field_name");
166
    $this->drupalGet("admin/structure/types/manage/$bundle/display");
167
  }
168

    
169
  /**
170
   * Creates a date field from an array of settings values.
171
   *
172
   * All values have defaults, only need to specify values that need to be
173
   * different.
174
   */
175
  protected function createMultiDateField($values = array()) {
176
    extract($values);
177

    
178
    $field_name = !empty($field_name) ? $field_name : 'field_test';
179
    $entity_type = !empty($entity_type) ? $entity_type : 'node';
180
    $bundle = !empty($bundle) ? $bundle : 'story';
181
    $label = !empty($label) ? $label : 'Test';
182
    $field_type = !empty($field_type) ? $field_type : 'datetime';
183
    $repeat = !empty($repeat) ? $repeat : 0;
184
    $todate = !empty($todate) ? $todate : 'optional';
185
    $widget_type = !empty($widget_type) ? $widget_type : 'date_select';
186
    $this->verbose(!empty($tz_handling));
187
    $tz_handling = !empty($tz_handling) ? $tz_handling : 'site';
188
    $granularity = !empty($granularity) ? $granularity : array('year', 'month', 'day', 'hour', 'minute');
189
    $year_range = !empty($year_range) ? $year_range : '2010:+1';
190
    $input_format = !empty($input_format) ? $input_format : date_default_format($widget_type);
191
    $input_format_custom = !empty($input_format_custom) ? $input_format_custom : '';
192
    $text_parts = !empty($text_parts) ? $text_parts : array();
193
    $increment = !empty($increment) ? $increment : 15;
194
    $default_value = !empty($default_value) ? $default_value : 'now';
195
    $default_value2 = !empty($default_value2) ? $default_value2 : 'blank';
196
    $default_format = !empty($default_format) ? $default_format : 'long';
197
    $cache_enabled = !empty($cache_enabled);
198
    $cache_count = !empty($cache_count) ? $cache_count : 4;
199
    $cardinality = !empty($cardinality) ? $cardinality : 1;
200

    
201
    $field = array(
202
      'field_name' => $field_name,
203
      'type' => $field_type,
204
      'cardinality' => $cardinality,
205
      'settings' => array(
206
        'granularity' => $granularity,
207
        'tz_handling' => $tz_handling,
208
        'timezone_db' => date_get_timezone_db($tz_handling),
209
        'repeat' => $repeat,
210
        'todate' => $todate,
211
        'cache_enabled' => $cache_enabled,
212
        'cache_count' => $cache_count,
213
      ),
214
    );
215
    $instance = array(
216
      'entity_type' => $entity_type,
217
      'field_name' => $field_name,
218
      'label' => $label,
219
      'bundle' => $bundle,
220
      // Move the date to the top.
221
      'weight' => -100,
222
      'widget' => array(
223
        'type' => $widget_type,
224
        // Increment for minutes and seconds, can be 1, 5, 10, 15, or 30.
225
        'settings' => array(
226
          'increment' => $increment,
227
          // The number of years to go back and forward in drop-down year
228
          // selectors.
229
          'year_range' => $year_range,
230
          'input_format' => $input_format,
231
          'input_format_custom' => $input_format_custom,
232
          'text_parts' => $text_parts,
233
          'label_position' => 'above',
234
          'repeat_collapsed' => 0,
235
        ),
236
        'weight' => -100,
237
      ),
238
      'settings' => array(
239
        'default_value' => $default_value,
240
        'default_value2' => $default_value2,
241
      ),
242
    );
243

    
244
    $instance['display'] = array(
245
      'default' => array(
246
        'label' => 'above',
247
        'type' => 'date_default',
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
        'weight' => 0 ,
258
      ),
259
      'teaser' => array(
260
        'label' => 'above',
261
        'type' => 'date_default',
262
        'weight' => 0,
263
        'settings' => array(
264
          'format_type' => $default_format,
265
          'show_repeat_rule' => 'show',
266
          'multiple_number' => '',
267
          'multiple_from' => '',
268
          'multiple_to' => '',
269
          'fromto' => 'both',
270
        ),
271
        'module' => 'date',
272
      ),
273
    );
274

    
275
    $field = field_create_field($field);
276
    $instance = field_create_instance($instance);
277

    
278
    field_info_cache_clear();
279
    field_cache_clear();
280

    
281
    // Look at how the field got configured.
282
    $this->drupalGet("admin/structure/types/manage/$bundle/fields");
283
    $this->drupalGet("admin/structure/types/manage/$bundle/fields/$field_name");
284
    $this->drupalGet("admin/structure/types/manage/$bundle/display");
285
  }
286

    
287
  /**
288
   * @todo.
289
   */
290
  protected function deleteDateField($label, $bundle = 'story', $bundle_name = 'Story') {
291
    $this->drupalGet("admin/structure/types/manage/$bundle/fields");
292
    $this->clickLink('delete');
293
    $this->drupalPost(NULL, NULL, t('Delete'));
294
    $this->assertText("The field $label has been deleted from the $bundle_name content type.", 'Removed date field.');
295
  }
296

    
297
  /**
298
   * {@inheritdoc}
299
   */
300
  protected function verbose($message, $title = NULL) {
301
    // Handle arrays, objects, etc.
302
    if (!is_string($message)) {
303
      $message = "<pre>\n" . print_r($message, TRUE) . "\n</pre>\n";
304
    }
305

    
306
    // Optional title to go before the output.
307
    if (!empty($title)) {
308
      $title = '<h2>' . check_plain($title) . "</h2>\n";
309
    }
310

    
311
    parent::verbose($title . $message);
312
  }
313

    
314
  /**
315
   * Tests that date field functions properly.
316
   */
317
  public function dateForm($field_name, $field_type, $widget_type, $todate = TRUE) {
318
    $this->verbose(func_get_args());
319

    
320
    $edit = array();
321
    $edit['title'] = $this->randomName(8);
322
    if ($widget_type == 'date_select') {
323
      $edit[$field_name . '[und][0][value][year]'] = '2010';
324
      $edit[$field_name . '[und][0][value][month]'] = '10';
325
      $edit[$field_name . '[und][0][value][day]'] = '7';
326
      $edit[$field_name . '[und][0][value][hour]'] = '10';
327
      $edit[$field_name . '[und][0][value][minute]'] = '30';
328
      if ($todate) {
329
        $edit[$field_name . '[und][0][show_todate]'] = '1';
330
        $edit[$field_name . '[und][0][value2][year]'] = '2010';
331
        $edit[$field_name . '[und][0][value2][month]'] = '10';
332
        $edit[$field_name . '[und][0][value2][day]'] = '7';
333
        $edit[$field_name . '[und][0][value2][hour]'] = '11';
334
        $edit[$field_name . '[und][0][value2][minute]'] = '30';
335
      }
336
    }
337
    elseif ($widget_type == 'date_text') {
338
      $edit[$field_name . '[und][0][value][date]'] = '10/07/2010 - 10:30';
339
      if ($todate) {
340
        $edit[$field_name . '[und][0][show_todate]'] = '1';
341
        $edit[$field_name . '[und][0][value2][date]'] = '10/07/2010 - 11:30';
342
      }
343
    }
344
    elseif ($widget_type == 'date_popup') {
345
      $edit[$field_name . '[und][0][value][date]'] = '10/07/2010';
346
      $edit[$field_name . '[und][0][value][time]'] = '10:30';
347
      if ($todate) {
348
        $edit[$field_name . '[und][0][show_todate]'] = '1';
349
        $edit[$field_name . '[und][0][value2][date]'] = '10/07/2010';
350
        $edit[$field_name . '[und][0][value2][time]'] = '11:30';
351
      }
352
    }
353

    
354
    // Test that the date is displayed correctly using both the 'short' and
355
    // 'long' date types.
356
    // For the short type, save an explicit format and assert that is the one
357
    // which is displayed.
358
    variable_set('date_format_short', 'l, m/d/Y - H:i:s');
359
    $instance = field_info_instance('node', $field_name, 'story');
360
    $instance['display']['default']['settings']['format_type'] = 'short';
361
    field_update_instance($instance);
362
    $this->drupalPost('node/add/story', $edit, t('Save'));
363
    $this->assertText($edit['title'], "Node has been created");
364
    $should_be = $todate ? 'Thursday, 10/07/2010 - 10:30 to 11:30' : 'Thursday, 10/07/2010 - 10:30';
365
    $this->assertText($should_be, "Found the correct date for a $field_type field using the $widget_type widget displayed using the short date format.");
366

    
367
    // For the long format, do not save anything, and assert that the displayed
368
    // date uses the expected default value of this format provided by Drupal
369
    // core ('l, F j, Y - H:i').
370
    $instance = field_info_instance('node', $field_name, 'story');
371
    $instance['display']['default']['settings']['format_type'] = 'long';
372
    field_update_instance($instance);
373
    $this->drupalPost('node/add/story', $edit, t('Save'));
374
    $this->assertText($edit['title'], "Node has been created");
375
    $should_be = $todate ? 'Thursday, October 7, 2010 - 10:30 to 11:30' : 'Thursday, October 7, 2010 - 10:30';
376
    $this->assertText($should_be, "Found the correct date for a $field_type field using the $widget_type widget displayed using the long date format.");
377
  }
378

    
379
  /**
380
   * Run some tests against a specific field type/widget combination.
381
   *
382
   * @param string $field_type
383
   *   The field type to use.
384
   * @param string $widget_type
385
   *   The widget type to use.
386
   * @param bool $delete_when_done
387
   *   Whether to delete the field when it's finished; defaults to TRUE.
388
   */
389
  protected function checkDateField($field_type, $widget_type, $delete_when_done = TRUE) {
390
    $field_name = "field_test_$widget_type";
391
    $label = 'Test';
392
    $options = array(
393
      'label' => $label,
394
      'widget_type' => $widget_type,
395
      'field_name' => $field_name,
396
      'field_type' => $field_type,
397
      'input_format' => 'm/d/Y - H:i',
398
    );
399
    $this->createDateField($options);
400
    $this->dateForm($field_name, $field_type, $widget_type);
401
    if ($delete_when_done) {
402
      $this->deleteDateField($label);
403
    }
404
  }
405

    
406
}