Projet

Général

Profil

Paste
Télécharger (11,7 ko) Statistiques
| Branche: | Révision:

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

1
<?php
2

    
3
/**
4
 * @file
5
 * Tests for plugins/FeedsTermProcessor.inc.
6
 */
7

    
8
/**
9
 * Test aggregating a feed as data records.
10
 */
11
class FeedsCSVtoTermsTest extends FeedsWebTestCase {
12

    
13
  /**
14
   * {@inheritdoc}
15
   */
16
  public static function getInfo() {
17
    return array(
18
      'name' => 'Processor: Taxonomy',
19
      'description' => 'Tests a standalone import configuration that uses file fetcher and CSV parser to import taxonomy terms from a CSV file.',
20
      'group' => 'Feeds',
21
    );
22
  }
23

    
24
  /**
25
   * Set up test.
26
   */
27
  public function setUp() {
28
    parent::setUp();
29

    
30
    // Create an importer.
31
    $this->createImporterConfiguration('Term import', 'term_import');
32

    
33
    // Set and configure plugins and mappings.
34
    $this->setPlugin('term_import', 'FeedsFileFetcher');
35
    $this->setPlugin('term_import', 'FeedsCSVParser');
36
    $this->setPlugin('term_import', 'FeedsTermProcessor');
37

    
38
    // Create vocabulary.
39
    $edit = array(
40
      'name' => 'Addams vocabulary',
41
      'machine_name' => 'addams',
42
    );
43
    $this->drupalPost('admin/structure/taxonomy/add', $edit, t('Save'));
44

    
45
    $this->setSettings('term_import', 'FeedsTermProcessor', array('bundle' => 'addams'));
46

    
47
    // Use standalone form.
48
    $this->setSettings('term_import', NULL, array('content_type' => ''));
49
  }
50

    
51
  /**
52
   * Test term creation, refreshing/deleting feeds and feed items.
53
   */
54
  public function test() {
55

    
56
    $mappings = array(
57
      0 => array(
58
        'source' => 'name',
59
        'target' => 'name',
60
        'unique' => 1,
61
      ),
62
    );
63
    $this->addMappings('term_import', $mappings);
64

    
65
    // Import and assert.
66
    $this->importFile('term_import', $this->absolutePath() . '/tests/feeds/users.csv');
67
    $this->assertText('Created 5 terms');
68
    $this->drupalGet('admin/structure/taxonomy/addams');
69
    $this->assertText('Morticia');
70
    $this->assertText('Fester');
71
    $this->assertText('Gomez');
72
    $this->assertText('Pugsley');
73

    
74
    // Import again.
75
    $this->importFile('term_import', $this->absolutePath() . '/tests/feeds/users.csv');
76
    $this->assertText('There are no new terms.');
77

    
78
    // Force update.
79
    $this->setSettings('term_import', 'FeedsTermProcessor', array(
80
      'skip_hash_check' => TRUE,
81
      'update_existing' => 2,
82
    ));
83
    $this->importFile('term_import', $this->absolutePath() . '/tests/feeds/users.csv');
84
    $this->assertText('Updated 5 terms.');
85

    
86
    // Add a term manually, delete all terms, this term should still stand.
87
    $edit = array(
88
      'name' => 'Cousin Itt',
89
    );
90
    $this->drupalPost('admin/structure/taxonomy/addams/add', $edit, t('Save'));
91
    $this->drupalPost('import/term_import/delete-items', array(), t('Delete'));
92
    $this->drupalGet('admin/structure/taxonomy/addams');
93
    $this->assertText('Cousin Itt');
94
    $this->assertNoText('Morticia');
95
    $this->assertNoText('Fester');
96
    $this->assertNoText('Gomez');
97
    $this->assertNoText('Pugsley');
98
  }
99

    
100
  /**
101
   * Test that saving an invalid vocabulary throws an exception.
102
   */
103
  public function testInvalidVocabulary() {
104

    
105
    $mappings = array(
106
      0 => array(
107
        'source' => 'name',
108
        'target' => 'name',
109
        'unique' => 1,
110
      ),
111
    );
112
    $this->addMappings('term_import', $mappings);
113

    
114
    // Force configuration to be invalid.
115
    $config = unserialize(db_query("SELECT config FROM {feeds_importer} WHERE id = :id", array(':id' => 'term_import'))->fetchField());
116
    $config['processor']['config']['bundle'] = 'does_not_exist';
117
    db_update('feeds_importer')
118
      ->fields(array('config' => serialize($config)))
119
      ->condition('id', 'term_import')
120
      ->execute();
121

    
122
    // Import and assert.
123
    $this->importFile('term_import', $this->absolutePath() . '/tests/feeds/users.csv');
124
    $this->assertText(t('No vocabulary defined for Taxonomy Term processor.'));
125
  }
126

    
127
  /**
128
   * Tests that terms mapped to their parent by GUID are from the same vocabulary.
129
   */
130
  public function testParentTargetByGUID() {
131
    // Create an other vocabulary.
132
    $vocabulary1 = 'addams';
133
    $vocabulary2 = strtolower($this->randomName());
134
    $edit = array(
135
      'name' => $this->randomString(),
136
      'machine_name' => $vocabulary2,
137
    );
138
    $this->drupalPost('admin/structure/taxonomy/add', $edit, t('Save'));
139

    
140
    // Add mappings for the first importer.
141
    $this->addMappings('term_import',
142
      array(
143
        0 => array(
144
          'source' => 'guid',
145
          'target' => 'guid',
146
          'unique' => TRUE,
147
        ),
148
        1 => array(
149
          'source' => 'name',
150
          'target' => 'name',
151
        ),
152
        2 => array(
153
          'source' => 'parentguid',
154
          'target' => 'parentguid',
155
        ),
156
      )
157
    );
158

    
159
    // Create a second importer.
160
    $this->createImporterConfiguration('Term import 2', 'term_import2');
161
    $this->setSettings('term_import2', NULL, array('content_type' => ''));
162

    
163
    // Set and configure plugins and mappings.
164
    $this->setPlugin('term_import2', 'FeedsFileFetcher');
165
    $this->setPlugin('term_import2', 'FeedsCSVParser');
166
    $this->setPlugin('term_import2', 'FeedsTermProcessor');
167
    $this->setSettings('term_import2', 'FeedsTermProcessor', array('bundle' => $vocabulary2));
168

    
169
    // Add mappings for the second importer.
170
    $this->addMappings('term_import2',
171
      array(
172
        0 => array(
173
          'source' => 'guid',
174
          'target' => 'guid',
175
          'unique' => TRUE,
176
        ),
177
        1 => array(
178
          'source' => 'name',
179
          'target' => 'name',
180
        ),
181
        2 => array(
182
          'source' => 'parentguid',
183
          'target' => 'parentguid',
184
        ),
185
      )
186
    );
187

    
188
    $values = array(
189
      1 => 'Europe',
190
      2 => 'Belgium',
191
    );
192

    
193
    // Import file using the first importer.
194
    $this->importFile('term_import', $this->absolutePath() . '/tests/feeds/terms.csv');
195
    $this->assertText('Created 2 terms.');
196

    
197
    // Assert that two terms were created in the first vocabulary.
198
    $terms = entity_load('taxonomy_term', array_keys($values));
199
    foreach ($terms as $tid => $term) {
200
      $this->assertEqual($values[$tid], $term->name);
201
      $this->assertEqual($vocabulary1, $term->vocabulary_machine_name);
202
    }
203
    // Assert that the second term's parent is the first term.
204
    $parents = taxonomy_get_parents($terms[2]->tid);
205
    $message = format_string('The term @term is correctly linked to its parent.', array('@term' => $terms[2]->name));
206
    if (!empty($parents)) {
207
      $parent = current($parents);
208
      $this->assertEqual(1, $parent->tid, $message);
209
    }
210
    else {
211
      $this->fail($message);
212
    }
213

    
214
    $values = array(
215
      3 => 'Europe',
216
      4 => 'Belgium',
217
    );
218

    
219
    // Now import the file using the second importer.
220
    $this->importFile('term_import2', $this->absolutePath() . '/tests/feeds/terms.csv');
221
    $this->assertText('Created 2 terms.');
222

    
223
    // Assert that two terms were created in the second vocabulary.
224
    $terms = entity_load('taxonomy_term', array_keys($values));
225
    foreach ($terms as $tid => $term) {
226
      $this->assertEqual($values[$tid], $term->name);
227
      $this->assertEqual($vocabulary2, $term->vocabulary_machine_name);
228
    }
229
    // Assert that the second term's parent is the first term.
230
    $parents = taxonomy_get_parents($terms[4]->tid);
231
    $message = format_string('The term @term is correctly linked to its parent.', array('@term' => $terms[4]->name));
232
    if (!empty($parents)) {
233
      $parent = current($parents);
234
      $this->assertEqual(3, $parent->tid, $message);
235
    }
236
    else {
237
      $this->fail($message);
238
    }
239
  }
240

    
241
  /**
242
   * Tests that terms mapped to their parent by GUID are from the same vocabulary.
243
   */
244
  public function testParentTargetByName() {
245
    // Create an other vocabulary.
246
    $vocabulary1 = 'addams';
247
    $vocabulary2 = strtolower($this->randomName());
248
    $edit = array(
249
      'name' => $this->randomString(),
250
      'machine_name' => $vocabulary2,
251
    );
252
    $this->drupalPost('admin/structure/taxonomy/add', $edit, t('Save'));
253

    
254
    // Add mappings for the first importer.
255
    $this->addMappings('term_import',
256
      array(
257
        0 => array(
258
          'source' => 'guid',
259
          'target' => 'guid',
260
          'unique' => TRUE,
261
        ),
262
        1 => array(
263
          'source' => 'name',
264
          'target' => 'name',
265
        ),
266
        2 => array(
267
          'source' => 'parent',
268
          'target' => 'parent',
269
        ),
270
      )
271
    );
272

    
273
    // Create a second importer.
274
    $this->createImporterConfiguration('Term import 2', 'term_import2');
275
    $this->setSettings('term_import2', NULL, array('content_type' => ''));
276

    
277
    // Set and configure plugins and mappings.
278
    $this->setPlugin('term_import2', 'FeedsFileFetcher');
279
    $this->setPlugin('term_import2', 'FeedsCSVParser');
280
    $this->setPlugin('term_import2', 'FeedsTermProcessor');
281
    $this->setSettings('term_import2', 'FeedsTermProcessor', array('bundle' => $vocabulary2));
282

    
283
    // Add mappings for the second importer.
284
    $this->addMappings('term_import2',
285
      array(
286
        0 => array(
287
          'source' => 'guid',
288
          'target' => 'guid',
289
          'unique' => TRUE,
290
        ),
291
        1 => array(
292
          'source' => 'name',
293
          'target' => 'name',
294
        ),
295
        2 => array(
296
          'source' => 'parent',
297
          'target' => 'parent',
298
        ),
299
      )
300
    );
301

    
302
    $values = array(
303
      1 => 'Europe',
304
      2 => 'Belgium',
305
    );
306

    
307
    // Import file using the first importer.
308
    $this->importFile('term_import', $this->absolutePath() . '/tests/feeds/terms.csv');
309
    $this->assertText('Created 2 terms.');
310

    
311
    // Assert that two terms were created in the first vocabulary.
312
    $terms = entity_load('taxonomy_term', array_keys($values));
313
    foreach ($terms as $tid => $term) {
314
      $this->assertEqual($values[$tid], $term->name);
315
      $this->assertEqual($vocabulary1, $term->vocabulary_machine_name);
316
    }
317
    // Assert that the second term's parent is the first term.
318
    $parents = taxonomy_get_parents($terms[2]->tid);
319
    $message = format_string('The term @term is correctly linked to its parent.', array('@term' => $terms[2]->name));
320
    if (!empty($parents)) {
321
      $parent = current($parents);
322
      $this->assertEqual(1, $parent->tid, $message);
323
    }
324
    else {
325
      $this->fail($message);
326
    }
327

    
328
    $values = array(
329
      3 => 'Europe',
330
      4 => 'Belgium',
331
    );
332

    
333
    // Now import the file using the second importer.
334
    $this->importFile('term_import2', $this->absolutePath() . '/tests/feeds/terms.csv');
335
    $this->assertText('Created 2 terms.');
336

    
337
    // Assert that two terms were created in the second vocabulary.
338
    $terms = entity_load('taxonomy_term', array_keys($values));
339
    foreach ($terms as $tid => $term) {
340
      $this->assertEqual($values[$tid], $term->name);
341
      $this->assertEqual($vocabulary2, $term->vocabulary_machine_name);
342
    }
343
    // Assert that the second term's parent is the first term.
344
    $parents = taxonomy_get_parents($terms[4]->tid);
345
    $message = format_string('The term @term is correctly linked to its parent.', array('@term' => $terms[4]->name));
346
    if (!empty($parents)) {
347
      $parent = current($parents);
348
      $this->assertEqual(3, $parent->tid, $message);
349
    }
350
    else {
351
      $this->fail($message);
352
    }
353
  }
354

    
355
  /**
356
   * Test replacing terms on subsequent imports.
357
   */
358
  public function testReplaceTerms() {
359
    $mappings = array(
360
      0 => array(
361
        'source' => 'name',
362
        'target' => 'name',
363
        'unique' => 1,
364
      ),
365
    );
366
    $this->addMappings('term_import', $mappings);
367

    
368
    // Configure the processor to "Replace existing terms".
369
    $this->setSettings('term_import', 'FeedsTermProcessor', array(
370
      'skip_hash_check' => TRUE,
371
      'update_existing' => 1,
372
    ));
373

    
374
    // Import first time.
375
    $this->importFile('term_import', $this->absolutePath() . '/tests/feeds/users.csv');
376
    $this->assertText('Created 5 terms');
377

    
378
    // Import again to replace terms.
379
    $this->importFile('term_import', $this->absolutePath() . '/tests/feeds/users.csv');
380
    $this->assertText('Updated 5 terms.');
381
  }
382

    
383
}