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