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
|
public static function getInfo() {
|
13
|
return array(
|
14
|
'name' => 'Processor: Taxonomy',
|
15
|
'description' => 'Tests a standalone import configuration that uses file fetcher and CSV parser to import taxonomy terms from a CSV file.',
|
16
|
'group' => 'Feeds',
|
17
|
);
|
18
|
}
|
19
|
|
20
|
/**
|
21
|
* Set up test.
|
22
|
*/
|
23
|
public function setUp() {
|
24
|
parent::setUp();
|
25
|
|
26
|
// Create an importer.
|
27
|
$this->createImporterConfiguration('Term import', 'term_import');
|
28
|
|
29
|
// Set and configure plugins and mappings.
|
30
|
$this->setPlugin('term_import', 'FeedsFileFetcher');
|
31
|
$this->setPlugin('term_import', 'FeedsCSVParser');
|
32
|
$this->setPlugin('term_import', 'FeedsTermProcessor');
|
33
|
|
34
|
// Create vocabulary.
|
35
|
$edit = array(
|
36
|
'name' => 'Addams vocabulary',
|
37
|
'machine_name' => 'addams',
|
38
|
);
|
39
|
$this->drupalPost('admin/structure/taxonomy/add', $edit, t('Save'));
|
40
|
|
41
|
$this->setSettings('term_import', 'FeedsTermProcessor', array('bundle' => 'addams'));
|
42
|
|
43
|
// Use standalone form.
|
44
|
$this->setSettings('term_import', NULL, array('content_type' => ''));
|
45
|
}
|
46
|
|
47
|
/**
|
48
|
* Test term creation, refreshing/deleting feeds and feed items.
|
49
|
*/
|
50
|
public function test() {
|
51
|
|
52
|
$mappings = array(
|
53
|
0 => array(
|
54
|
'source' => 'name',
|
55
|
'target' => 'name',
|
56
|
'unique' => 1,
|
57
|
),
|
58
|
);
|
59
|
$this->addMappings('term_import', $mappings);
|
60
|
|
61
|
// Import and assert.
|
62
|
$this->importFile('term_import', $this->absolutePath() . '/tests/feeds/users.csv');
|
63
|
$this->assertText('Created 5 terms');
|
64
|
$this->drupalGet('admin/structure/taxonomy/addams');
|
65
|
$this->assertText('Morticia');
|
66
|
$this->assertText('Fester');
|
67
|
$this->assertText('Gomez');
|
68
|
$this->assertText('Pugsley');
|
69
|
|
70
|
// Import again.
|
71
|
$this->importFile('term_import', $this->absolutePath() . '/tests/feeds/users.csv');
|
72
|
$this->assertText('There are no new terms.');
|
73
|
|
74
|
// Force update.
|
75
|
$this->setSettings('term_import', 'FeedsTermProcessor', array(
|
76
|
'skip_hash_check' => TRUE,
|
77
|
'update_existing' => 2,
|
78
|
));
|
79
|
$this->importFile('term_import', $this->absolutePath() . '/tests/feeds/users.csv');
|
80
|
$this->assertText('Updated 5 terms.');
|
81
|
|
82
|
// Add a term manually, delete all terms, this term should still stand.
|
83
|
$edit = array(
|
84
|
'name' => 'Cousin Itt',
|
85
|
);
|
86
|
$this->drupalPost('admin/structure/taxonomy/addams/add', $edit, t('Save'));
|
87
|
$this->drupalPost('import/term_import/delete-items', array(), t('Delete'));
|
88
|
$this->drupalGet('admin/structure/taxonomy/addams');
|
89
|
$this->assertText('Cousin Itt');
|
90
|
$this->assertNoText('Morticia');
|
91
|
$this->assertNoText('Fester');
|
92
|
$this->assertNoText('Gomez');
|
93
|
$this->assertNoText('Pugsley');
|
94
|
}
|
95
|
|
96
|
/**
|
97
|
* Test that saving an invalid vocabulary throws an exception.
|
98
|
*/
|
99
|
public function testInvalidVocabulary() {
|
100
|
|
101
|
$mappings = array(
|
102
|
0 => array(
|
103
|
'source' => 'name',
|
104
|
'target' => 'name',
|
105
|
'unique' => 1,
|
106
|
),
|
107
|
);
|
108
|
$this->addMappings('term_import', $mappings);
|
109
|
|
110
|
// Force configuration to be invalid.
|
111
|
$config = unserialize(db_query("SELECT config FROM {feeds_importer} WHERE id = :id", array(':id' => 'term_import'))->fetchField());
|
112
|
$config['processor']['config']['bundle'] = 'does_not_exist';
|
113
|
db_update('feeds_importer')
|
114
|
->fields(array('config' => serialize($config)))
|
115
|
->condition('id', 'term_import')
|
116
|
->execute();
|
117
|
|
118
|
// Import and assert.
|
119
|
$this->importFile('term_import', $this->absolutePath() . '/tests/feeds/users.csv');
|
120
|
$this->assertText(t('No vocabulary defined for Taxonomy Term processor.'));
|
121
|
}
|
122
|
|
123
|
/**
|
124
|
* Tests that terms mapped to their parent by GUID are from the same vocabulary.
|
125
|
*/
|
126
|
public function testParentTargetByGUID() {
|
127
|
// Create an other vocabulary.
|
128
|
$vocabulary1 = 'addams';
|
129
|
$vocabulary2 = strtolower($this->randomName());
|
130
|
$edit = array(
|
131
|
'name' => $this->randomString(),
|
132
|
'machine_name' => $vocabulary2,
|
133
|
);
|
134
|
$this->drupalPost('admin/structure/taxonomy/add', $edit, t('Save'));
|
135
|
|
136
|
// Add mappings for the first importer.
|
137
|
$this->addMappings('term_import',
|
138
|
array(
|
139
|
0 => array(
|
140
|
'source' => 'guid',
|
141
|
'target' => 'guid',
|
142
|
'unique' => TRUE,
|
143
|
),
|
144
|
1 => array(
|
145
|
'source' => 'name',
|
146
|
'target' => 'name',
|
147
|
),
|
148
|
2 => array(
|
149
|
'source' => 'parentguid',
|
150
|
'target' => 'parentguid',
|
151
|
),
|
152
|
)
|
153
|
);
|
154
|
|
155
|
// Create a second importer.
|
156
|
$this->createImporterConfiguration('Term import 2', 'term_import2');
|
157
|
$this->setSettings('term_import2', NULL, array('content_type' => ''));
|
158
|
|
159
|
// Set and configure plugins and mappings.
|
160
|
$this->setPlugin('term_import2', 'FeedsFileFetcher');
|
161
|
$this->setPlugin('term_import2', 'FeedsCSVParser');
|
162
|
$this->setPlugin('term_import2', 'FeedsTermProcessor');
|
163
|
$this->setSettings('term_import2', 'FeedsTermProcessor', array('bundle' => $vocabulary2));
|
164
|
|
165
|
// Add mappings for the second importer.
|
166
|
$this->addMappings('term_import2',
|
167
|
array(
|
168
|
0 => array(
|
169
|
'source' => 'guid',
|
170
|
'target' => 'guid',
|
171
|
'unique' => TRUE,
|
172
|
),
|
173
|
1 => array(
|
174
|
'source' => 'name',
|
175
|
'target' => 'name',
|
176
|
),
|
177
|
2 => array(
|
178
|
'source' => 'parentguid',
|
179
|
'target' => 'parentguid',
|
180
|
),
|
181
|
)
|
182
|
);
|
183
|
|
184
|
$values = array(
|
185
|
1 => 'Europe',
|
186
|
2 => 'Belgium',
|
187
|
);
|
188
|
|
189
|
// Import file using the first importer.
|
190
|
$this->importFile('term_import', $this->absolutePath() . '/tests/feeds/terms.csv');
|
191
|
$this->assertText('Created 2 terms.');
|
192
|
|
193
|
// Assert that two terms were created in the first vocabulary.
|
194
|
$terms = entity_load('taxonomy_term', array_keys($values));
|
195
|
foreach ($terms as $tid => $term) {
|
196
|
$this->assertEqual($values[$tid], $term->name);
|
197
|
$this->assertEqual($vocabulary1, $term->vocabulary_machine_name);
|
198
|
}
|
199
|
// Assert that the second term's parent is the first term.
|
200
|
$parents = taxonomy_get_parents($terms[2]->tid);
|
201
|
$message = format_string('The term @term is correctly linked to its parent.', array('@term' => $terms[2]->name));
|
202
|
if (!empty($parents)) {
|
203
|
$parent = current($parents);
|
204
|
$this->assertEqual(1, $parent->tid, $message);
|
205
|
}
|
206
|
else {
|
207
|
$this->fail($message);
|
208
|
}
|
209
|
|
210
|
$values = array(
|
211
|
3 => 'Europe',
|
212
|
4 => 'Belgium',
|
213
|
);
|
214
|
|
215
|
// Now import the file using the second importer.
|
216
|
$this->importFile('term_import2', $this->absolutePath() . '/tests/feeds/terms.csv');
|
217
|
$this->assertText('Created 2 terms.');
|
218
|
|
219
|
// Assert that two terms were created in the second vocabulary.
|
220
|
$terms = entity_load('taxonomy_term', array_keys($values));
|
221
|
foreach ($terms as $tid => $term) {
|
222
|
$this->assertEqual($values[$tid], $term->name);
|
223
|
$this->assertEqual($vocabulary2, $term->vocabulary_machine_name);
|
224
|
}
|
225
|
// Assert that the second term's parent is the first term.
|
226
|
$parents = taxonomy_get_parents($terms[4]->tid);
|
227
|
$message = format_string('The term @term is correctly linked to its parent.', array('@term' => $terms[4]->name));
|
228
|
if (!empty($parents)) {
|
229
|
$parent = current($parents);
|
230
|
$this->assertEqual(3, $parent->tid, $message);
|
231
|
}
|
232
|
else {
|
233
|
$this->fail($message);
|
234
|
}
|
235
|
}
|
236
|
|
237
|
/**
|
238
|
* Tests that terms mapped to their parent by GUID are from the same vocabulary.
|
239
|
*/
|
240
|
public function testParentTargetByName() {
|
241
|
// Create an other vocabulary.
|
242
|
$vocabulary1 = 'addams';
|
243
|
$vocabulary2 = strtolower($this->randomName());
|
244
|
$edit = array(
|
245
|
'name' => $this->randomString(),
|
246
|
'machine_name' => $vocabulary2,
|
247
|
);
|
248
|
$this->drupalPost('admin/structure/taxonomy/add', $edit, t('Save'));
|
249
|
|
250
|
// Add mappings for the first importer.
|
251
|
$this->addMappings('term_import',
|
252
|
array(
|
253
|
0 => array(
|
254
|
'source' => 'guid',
|
255
|
'target' => 'guid',
|
256
|
'unique' => TRUE,
|
257
|
),
|
258
|
1 => array(
|
259
|
'source' => 'name',
|
260
|
'target' => 'name',
|
261
|
),
|
262
|
2 => array(
|
263
|
'source' => 'parent',
|
264
|
'target' => 'parent',
|
265
|
),
|
266
|
)
|
267
|
);
|
268
|
|
269
|
// Create a second importer.
|
270
|
$this->createImporterConfiguration('Term import 2', 'term_import2');
|
271
|
$this->setSettings('term_import2', NULL, array('content_type' => ''));
|
272
|
|
273
|
// Set and configure plugins and mappings.
|
274
|
$this->setPlugin('term_import2', 'FeedsFileFetcher');
|
275
|
$this->setPlugin('term_import2', 'FeedsCSVParser');
|
276
|
$this->setPlugin('term_import2', 'FeedsTermProcessor');
|
277
|
$this->setSettings('term_import2', 'FeedsTermProcessor', array('bundle' => $vocabulary2));
|
278
|
|
279
|
// Add mappings for the second importer.
|
280
|
$this->addMappings('term_import2',
|
281
|
array(
|
282
|
0 => array(
|
283
|
'source' => 'guid',
|
284
|
'target' => 'guid',
|
285
|
'unique' => TRUE,
|
286
|
),
|
287
|
1 => array(
|
288
|
'source' => 'name',
|
289
|
'target' => 'name',
|
290
|
),
|
291
|
2 => array(
|
292
|
'source' => 'parent',
|
293
|
'target' => 'parent',
|
294
|
),
|
295
|
)
|
296
|
);
|
297
|
|
298
|
$values = array(
|
299
|
1 => 'Europe',
|
300
|
2 => 'Belgium',
|
301
|
);
|
302
|
|
303
|
// Import file using the first importer.
|
304
|
$this->importFile('term_import', $this->absolutePath() . '/tests/feeds/terms.csv');
|
305
|
$this->assertText('Created 2 terms.');
|
306
|
|
307
|
// Assert that two terms were created in the first vocabulary.
|
308
|
$terms = entity_load('taxonomy_term', array_keys($values));
|
309
|
foreach ($terms as $tid => $term) {
|
310
|
$this->assertEqual($values[$tid], $term->name);
|
311
|
$this->assertEqual($vocabulary1, $term->vocabulary_machine_name);
|
312
|
}
|
313
|
// Assert that the second term's parent is the first term.
|
314
|
$parents = taxonomy_get_parents($terms[2]->tid);
|
315
|
$message = format_string('The term @term is correctly linked to its parent.', array('@term' => $terms[2]->name));
|
316
|
if (!empty($parents)) {
|
317
|
$parent = current($parents);
|
318
|
$this->assertEqual(1, $parent->tid, $message);
|
319
|
}
|
320
|
else {
|
321
|
$this->fail($message);
|
322
|
}
|
323
|
|
324
|
$values = array(
|
325
|
3 => 'Europe',
|
326
|
4 => 'Belgium',
|
327
|
);
|
328
|
|
329
|
// Now import the file using the second importer.
|
330
|
$this->importFile('term_import2', $this->absolutePath() . '/tests/feeds/terms.csv');
|
331
|
$this->assertText('Created 2 terms.');
|
332
|
|
333
|
// Assert that two terms were created in the second vocabulary.
|
334
|
$terms = entity_load('taxonomy_term', array_keys($values));
|
335
|
foreach ($terms as $tid => $term) {
|
336
|
$this->assertEqual($values[$tid], $term->name);
|
337
|
$this->assertEqual($vocabulary2, $term->vocabulary_machine_name);
|
338
|
}
|
339
|
// Assert that the second term's parent is the first term.
|
340
|
$parents = taxonomy_get_parents($terms[4]->tid);
|
341
|
$message = format_string('The term @term is correctly linked to its parent.', array('@term' => $terms[4]->name));
|
342
|
if (!empty($parents)) {
|
343
|
$parent = current($parents);
|
344
|
$this->assertEqual(3, $parent->tid, $message);
|
345
|
}
|
346
|
else {
|
347
|
$this->fail($message);
|
348
|
}
|
349
|
}
|
350
|
|
351
|
/**
|
352
|
* Test replacing terms on subsequent imports.
|
353
|
*/
|
354
|
public function testReplaceTerms() {
|
355
|
$mappings = array(
|
356
|
0 => array(
|
357
|
'source' => 'name',
|
358
|
'target' => 'name',
|
359
|
'unique' => 1,
|
360
|
),
|
361
|
);
|
362
|
$this->addMappings('term_import', $mappings);
|
363
|
|
364
|
// Configure the processor to "Replace existing terms".
|
365
|
$this->setSettings('term_import', 'FeedsTermProcessor', array(
|
366
|
'skip_hash_check' => TRUE,
|
367
|
'update_existing' => 1,
|
368
|
));
|
369
|
|
370
|
// Import first time.
|
371
|
$this->importFile('term_import', $this->absolutePath() . '/tests/feeds/users.csv');
|
372
|
$this->assertText('Created 5 terms');
|
373
|
|
374
|
// Import again to replace terms.
|
375
|
$this->importFile('term_import', $this->absolutePath() . '/tests/feeds/users.csv');
|
376
|
$this->assertText('Updated 5 terms.');
|
377
|
}
|
378
|
|
379
|
}
|