Projet

Général

Profil

Paste
Télécharger (8,48 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / feeds / tests / feeds_parser_csv.test @ 651307cd

1
<?php
2

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

    
8
/**
9
 * Tests the CSV parser using the UI.
10
 */
11
class FeedsCSVParserTestCase extends FeedsWebTestCase {
12
  public static function getInfo() {
13
    return array(
14
      'name' => 'CSV parser functional tests',
15
      'description' => 'Tests the CSV parser using the UI.',
16
      'group' => 'Feeds',
17
    );
18
  }
19

    
20
  /**
21
   * Tests parsing a CSV when the mbstring extension is not available.
22
   */
23
  public function testMbstringExtensionDisabled() {
24
    // Set "feeds_use_mbstring" to FALSE to emulate that the mbstring extension
25
    // is not loaded.
26
    variable_set('feeds_use_mbstring', FALSE);
27

    
28
    // Remove items after parsing because in < PHP 5.4 processing items with
29
    // encoding issues leads to test failures because check_plain() can only
30
    // handle UTF-8 encoded strings.
31
    // @see feeds_tests_feeds_after_parse()
32
    variable_set('feeds_tests_feeds_after_parse_empty_items', TRUE);
33

    
34
    // Create node type.
35
    $node_type = $this->drupalCreateContentType();
36

    
37
    // Create and configure importer.
38
    $this->createImporterConfiguration('Content CSV', 'csv');
39
    $this->setPlugin('csv', 'FeedsFileFetcher');
40
    $this->setPlugin('csv', 'FeedsCSVParser');
41
    $this->setSettings('csv', 'FeedsNodeProcessor', array('bundle' => $node_type->type));
42
    $this->addMappings('csv', array(
43
      0 => array(
44
        'source' => 'id',
45
        'target' => 'guid',
46
      ),
47
      1 => array(
48
        'source' => 'text',
49
        'target' => 'title',
50
      ),
51
    ));
52

    
53
    // Ensure that on the CSV parser settings page a message is shown about that
54
    // the mbstring extension is not available.
55
    $this->drupalGet('admin/structure/feeds/csv/settings/FeedsCSVParser');
56
    $this->assertNoField('encoding');
57
    $this->assertText('PHP mbstring extension must be available for character encoding conversion.');
58

    
59
    // Try to import a CSV file that is not UTF-8 encoded. No encoding warning
60
    // should be shown, but import should fail.
61
    $this->importFile('csv', $this->absolutePath() . '/tests/feeds/encoding_SJIS.csv');
62
    $this->assertNoText('Source file is not in UTF-8 encoding.');
63
  }
64

    
65
  /**
66
   * Tests an encoding failure during parsing a CSV.
67
   */
68
  public function testEncodingFailure() {
69
    // Create node type.
70
    $node_type = $this->drupalCreateContentType();
71

    
72
    // Create and configure importer.
73
    $this->createImporterConfiguration('Content CSV', 'csv');
74
    $this->setPlugin('csv', 'FeedsFileFetcher');
75
    $this->setPlugin('csv', 'FeedsCSVParser');
76
    $this->setSettings('csv', 'FeedsNodeProcessor', array('bundle' => $node_type->type));
77
    $this->addMappings('csv', array(
78
      0 => array(
79
        'source' => 'id',
80
        'target' => 'guid',
81
      ),
82
      1 => array(
83
        'source' => 'text',
84
        'target' => 'title',
85
      ),
86
    ));
87

    
88
    // Ensure that on the CSV parser settings page a setting for encoding is
89
    // shown.
90
    $this->drupalGet('admin/structure/feeds/csv/settings/FeedsCSVParser');
91
    $this->assertField('encoding');
92
    $this->assertNoText('PHP mbstring extension must be available for character encoding conversion.');
93

    
94
    // Try to import a CSV file that is not UTF-8 encoded. Import should be
95
    // halted and an encoding warning should be shown.
96
    $this->importFile('csv', $this->absolutePath() . '/tests/feeds/encoding_SJIS.csv');
97
    $this->assertNoText('Failed importing 4 nodes.');
98
    $this->assertText('Source file is not in UTF-8 encoding.');
99
  }
100

    
101
  /**
102
   * Tests if a CSV template is generated properly using various settings.
103
   *
104
   * @see ::getTemplateDataProvider()
105
   */
106
  public function testGetTemplate() {
107
    // Create node type.
108
    $node_type = $this->drupalCreateContentType();
109

    
110
    foreach ($this->getTemplateDataProvider() as $key => $testdata) {
111
      // Prepend 'csv' to importer machine name as '0' is not a valid machine
112
      // name.
113
      $key = 'csv' . $key;
114

    
115
      // Create and configure importer.
116
      $this->createImporterConfiguration('Content CSV', $key);
117
      $this->setPlugin($key, 'FeedsCSVParser');
118
      $this->setSettings($key, 'FeedsCSVParser', array(
119
        'delimiter' => $testdata['delimiter'],
120
      ));
121
      $this->setSettings($key, 'FeedsNodeProcessor', array('bundle' => $node_type->type));
122
      $this->addMappings($key, $testdata['mapping']);
123

    
124
      // Get CSV template and assert result.
125
      $this->drupalGet('import/' . $key . '/template');
126
      $this->assertRaw($testdata['expected']);
127
    }
128
  }
129

    
130
  /**
131
   * Data provider for ::testGetTemplate().
132
   */
133
  protected function getTemplateDataProvider() {
134
    return array(
135
      // Delimiter ',' test. Source keys containing a ',' should be wrapped in
136
      // quotes.
137
      array(
138
        'delimiter' => ',',
139
        'mapping' => array(
140
          array(
141
            'source' => 'title+;|',
142
            'target' => 'title',
143
          ),
144
          array(
145
            'source' => 'alpha, beta + gamma',
146
            'target' => 'body',
147
          ),
148
          array(
149
            'source' => 'guid',
150
            'target' => 'guid',
151
          ),
152
        ),
153
        'expected' => 'title+;|,"alpha, beta + gamma",guid',
154
      ),
155

    
156
      // Delimiter ';' test. Source keys containing a ';' should be wrapped in
157
      // quotes.
158
      array(
159
        'delimiter' => ';',
160
        'mapping' => array(
161
          array(
162
            'source' => 'title;)',
163
            'target' => 'title',
164
          ),
165
          array(
166
            'source' => 'alpha, beta + gamma',
167
            'target' => 'body',
168
          ),
169
          array(
170
            'source' => 'guid',
171
            'target' => 'guid',
172
          ),
173
        ),
174
        'expected' => '"title;)";alpha, beta + gamma;guid',
175
      ),
176

    
177
      // Delimiter 'TAB' test.
178
      array(
179
        'delimiter' => 'TAB',
180
        'mapping' => array(
181
          array(
182
            'source' => 'title,;|',
183
            'target' => 'title',
184
          ),
185
          array(
186
            'source' => 'alpha, beta + gamma',
187
            'target' => 'body',
188
          ),
189
          array(
190
            'source' => 'guid',
191
            'target' => 'guid',
192
          ),
193
        ),
194
        'expected' => 'title,;|	alpha, beta + gamma	guid',
195
      ),
196

    
197
      // Delimiter '|' test. Source keys containing a '|' should be wrapped in
198
      // quotes.
199
      array(
200
        'delimiter' => '|',
201
        'mapping' => array(
202
          array(
203
            'source' => 'title+;,',
204
            'target' => 'title',
205
          ),
206
          array(
207
            'source' => 'alpha|beta|gamma',
208
            'target' => 'body',
209
          ),
210
          array(
211
            'source' => 'guid',
212
            'target' => 'guid',
213
          ),
214
        ),
215
        'expected' => 'title+;,|"alpha|beta|gamma"|guid',
216
      ),
217

    
218
      // Delimiter '+' test. Source keys containing a '+' should be wrapped in
219
      // quotes.
220
      array(
221
        'delimiter' => '+',
222
        'mapping' => array(
223
          array(
224
            'source' => 'title,;|',
225
            'target' => 'title',
226
          ),
227
          array(
228
            'source' => 'alpha, beta + gamma',
229
            'target' => 'body',
230
          ),
231
          array(
232
            'source' => 'guid',
233
            'target' => 'guid',
234
          ),
235
        ),
236
        'expected' => 'title,;|+"alpha, beta + gamma"+guid',
237
      ),
238

    
239
      // Ensure that when a source key is used multiple times in mapping, the
240
      // key is only printed once in the CSV template.
241
      array(
242
        'delimiter' => ',',
243
        'mapping' => array(
244
          array(
245
            'source' => 'text',
246
            'target' => 'title',
247
          ),
248
          array(
249
            'source' => 'guid',
250
            'target' => 'guid',
251
          ),
252
          array(
253
            'source' => 'date',
254
            'target' => 'created',
255
          ),
256
          array(
257
            'source' => 'date',
258
            'target' => 'changed',
259
          ),
260
          array(
261
            'source' => 'text',
262
            'target' => 'body',
263
          ),
264
        ),
265
        'expected' => 'text,guid,date',
266
      ),
267

    
268
      // Special characters. Things like '&' shouldn't be converted to '&amp;'
269
      // for example.
270
      array(
271
        'delimiter' => ',',
272
        'mapping' => array(
273
          array(
274
            'source' => '&',
275
            'target' => 'title',
276
          ),
277
          array(
278
            'source' => 'alpha&beta',
279
            'target' => 'body',
280
          ),
281
          array(
282
            'source' => '<created>',
283
            'target' => 'created',
284
          ),
285
          array(
286
            'source' => '\'guid\'',
287
            'target' => 'guid',
288
          ),
289
        ),
290
        'expected' => '&,alpha&beta,<created>,\'guid\'',
291
      ),
292
    );
293
  }
294

    
295
}