Projet

Général

Profil

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

root / drupal7 / sites / all / modules / feeds / tests / feeds_mapper_date.test @ ed9a13f1

1
<?php
2

    
3
/**
4
 * @file
5
 * Contains FeedsMapperDateTestCase.
6
 */
7

    
8
/**
9
 * Test case for CCK date field mapper mappers/date.inc.
10
 *
11
 * @todo: Add test method iCal
12
 * @todo: Add test method for end date
13
 */
14
class FeedsMapperDateTestCase extends FeedsMapperTestCase {
15

    
16
  /**
17
   * {@inheritdoc}
18
   */
19
  public static function getInfo() {
20
    return array(
21
      'name' => 'Mapper: Date',
22
      'description' => 'Test Feeds Mapper support for CCK Date fields.',
23
      'group' => 'Feeds',
24
      'dependencies' => array('date'),
25
    );
26
  }
27

    
28
  /**
29
   * {@inheritdoc}
30
   */
31
  public function setUp() {
32
    parent::setUp(array('date_api', 'date'));
33
    variable_set('date_default_timezone', 'UTC');
34
  }
35

    
36
  /**
37
   * Basic test loading a single entry CSV file.
38
   */
39
  public function test() {
40
    $this->drupalGet('admin/config/regional/settings');
41

    
42
    // Create content type.
43
    $typename = $this->createContentType(array(), array(
44
      'date' => 'date',
45
      'datestamp' => 'datestamp',
46
      'datetime' => 'datetime',
47
    ));
48

    
49
    // Hack to get date fields to not round to every 15 minutes.
50
    foreach (array('date', 'datestamp', 'datetime') as $field) {
51
      $field = 'field_' . $field;
52
      $edit = array(
53
        'widget_type' => 'date_select',
54
      );
55
      $this->drupalPost('admin/structure/types/manage/' . $typename . '/fields/' . $field . '/widget-type', $edit, 'Continue');
56
      $edit = array(
57
        'instance[widget][settings][increment]' => 1,
58
      );
59
      $this->drupalPost('admin/structure/types/manage/' . $typename . '/fields/' . $field, $edit, 'Save settings');
60
      $edit = array(
61
        'widget_type' => 'date_text',
62
      );
63
      $this->drupalPost('admin/structure/types/manage/' . $typename . '/fields/' . $field . '/widget-type', $edit, 'Continue');
64
    }
65

    
66
    // Create and configure importer.
67
    $this->createImporterConfiguration('Date RSS', 'daterss');
68
    $this->setSettings('daterss', NULL, array(
69
      'content_type' => '',
70
      'import_period' => FEEDS_SCHEDULE_NEVER,
71
    ));
72
    $this->setPlugin('daterss', 'FeedsFileFetcher');
73
    $this->setSettings('daterss', 'FeedsNodeProcessor', array(
74
      'bundle' => $typename,
75
    ));
76
    $this->addMappings('daterss', array(
77
      0 => array(
78
        'source' => 'title',
79
        'target' => 'title',
80
      ),
81
      1 => array(
82
        'source' => 'description',
83
        'target' => 'body',
84
      ),
85
      2 => array(
86
        'source' => 'timestamp',
87
        'target' => 'field_date:start',
88
      ),
89
      3 => array(
90
        'source' => 'timestamp',
91
        'target' => 'field_datestamp:start',
92
      ),
93
      4 => array(
94
        'source' => 'timestamp',
95
        'target' => 'field_datetime:start',
96
      ),
97
    ));
98

    
99
    $edit = array(
100
      'allowed_extensions' => 'rss2',
101
    );
102
    $this->drupalPost('admin/structure/feeds/daterss/settings/FeedsFileFetcher', $edit, 'Save');
103

    
104
    // Import CSV file.
105
    $this->importFile('daterss', $this->absolutePath() . '/tests/feeds/googlenewstz.rss2');
106
    $this->assertText('Created 6 nodes');
107

    
108
    // Check the imported nodes.
109
    $values = array(
110
      '01/06/2010 - 19:26',
111
      '01/06/2010 - 10:21',
112
      '01/06/2010 - 13:42',
113
      '01/06/2010 - 06:05',
114
      '01/06/2010 - 11:26',
115
      '01/07/2010 - 00:26',
116
    );
117
    for ($i = 1; $i <= 6; $i++) {
118
      $this->drupalGet("node/$i/edit");
119
      $this->assertNodeFieldValue('date', $values[$i - 1]);
120
      $this->assertNodeFieldValue('datestamp', $values[$i - 1]);
121
      $this->assertNodeFieldValue('datetime', $values[$i - 1]);
122
    }
123
  }
124

    
125
  /**
126
   * {@inheritdoc}
127
   */
128
  protected function getFormFieldsNames($field_name, $index) {
129
    if (in_array($field_name, array('date', 'datetime', 'datestamp'))) {
130
      return array("field_{$field_name}[und][{$index}][value][date]");
131
    }
132
    else {
133
      return parent::getFormFieldsNames($field_name, $index);
134
    }
135
  }
136

    
137
  /**
138
   * Tests importing dates using the timezone mapping option.
139
   */
140
  public function testTimezoneMappingOption() {
141
    // Create content type.
142
    $typename = $this->createContentType(array(), array(
143
      'date' => 'date',
144
      'datestamp' => 'datestamp',
145
      'datetime' => 'datetime',
146
    ));
147

    
148
    // Hack to get date fields to not round to every 15 minutes.
149
    foreach (array('date', 'datestamp', 'datetime') as $field) {
150
      $field = 'field_' . $field;
151
      $edit = array(
152
        'widget_type' => 'date_select',
153
      );
154
      $this->drupalPost('admin/structure/types/manage/' . $typename . '/fields/' . $field . '/widget-type', $edit, 'Continue');
155
      $edit = array(
156
        'instance[widget][settings][increment]' => 1,
157
        'field[settings][enddate_get]' => 1,
158
        'instance[settings][default_value]' => 'blank',
159
      );
160
      $this->drupalPost('admin/structure/types/manage/' . $typename . '/fields/' . $field, $edit, 'Save settings');
161
      $edit = array(
162
        'widget_type' => 'date_text',
163
      );
164
      $this->drupalPost('admin/structure/types/manage/' . $typename . '/fields/' . $field . '/widget-type', $edit, 'Continue');
165
    }
166

    
167
    // Create and configure importer.
168
    $this->createImporterConfiguration('Content CSV', 'csv');
169
    $this->setSettings('csv', NULL, array(
170
      'content_type' => '',
171
      'import_period' => FEEDS_SCHEDULE_NEVER,
172
    ));
173
    $this->setPlugin('csv', 'FeedsFileFetcher');
174
    $this->setPlugin('csv', 'FeedsCSVParser');
175
    $this->setSettings('csv', 'FeedsNodeProcessor', array(
176
      'bundle' => $typename,
177
    ));
178
    $this->addMappings('csv', array(
179
      0 => array(
180
        'source' => 'title',
181
        'target' => 'title',
182
        'unique' => TRUE,
183
      ),
184
      // Los Angeles == UTC-08:00.
185
      1 => array(
186
        'source' => 'datetime_start',
187
        'target' => 'field_date:start',
188
        'timezone' => 'America/Los_Angeles',
189
      ),
190
      2 => array(
191
        'source' => 'datetime_end',
192
        'target' => 'field_date:end',
193
        'timezone' => 'America/Los_Angeles',
194
      ),
195
      // Amsterdam == UTC+01:00.
196
      3 => array(
197
        'source' => 'datetime_start',
198
        'target' => 'field_datestamp:start',
199
        'timezone' => 'Europe/Amsterdam',
200
      ),
201
      4 => array(
202
        'source' => 'datetime_end',
203
        'target' => 'field_datestamp:end',
204
        'timezone' => 'Europe/Amsterdam',
205
      ),
206
      // Sydney == UTC+10:00.
207
      5 => array(
208
        'source' => 'datetime_start',
209
        'target' => 'field_datetime:start',
210
        'timezone' => 'Australia/Sydney',
211
      ),
212
      6 => array(
213
        'source' => 'datetime_end',
214
        'target' => 'field_datetime:end',
215
        'timezone' => 'Australia/Sydney',
216
      ),
217
    ));
218

    
219
    // Import CSV file.
220
    $this->importFile('csv', $this->absolutePath() . '/tests/feeds/content_date.csv');
221
    $this->assertText('Created 3 nodes');
222

    
223
    // Check the imported nodes.
224
    $date_values = array(
225
      // Wintertime.
226
      // (Hear me calling).
227
      1 => array(
228
        'field_date_start' => '11/05/1955 - 20:00',
229
        'field_date_end' => '11/05/1955 - 23:00',
230
        'field_datestamp_start' => '11/05/1955 - 11:00',
231
        'field_datestamp_end' => '11/05/1955 - 14:00',
232
        'field_datetime_start' => '11/05/1955 - 02:00',
233
        'field_datetime_end' => '11/05/1955 - 05:00',
234
      ),
235
      // Summertime =+0100.
236
      // (Dee dee dee).
237
      2 => array(
238
        'field_date_start' => '10/22/2015 - 06:29',
239
        'field_date_end' => '10/22/2015 - 09:29',
240
        'field_datestamp_start' => '10/21/2015 - 21:29',
241
        'field_datestamp_end' => '10/22/2015 - 00:29',
242
        'field_datetime_start' => '10/21/2015 - 12:29',
243
        'field_datetime_end' => '10/21/2015 - 15:29',
244
      ),
245
      // Timezone is specified in string, all UTC time.
246
      3 => array(
247
        'field_date_start' => '02/09/2018 - 00:00',
248
        'field_date_end' => '02/10/2018 - 22:00',
249
        'field_datestamp_start' => '02/09/2018 - 00:00',
250
        'field_datestamp_end' => '02/10/2018 - 22:00',
251
        'field_datetime_start' => '02/09/2018 - 00:00',
252
        'field_datetime_end' => '02/10/2018 - 22:00',
253
      ),
254
    );
255
    for ($i = 1; $i <= 3; $i++) {
256
      $this->drupalGet("node/$i/edit");
257
      $this->assertFieldByName('field_date[und][0][value][date]', $date_values[$i]['field_date_start']);
258
      $this->assertFieldByName('field_date[und][0][value2][date]', $date_values[$i]['field_date_end']);
259
      $this->assertFieldByName('field_datestamp[und][0][value][date]', $date_values[$i]['field_datestamp_start']);
260
      $this->assertFieldByName('field_datestamp[und][0][value2][date]', $date_values[$i]['field_datestamp_end']);
261
      $this->assertFieldByName('field_datetime[und][0][value][date]', $date_values[$i]['field_datetime_start']);
262
      $this->assertFieldByName('field_datetime[und][0][value2][date]', $date_values[$i]['field_datetime_end']);
263
    }
264
  }
265

    
266
  /**
267
   * Tests if values are cleared out when an empty value is provided.
268
   */
269
  public function testClearOutValues() {
270
    // Create content type.
271
    $typename = $this->createContentType(array(), array(
272
      'date' => 'date',
273
      'datestamp' => 'datestamp',
274
      'datetime' => 'datetime',
275
    ));
276

    
277
    // Hack to get date fields to not round to every 15 minutes.
278
    foreach (array('date', 'datestamp', 'datetime') as $field) {
279
      $field = 'field_' . $field;
280
      $edit = array(
281
        'widget_type' => 'date_select',
282
      );
283
      $this->drupalPost('admin/structure/types/manage/' . $typename . '/fields/' . $field . '/widget-type', $edit, 'Continue');
284
      $edit = array(
285
        'instance[widget][settings][increment]' => 1,
286
        'field[settings][enddate_get]' => 1,
287
        'instance[settings][default_value]' => 'blank',
288
      );
289
      $this->drupalPost('admin/structure/types/manage/' . $typename . '/fields/' . $field, $edit, 'Save settings');
290
      $edit = array(
291
        'widget_type' => 'date_text',
292
      );
293
      $this->drupalPost('admin/structure/types/manage/' . $typename . '/fields/' . $field . '/widget-type', $edit, 'Continue');
294
    }
295

    
296
    // Create and configure importer.
297
    $this->createImporterConfiguration('Content CSV', 'csv');
298
    $this->setSettings('csv', NULL, array(
299
      'content_type' => '',
300
      'import_period' => FEEDS_SCHEDULE_NEVER,
301
    ));
302
    $this->setPlugin('csv', 'FeedsFileFetcher');
303
    $this->setPlugin('csv', 'FeedsCSVParser');
304
    $this->setSettings('csv', 'FeedsNodeProcessor', array(
305
      'bundle' => $typename,
306
      'update_existing' => 1,
307
    ));
308
    $this->addMappings('csv', array(
309
      0 => array(
310
        'source' => 'title',
311
        'target' => 'title',
312
        'unique' => TRUE,
313
      ),
314
      1 => array(
315
        'source' => 'created',
316
        'target' => 'field_date:start',
317
      ),
318
      2 => array(
319
        'source' => 'end',
320
        'target' => 'field_date:end',
321
      ),
322
      3 => array(
323
        'source' => 'created',
324
        'target' => 'field_datestamp:start',
325
      ),
326
      4 => array(
327
        'source' => 'end',
328
        'target' => 'field_datestamp:end',
329
      ),
330
      5 => array(
331
        'source' => 'created',
332
        'target' => 'field_datetime:start',
333
      ),
334
      6 => array(
335
        'source' => 'end',
336
        'target' => 'field_datetime:end',
337
      ),
338
    ));
339

    
340
    // Import CSV file.
341
    $this->importFile('csv', $this->absolutePath() . '/tests/feeds/content_date.csv');
342
    $this->assertText('Created 3 nodes');
343

    
344
    // Check the imported nodes.
345
    $date_values = array(
346
      1 => array(
347
        'created' => '09/03/2009 - 00:12',
348
        'end' => '11/03/2012 - 09:58',
349
      ),
350
      2 => array(
351
        'created' => '09/02/2009 - 22:59',
352
        'end' => '11/03/2012 - 08:46',
353
      ),
354
    );
355
    for ($i = 1; $i <= 2; $i++) {
356
      $this->drupalGet("node/$i/edit");
357
      $this->assertFieldByName('field_date[und][0][value][date]', $date_values[$i]['created']);
358
      $this->assertFieldByName('field_date[und][0][value2][date]', $date_values[$i]['end']);
359
      $this->assertFieldByName('field_datestamp[und][0][value][date]', $date_values[$i]['created']);
360
      $this->assertFieldByName('field_datestamp[und][0][value2][date]', $date_values[$i]['end']);
361
      $this->assertFieldByName('field_datetime[und][0][value][date]', $date_values[$i]['created']);
362
      $this->assertFieldByName('field_datetime[und][0][value2][date]', $date_values[$i]['end']);
363
    }
364

    
365
    // Import CSV file with empty values.
366
    $this->importFile('csv', $this->absolutePath() . '/tests/feeds/content_empty.csv');
367
    $this->assertText('Updated 2 nodes');
368

    
369
    // Check if all values were cleared out for both nodes.
370
    for ($i = 1; $i <= 2; $i++) {
371
      $this->drupalGet("node/$i/edit");
372
      $this->assertFieldByName('field_date[und][0][value][date]', '');
373
      $this->assertFieldByName('field_date[und][0][value2][date]', '');
374
      $this->assertFieldByName('field_datestamp[und][0][value][date]', '');
375
      $this->assertFieldByName('field_datestamp[und][0][value2][date]', '');
376
      $this->assertFieldByName('field_datetime[und][0][value][date]', '');
377
      $this->assertFieldByName('field_datetime[und][0][value2][date]', '');
378
      $this->drupalGet("node/$i");
379
      $this->assertNoText('date_label');
380
      $this->assertNoText('datestamp_label');
381
      $this->assertNoText('datetime_label');
382
    }
383

    
384
    // Re-import the first file again and check if the values returned.
385
    $this->importFile('csv', $this->absolutePath() . '/tests/feeds/content_date.csv');
386
    $this->assertText('Updated 2 nodes');
387
    for ($i = 1; $i <= 2; $i++) {
388
      $this->drupalGet("node/$i/edit");
389
      $this->assertFieldByName('field_date[und][0][value][date]', $date_values[$i]['created']);
390
      $this->assertFieldByName('field_date[und][0][value2][date]', $date_values[$i]['end']);
391
      $this->assertFieldByName('field_datestamp[und][0][value][date]', $date_values[$i]['created']);
392
      $this->assertFieldByName('field_datestamp[und][0][value2][date]', $date_values[$i]['end']);
393
      $this->assertFieldByName('field_datetime[und][0][value][date]', $date_values[$i]['created']);
394
      $this->assertFieldByName('field_datetime[und][0][value2][date]', $date_values[$i]['end']);
395
    }
396

    
397
    // Import CSV file with non-existent values.
398
    $this->importFile('csv', $this->absolutePath() . '/tests/feeds/content_non_existent.csv');
399
    $this->assertText('Updated 2 nodes');
400

    
401
    // Check if all values were cleared out for node 1.
402
    $this->drupalGet('node/1/edit');
403
    $this->assertFieldByName('field_date[und][0][value2][date]', '');
404
    $this->assertFieldByName('field_date[und][0][value2][date]', '');
405
    $this->assertFieldByName('field_datestamp[und][0][value2][date]', '');
406
    $this->assertFieldByName('field_datestamp[und][0][value2][date]', '');
407
    $this->assertFieldByName('field_datetime[und][0][value2][date]', '');
408
    $this->assertFieldByName('field_datetime[und][0][value2][date]', '');
409
    // Check if labels for fields that should be cleared out are not shown.
410
    $this->drupalGet('node/1');
411
    $this->assertNoText('date_label');
412
    $this->assertNoText('datestamp_label');
413
    $this->assertNoText('datetime_label');
414
  }
415

    
416
}