1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Tests for feeds_tamper.module.
|
6
|
*/
|
7
|
|
8
|
/**
|
9
|
* Base class for Feeds Tamper tests.
|
10
|
*/
|
11
|
class FeedsTamperWebTestHelper extends FeedsWebTestCase {
|
12
|
|
13
|
public function setUp() {
|
14
|
$args = func_get_args();
|
15
|
|
16
|
// Build the list of required modules which can be altered by passing in an
|
17
|
// array of module names to setUp().
|
18
|
if (isset($args[0])) {
|
19
|
if (is_array($args[0])) {
|
20
|
$modules = $args[0];
|
21
|
}
|
22
|
else {
|
23
|
$modules = $args;
|
24
|
}
|
25
|
}
|
26
|
else {
|
27
|
$modules = array();
|
28
|
}
|
29
|
if (isset($args[1]) && is_array($args[1])) {
|
30
|
$permissions = $args[1];
|
31
|
}
|
32
|
else {
|
33
|
$permissions = array();
|
34
|
}
|
35
|
$modules[] = 'feeds_tamper';
|
36
|
parent::setUp($modules, $permissions);
|
37
|
// Detect if Feeds menu structure has changed. This will take a while to be
|
38
|
// released, but since I run dev it needs to work.
|
39
|
$feeds_menu = feeds_ui_menu();
|
40
|
if (isset($feeds_menu['admin/structure/feeds/list'])) {
|
41
|
$this->feeds_base = 'admin/structure/feeds/edit';
|
42
|
}
|
43
|
else {
|
44
|
$this->feeds_base = 'admin/structure/feeds';
|
45
|
}
|
46
|
}
|
47
|
|
48
|
protected function addTamperPlugin($importer_id, $source, $plugin_id, $settings = array(), $description = '', $id = NULL) {
|
49
|
$plugin_info = feeds_tamper_get_plugin($plugin_id);
|
50
|
if ($plugin_info['validate']) {
|
51
|
$plugin_info['validate']($settings);
|
52
|
}
|
53
|
$plugin = feeds_tamper_new_instance();
|
54
|
if (!empty($settings)) {
|
55
|
$plugin->settings = $settings;
|
56
|
}
|
57
|
$plugin->importer = $importer_id;
|
58
|
$plugin->source = $source;
|
59
|
$plugin->plugin_id = $plugin_id;
|
60
|
if (!strlen($description)) {
|
61
|
$description = $this->randomString(255);
|
62
|
}
|
63
|
if (!isset($id)) {
|
64
|
$id = $this->randomName();
|
65
|
}
|
66
|
$plugin->id = $id;
|
67
|
$plugin->description = $description;
|
68
|
$this->assertEqual(SAVED_NEW, feeds_tamper_save_instance($plugin));
|
69
|
$result = db_query("SELECT * FROM {feeds_tamper} WHERE id = :id", array(':id' => $plugin->id));
|
70
|
// test that default weight is added.
|
71
|
$instance = $result->fetchObject();
|
72
|
$this->assertTrue(($instance->weight >= 0));
|
73
|
$this->assertEqual($instance->description, $description);
|
74
|
// Check for only 1 item.
|
75
|
$this->assertIdentical($result->rowCount(), 1);
|
76
|
return $id;
|
77
|
}
|
78
|
|
79
|
protected function disableTamperPlugin($id) {
|
80
|
$plugin = feeds_tamper_load_instance($id);
|
81
|
$plugin->disabled = TRUE;
|
82
|
$this->assertEqual(SAVED_UPDATED, feeds_tamper_save_instance($plugin));
|
83
|
// reload and test that changes were kept.
|
84
|
$plugin = feeds_tamper_load_instance($id);
|
85
|
$this->assertIdentical(TRUE, $plugin->disabled);
|
86
|
}
|
87
|
|
88
|
protected function setTamperPluginWeight($importer_id, $source, $id, $weight) {
|
89
|
}
|
90
|
|
91
|
protected function deleteTamperPlugin($id) {
|
92
|
feeds_tamper_delete_instance($id);
|
93
|
//$source = db_query("SELECT * FROM {feeds_source} WHERE feed_nid = :nid", array(':nid' => $nid))->fetchObject();
|
94
|
}
|
95
|
|
96
|
protected function assertUppercaseDevseedFeedContent() {
|
97
|
$this->drupalGet('node');
|
98
|
$this->assertText('SCALING THE OPEN ATRIUM UI');
|
99
|
$this->assertText("PERU'S SOFTWARE FREEDOM DAY: IMPRESSIONS & PHOTOS");
|
100
|
$this->assertText('WEEK IN DC TECH: SEPTEMBER 21 EDITION');
|
101
|
$this->assertText('INTEGRATING THE SITEMINDER ACCESS SYSTEM IN AN OPEN ATRIUM-BASED INTRANET');
|
102
|
$this->assertText('OPEN DATA FOR MICROFINANCE: THE NEW MIXMARKET.ORG');
|
103
|
$this->assertText('WEEK IN DC TECH: SEPTEMBER 28TH EDITION');
|
104
|
$this->assertText('SEPTEMBER GEODC MEETUP TONIGHT');
|
105
|
$this->assertText('MAPPING INNOVATION AT THE WORLD BANK WITH OPEN ATRIUM');
|
106
|
$this->assertText('WEEK IN DC TECH: OCTOBER 5TH EDITION');
|
107
|
$this->assertText('OPEN ATRIUM TRANSLATION WORKFLOW: TWO WAY TRANSLATION UPDATES');
|
108
|
}
|
109
|
|
110
|
protected function assertDevseedFeedContent() {
|
111
|
$this->drupalGet('node');
|
112
|
$this->asserttext('Scaling the Open Atrium UI');
|
113
|
$this->asserttext("Peru's Software Freedom Day: Impressions & Photos");
|
114
|
$this->asserttext('Week in DC Tech: September 21 Edition');
|
115
|
$this->asserttext('Integrating the Siteminder Access System in an Open Atrium-based Intranet');
|
116
|
$this->asserttext('Open Data for Microfinance: The New MIXMarket.org');
|
117
|
$this->asserttext('Week in DC Tech: September 28th Edition');
|
118
|
$this->asserttext('September GeoDC Meetup Tonight');
|
119
|
$this->asserttext('Mapping Innovation at the World Bank with Open Atrium');
|
120
|
$this->asserttext('Week in DC Tech: October 5th Edition');
|
121
|
$this->asserttext('Open Atrium Translation Workflow: Two Way Translation Updates');
|
122
|
}
|
123
|
|
124
|
protected function assertHashedDevseedFeedContent() {
|
125
|
$this->drupalGet('node');
|
126
|
$this->asserttext('7e6719e920a73954fe94c931b2715efe');
|
127
|
$this->asserttext('8ea53182dfa501e4aa997a6030931125');
|
128
|
$this->asserttext('ad560610949586a1d477585ce48777e8');
|
129
|
$this->asserttext('e84c1cf48e264224d7a6e20d6def66de');
|
130
|
$this->asserttext('14aa7188d867973831e4232f67c070c3');
|
131
|
$this->asserttext('bb5a525f6e89da7ecbafa09a10b01529');
|
132
|
$this->asserttext('50c2feb66fbd6144334a14684b3cf547');
|
133
|
$this->asserttext('072f06df55a62bd694c4544d2397bf82');
|
134
|
$this->asserttext('b02c3e6bdcfdaac72b17a14f4f0eae27');
|
135
|
$this->asserttext('a628fc19e8fcb2b9c987412a7fcd995c');
|
136
|
}
|
137
|
|
138
|
}
|
139
|
|
140
|
/**
|
141
|
* Simple tests for api functionality.
|
142
|
*/
|
143
|
class FeedsTamperWebTestCase extends FeedsTamperWebTestHelper {
|
144
|
|
145
|
public static function getInfo() {
|
146
|
return array(
|
147
|
'name' => 'Feeds Tamper',
|
148
|
'description' => 'Regression tests for Feeds Tamper.',
|
149
|
'group' => 'Feeds Tamper',
|
150
|
);
|
151
|
}
|
152
|
|
153
|
public function setUp() {
|
154
|
parent::setUp();
|
155
|
|
156
|
$this->createImporterConfiguration();
|
157
|
$this->addMappings('syndication', array(
|
158
|
0 => array(
|
159
|
'source' => 'title',
|
160
|
'target' => 'title',
|
161
|
'unique' => FALSE,
|
162
|
),
|
163
|
1 => array(
|
164
|
'source' => 'guid',
|
165
|
'target' => 'guid',
|
166
|
'unique' => TRUE,
|
167
|
),
|
168
|
2 => array(
|
169
|
'source' => 'description',
|
170
|
'target' => 'body',
|
171
|
),
|
172
|
));
|
173
|
// Set update existing to simplify our lives.
|
174
|
$this->setSettings('syndication', 'FeedsNodeProcessor', array('update_existing' => 1));
|
175
|
}
|
176
|
|
177
|
public function testBasic() {
|
178
|
// Test basic plugin adding.
|
179
|
// Add a no-op explode/implode sequence to verify that we can switch types.
|
180
|
$this->addTamperPlugin('syndication', 'title', 'explode', array('separator' => '$', 'limit' => ''));
|
181
|
$id = $this->addTamperPlugin('syndication', 'title', 'convert_case', array('mode' => 0));
|
182
|
$this->addTamperPlugin('syndication', 'title', 'implode', array('glue' => ''));
|
183
|
|
184
|
// Create feed node, running import automatically.
|
185
|
$nid = $this->createFeedNode();
|
186
|
// Assert that all titles were uppercased.
|
187
|
$this->assertUppercaseDevseedFeedContent();
|
188
|
// Disable plugin.
|
189
|
$this->disableTamperPlugin($id);
|
190
|
//$this->deleteTamperPlugin($id);
|
191
|
$this->drupalPost('node/' . $nid . '/import', array(), 'Import');
|
192
|
$this->assertText('Updated 10 nodes.');
|
193
|
$this->assertDevseedFeedContent();
|
194
|
}
|
195
|
|
196
|
/**
|
197
|
* Integration tests for the copy plugin.
|
198
|
*/
|
199
|
public function testCopyPlugin() {
|
200
|
$this->removeMappings('syndication', array(
|
201
|
2 => array(
|
202
|
'source' => 'description',
|
203
|
'target' => 'body',
|
204
|
),
|
205
|
));
|
206
|
|
207
|
$this->addMappings('syndication', array(
|
208
|
2 => array(
|
209
|
'source' => 'Blank source 1',
|
210
|
'target' => 'body',
|
211
|
),
|
212
|
));
|
213
|
|
214
|
// Test copy to.
|
215
|
// The case should get converted for the title, but not the body.
|
216
|
$copy = $this->addTamperPlugin('syndication', 'title', 'copy', array('to_from' => 'to', 'source' => 'Blank source 1'));
|
217
|
$convert_case = $this->addTamperPlugin('syndication', 'title', 'convert_case', array('mode' => 0));
|
218
|
|
219
|
// Create feed node, running import automatically.
|
220
|
$nid = $this->createFeedNode();
|
221
|
$this->drupalGet('node/2/edit');
|
222
|
$this->assertFieldByXPath("//textarea[@name='body[und][0][value]']", 'Open Atrium Translation Workflow: Two Way Translation Updates', 'The textfield widget is populated.');
|
223
|
$this->assertFieldByName('title', 'OPEN ATRIUM TRANSLATION WORKFLOW: TWO WAY TRANSLATION UPDATES');
|
224
|
|
225
|
$this->disableTamperPlugin($convert_case);
|
226
|
$this->addTamperPlugin('syndication', 'Blank source 1', 'convert_case', array('mode' => 0));
|
227
|
$this->drupalPost('node/' . $nid . '/import', array(), 'Import');
|
228
|
$this->drupalGet('node/2/edit');
|
229
|
$this->assertFieldByXPath("//textarea[@name='body[und][0][value]']", 'OPEN ATRIUM TRANSLATION WORKFLOW: TWO WAY TRANSLATION UPDATES', 'The textfield widget is populated.');
|
230
|
$this->assertFieldByName('title', 'Open Atrium Translation Workflow: Two Way Translation Updates');
|
231
|
}
|
232
|
|
233
|
}
|
234
|
|
235
|
/**
|
236
|
* Tests the special case that is FeedsCSVParser.
|
237
|
*/
|
238
|
class FeedsTamperCSVTestCase extends FeedsTamperWebTestHelper {
|
239
|
|
240
|
public static function getInfo() {
|
241
|
return array(
|
242
|
'name' => 'Special case: CSV parser',
|
243
|
'description' => 'Tests the special case that is FeedsCSVParser. See <a href="http://drupal.org/node/1133724" target="_blank">#1133724</a>.',
|
244
|
'group' => 'Feeds Tamper',
|
245
|
);
|
246
|
}
|
247
|
|
248
|
public function test() {
|
249
|
$importer_name = $this->randomName();
|
250
|
$importer_id = drupal_strtolower($importer_name);
|
251
|
// Set up an importer.
|
252
|
$this->createImporterConfiguration($importer_name, $importer_id);
|
253
|
// Set and configure plugins and mappings.
|
254
|
$this->setPlugin($importer_id, 'FeedsCSVParser');
|
255
|
// We have a space in the source cause that was a problem at one point.
|
256
|
$mappings = array(
|
257
|
0 => array(
|
258
|
'source' => 'a /T~!@#$%^&*()_+`<>;:itle✈',
|
259
|
'target' => 'title',
|
260
|
),
|
261
|
1 => array(
|
262
|
'source' => 'a Body.',
|
263
|
'target' => 'body',
|
264
|
),
|
265
|
2 => array(
|
266
|
'source' => 'a GUID',
|
267
|
'target' => 'guid',
|
268
|
'unique' => TRUE,
|
269
|
),
|
270
|
3 => array(
|
271
|
'source' => 'Blank source 1',
|
272
|
'target' => 'uid',
|
273
|
),
|
274
|
);
|
275
|
$this->addMappings($importer_id, $mappings);
|
276
|
// Set update existing.
|
277
|
$this->setSettings($importer_id, 'FeedsNodeProcessor', array('update_existing' => 1));
|
278
|
|
279
|
// Test basic plugin adding.
|
280
|
$settings = array(
|
281
|
'mode' => 0,
|
282
|
);
|
283
|
$id = $this->addTamperPlugin($importer_id, 'a /T~!@#$%^&*()_+`<>;:itle✈', 'convert_case', $settings);
|
284
|
$id2 = $this->addTamperPlugin($importer_id, 'a Body.', 'convert_case', $settings);
|
285
|
|
286
|
// Verify that Blank source works for CSV parser.
|
287
|
$this->addTamperPlugin($importer_id, 'Blank source 1', 'default_value', array('default_value' => $this->admin_user->uid));
|
288
|
|
289
|
$path = $GLOBALS['base_url'] . '/' . drupal_get_path('module', 'feeds_tamper') . '/tests/feeds_tamper/';
|
290
|
$nid = $this->createFeedNode($importer_id, $path . 'csv_test.csv', 'Testing CSV Parser');
|
291
|
$this->assertText('Basic page Testing CSV Parser has been created.');
|
292
|
$this->assertText('Created 2 nodes.');
|
293
|
$this->drupalGet('node');
|
294
|
$this->assertText('HELLO WORLD');
|
295
|
$this->assertText('THIS IS A GREAT BODY.');
|
296
|
$this->assertText('WORLD DOES NOT RESPOND');
|
297
|
$this->assertText('THIS IS A SAD BODY.');
|
298
|
$this->disableTamperPlugin($id);
|
299
|
$this->disableTamperPlugin($id2);
|
300
|
$this->drupalPost('node/' . $nid . '/import', array(), 'Import');
|
301
|
$this->assertText('Updated 2 nodes.');
|
302
|
$this->drupalGet('node');
|
303
|
$this->assertText('Hello world');
|
304
|
$this->assertText('This is a great body.');
|
305
|
$this->assertText('World does not respond');
|
306
|
$this->assertText('This is a sad body.');
|
307
|
|
308
|
$this->drupalGet('node/2/edit');
|
309
|
$this->assertFieldByName('name', $this->admin_user->name);
|
310
|
}
|
311
|
|
312
|
public function testRewrite() {
|
313
|
$importer_name = $this->randomName();
|
314
|
$importer_id = drupal_strtolower($importer_name);
|
315
|
// Set up an importer.
|
316
|
$this->createImporterConfiguration($importer_name, $importer_id);
|
317
|
// Set and configure plugins and mappings.
|
318
|
$this->setPlugin($importer_id, 'FeedsCSVParser');
|
319
|
// We have a space in the source cause that was a problem at one point.
|
320
|
$mappings = array(
|
321
|
0 => array(
|
322
|
'source' => 'a /T~!@#$%^&*()_+`<>;:itle✈',
|
323
|
'target' => 'title',
|
324
|
),
|
325
|
1 => array(
|
326
|
'source' => 'a Body.',
|
327
|
'target' => 'Temporary target 1',
|
328
|
),
|
329
|
2 => array(
|
330
|
'source' => 'Blank source 1',
|
331
|
'target' => 'body',
|
332
|
),
|
333
|
);
|
334
|
$this->addMappings($importer_id, $mappings);
|
335
|
|
336
|
// Copy the field to the node's body field.
|
337
|
$this->addTamperPlugin($importer_id, 'Blank source 1', 'rewrite', array('text' => '[a body.]'));
|
338
|
|
339
|
$path = $GLOBALS['base_url'] . '/' . drupal_get_path('module', 'feeds_tamper') . '/tests/feeds_tamper/';
|
340
|
$nid = $this->createFeedNode($importer_id, $path . 'csv_test.csv', 'Testing CSV Parser');
|
341
|
$this->assertText('Basic page Testing CSV Parser has been created.');
|
342
|
$this->assertText('Created 2 nodes.');
|
343
|
$this->drupalGet('node');
|
344
|
$this->assertText('Hello world');
|
345
|
$this->assertText('This is a great body.');
|
346
|
$this->assertText('World does not respond');
|
347
|
$this->assertText('This is a sad body.');
|
348
|
}
|
349
|
|
350
|
}
|