Projet

Général

Profil

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

root / drupal7 / sites / all / modules / feeds / tests / feeds_mapper_taxonomy.test @ 41cc1b08

1
<?php
2

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

    
8
/**
9
 * Test case for taxonomy mapper mappers/taxonomy.inc.
10
 */
11
class FeedsMapperTaxonomyTestCase extends FeedsMapperTestCase {
12

    
13
  public static function getInfo() {
14
    return array(
15
      'name' => 'Mapper: Taxonomy',
16
      'description' => 'Test Feeds Mapper support for Taxonomy.',
17
      'group' => 'Feeds',
18
    );
19
  }
20

    
21
  public function setUp() {
22
    parent::setUp();
23

    
24
    // Add Tags vocabulary
25
    $edit = array(
26
      'name' => 'Tags',
27
      'machine_name' => 'tags',
28
    );
29
    $this->drupalPost('admin/structure/taxonomy/add', $edit, 'Save');
30

    
31
    $edit = array(
32
        'name' => 'term1',
33
      );
34
    $this->drupalPost('admin/structure/taxonomy/tags/add', $edit, t('Save'));
35
    $this->assertText('Created new term term1.');
36

    
37
    // Create term reference field.
38
    $field = array(
39
      'field_name' => 'field_tags',
40
      'type' => 'taxonomy_term_reference',
41
      'cardinality' => FIELD_CARDINALITY_UNLIMITED,
42
      'settings' => array(
43
        'allowed_values' => array(
44
          array(
45
            'vocabulary' => 'tags',
46
            'parent' => 0,
47
          ),
48
        ),
49
      ),
50
    );
51
    field_create_field($field);
52

    
53
    // Add term reference field to feed item bundle.
54
    $this->instance = array(
55
      'field_name' => 'field_tags',
56
      'bundle' => 'article',
57
      'entity_type' => 'node',
58
      'widget' => array(
59
        'type' => 'options_select',
60
      ),
61
      'display' => array(
62
        'default' => array(
63
          'type' => 'taxonomy_term_reference_link',
64
        ),
65
      ),
66
    );
67
    field_create_instance($this->instance);
68

    
69
    // Add term reference field to feed node bundle.
70
    $this->instance = array(
71
      'field_name' => 'field_tags',
72
      'bundle' => 'page',
73
      'entity_type' => 'node',
74
      'widget' => array(
75
        'type' => 'options_select',
76
      ),
77
      'display' => array(
78
        'default' => array(
79
          'type' => 'taxonomy_term_reference_link',
80
        ),
81
      ),
82
    );
83
    field_create_instance($this->instance);
84

    
85
    // Create an importer configuration with basic mapping.
86
    $this->createImporterConfiguration('Syndication', 'syndication');
87
    $this->addMappings('syndication',
88
      array(
89
        0 => array(
90
          'source' => 'title',
91
          'target' => 'title',
92
        ),
93
        1 => array(
94
          'source' => 'description',
95
          'target' => 'body',
96
        ),
97
        2 => array(
98
          'source' => 'timestamp',
99
          'target' => 'created',
100
        ),
101
        3 => array(
102
          'source' => 'url',
103
          'target' => 'url',
104
          'unique' => TRUE,
105
        ),
106
        4 => array(
107
          'source' => 'guid',
108
          'target' => 'guid',
109
          'unique' => TRUE,
110
        ),
111
      )
112
    );
113
  }
114

    
115
  /**
116
   * Tests inheriting taxonomy from the feed node.
117
   */
118
  public function testInheritTaxonomy() {
119
    // Adjust importer settings
120
    $this->setSettings('syndication', NULL, array('import_period' => FEEDS_SCHEDULE_NEVER));
121
    $this->setSettings('syndication', NULL, array('import_on_create' => FALSE));
122
    $this->assertText('Do not import on submission');
123

    
124
    // Map feed node's taxonomy to feed item node's taxonomy.
125
    $mappings = array(
126
      5 => array(
127
        'source' => 'parent:taxonomy:tags',
128
        'target' => 'field_tags',
129
      ),
130
    );
131
    $this->addMappings('syndication', $mappings);
132

    
133
    // Create feed node and add term term1.
134
    $langcode = LANGUAGE_NONE;
135
    $nid = $this->createFeedNode('syndication', NULL, 'Syndication');
136
    $term = taxonomy_get_term_by_name('term1');
137
    $term = reset($term);
138
    $edit = array(
139
      'field_tags' . '[' . $langcode . '][]' => $term->tid,
140
    );
141
    $this->drupalPost("node/$nid/edit", $edit, t('Save'));
142
    $this->assertTaxonomyTerm($term->name);
143

    
144
    // Import nodes.
145
    $this->drupalPost("node/$nid/import", array(), 'Import');
146
    $this->assertText('Created 10 nodes.');
147

    
148
    $count = db_query("SELECT COUNT(*) FROM {taxonomy_index}")->fetchField();
149

    
150
    // There should be one term for each node imported plus the term on the feed node.
151
    $this->assertEqual(11, $count, 'Found correct number of tags for all feed nodes and feed items.');
152
  }
153

    
154
  /**
155
   * Tests searching taxonomy terms by name.
156
   */
157
  public function testSearchByName() {
158
    $terms = array(
159
      'Drupal',
160
      'localization',
161
      'localization client',
162
      'localization server',
163
      'open atrium',
164
      'translation',
165
      'translation server',
166
      'Drupal planet',
167
    );
168

    
169
    $this->setSettings('syndication', 'FeedsNodeProcessor', array(
170
      'skip_hash_check' => TRUE,
171
      'update_existing' => 2,
172
    ));
173
    $mappings = array(
174
      5 => array(
175
        'source' => 'tags',
176
        'target' => 'field_tags',
177
        'term_search' => 0,
178
      ),
179
    );
180
    $this->addMappings('syndication', $mappings);
181
    $nid = $this->createFeedNode('syndication', NULL, 'Syndication');
182
    $this->assertText('Created 10 nodes.');
183
    // Check that terms we not auto-created.
184
    $this->drupalGet('node/2');
185
    foreach ($terms as $term) {
186
      $this->assertNoTaxonomyTerm($term);
187
    }
188
    $this->drupalGet('node/3');
189
    $this->assertNoTaxonomyTerm('Washington DC');
190

    
191
    // Change the mapping configuration.
192
    $this->removeMappings('syndication', $mappings);
193
    // Turn on autocreate.
194
    $mappings[5]['autocreate'] = TRUE;
195
    $this->addMappings('syndication', $mappings);
196
    $this->drupalPost('node/' . $nid . '/import', array(), t('Import'));
197
    $this->assertText('Updated 10 nodes.');
198

    
199
    $this->drupalGet('node/2');
200
    foreach ($terms as $term) {
201
      $this->assertTaxonomyTerm($term);
202
    }
203
    $this->drupalGet('node/3');
204
    $this->assertTaxonomyTerm('Washington DC');
205

    
206
    $names = db_query('SELECT name FROM {taxonomy_term_data}')->fetchCol();
207
    $this->assertEqual(count($names), 31, 'Found correct number of terms in the database.');
208

    
209
    // Run import again. This verifys that the terms we found by name.
210
    $this->drupalPost('node/' . $nid . '/import', array(), t('Import'));
211
    $this->assertText('Updated 10 nodes.');
212
    $names = db_query('SELECT name FROM {taxonomy_term_data}')->fetchCol();
213
    $this->assertEqual(count($names), 31, 'Found correct number of terms in the database.');
214
  }
215

    
216
  /**
217
   * Tests mapping to taxonomy terms by tid.
218
   */
219
  public function testSearchByID() {
220
    // Create 10 terms. The first one was created in setup.
221
    $terms = array(1);
222
    foreach (range(2, 10) as $i) {
223
      $term = (object) array(
224
        'name' => 'term' . $i,
225
        'vid' => 1,
226
      );
227
      taxonomy_term_save($term);
228
      $terms[] = $term->tid;
229
    }
230

    
231
    FeedsPlugin::loadMappers();
232

    
233
    $entity = new stdClass();
234
    $target = 'field_tags';
235
    $mapping = array(
236
      'term_search' => FEEDS_TAXONOMY_SEARCH_TERM_ID,
237
    );
238

    
239
    $source = FeedsSource::instance('tmp', 0);
240

    
241
    taxonomy_feeds_set_target($source, $entity, $target, $terms, $mapping);
242
    $this->assertEqual(count($entity->field_tags[LANGUAGE_NONE]), 10);
243

    
244
    // Test a second mapping with a bogus term id.
245
    taxonomy_feeds_set_target($source, $entity, $target, array(1234), $mapping);
246
    $this->assertEqual(count($entity->field_tags[LANGUAGE_NONE]), 10);
247
  }
248

    
249
  /**
250
   * Tests mapping to a taxonomy term's guid.
251
   */
252
  public function testSearchByGUID() {
253
    // Create 10 terms. The first one was created in setup.
254
    $tids = array(1);
255
    foreach (range(2, 10) as $i) {
256
      $term = (object) array(
257
        'name' => 'term' . $i,
258
        'vid' => 1,
259
      );
260
      taxonomy_term_save($term);
261
      $tids[] = $term->tid;
262
    }
263

    
264
    // Create a bunch of bogus imported terms.
265
    $guids = array();
266
    foreach ($tids as $tid) {
267
      $guid = 100 * $tid;
268
      $guids[] = $guid;
269
      $record = array(
270
        'entity_type' => 'taxonomy_term',
271
        'entity_id' => $tid,
272
        'id' => 'does_not_exist',
273
        'feed_nid' => 0,
274
        'imported' => REQUEST_TIME,
275
        'url' => '',
276
        'guid' => $guid,
277
      );
278
      drupal_write_record('feeds_item', $record);
279
    }
280

    
281
    FeedsPlugin::loadMappers();
282

    
283
    $entity = new stdClass();
284
    $target = 'field_tags';
285
    $mapping = array(
286
      'term_search' => FEEDS_TAXONOMY_SEARCH_TERM_GUID,
287
    );
288

    
289
    $source = FeedsSource::instance('tmp', 0);
290

    
291
    taxonomy_feeds_set_target($source, $entity, $target, $guids, $mapping);
292
    $this->assertEqual(count($entity->field_tags[LANGUAGE_NONE]), 10);
293
    foreach ($entity->field_tags[LANGUAGE_NONE] as $delta => $values) {
294
      $this->assertEqual($tids[$delta], $values['tid'], 'Correct term id foud.');
295
    }
296

    
297
    // Test a second mapping with a bogus term id.
298
    taxonomy_feeds_set_target($source, $entity, $target, array(1234), $mapping);
299
    $this->assertEqual(count($entity->field_tags[LANGUAGE_NONE]), 10);
300
    foreach ($entity->field_tags[LANGUAGE_NONE] as $delta => $values) {
301
      $this->assertEqual($tids[$delta], $values['tid'], 'Correct term id foud.');
302
    }
303
  }
304

    
305
  /**
306
   * Tests importing empty values
307
   */
308
  public function testBlankSourceValues() {
309
    // Create a CSV importer configuration.
310
    $this->createImporterConfiguration('Node import from CSV', 'node');
311
    $this->setPlugin('node', 'FeedsFileFetcher');
312
    $this->setPlugin('node', 'FeedsCSVParser');
313
    $this->setSettings('node', 'FeedsNodeProcessor', array('bundle' => 'article'));
314
    $this->setSettings('node', NULL, array('content_type' => ''));
315
    $this->addMappings('node', array(
316
      0 => array(
317
        'source' => 'title',
318
        'target' => 'title',
319
      ),
320
      1 => array(
321
        'source' => 'tags',
322
        'target' => 'field_tags',
323
        'term_search' => 0,
324
        'autocreate' => 1,
325
      ),
326
      2 => array(
327
        'source' => 'guid',
328
        'target' => 'guid',
329
        'unique' => TRUE,
330
      ),
331
    ));
332

    
333
    // Verify that there are 5 nodes total.
334
    $this->importFile('node', $this->absolutePath() . '/tests/feeds/taxonomy_empty_terms.csv');
335
    $this->assertText('Created 5 nodes');
336

    
337
    // Make sure only two terms were added
338
    $names = db_query('SELECT name FROM {taxonomy_term_data}')->fetchCol();
339
    $this->assertEqual(count($names), 2, 'Found correct number of terms in the database.');
340

    
341
    // Make sure the correct terms were created
342
    $terms = array(
343
      'term1',
344
      '0',
345
    );
346
    foreach ($terms as $term_name) {
347
      $this->assertTrue(in_array($term_name, $names), 'Correct term created');
348
    }
349
  }
350

    
351
  /**
352
   * Tests that there are no errors when trying to map to an invalid vocabulary.
353
   */
354
  public function testMissingVocabulary() {
355
    $this->addMappings('syndication', array(
356
      5 => array(
357
        'source' => 'tags',
358
        'target' => 'field_tags',
359
        'term_search' => 0,
360
        'autocreate' => TRUE,
361
      ),
362
    ));
363

    
364
    // Create an invalid configuration.
365
    db_delete('taxonomy_vocabulary')->execute();
366

    
367
    $this->createFeedNode('syndication', NULL, 'Syndication');
368
    $this->assertText('Created 10 nodes.');
369
  }
370

    
371
  /**
372
   * Tests if values are cleared out when an empty value or no value
373
   * is provided.
374
   */
375
  public function testClearOutValues() {
376
    // Create a CSV importer configuration.
377
    $this->createImporterConfiguration('Node import from CSV', 'node');
378
    $this->setSettings('node', NULL, array(
379
      'content_type' => '',
380
    ));
381
    $this->setPlugin('node', 'FeedsFileFetcher');
382
    $this->setPlugin('node', 'FeedsCSVParser');
383
    $this->setSettings('node', 'FeedsNodeProcessor', array(
384
      'bundle' => 'article',
385
      'update_existing' => 1,
386
    ));
387
    $this->addMappings('node', array(
388
      0 => array(
389
        'source' => 'title',
390
        'target' => 'title',
391
      ),
392
      1 => array(
393
        'source' => 'alpha',
394
        'target' => 'field_tags',
395
        'term_search' => 0,
396
        'autocreate' => 1,
397
      ),
398
      2 => array(
399
        'source' => 'guid',
400
        'target' => 'guid',
401
        'unique' => TRUE,
402
      ),
403
    ));
404

    
405
    $this->importFile('node', $this->absolutePath() . '/tests/feeds/content.csv');
406
    $this->assertText('Created 2 nodes');
407

    
408
    // Check the imported nodes.
409
    $terms1 = taxonomy_get_term_by_name('Lorem');
410
    $term1 = reset($terms1);
411
    $terms2 = taxonomy_get_term_by_name('Ut wisi');
412
    $term2 = reset($terms2);
413
    $taxonomy_values = array(
414
      1 => $term1->tid,
415
      2 => $term2->tid,
416
    );
417
    for ($i = 1; $i <= 2; $i++) {
418
      $this->drupalGet("node/$i/edit");
419
      $this->assertFieldByName('field_tags[und][]', $taxonomy_values[$i]);
420
    }
421

    
422
    // Import CSV file with empty values.
423
    $this->importFile('node', $this->absolutePath() . '/tests/feeds/content_empty.csv');
424
    $this->assertText('Updated 2 nodes');
425

    
426
    // Check if the taxonomy reference field was cleared out for node 1.
427
    $this->drupalGet('node/1/edit');
428
    $this->assertFieldByName('field_tags[und][]', '_none');
429
    $this->drupalGet('node/1');
430
    $this->assertNoText('field_tags');
431

    
432
    // Check if zero's didn't cleared out the taxonomy reference field for
433
    // node 2.
434
    $terms0 = taxonomy_get_term_by_name('0');
435
    $term0 = reset($terms0);
436
    $this->drupalGet('node/2/edit');
437
    $this->assertFieldByName('field_tags[und][]', $term0->tid);
438
    $this->drupalGet('node/2');
439
    $this->assertText('field_tags');
440

    
441
    // Re-import the first file again and check if the values returned.
442
    $this->importFile('node', $this->absolutePath() . '/tests/feeds/content.csv');
443
    $this->assertText('Updated 2 nodes');
444
    for ($i = 1; $i <= 2; $i++) {
445
      $this->drupalGet("node/$i/edit");
446
      $this->assertFieldByName('field_tags[und][]', $taxonomy_values[$i]);
447
    }
448

    
449
    // Import CSV file with non-existent values.
450
    $this->importFile('node', $this->absolutePath() . '/tests/feeds/content_non_existent.csv');
451
    $this->assertText('Updated 2 nodes');
452

    
453
    // Check if the taxonomy reference field was cleared out for node 1.
454
    $this->drupalGet('node/1/edit');
455
    $this->assertFieldByName('field_tags[und][]', '_none');
456
    $this->drupalGet('node/1');
457
    $this->assertNoText('field_tags');
458
  }
459

    
460
  /**
461
   * Finds node style taxonomy term markup in DOM.
462
   */
463
  public function assertTaxonomyTerm($term) {
464
    $term = check_plain($term);
465
    $this->assertPattern('/<a href="\/.*taxonomy\/term\/[0-9]+">' . $term . '<\/a>/', 'Found ' . $term);
466
  }
467

    
468
  /**
469
   * Asserts that the term does not exist on a node page.
470
   */
471
  public function assertNoTaxonomyTerm($term) {
472
    $term = check_plain($term);
473
    $this->assertNoPattern('/<a href="\/.*taxonomy\/term\/[0-9]+">' . $term . '<\/a>/', 'Did not find ' . $term);
474
  }
475

    
476
}