1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Test case for taxonomy mapper mappers/taxonomy.inc.
|
6
|
*/
|
7
|
|
8
|
/**
|
9
|
* Class for testing Feeds <em>content</em> mapper.
|
10
|
*/
|
11
|
class FeedsMapperTaxonomyTestCase extends FeedsMapperTestCase {
|
12
|
public static function getInfo() {
|
13
|
return array(
|
14
|
'name' => 'Mapper: Taxonomy',
|
15
|
'description' => 'Test Feeds Mapper support for Taxonomy.',
|
16
|
'group' => 'Feeds',
|
17
|
);
|
18
|
}
|
19
|
|
20
|
function setUp() {
|
21
|
parent::setUp();
|
22
|
|
23
|
// Add Tags vocabulary
|
24
|
$edit = array(
|
25
|
'name' => 'Tags',
|
26
|
'machine_name' => 'tags',
|
27
|
);
|
28
|
$this->drupalPost('admin/structure/taxonomy/add', $edit, 'Save');
|
29
|
|
30
|
$edit = array(
|
31
|
'name' => 'term1',
|
32
|
);
|
33
|
$this->drupalPost('admin/structure/taxonomy/tags/add', $edit, t('Save'));
|
34
|
$this->assertText('Created new term term1.');
|
35
|
|
36
|
// Create term reference field.
|
37
|
$field = array(
|
38
|
'field_name' => 'field_tags',
|
39
|
'type' => 'taxonomy_term_reference',
|
40
|
'cardinality' => FIELD_CARDINALITY_UNLIMITED,
|
41
|
'settings' => array(
|
42
|
'allowed_values' => array(
|
43
|
array(
|
44
|
'vocabulary' => 'tags',
|
45
|
'parent' => 0,
|
46
|
),
|
47
|
),
|
48
|
),
|
49
|
);
|
50
|
field_create_field($field);
|
51
|
|
52
|
// Add term reference field to feed item bundle.
|
53
|
$this->instance = array(
|
54
|
'field_name' => 'field_tags',
|
55
|
'bundle' => 'article',
|
56
|
'entity_type' => 'node',
|
57
|
'widget' => array(
|
58
|
'type' => 'options_select',
|
59
|
),
|
60
|
'display' => array(
|
61
|
'default' => array(
|
62
|
'type' => 'taxonomy_term_reference_link',
|
63
|
),
|
64
|
),
|
65
|
);
|
66
|
field_create_instance($this->instance);
|
67
|
|
68
|
// Add term reference field to feed node bundle.
|
69
|
$this->instance = array(
|
70
|
'field_name' => 'field_tags',
|
71
|
'bundle' => 'page',
|
72
|
'entity_type' => 'node',
|
73
|
'widget' => array(
|
74
|
'type' => 'options_select',
|
75
|
),
|
76
|
'display' => array(
|
77
|
'default' => array(
|
78
|
'type' => 'taxonomy_term_reference_link',
|
79
|
),
|
80
|
),
|
81
|
);
|
82
|
field_create_instance($this->instance);
|
83
|
|
84
|
// Create an importer configuration with basic mapping.
|
85
|
$this->createImporterConfiguration('Syndication', 'syndication');
|
86
|
$this->addMappings('syndication',
|
87
|
array(
|
88
|
0 => array(
|
89
|
'source' => 'title',
|
90
|
'target' => 'title',
|
91
|
),
|
92
|
1 => array(
|
93
|
'source' => 'description',
|
94
|
'target' => 'body',
|
95
|
),
|
96
|
2 => array(
|
97
|
'source' => 'timestamp',
|
98
|
'target' => 'created',
|
99
|
),
|
100
|
3 => array(
|
101
|
'source' => 'url',
|
102
|
'target' => 'url',
|
103
|
'unique' => TRUE,
|
104
|
),
|
105
|
4 => array(
|
106
|
'source' => 'guid',
|
107
|
'target' => 'guid',
|
108
|
'unique' => TRUE,
|
109
|
),
|
110
|
)
|
111
|
);
|
112
|
}
|
113
|
|
114
|
/**
|
115
|
* Tests inheriting taxonomy from the feed node.
|
116
|
*/
|
117
|
function testInheritTaxonomy() {
|
118
|
|
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
|
taxonomy_feeds_set_target(NULL, $entity, $target, $terms, $mapping);
|
240
|
$this->assertEqual(count($entity->field_tags[LANGUAGE_NONE]), 10);
|
241
|
|
242
|
// Test a second mapping with a bogus term id.
|
243
|
taxonomy_feeds_set_target(NULL, $entity, $target, array(1234), $mapping);
|
244
|
$this->assertEqual(count($entity->field_tags[LANGUAGE_NONE]), 10);
|
245
|
}
|
246
|
|
247
|
/**
|
248
|
* Tests mapping to a taxonomy term's guid.
|
249
|
*/
|
250
|
public function testSearchByGUID() {
|
251
|
// Create 10 terms. The first one was created in setup.
|
252
|
$tids = array(1);
|
253
|
foreach (range(2, 10) as $i) {
|
254
|
$term = (object) array(
|
255
|
'name' => 'term' . $i,
|
256
|
'vid' => 1,
|
257
|
);
|
258
|
taxonomy_term_save($term);
|
259
|
$tids[] = $term->tid;
|
260
|
}
|
261
|
|
262
|
// Create a bunch of bogus imported terms.
|
263
|
$guids = array();
|
264
|
foreach ($tids as $tid) {
|
265
|
$guid = 100 * $tid;
|
266
|
$guids[] = $guid;
|
267
|
$record = array(
|
268
|
'entity_type' => 'taxonomy_term',
|
269
|
'entity_id' => $tid,
|
270
|
'id' => 'does_not_exist',
|
271
|
'feed_nid' => 0,
|
272
|
'imported' => REQUEST_TIME,
|
273
|
'url' => '',
|
274
|
'guid' => $guid,
|
275
|
);
|
276
|
drupal_write_record('feeds_item', $record);
|
277
|
}
|
278
|
|
279
|
FeedsPlugin::loadMappers();
|
280
|
|
281
|
$entity = new stdClass();
|
282
|
$target = 'field_tags';
|
283
|
$mapping = array(
|
284
|
'term_search' => FEEDS_TAXONOMY_SEARCH_TERM_GUID,
|
285
|
);
|
286
|
|
287
|
taxonomy_feeds_set_target(NULL, $entity, $target, $guids, $mapping);
|
288
|
$this->assertEqual(count($entity->field_tags[LANGUAGE_NONE]), 10);
|
289
|
foreach ($entity->field_tags[LANGUAGE_NONE] as $delta => $values) {
|
290
|
$this->assertEqual($tids[$delta], $values['tid'], 'Correct term id foud.');
|
291
|
}
|
292
|
|
293
|
// Test a second mapping with a bogus term id.
|
294
|
taxonomy_feeds_set_target(NULL, $entity, $target, array(1234), $mapping);
|
295
|
$this->assertEqual(count($entity->field_tags[LANGUAGE_NONE]), 10);
|
296
|
foreach ($entity->field_tags[LANGUAGE_NONE] as $delta => $values) {
|
297
|
$this->assertEqual($tids[$delta], $values['tid'], 'Correct term id foud.');
|
298
|
}
|
299
|
}
|
300
|
|
301
|
/**
|
302
|
* Finds node style taxonomy term markup in DOM.
|
303
|
*/
|
304
|
public function assertTaxonomyTerm($term) {
|
305
|
$term = check_plain($term);
|
306
|
$this->assertPattern('/<a href="\/.*taxonomy\/term\/[0-9]+">' . $term . '<\/a>/', 'Found ' . $term);
|
307
|
}
|
308
|
|
309
|
/**
|
310
|
* Asserts that the term does not exist on a node page.
|
311
|
*/
|
312
|
public function assertNoTaxonomyTerm($term) {
|
313
|
$term = check_plain($term);
|
314
|
$this->assertNoPattern('/<a href="\/.*taxonomy\/term\/[0-9]+">' . $term . '<\/a>/', 'Did not find ' . $term);
|
315
|
}
|
316
|
}
|