Projet

Général

Profil

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

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

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
  public static function getInfo() {
17
    return array(
18
      'name' => 'Mapper: Date',
19
      'description' => 'Test Feeds Mapper support for CCK Date fields.',
20
      'group' => 'Feeds',
21
      'dependencies' => array('date'),
22
    );
23
  }
24

    
25
  public function setUp() {
26
    parent::setUp(array('date_api', 'date'));
27
    variable_set('date_default_timezone', 'UTC');
28
  }
29

    
30
  /**
31
   * Basic test loading a single entry CSV file.
32
   */
33
  public function test() {
34
    $this->drupalGet('admin/config/regional/settings');
35

    
36
    // Create content type.
37
    $typename = $this->createContentType(array(), array(
38
      'date' => 'date',
39
      'datestamp' => 'datestamp',
40
      'datetime' => 'datetime',
41
    ));
42

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

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

    
93
    $edit = array(
94
      'allowed_extensions' => 'rss2',
95
    );
96
    $this->drupalPost('admin/structure/feeds/daterss/settings/FeedsFileFetcher', $edit, 'Save');
97

    
98
    // Import CSV file.
99
    $this->importFile('daterss', $this->absolutePath() . '/tests/feeds/googlenewstz.rss2');
100
    $this->assertText('Created 6 nodes');
101

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

    
119
  protected function getFormFieldsNames($field_name, $index) {
120
    if (in_array($field_name, array('date', 'datetime', 'datestamp'))) {
121
      return array("field_{$field_name}[und][{$index}][value][date]");
122
    }
123
    else {
124
      return parent::getFormFieldsNames($field_name, $index);
125
    }
126
  }
127

    
128
  /**
129
   * Tests importing dates using the timezone mapping option.
130
   */
131
  public function testTimezoneMappingOption() {
132
    // Create content type.
133
    $typename = $this->createContentType(array(), array(
134
      'date' => 'date',
135
      'datestamp' => 'datestamp',
136
      'datetime' => 'datetime',
137
    ));
138

    
139
    // Hack to get date fields to not round to every 15 minutes.
140
    foreach (array('date', 'datestamp', 'datetime') as $field) {
141
      $field = 'field_' . $field;
142
      $edit = array(
143
        'widget_type' => 'date_select',
144
      );
145
      $this->drupalPost('admin/structure/types/manage/' . $typename . '/fields/' . $field . '/widget-type', $edit, 'Continue');
146
      $edit = array(
147
        'instance[widget][settings][increment]' => 1,
148
        'field[settings][enddate_get]' => 1,
149
        'instance[settings][default_value]' => 'blank',
150
      );
151
      $this->drupalPost('admin/structure/types/manage/' . $typename . '/fields/' . $field, $edit, 'Save settings');
152
      $edit = array(
153
        'widget_type' => 'date_text',
154
      );
155
      $this->drupalPost('admin/structure/types/manage/' . $typename . '/fields/' . $field . '/widget-type', $edit, 'Continue');
156
    }
157

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

    
210
    // Import CSV file.
211
    $this->importFile('csv', $this->absolutePath() . '/tests/feeds/content_date.csv');
212
    $this->assertText('Created 2 nodes');
213

    
214
    // Check the imported nodes.
215
    $date_values = array(
216
      // Wintertime.
217
      // (Hear me calling)
218
      1 => array(
219
        'field_date_start' => '11/05/1955 - 20:00',
220
        'field_date_end' => '11/05/1955 - 23:00',
221
        'field_datestamp_start' => '11/05/1955 - 11:00',
222
        'field_datestamp_end' => '11/05/1955 - 14:00',
223
        'field_datetime_start' => '11/05/1955 - 02:00',
224
        'field_datetime_end' => '11/05/1955 - 05:00',
225
      ),
226
      // Summertime =+0100.
227
      // (Dee dee dee)
228
      2 => array(
229
        'field_date_start' => '10/22/2015 - 06:29',
230
        'field_date_end' => '10/22/2015 - 09:29',
231
        'field_datestamp_start' => '10/21/2015 - 21:29',
232
        'field_datestamp_end' => '10/22/2015 - 00:29',
233
        'field_datetime_start' => '10/21/2015 - 12:29',
234
        'field_datetime_end' => '10/21/2015 - 15:29',
235
      ),
236
    );
237
    for ($i = 1; $i <= 2; $i++) {
238
      $this->drupalGet("node/$i/edit");
239
      $this->assertFieldByName('field_date[und][0][value][date]', $date_values[$i]['field_date_start']);
240
      $this->assertFieldByName('field_date[und][0][value2][date]', $date_values[$i]['field_date_end']);
241
      $this->assertFieldByName('field_datestamp[und][0][value][date]', $date_values[$i]['field_datestamp_start']);
242
      $this->assertFieldByName('field_datestamp[und][0][value2][date]', $date_values[$i]['field_datestamp_end']);
243
      $this->assertFieldByName('field_datetime[und][0][value][date]', $date_values[$i]['field_datetime_start']);
244
      $this->assertFieldByName('field_datetime[und][0][value2][date]', $date_values[$i]['field_datetime_end']);
245
    }
246
  }
247

    
248
  /**
249
   * Tests if values are cleared out when an empty value is provided.
250
   */
251
  public function testClearOutValues() {
252
    // Create content type.
253
    $typename = $this->createContentType(array(), array(
254
      'date' => 'date',
255
      'datestamp' => 'datestamp',
256
      'datetime' => 'datetime',
257
    ));
258

    
259
    // Hack to get date fields to not round to every 15 minutes.
260
    foreach (array('date', 'datestamp', 'datetime') as $field) {
261
      $field = 'field_' . $field;
262
      $edit = array(
263
        'widget_type' => 'date_select',
264
      );
265
      $this->drupalPost('admin/structure/types/manage/' . $typename . '/fields/' . $field . '/widget-type', $edit, 'Continue');
266
      $edit = array(
267
        'instance[widget][settings][increment]' => 1,
268
        'field[settings][enddate_get]' => 1,
269
        'instance[settings][default_value]' => 'blank',
270
      );
271
      $this->drupalPost('admin/structure/types/manage/' . $typename . '/fields/' . $field, $edit, 'Save settings');
272
      $edit = array(
273
        'widget_type' => 'date_text',
274
      );
275
      $this->drupalPost('admin/structure/types/manage/' . $typename . '/fields/' . $field . '/widget-type', $edit, 'Continue');
276
    }
277

    
278
    // Create and configure importer.
279
    $this->createImporterConfiguration('Content CSV', 'csv');
280
    $this->setSettings('csv', NULL, array(
281
      'content_type' => '',
282
      'import_period' => FEEDS_SCHEDULE_NEVER,
283
    ));
284
    $this->setPlugin('csv', 'FeedsFileFetcher');
285
    $this->setPlugin('csv', 'FeedsCSVParser');
286
    $this->setSettings('csv', 'FeedsNodeProcessor', array(
287
      'bundle' => $typename,
288
      'update_existing' => 1,
289
    ));
290
    $this->addMappings('csv', array(
291
      0 => array(
292
        'source' => 'title',
293
        'target' => 'title',
294
        'unique' => TRUE,
295
      ),
296
      1 => array(
297
        'source' => 'created',
298
        'target' => 'field_date:start',
299
      ),
300
      2 => array(
301
        'source' => 'end',
302
        'target' => 'field_date:end',
303
      ),
304
      3 => array(
305
        'source' => 'created',
306
        'target' => 'field_datestamp:start',
307
      ),
308
      4 => array(
309
        'source' => 'end',
310
        'target' => 'field_datestamp:end',
311
      ),
312
      5 => array(
313
        'source' => 'created',
314
        'target' => 'field_datetime:start',
315
      ),
316
      6 => array(
317
        'source' => 'end',
318
        'target' => 'field_datetime:end',
319
      ),
320
    ));
321

    
322
    // Import CSV file.
323
    $this->importFile('csv', $this->absolutePath() . '/tests/feeds/content_date.csv');
324
    $this->assertText('Created 2 nodes');
325

    
326
    // Check the imported nodes.
327
    $date_values = array(
328
      1 => array(
329
        'created' => '09/03/2009 - 00:12',
330
        'end' => '11/03/2012 - 09:58',
331
      ),
332
      2 => array(
333
        'created' => '09/02/2009 - 22:59',
334
        'end' => '11/03/2012 - 08:46',
335
      ),
336
    );
337
    for ($i = 1; $i <= 2; $i++) {
338
      $this->drupalGet("node/$i/edit");
339
      $this->assertFieldByName('field_date[und][0][value][date]', $date_values[$i]['created']);
340
      $this->assertFieldByName('field_date[und][0][value2][date]', $date_values[$i]['end']);
341
      $this->assertFieldByName('field_datestamp[und][0][value][date]', $date_values[$i]['created']);
342
      $this->assertFieldByName('field_datestamp[und][0][value2][date]', $date_values[$i]['end']);
343
      $this->assertFieldByName('field_datetime[und][0][value][date]', $date_values[$i]['created']);
344
      $this->assertFieldByName('field_datetime[und][0][value2][date]', $date_values[$i]['end']);
345
    }
346

    
347
    // Import CSV file with empty values.
348
    $this->importFile('csv', $this->absolutePath() . '/tests/feeds/content_empty.csv');
349
    $this->assertText('Updated 2 nodes');
350

    
351
    // Check if all values were cleared out for both nodes.
352
    for ($i = 1; $i <= 2; $i++) {
353
      $this->drupalGet("node/$i/edit");
354
      $this->assertFieldByName('field_date[und][0][value][date]', '');
355
      $this->assertFieldByName('field_date[und][0][value2][date]', '');
356
      $this->assertFieldByName('field_datestamp[und][0][value][date]', '');
357
      $this->assertFieldByName('field_datestamp[und][0][value2][date]', '');
358
      $this->assertFieldByName('field_datetime[und][0][value][date]', '');
359
      $this->assertFieldByName('field_datetime[und][0][value2][date]', '');
360
      $this->drupalGet("node/$i");
361
      $this->assertNoText('date_label');
362
      $this->assertNoText('datestamp_label');
363
      $this->assertNoText('datetime_label');
364
    }
365

    
366
    // Re-import the first file again and check if the values returned.
367
    $this->importFile('csv', $this->absolutePath() . '/tests/feeds/content_date.csv');
368
    $this->assertText('Updated 2 nodes');
369
    for ($i = 1; $i <= 2; $i++) {
370
      $this->drupalGet("node/$i/edit");
371
      $this->assertFieldByName('field_date[und][0][value][date]', $date_values[$i]['created']);
372
      $this->assertFieldByName('field_date[und][0][value2][date]', $date_values[$i]['end']);
373
      $this->assertFieldByName('field_datestamp[und][0][value][date]', $date_values[$i]['created']);
374
      $this->assertFieldByName('field_datestamp[und][0][value2][date]', $date_values[$i]['end']);
375
      $this->assertFieldByName('field_datetime[und][0][value][date]', $date_values[$i]['created']);
376
      $this->assertFieldByName('field_datetime[und][0][value2][date]', $date_values[$i]['end']);
377
    }
378

    
379
    // Import CSV file with non-existent values.
380
    $this->importFile('csv', $this->absolutePath() . '/tests/feeds/content_non_existent.csv');
381
    $this->assertText('Updated 2 nodes');
382

    
383
    // Check if all values were cleared out for node 1.
384
    $this->drupalGet('node/1/edit');
385
    $this->assertFieldByName('field_date[und][0][value2][date]', '');
386
    $this->assertFieldByName('field_date[und][0][value2][date]', '');
387
    $this->assertFieldByName('field_datestamp[und][0][value2][date]', '');
388
    $this->assertFieldByName('field_datestamp[und][0][value2][date]', '');
389
    $this->assertFieldByName('field_datetime[und][0][value2][date]', '');
390
    $this->assertFieldByName('field_datetime[und][0][value2][date]', '');
391
    // Check if labels for fields that should be cleared out are not shown.
392
    $this->drupalGet('node/1');
393
    $this->assertNoText('date_label');
394
    $this->assertNoText('datestamp_label');
395
    $this->assertNoText('datetime_label');
396
  }
397

    
398
}