1 | <?php |
---|
2 | |
---|
3 | /** |
---|
4 | * @file |
---|
5 | * Common functionality for all Feeds tests. |
---|
6 | */ |
---|
7 | |
---|
8 | /** |
---|
9 | * Test basic Data API functionality. |
---|
10 | */ |
---|
11 | class FeedsWebTestCase extends DrupalWebTestCase { |
---|
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 | |
---|
30 | $modules[] = 'taxonomy'; |
---|
31 | $modules[] = 'feeds'; |
---|
32 | $modules[] = 'feeds_ui'; |
---|
33 | //$modules[] = 'feeds_tests'; |
---|
34 | $modules[] = 'ctools'; |
---|
35 | $modules[] = 'job_scheduler'; |
---|
36 | $modules = array_unique($modules); |
---|
37 | parent::setUp($modules); |
---|
38 | |
---|
39 | // Build the list of required administration permissions. Additional |
---|
40 | // permissions can be passed as an array into setUp()'s second parameter. |
---|
41 | if (isset($args[1]) && is_array($args[1])) { |
---|
42 | $permissions = $args[1]; |
---|
43 | } |
---|
44 | else { |
---|
45 | $permissions = array(); |
---|
46 | } |
---|
47 | |
---|
48 | $permissions[] = 'access content'; |
---|
49 | $permissions[] = 'administer site configuration'; |
---|
50 | $permissions[] = 'administer content types'; |
---|
51 | $permissions[] = 'administer nodes'; |
---|
52 | $permissions[] = 'administer taxonomy'; |
---|
53 | $permissions[] = 'administer users'; |
---|
54 | $permissions[] = 'administer feeds'; |
---|
55 | |
---|
56 | // Create an admin user and log in. |
---|
57 | $this->admin_user = $this->drupalCreateUser($permissions); |
---|
58 | $this->drupalLogin($this->admin_user); |
---|
59 | } |
---|
60 | |
---|
61 | /** |
---|
62 | * Absolute path to Drupal root. |
---|
63 | */ |
---|
64 | public function absolute() { |
---|
65 | return realpath(getcwd()); |
---|
66 | } |
---|
67 | |
---|
68 | /** |
---|
69 | * Get the absolute directory path of the feeds module. |
---|
70 | */ |
---|
71 | public function absolutePath() { |
---|
72 | return $this->absolute() .'/'. drupal_get_path('module', 'feeds'); |
---|
73 | } |
---|
74 | |
---|
75 | /** |
---|
76 | * Generate an OPML test feed. |
---|
77 | * |
---|
78 | * The purpose of this function is to create a dynamic OPML feed that points |
---|
79 | * to feeds included in this test. |
---|
80 | */ |
---|
81 | public function generateOPML() { |
---|
82 | $path = $GLOBALS['base_url'] .'/'. drupal_get_path('module', 'feeds') .'/tests/feeds/'; |
---|
83 | |
---|
84 | $output = |
---|
85 | '<?xml version="1.0" encoding="utf-8"?> |
---|
86 | <opml version="1.1"> |
---|
87 | <head> |
---|
88 | <title>Feeds test OPML</title> |
---|
89 | <dateCreated>Fri, 16 Oct 2009 02:53:17 GMT</dateCreated> |
---|
90 | <ownerName></ownerName> |
---|
91 | </head> |
---|
92 | <body> |
---|
93 | <outline text="Feeds test group" > |
---|
94 | <outline title="Development Seed - Technological Solutions for Progressive Organizations" text="" xmlUrl="'. $path .'developmentseed.rss2" type="rss" /> |
---|
95 | <outline title="Magyar Nemzet Online - H\'rek" text="" xmlUrl="'. $path .'feed_without_guid.rss2" type="rss" /> |
---|
96 | <outline title="Drupal planet" text="" type="rss" xmlUrl="'. $path .'drupalplanet.rss2" /> |
---|
97 | </outline> |
---|
98 | </body> |
---|
99 | </opml>'; |
---|
100 | |
---|
101 | // UTF 8 encode output string and write it to disk |
---|
102 | $output = utf8_encode($output); |
---|
103 | $file = $this->absolute() .'/'. file_directory_path() .'/test-opml-'. $this->randomName() .'.opml'; |
---|
104 | $handle = fopen($file, 'w'); |
---|
105 | fwrite($handle, $output); |
---|
106 | fclose($handle); |
---|
107 | return $file; |
---|
108 | } |
---|
109 | |
---|
110 | /** |
---|
111 | * Create an importer configuration. |
---|
112 | * |
---|
113 | * @param $name |
---|
114 | * The natural name of the feed. |
---|
115 | * @param $id |
---|
116 | * The persistent id of the feed. |
---|
117 | * @param $edit |
---|
118 | * Optional array that defines the basic settings for the feed in a format |
---|
119 | * that can be posted to the feed's basic settings form. |
---|
120 | */ |
---|
121 | public function createImporterConfiguration($name = 'Syndication', $id = 'syndication') { |
---|
122 | // Create new feed configuration. |
---|
123 | $this->drupalGet('admin/build/feeds'); |
---|
124 | $this->clickLink('New importer'); |
---|
125 | $edit = array( |
---|
126 | 'name' => $name, |
---|
127 | 'id' => $id, |
---|
128 | ); |
---|
129 | $this->drupalPost('admin/build/feeds/create', $edit, 'Create'); |
---|
130 | |
---|
131 | // Assert message and presence of default plugins. |
---|
132 | $this->assertText('Your configuration has been created with default settings.'); |
---|
133 | $this->assertPlugins($id, 'FeedsHTTPFetcher', 'FeedsSyndicationParser', 'FeedsNodeProcessor'); |
---|
134 | // Per default attach to page content type. |
---|
135 | $this->setSettings($id, NULL, array('content_type' => 'page')); |
---|
136 | } |
---|
137 | |
---|
138 | /** |
---|
139 | * Choose a plugin for a importer configuration and assert it. |
---|
140 | * |
---|
141 | * @param $id |
---|
142 | * The importer configuration's id. |
---|
143 | * @param $plugin_key |
---|
144 | * The key string of the plugin to choose (one of the keys defined in |
---|
145 | * feeds_feeds_plugins()). |
---|
146 | */ |
---|
147 | public function setPlugin($id, $plugin_key) { |
---|
148 | if ($type = feeds_plugin_type($plugin_key)) { |
---|
149 | $edit = array( |
---|
150 | 'plugin_key' => $plugin_key, |
---|
151 | ); |
---|
152 | $this->drupalPost("admin/build/feeds/edit/$id/$type", $edit, 'Save'); |
---|
153 | |
---|
154 | // Assert actual configuration. |
---|
155 | $config = unserialize(db_result(db_query("SELECT config FROM {feeds_importer} WHERE id = '%s'", $id))); |
---|
156 | $this->assertEqual($config[$type]['plugin_key'], $plugin_key, 'Verified correct '. $type .' ('. $plugin_key .').'); |
---|
157 | } |
---|
158 | } |
---|
159 | |
---|
160 | /** |
---|
161 | * Set importer or plugin settings. |
---|
162 | * |
---|
163 | * @param $id |
---|
164 | * The importer configuration's id. |
---|
165 | * @param $plugin |
---|
166 | * The plugin (class) name, or NULL to set importer's settings |
---|
167 | * @param $settings |
---|
168 | * The settings to set. |
---|
169 | */ |
---|
170 | public function setSettings($id, $plugin, $settings) { |
---|
171 | $this->drupalPost('admin/build/feeds/edit/'. $id .'/settings/'. $plugin, $settings, 'Save'); |
---|
172 | $this->assertText('Your changes have been saved.'); |
---|
173 | } |
---|
174 | |
---|
175 | /** |
---|
176 | * Create a test feed node. Test user has to have sufficient permissions: |
---|
177 | * |
---|
178 | * * create [type] content |
---|
179 | * * use feeds |
---|
180 | * |
---|
181 | * Assumes that page content type has been configured with |
---|
182 | * createImporterConfiguration() as a feed content type. |
---|
183 | * |
---|
184 | * @return |
---|
185 | * The node id of the node created. |
---|
186 | */ |
---|
187 | public function createFeedNode($id = 'syndication', $feed_url = NULL, $title = '', $content_type = NULL) { |
---|
188 | if (empty($feed_url)) { |
---|
189 | $feed_url = $GLOBALS['base_url'] .'/'. drupal_get_path('module', 'feeds') .'/tests/feeds/developmentseed.rss2'; |
---|
190 | } |
---|
191 | |
---|
192 | // If content type not given, retrieve it. |
---|
193 | if (!$content_type) { |
---|
194 | $result = db_query("SELECT config FROM {feeds_importer} WHERE id = '%s'", $id); |
---|
195 | $config = unserialize(db_result($result)); |
---|
196 | $content_type = $config['content_type']; |
---|
197 | $this->assertFalse(empty($content_type), 'Valid content type found: '. $content_type); |
---|
198 | } |
---|
199 | |
---|
200 | // Create a feed node. |
---|
201 | $edit = array( |
---|
202 | 'title' => $title, |
---|
203 | 'feeds[FeedsHTTPFetcher][source]' => $feed_url, |
---|
204 | ); |
---|
205 | $this->drupalPost('node/add/'. str_replace('_', '-', $content_type), $edit, 'Save'); |
---|
206 | $this->assertText('has been created.'); |
---|
207 | |
---|
208 | // Get the node id from URL. |
---|
209 | $nid = $this->getNid($this->getUrl()); |
---|
210 | |
---|
211 | // Check whether feed got recorded in feeds_source table. |
---|
212 | $result = db_result(db_query("SELECT COUNT(*) FROM {feeds_source} WHERE id = '%s' AND feed_nid = %d", $id, $nid)); |
---|
213 | $this->assertEqual(1, $result); |
---|
214 | |
---|
215 | $source = db_fetch_object(db_query("SELECT * FROM {feeds_source} WHERE id = '%s' AND feed_nid = %d", $id, $nid)); |
---|
216 | $config = unserialize($source->config); |
---|
217 | $this->assertEqual($config['FeedsHTTPFetcher']['source'], $feed_url, t('URL in DB correct.')); |
---|
218 | return $nid; |
---|
219 | } |
---|
220 | |
---|
221 | /** |
---|
222 | * Edit the configuration of a feed node to test update behavior. |
---|
223 | * |
---|
224 | * @param $nid |
---|
225 | * The nid to edit. |
---|
226 | * @param $feed_url |
---|
227 | * The new (absolute) feed URL to use. |
---|
228 | * @param $title |
---|
229 | * Optional parameter to change title of feed node. |
---|
230 | */ |
---|
231 | public function editFeedNode($nid, $feed_url, $title = '') { |
---|
232 | $edit = array( |
---|
233 | 'title' => $title, |
---|
234 | 'feeds[FeedsHTTPFetcher][source]' => $feed_url, |
---|
235 | ); |
---|
236 | // Check that the update was saved. |
---|
237 | $this->drupalPost('node/' . $nid . '/edit', $edit, 'Save'); |
---|
238 | $this->assertText('has been updated.'); |
---|
239 | |
---|
240 | // Check that the URL was updated in the feeds_source table. |
---|
241 | $source = db_fetch_object(db_query("SELECT * FROM {feeds_source} WHERE feed_nid = %d", $nid)); |
---|
242 | $config = unserialize($source->config); |
---|
243 | $this->assertEqual($config['FeedsHTTPFetcher']['source'], $feed_url, t('URL in DB correct.')); |
---|
244 | } |
---|
245 | |
---|
246 | /** |
---|
247 | * Batch create a variable amount of feed nodes. All will have the |
---|
248 | * same URL configured. |
---|
249 | * |
---|
250 | * @return |
---|
251 | * An array of node ids of the nodes created. |
---|
252 | */ |
---|
253 | public function createFeedNodes($id = 'syndication', $num = 20, $content_type = NULL) { |
---|
254 | $nids = array(); |
---|
255 | for ($i = 0; $i < $num; $i++) { |
---|
256 | $nids[] = $this->createFeedNode($id, NULL, $this->randomName(), $content_type); |
---|
257 | } |
---|
258 | return $nids; |
---|
259 | } |
---|
260 | |
---|
261 | /** |
---|
262 | * Import a URL through the import form. Assumes FeedsHTTPFetcher in place. |
---|
263 | */ |
---|
264 | public function importURL($id, $feed_url = NULL) { |
---|
265 | if (empty($feed_url)) { |
---|
266 | $feed_url = $GLOBALS['base_url'] .'/'. drupal_get_path('module', 'feeds') .'/tests/feeds/developmentseed.rss2'; |
---|
267 | } |
---|
268 | $edit = array( |
---|
269 | 'feeds[FeedsHTTPFetcher][source]' => $feed_url, |
---|
270 | ); |
---|
271 | $nid = $this->drupalPost('import/'. $id, $edit, 'Import'); |
---|
272 | |
---|
273 | // Check whether feed got recorded in feeds_source table. |
---|
274 | $this->assertEqual(1, db_result(db_query("SELECT COUNT(*) FROM {feeds_source} WHERE id = '%s' AND feed_nid = 0", $id))); |
---|
275 | $source = db_fetch_object(db_query("SELECT * FROM {feeds_source} WHERE id = '%s' AND feed_nid = 0", $id)); |
---|
276 | $config = unserialize($source->config); |
---|
277 | $this->assertEqual($config['FeedsHTTPFetcher']['source'], $feed_url, t('URL in DB correct.')); |
---|
278 | |
---|
279 | // Check whether feed got properly added to scheduler. |
---|
280 | $this->assertEqual(1, db_result(db_query("SELECT COUNT(*) FROM {job_schedule} WHERE type = '%s' AND id = 0 AND callback = 'feeds_source_import' AND last <> 0 AND scheduled = 0", $id))); |
---|
281 | // There must be only one entry for callback 'expire' - no matter what the feed_nid is. |
---|
282 | $this->assertEqual(0, db_result(db_query("SELECT COUNT(*) FROM {job_schedule} WHERE type = '%s' AND callback = 'feeds_importer_expire' AND last <> 0 AND scheduled = 0", $id))); |
---|
283 | } |
---|
284 | |
---|
285 | /** |
---|
286 | * Import a file through the import form. Assumes FeedsFileFetcher in place. |
---|
287 | */ |
---|
288 | public function importFile($id, $file) { |
---|
289 | |
---|
290 | $this->assertTrue(file_exists($file), 'Source file exists'); |
---|
291 | $edit = array( |
---|
292 | 'files[feeds]' => $file, |
---|
293 | ); |
---|
294 | $this->drupalPost('import/'. $id, $edit, 'Import'); |
---|
295 | } |
---|
296 | |
---|
297 | /** |
---|
298 | * Assert a feeds configuration's plugins. |
---|
299 | * |
---|
300 | * @deprecated: |
---|
301 | * Use setPlugin() instead. |
---|
302 | * |
---|
303 | * @todo Refactor users of assertPlugin() and make them use setPugin() instead. |
---|
304 | */ |
---|
305 | public function assertPlugins($id, $fetcher, $parser, $processor) { |
---|
306 | // Assert actual configuration. |
---|
307 | $config = unserialize(db_result(db_query("SELECT config FROM {feeds_importer} WHERE id = '%s'", $id))); |
---|
308 | |
---|
309 | $this->assertEqual($config['fetcher']['plugin_key'], $fetcher, 'Correct fetcher'); |
---|
310 | $this->assertEqual($config['parser']['plugin_key'], $parser, 'Correct parser'); |
---|
311 | $this->assertEqual($config['processor']['plugin_key'], $processor, 'Correct processor'); |
---|
312 | } |
---|
313 | |
---|
314 | /** |
---|
315 | * Add mappings to a given configuration. |
---|
316 | * |
---|
317 | * @param $mappings |
---|
318 | * An array of mapping arrays. Each mapping array must have a source and |
---|
319 | * an target key and can have a unique key. |
---|
320 | * |
---|
321 | * @see FeedsRSStoDataTest class. |
---|
322 | */ |
---|
323 | public function addMappings($id, $mappings) { |
---|
324 | |
---|
325 | $path = 'admin/build/feeds/edit/'. $id .'/mapping'; |
---|
326 | |
---|
327 | // Iterate through all mappings and add the via the form. |
---|
328 | foreach ($mappings as $i => $mapping) { |
---|
329 | |
---|
330 | // Get unique flag and unset it - otherwise drupalPost will complain that |
---|
331 | // there is no form element named "unique". |
---|
332 | $unique = !empty($mapping['unique']); |
---|
333 | unset($mapping['unique']); |
---|
334 | $this->drupalPost($path, $mapping, t('Add')); |
---|
335 | |
---|
336 | // If unique was set, set the last mapping's unique flag. |
---|
337 | if ($unique) { |
---|
338 | $edit = array( |
---|
339 | 'unique_flags['. $i .']' => 1, |
---|
340 | ); |
---|
341 | $this->drupalPost($path, $edit, t('Save')); |
---|
342 | } |
---|
343 | } |
---|
344 | } |
---|
345 | |
---|
346 | /** |
---|
347 | * Helper function, retrieves node id from a URL. |
---|
348 | */ |
---|
349 | public function getNid($url) { |
---|
350 | $matches = array(); |
---|
351 | preg_match('/node\/(\d+?)$/', $url, $matches); |
---|
352 | $nid = $matches[1]; |
---|
353 | if (!is_numeric($nid)) { |
---|
354 | $this->error(t('Could not find node id, found @nid instead.', array('@nid' => $nid))); |
---|
355 | } |
---|
356 | return $nid; |
---|
357 | } |
---|
358 | } |
---|
359 | |
---|
360 | /** |
---|
361 | * Provides a wrapper for DrupalUnitTestCase for Feeds unit testing. |
---|
362 | */ |
---|
363 | class FeedsUnitTestHelper extends DrupalUnitTestCase { |
---|
364 | public function setUp() { |
---|
365 | parent::setUp(); |
---|
366 | |
---|
367 | // Manually include the feeds module. |
---|
368 | // @todo Allow an array of modules from the child class. |
---|
369 | drupal_load('module', 'feeds'); |
---|
370 | } |
---|
371 | } |
---|
372 | |
---|
373 | class FeedsUnitTestCase extends FeedsUnitTestHelper { |
---|
374 | public static function getInfo() { |
---|
375 | return array( |
---|
376 | 'name' => 'Unit tests', |
---|
377 | 'description' => 'Test basic low-level Feeds module functionality.', |
---|
378 | 'group' => 'Feeds', |
---|
379 | ); |
---|
380 | } |
---|
381 | |
---|
382 | /** |
---|
383 | * Test valid absolute urls. |
---|
384 | * |
---|
385 | * @see ValidUrlTestCase |
---|
386 | * |
---|
387 | * @todo Remove when http://drupal.org/node/1191252 is fixed. |
---|
388 | */ |
---|
389 | function testFeedsValidURL() { |
---|
390 | $url_schemes = array('http', 'https', 'ftp', 'feed', 'webcal'); |
---|
391 | $valid_absolute_urls = array( |
---|
392 | 'example.com', |
---|
393 | 'www.example.com', |
---|
394 | 'ex-ample.com', |
---|
395 | '3xampl3.com', |
---|
396 | 'example.com/paren(the)sis', |
---|
397 | 'example.com/index.html#pagetop', |
---|
398 | 'example.com:8080', |
---|
399 | 'subdomain.example.com', |
---|
400 | 'example.com/index.php?q=node', |
---|
401 | 'example.com/index.php?q=node¶m=false', |
---|
402 | 'user@www.example.com', |
---|
403 | 'user:pass@www.example.com:8080/login.php?do=login&style=%23#pagetop', |
---|
404 | '127.0.0.1', |
---|
405 | 'example.org?', |
---|
406 | 'john%20doe:secret:foo@example.org/', |
---|
407 | 'example.org/~,$\'*;', |
---|
408 | 'caf%C3%A9.example.org', |
---|
409 | '[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]:80/index.html', |
---|
410 | 'graph.asfdasdfasdf.com/blarg/feed?access_token=133283760145143|tGew8jbxi1ctfVlYh35CPYij1eE', |
---|
411 | ); |
---|
412 | |
---|
413 | foreach ($url_schemes as $scheme) { |
---|
414 | foreach ($valid_absolute_urls as $url) { |
---|
415 | $test_url = $scheme . '://' . $url; |
---|
416 | $valid_url = feeds_valid_url($test_url, TRUE); |
---|
417 | $this->assertTrue($valid_url, t('@url is a valid url.', array('@url' => $test_url))); |
---|
418 | } |
---|
419 | } |
---|
420 | |
---|
421 | $invalid_ablosule_urls = array( |
---|
422 | '', |
---|
423 | 'ex!ample.com', |
---|
424 | 'ex%ample.com', |
---|
425 | ); |
---|
426 | |
---|
427 | foreach ($url_schemes as $scheme) { |
---|
428 | foreach ($invalid_ablosule_urls as $url) { |
---|
429 | $test_url = $scheme . '://' . $url; |
---|
430 | $valid_url = feeds_valid_url($test_url, TRUE); |
---|
431 | $this->assertFalse($valid_url, t('@url is NOT a valid url.', array('@url' => $test_url))); |
---|
432 | } |
---|
433 | } |
---|
434 | } |
---|
435 | } |
---|