db2a06c57d5b8575bbe2b910a60c38346f40476d
[project/feeds.git] / tests / feeds_processor_node.test
1 <?php
2 // $Id$
3
4 /**
5 * @file
6 * Tests for plugins/FeedsNodeProcessor.inc.
7 */
8
9 // Require FeedsWebTestCase class definition.
10 require_once(dirname(__FILE__) .'/feeds.test.inc');
11
12 /**
13 * Test aggregating a feed as node items.
14 */
15 class FeedsRSStoNodesTest extends FeedsWebTestCase {
16
17 /**
18 * Describe this test.
19 */
20 public function getInfo() {
21 return array(
22 'name' => t('RSS import to nodes'),
23 'description' => t('Tests a feed configuration that is attached to a content type, uses HTTP fetcher, common syndication parser and a node processor. Repeats the same test for an importer configuration that is not attached to a content type and for a configuration that is attached to a content type and uses the file fetcher.'),
24 'group' => t('Feeds'),
25 );
26 }
27
28 /**
29 * Set up test.
30 */
31 public function setUp() {
32 parent::setUp('feeds', 'feeds_ui', 'ctools', 'job_scheduler');
33 $this->drupalLogin(
34 $this->drupalCreateUser(
35 array(
36 'administer feeds', 'administer nodes', 'administer content types',
37 )
38 )
39 );
40 }
41
42 /**
43 * Test node creation, refreshing/deleting feeds and feed items.
44 */
45 public function test() {
46 // Set the teaser length to unlimited otherwise tests looking for text on
47 // nodes will fail.
48 $edit = array(
49 'teaser_length' => 0,
50 );
51 $this->drupalPost('admin/content/node-settings', $edit, 'Save configuration');
52
53 // Create an importer configuration.
54 $this->createImporterConfiguration('Syndication', 'syndication');
55 $this->addMappings('syndication',
56 array(
57 array(
58 'source' => 'title',
59 'target' => 'title',
60 'unique' => FALSE,
61 ),
62 array(
63 'source' => 'description',
64 'target' => 'body',
65 'unique' => FALSE,
66 ),
67 array(
68 'source' => 'timestamp',
69 'target' => 'created',
70 'unique' => FALSE,
71 ),
72 array(
73 'source' => 'url',
74 'target' => 'url',
75 'unique' => TRUE,
76 ),
77 array(
78 'source' => 'guid',
79 'target' => 'guid',
80 'unique' => TRUE,
81 ),
82 )
83 );
84
85 $nid = $this->createFeedNode();
86 // Assert 10 items aggregated after creation of the node.
87 $this->assertText('Created 10 Story nodes.');
88
89 // Navigate to feed node, there should be Feeds tabs visible.
90 $this->drupalGet('node/'. $nid);
91 $this->assertRaw('node/'. $nid .'/import');
92 $this->assertRaw('node/'. $nid .'/delete-items');
93
94 // Navigate to a non-feed node, there should be no Feeds tabs visible.
95 $story_nid = db_query_range('SELECT nid FROM {node} WHERE type = "story"', 0, 1)->fetchField();
96 $this->drupalGet('node/'. $story_nid);
97 $this->assertNoRaw('node/'. $story_nid .'/import');
98 $this->assertNoRaw('node/'. $story_nid .'/delete-items');
99 $this->assertEqual("Created/updated by FeedsNodeProcessor", db_query("SELECT nr.log FROM {node} n JOIN {node_revisions} nr ON n.vid = nr.vid WHERE n.nid = :nid", array(':nid' => $story_nid))->fetchField());
100
101 // Assert accuracy of aggregated information.
102 $this->drupalGet('node');
103 $this->assertPattern('/<span class="submitted">(.*?)Anonymous<\/span>/');
104 $this->assertText('Open Atrium Translation Workflow: Two Way Translation Updates');
105 $this->assertText('Tue, 10/06/2009');
106 $this->assertText('A new translation process for Open Atrium &amp; integration with Localize Drupal');
107 $this->assertText('Week in DC Tech: October 5th Edition');
108 $this->assertText('Mon, 10/05/2009');
109 $this->assertText('There are some great technology events happening this week');
110 $this->assertText('Mapping Innovation at the World Bank with Open Atrium');
111 $this->assertText('Fri, 10/02/2009');
112 $this->assertText('Open Atrium is being used as a base platform for collaboration');
113 $this->assertText('September GeoDC Meetup Tonight');
114 $this->assertText('Wed, 09/30/2009');
115 $this->assertText('Today is the last Wednesday of the month');
116 $this->assertText('Week in DC Tech: September 28th Edition');
117 $this->assertText('Mon, 09/28/2009');
118 $this->assertText('Looking to geek out this week? There are a bunch of');
119 $this->assertText('Open Data for Microfinance: The New MIXMarket.org');
120 $this->assertText('Thu, 09/24/2009');
121 $this->assertText('There are profiles for every country that the MIX Market is hosting.');
122 $this->assertText('Integrating the Siteminder Access System in an Open Atrium-based Intranet');
123 $this->assertText('Tue, 09/22/2009');
124 $this->assertText('In addition to authentication, the Siteminder system');
125 $this->assertText('Week in DC Tech: September 21 Edition');
126 $this->assertText('Mon, 09/21/2009');
127 $this->assertText('an interesting variety of technology events happening in Washington, DC ');
128 $this->assertText('s Software Freedom Day: Impressions &amp; Photos');
129 $this->assertText('Mon, 09/21/2009');
130 $this->assertText('Presenting on Features in Drupal and Open Atrium');
131 $this->assertText('Scaling the Open Atrium UI');
132 $this->assertText('Fri, 09/18/2009');
133 $this->assertText('The first major change is switching');
134
135 // Assert DB status.
136 $count = db_query("SELECT COUNT(*) FROM {node} n INNER JOIN {feeds_node_item} fn ON n.nid = fn.nid")->fetchField();
137 $this->assertEqual($count, 10, 'Accurate number of items in database.');
138
139 // Assert default input format on first imported feed node.
140 $format = db_query_range("SELECT nr.format FROM {feeds_node_item} fi JOIN {node} n ON fi.nid = n.nid JOIN {node_revisions} nr ON n.vid = nr.vid", 0, 1)->fetchField();
141 $this->assertEqual($format, filter_fallback_format(), 'Using default Input format.');
142
143 // Import again.
144 $this->drupalPost('node/'. $nid .'/import', array(), 'Import');
145 $this->assertText('There is no new content.');
146
147 // Assert DB status, there still shouldn't be more than 10 items.
148 $count = db_query("SELECT COUNT(*) FROM {node} n INNER JOIN {feeds_node_item} fn ON n.nid = fn.nid")->fetchField();
149 $this->assertEqual($count, 10, 'Accurate number of items in database.');
150
151 // All of the above tests should have produced published nodes, set default
152 // to unpublished, import again.
153 $count = db_query("SELECT COUNT(*) FROM {node} n INNER JOIN {feeds_node_item} fn ON n.nid = fn.nid WHERE n.status = 1")->fetchField();
154 $this->assertEqual($count, 10, 'All items are published.');
155 $edit = array(
156 'node_options[status]' => FALSE,
157 );
158 $this->drupalPost('admin/content/node-type/story', $edit, t('Save content type'));
159 $this->drupalPost('node/'. $nid .'/delete-items', array(), 'Delete');
160 $this->drupalPost('node/'. $nid .'/import', array(), 'Import');
161 $count = db_query("SELECT COUNT(*) FROM {node} n INNER JOIN {feeds_node_item} fn ON n.nid = fn.nid WHERE n.status = 0")->fetchField();
162 $this->assertEqual($count, 10, 'No items are published.');
163 $edit = array(
164 'node_options[status]' => TRUE,
165 );
166 $this->drupalPost('admin/content/node-type/story', $edit, t('Save content type'));
167 $this->drupalPost('node/'. $nid .'/delete-items', array(), 'Delete');
168
169 // Enable replace existing and import updated feed file.
170 $this->drupalPost('node/'. $nid .'/import', array(), 'Import');
171 $this->setSettings('syndication', 'FeedsNodeProcessor', array('update_existing' => 1));
172 $feed_url = $GLOBALS['base_url'] .'/'. drupal_get_path('module', 'feeds') .'/tests/feeds/developmentseed_changes.rss2';
173 $this->editFeedNode($nid, $feed_url);
174 $this->drupalPost('node/' . $nid . '/import', array(), 'Import');
175 $this->assertText('Updated 2 Story nodes.');
176
177 // Assert accuracy of aggregated content (check 2 updates, one original).
178 $this->drupalGet('node');
179 $this->assertText('Managing News Translation Workflow: Two Way Translation Updates');
180 $this->assertText('Presenting on Features in Drupal and Managing News');
181 $this->assertText('Scaling the Open Atrium UI');
182
183 // Import again.
184 $this->drupalPost('node/'. $nid .'/import', array(), 'Import');
185 $this->assertText('There is no new content.');
186
187 // Assert DB status, there still shouldn't be more than 10 items.
188 $count = db_query("SELECT COUNT(*) FROM {feeds_node_item}")->fetchField();
189 $this->assertEqual($count, 10, 'Accurate number of items in database.');
190
191 // Now delete all items.
192 $this->drupalPost('node/'. $nid .'/delete-items', array(), 'Delete');
193 $this->assertText('Deleted 10 nodes.');
194
195 // Assert DB status, now there should be no items.
196 $count = db_query("SELECT COUNT(*) FROM {feeds_node_item}")->fetchField();
197 $this->assertEqual($count, 0, 'Accurate number of items in database.');
198
199 // Change author.
200 $author = $this->drupalCreateUser();
201 $this->setSettings('syndication', 'FeedsNodeProcessor', array('author' => $author->name));
202
203 // Change input format.
204 $this->setSettings('syndication', 'FeedsNodeProcessor', array('input_format' => filter_fallback_format() + 1));
205
206 // Import again.
207 $this->drupalPost('node/'. $nid .'/import', array(), 'Import');
208 $this->assertText('Created 10 Story nodes.');
209
210 // Assert author.
211 $this->drupalGet('node');
212 $this->assertPattern('/<span class="submitted">(.*?)'. check_plain($author->name) .'<\/span>/');
213 $count = db_query("SELECT COUNT(*) FROM {feeds_node_item} fi JOIN {node} n ON fi.nid = n.nid WHERE n.uid = :uid", array(':uid' => $author->uid))->fetchField();
214 $this->assertEqual($count, 10, 'Accurate number of items in database.');
215
216 // Assert input format.
217 $format = db_query_range("SELECT nr.format FROM {feeds_node_item} fi JOIN {node} n ON fi.nid = n.nid JOIN {node_revisions} nr ON n.vid = nr.vid", 0, 1)->fetchField();
218 $this->assertEqual($format, filter_fallback_format() + 1, 'Set non-default Input format.');
219
220 // Set to update existing, remove authorship of above nodes and import again.
221 $this->setSettings('syndication', 'FeedsNodeProcessor', array('update_existing' => 2));
222 db_query("UPDATE {node} n JOIN {feeds_node_item} fi ON n.nid = fi.nid SET n.uid = 0, fi.hash=''");
223 $this->drupalPost('node/'. $nid .'/import', array(), 'Import');
224 $this->drupalGet('node');
225 $this->assertNoPattern('/<span class="submitted">(.*?)'. check_plain($author->name) .'<\/span>/');
226 $count = db_query("SELECT COUNT(*) FROM {feeds_node_item} fi JOIN {node} n ON fi.nid = n.nid WHERE n.uid = :uid", array(':uid' => $author->uid))->fetchField();
227 $this->assertEqual($count, 0, 'Accurate number of items in database.');
228
229 // Map feed node's author to feed item author, update - feed node's items
230 // should now be assigned to feed node author.
231 $this->addMappings('syndication',
232 array(
233 array(
234 'source' => 'parent:uid',
235 'target' => 'uid',
236 ),
237 )
238 );
239 $this->drupalPost('node/'. $nid .'/import', array(), 'Import');
240 $this->drupalGet('node');
241 $this->assertNoPattern('/<span class="submitted">(.*?)'. check_plain($author->name) .'<\/span>/');
242 $uid = db_query("SELECT uid FROM {node} WHERE nid = :nid", array(':nid' => $nid))->fetchField();
243 $count = db_query("SELECT COUNT(*) FROM {node} WHERE uid = :uid", array(':uid' =>$uid))->fetchField();
244 $this->assertEqual($count, 11, 'All feed item nodes are assigned to feed node author.');
245
246 // Login with new user with only access content permissions.
247 $this->drupalLogin(
248 $this->drupalCreateUser()
249 );
250 // Navigate to feed node, there should be no Feeds tabs visible.
251 $this->drupalGet('node/'. $nid);
252 $this->assertNoRaw('node/'. $nid .'/import');
253 $this->assertNoRaw('node/'. $nid .'/delete-items');
254
255 // Now create a second feed configuration that is not attached to a content
256 // type and run tests on importing/purging.
257
258 // Login with sufficient permissions.
259 $this->drupalLogin(
260 $this->drupalCreateUser(array('administer feeds', 'administer nodes'))
261 );
262 // Remove all items again so that next test can check for them.
263 $this->drupalPost('node/'. $nid .'/delete-items', array(), 'Delete');
264
265 // Create an importer, not attached to content type.
266 $this->createImporterConfiguration('Syndication standalone', 'syndication_standalone');
267 $edit = array(
268 'content_type' => '',
269 );
270 $this->drupalPost('admin/structure/feeds/edit/syndication_standalone/settings', $edit, 'Save');
271 $this->addMappings('syndication_standalone',
272 array(
273 array(
274 'source' => 'title',
275 'target' => 'title',
276 'unique' => FALSE,
277 ),
278 array(
279 'source' => 'description',
280 'target' => 'body',
281 'unique' => FALSE,
282 ),
283 array(
284 'source' => 'timestamp',
285 'target' => 'created',
286 'unique' => FALSE,
287 ),
288 array(
289 'source' => 'url',
290 'target' => 'url',
291 'unique' => TRUE,
292 ),
293 array(
294 'source' => 'guid',
295 'target' => 'guid',
296 'unique' => TRUE,
297 ),
298 )
299 );
300
301 // Import, assert 10 items aggregated after creation of the node.
302 $this->importURL('syndication_standalone');
303 $this->assertText('Created 10 Story nodes.');
304
305 // Assert accuracy of aggregated information.
306 $this->drupalGet('node');
307 $this->assertText('Open Atrium Translation Workflow: Two Way Translation Updates');
308 $this->assertText('Tue, 10/06/2009');
309 $this->assertText('A new translation process for Open Atrium &amp; integration with Localize Drupal');
310 $this->assertText('Week in DC Tech: October 5th Edition');
311 $this->assertText('Mon, 10/05/2009');
312 $this->assertText('There are some great technology events happening this week');
313 $this->assertText('Mapping Innovation at the World Bank with Open Atrium');
314 $this->assertText('Fri, 10/02/2009');
315 $this->assertText('Open Atrium is being used as a base platform for collaboration');
316 $this->assertText('September GeoDC Meetup Tonight');
317 $this->assertText('Wed, 09/30/2009');
318 $this->assertText('Today is the last Wednesday of the month');
319 $this->assertText('Week in DC Tech: September 28th Edition');
320 $this->assertText('Mon, 09/28/2009');
321 $this->assertText('Looking to geek out this week? There are a bunch of');
322 $this->assertText('Open Data for Microfinance: The New MIXMarket.org');
323 $this->assertText('Thu, 09/24/2009');
324 $this->assertText('There are profiles for every country that the MIX Market is hosting.');
325 $this->assertText('Integrating the Siteminder Access System in an Open Atrium-based Intranet');
326 $this->assertText('Tue, 09/22/2009');
327 $this->assertText('In addition to authentication, the Siteminder system');
328 $this->assertText('Week in DC Tech: September 21 Edition');
329 $this->assertText('Mon, 09/21/2009');
330 $this->assertText('an interesting variety of technology events happening in Washington, DC ');
331 $this->assertText('s Software Freedom Day: Impressions &amp; Photos');
332 $this->assertText('Mon, 09/21/2009');
333 $this->assertText('Presenting on Features in Drupal and Open Atrium');
334 $this->assertText('Scaling the Open Atrium UI');
335 $this->assertText('Fri, 09/18/2009');
336 $this->assertText('The first major change is switching');
337
338 // Assert DB status.
339 $count = db_query("SELECT COUNT(*) FROM {feeds_node_item}")->fetchField();
340 $this->assertEqual($count, 10, 'Accurate number of items in database.');
341
342 // Import again.
343 $this->drupalPost('import/syndication_standalone', array(), 'Import');
344 $this->assertText('There is no new content.');
345
346 // Assert DB status, there still shouldn't be more than 10 items.
347 $count = db_query("SELECT COUNT(*) FROM {feeds_node_item}")->fetchField();
348 $this->assertEqual($count, 10, 'Accurate number of items in database.');
349
350 // Enable replace existing and import updated feed file.
351 $this->setSettings('syndication_standalone', 'FeedsNodeProcessor', array('update_existing' => 1));
352 $feed_url = $GLOBALS['base_url'] .'/'. drupal_get_path('module', 'feeds') . '/tests/feeds/developmentseed_changes.rss2';
353 $this->importURL('syndication_standalone', $feed_url);
354 $this->assertText('Updated 2 Story nodes.');
355
356 // Assert accuracy of aggregated information (check 2 updates, one orig).
357 $this->drupalGet('node');
358 $this->assertText('Managing News Translation Workflow: Two Way Translation Updates');
359 $this->assertText('Presenting on Features in Drupal and Managing News');
360 $this->assertText('Scaling the Open Atrium UI');
361
362 // Import again.
363 $this->drupalPost('import/syndication_standalone', array(), 'Import');
364 $this->assertText('There is no new content.');
365
366 // Assert DB status, there still shouldn't be more than 10 items.
367 $count = db_query("SELECT COUNT(*) FROM {feeds_node_item}")->fetchField();
368 $this->assertEqual($count, 10, 'Accurate number of items in database.');
369
370 // Now delete all items.
371 $this->drupalPost('import/syndication_standalone/delete-items', array(), 'Delete');
372 $this->assertText('Deleted 10 nodes.');
373
374 // Assert DB status, now there should be no items.
375 $count = db_query("SELECT COUNT(*) FROM {feeds_node_item}")->fetchField();
376 $this->assertEqual($count, 0, 'Accurate number of items in database.');
377
378 // Import again, we should find new content.
379 $this->drupalPost('import/syndication_standalone', array(), 'Import');
380 $this->assertText('Created 10 Story nodes.');
381
382 // Assert DB status, there should be 10 again.
383 $count = db_query("SELECT COUNT(*) FROM {feeds_node_item}")->fetchField();
384 $this->assertEqual($count, 10, 'Accurate number of items in database.');
385
386 // Login with new user with only access content permissions.
387 $this->drupalLogin(
388 $this->drupalCreateUser()
389 );
390 // Navigate to feed import form, access should be denied.
391 $this->drupalGet('import/syndication_standalone');
392 $this->assertResponse(403);
393
394 // Use File Fetcher.
395 $this->drupalLogin(
396 $this->drupalCreateUser(array('administer feeds', 'administer nodes'))
397 );
398 $this->setPlugin('syndication', 'FeedsFileFetcher');
399 // Create a feed node.
400 $edit = array(
401 'files[feeds]' => $this->absolutePath() .'/tests/feeds/drupalplanet.rss2',
402 );
403 $this->drupalPost('node/add/page', $edit, 'Save');
404 $this->assertText('has been created.');
405 $this->assertText('Created 25 Story nodes.');
406 }
407 }