feeds.test 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720
  1. <?php
  2. /**
  3. * @file
  4. * Common functionality for all Feeds tests.
  5. */
  6. /**
  7. * Test basic Data API functionality.
  8. */
  9. class FeedsWebTestCase extends DrupalWebTestCase {
  10. protected $profile = 'testing';
  11. public function setUp() {
  12. $args = func_get_args();
  13. // Build the list of required modules which can be altered by passing in an
  14. // array of module names to setUp().
  15. if (isset($args[0])) {
  16. if (is_array($args[0])) {
  17. $modules = $args[0];
  18. }
  19. else {
  20. $modules = $args;
  21. }
  22. }
  23. else {
  24. $modules = array();
  25. }
  26. $modules[] = 'taxonomy';
  27. $modules[] = 'image';
  28. $modules[] = 'file';
  29. $modules[] = 'field';
  30. $modules[] = 'field_ui';
  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. // Add text formats Directly.
  39. $filtered_html_format = array(
  40. 'format' => 'filtered_html',
  41. 'name' => 'Filtered HTML',
  42. 'weight' => 0,
  43. 'filters' => array(
  44. // URL filter.
  45. 'filter_url' => array(
  46. 'weight' => 0,
  47. 'status' => 1,
  48. ),
  49. // HTML filter.
  50. 'filter_html' => array(
  51. 'weight' => 1,
  52. 'status' => 1,
  53. ),
  54. // Line break filter.
  55. 'filter_autop' => array(
  56. 'weight' => 2,
  57. 'status' => 1,
  58. ),
  59. // HTML corrector filter.
  60. 'filter_htmlcorrector' => array(
  61. 'weight' => 10,
  62. 'status' => 1,
  63. ),
  64. ),
  65. );
  66. $filtered_html_format = (object) $filtered_html_format;
  67. filter_format_save($filtered_html_format);
  68. // Build the list of required administration permissions. Additional
  69. // permissions can be passed as an array into setUp()'s second parameter.
  70. if (isset($args[1]) && is_array($args[1])) {
  71. $permissions = $args[1];
  72. }
  73. else {
  74. $permissions = array();
  75. }
  76. $permissions[] = 'access content';
  77. $permissions[] = 'administer site configuration';
  78. $permissions[] = 'administer content types';
  79. $permissions[] = 'administer nodes';
  80. $permissions[] = 'bypass node access';
  81. $permissions[] = 'administer taxonomy';
  82. $permissions[] = 'administer users';
  83. $permissions[] = 'administer feeds';
  84. $permissions[] = 'administer filters';
  85. // Create an admin user and log in.
  86. $this->admin_user = $this->drupalCreateUser($permissions);
  87. $this->drupalLogin($this->admin_user);
  88. $types = array(
  89. array(
  90. 'type' => 'page',
  91. 'name' => 'Basic page',
  92. 'node_options[status]' => 1,
  93. 'node_options[promote]' => 0,
  94. ),
  95. array(
  96. 'type' => 'article',
  97. 'name' => 'Article',
  98. 'node_options[status]' => 1,
  99. 'node_options[promote]' => 1,
  100. ),
  101. );
  102. foreach ($types as $type) {
  103. $this->drupalPost('admin/structure/types/add', $type, 'Save content type');
  104. $this->assertText("The content type " . $type['name'] . " has been added.");
  105. }
  106. }
  107. /**
  108. * Absolute path to Drupal root.
  109. */
  110. public function absolute() {
  111. return realpath(getcwd());
  112. }
  113. /**
  114. * Get the absolute directory path of the feeds module.
  115. */
  116. public function absolutePath() {
  117. return $this->absolute() . '/' . drupal_get_path('module', 'feeds');
  118. }
  119. /**
  120. * Generate an OPML test feed.
  121. *
  122. * The purpose of this function is to create a dynamic OPML feed that points
  123. * to feeds included in this test.
  124. */
  125. public function generateOPML() {
  126. $path = $GLOBALS['base_url'] . '/' . drupal_get_path('module', 'feeds') . '/tests/feeds/';
  127. $output =
  128. '<?xml version="1.0" encoding="utf-8"?>
  129. <opml version="1.1">
  130. <head>
  131. <title>Feeds test OPML</title>
  132. <dateCreated>Fri, 16 Oct 2009 02:53:17 GMT</dateCreated>
  133. <ownerName></ownerName>
  134. </head>
  135. <body>
  136. <outline text="Feeds test group" >
  137. <outline title="Development Seed - Technological Solutions for Progressive Organizations" text="" xmlUrl="' . $path . 'developmentseed.rss2" type="rss" />
  138. <outline title="Magyar Nemzet Online - H\'rek" text="" xmlUrl="' . $path . 'feed_without_guid.rss2" type="rss" />
  139. <outline title="Drupal planet" text="" type="rss" xmlUrl="' . $path . 'drupalplanet.rss2" />
  140. </outline>
  141. </body>
  142. </opml>';
  143. // UTF 8 encode output string and write it to disk
  144. $output = utf8_encode($output);
  145. $filename = file_default_scheme() . '://test-opml-' . $this->randomName() . '.opml';
  146. $filename = file_unmanaged_save_data($output, $filename);
  147. return $filename;
  148. }
  149. /**
  150. * Create an importer configuration.
  151. *
  152. * @param $name
  153. * The natural name of the feed.
  154. * @param $id
  155. * The persistent id of the feed.
  156. * @param $edit
  157. * Optional array that defines the basic settings for the feed in a format
  158. * that can be posted to the feed's basic settings form.
  159. */
  160. public function createImporterConfiguration($name = 'Syndication', $id = 'syndication') {
  161. // Create new feed configuration.
  162. $this->drupalGet('admin/structure/feeds');
  163. $this->clickLink('Add importer');
  164. $edit = array(
  165. 'name' => $name,
  166. 'id' => $id,
  167. );
  168. $this->drupalPost('admin/structure/feeds/create', $edit, 'Create');
  169. // Assert message and presence of default plugins.
  170. $this->assertText('Your configuration has been created with default settings.');
  171. $this->assertPlugins($id, 'FeedsHTTPFetcher', 'FeedsSyndicationParser', 'FeedsNodeProcessor');
  172. // Per default attach to page content type.
  173. $this->setSettings($id, NULL, array('content_type' => 'page'));
  174. // Per default attached to article content type.
  175. $this->setSettings($id, 'FeedsNodeProcessor', array('bundle' => 'article'));
  176. }
  177. /**
  178. * Choose a plugin for a importer configuration and assert it.
  179. *
  180. * @param $id
  181. * The importer configuration's id.
  182. * @param $plugin_key
  183. * The key string of the plugin to choose (one of the keys defined in
  184. * feeds_feeds_plugins()).
  185. */
  186. public function setPlugin($id, $plugin_key) {
  187. if ($type = FeedsPlugin::typeOf($plugin_key)) {
  188. $edit = array(
  189. 'plugin_key' => $plugin_key,
  190. );
  191. $this->drupalPost("admin/structure/feeds/$id/$type", $edit, 'Save');
  192. // Assert actual configuration.
  193. $config = unserialize(db_query("SELECT config FROM {feeds_importer} WHERE id = :id", array(':id' => $id))->fetchField());
  194. $this->assertEqual($config[$type]['plugin_key'], $plugin_key, 'Verified correct ' . $type . ' (' . $plugin_key . ').');
  195. }
  196. }
  197. /**
  198. * Set importer or plugin settings.
  199. *
  200. * @param $id
  201. * The importer configuration's id.
  202. * @param $plugin
  203. * The plugin (class) name, or NULL to set importer's settings
  204. * @param $settings
  205. * The settings to set.
  206. */
  207. public function setSettings($id, $plugin, $settings) {
  208. $this->drupalPost('admin/structure/feeds/' . $id . '/settings/' . $plugin, $settings, 'Save');
  209. $this->assertText('Your changes have been saved.');
  210. }
  211. /**
  212. * Create a test feed node. Test user has to have sufficient permissions:
  213. *
  214. * * create [type] content
  215. * * use feeds
  216. *
  217. * Assumes that page content type has been configured with
  218. * createImporterConfiguration() as a feed content type.
  219. *
  220. * @return
  221. * The node id of the node created.
  222. */
  223. public function createFeedNode($id = 'syndication', $feed_url = NULL, $title = '', $content_type = NULL) {
  224. if (empty($feed_url)) {
  225. $feed_url = $GLOBALS['base_url'] . '/' . drupal_get_path('module', 'feeds') . '/tests/feeds/developmentseed.rss2';
  226. }
  227. // If content type not given, retrieve it.
  228. if (!$content_type) {
  229. $result= db_select('feeds_importer', 'f')
  230. ->condition('f.id', $id, '=')
  231. ->fields('f', array('config'))
  232. ->execute();
  233. $config = unserialize($result->fetchField());
  234. $content_type = $config['content_type'];
  235. $this->assertFalse(empty($content_type), 'Valid content type found: ' . $content_type);
  236. }
  237. // Create a feed node.
  238. $edit = array(
  239. 'title' => $title,
  240. 'feeds[FeedsHTTPFetcher][source]' => $feed_url,
  241. );
  242. $this->drupalPost('node/add/' . str_replace('_', '-', $content_type), $edit, 'Save');
  243. $this->assertText('has been created.');
  244. // Get the node id from URL.
  245. $nid = $this->getNid($this->getUrl());
  246. // Check whether feed got recorded in feeds_source table.
  247. $query = db_select('feeds_source', 's')
  248. ->condition('s.id', $id, '=')
  249. ->condition('s.feed_nid', $nid, '=');
  250. $query->addExpression("COUNT(*)");
  251. $result = $query->execute()->fetchField();
  252. $this->assertEqual(1, $result);
  253. $source = db_select('feeds_source', 's')
  254. ->condition('s.id', $id, '=')
  255. ->condition('s.feed_nid', $nid, '=')
  256. ->fields('s', array('config'))
  257. ->execute()->fetchObject();
  258. $config = unserialize($source->config);
  259. $this->assertEqual($config['FeedsHTTPFetcher']['source'], $feed_url, t('URL in DB correct.'));
  260. return $nid;
  261. }
  262. /**
  263. * Edit the configuration of a feed node to test update behavior.
  264. *
  265. * @param $nid
  266. * The nid to edit.
  267. * @param $feed_url
  268. * The new (absolute) feed URL to use.
  269. * @param $title
  270. * Optional parameter to change title of feed node.
  271. */
  272. public function editFeedNode($nid, $feed_url, $title = '') {
  273. $edit = array(
  274. 'title' => $title,
  275. 'feeds[FeedsHTTPFetcher][source]' => $feed_url,
  276. );
  277. // Check that the update was saved.
  278. $this->drupalPost('node/' . $nid . '/edit', $edit, 'Save');
  279. $this->assertText('has been updated.');
  280. // Check that the URL was updated in the feeds_source table.
  281. $source = db_query("SELECT * FROM {feeds_source} WHERE feed_nid = :nid", array(':nid' => $nid))->fetchObject();
  282. $config = unserialize($source->config);
  283. $this->assertEqual($config['FeedsHTTPFetcher']['source'], $feed_url, t('URL in DB correct.'));
  284. }
  285. /**
  286. * Batch create a variable amount of feed nodes. All will have the
  287. * same URL configured.
  288. *
  289. * @return
  290. * An array of node ids of the nodes created.
  291. */
  292. public function createFeedNodes($id = 'syndication', $num = 20, $content_type = NULL) {
  293. $nids = array();
  294. for ($i = 0; $i < $num; $i++) {
  295. $nids[] = $this->createFeedNode($id, NULL, $this->randomName(), $content_type);
  296. }
  297. return $nids;
  298. }
  299. /**
  300. * Import a URL through the import form. Assumes FeedsHTTPFetcher in place.
  301. */
  302. public function importURL($id, $feed_url = NULL) {
  303. if (empty($feed_url)) {
  304. $feed_url = $GLOBALS['base_url'] . '/' . drupal_get_path('module', 'feeds') . '/tests/feeds/developmentseed.rss2';
  305. }
  306. $edit = array(
  307. 'feeds[FeedsHTTPFetcher][source]' => $feed_url,
  308. );
  309. $nid = $this->drupalPost('import/' . $id, $edit, 'Import');
  310. // Check whether feed got recorded in feeds_source table.
  311. $this->assertEqual(1, db_query("SELECT COUNT(*) FROM {feeds_source} WHERE id = :id AND feed_nid = 0", array(':id' => $id))->fetchField());
  312. $source = db_query("SELECT * FROM {feeds_source} WHERE id = :id AND feed_nid = 0", array(':id' => $id))->fetchObject();
  313. $config = unserialize($source->config);
  314. $this->assertEqual($config['FeedsHTTPFetcher']['source'], $feed_url, t('URL in DB correct.'));
  315. // Check whether feed got properly added to scheduler.
  316. $this->assertEqual(1, db_query("SELECT COUNT(*) FROM {job_schedule} WHERE type = :id AND id = 0 AND name = 'feeds_source_import' AND last <> 0 AND scheduled = 0", array(':id' => $id))->fetchField());
  317. // Check expire scheduler.
  318. if (feeds_importer($id)->processor->expiryTime() == FEEDS_EXPIRE_NEVER) {
  319. $this->assertEqual(0, db_query("SELECT COUNT(*) FROM {job_schedule} WHERE type = :id AND id = 0 AND name = 'feeds_source_expire'", array(':id' => $id))->fetchField());
  320. }
  321. else {
  322. $this->assertEqual(1, db_query("SELECT COUNT(*) FROM {job_schedule} WHERE type = :id AND id = 0 AND name = 'feeds_source_expire'", array(':id' => $id))->fetchField());
  323. }
  324. }
  325. /**
  326. * Import a file through the import form. Assumes FeedsFileFetcher in place.
  327. */
  328. public function importFile($id, $file) {
  329. $this->assertTrue(file_exists($file), 'Source file exists');
  330. $edit = array(
  331. 'files[feeds]' => $file,
  332. );
  333. $this->drupalPost('import/' . $id, $edit, 'Import');
  334. }
  335. /**
  336. * Assert a feeds configuration's plugins.
  337. *
  338. * @deprecated:
  339. * Use setPlugin() instead.
  340. *
  341. * @todo Refactor users of assertPlugin() and make them use setPugin() instead.
  342. */
  343. public function assertPlugins($id, $fetcher, $parser, $processor) {
  344. // Assert actual configuration.
  345. $config = unserialize(db_query("SELECT config FROM {feeds_importer} WHERE id = :id", array(':id' => $id))->fetchField());
  346. $this->assertEqual($config['fetcher']['plugin_key'], $fetcher, 'Correct fetcher');
  347. $this->assertEqual($config['parser']['plugin_key'], $parser, 'Correct parser');
  348. $this->assertEqual($config['processor']['plugin_key'], $processor, 'Correct processor');
  349. }
  350. /**
  351. * Overrides DrupalWebTestCase::assertFieldByXPath().
  352. *
  353. * The core version has a bug, this is the D8 version.
  354. *
  355. * @todo Remove once https://drupal.org/node/2105617 lands.
  356. */
  357. protected function assertFieldByXPath($xpath, $value = NULL, $message = '', $group = 'Other') {
  358. $fields = $this->xpath($xpath);
  359. // If value specified then check array for match.
  360. $found = TRUE;
  361. if (isset($value)) {
  362. $found = FALSE;
  363. if ($fields) {
  364. foreach ($fields as $field) {
  365. if (isset($field['value']) && $field['value'] == $value) {
  366. // Input element with correct value.
  367. $found = TRUE;
  368. }
  369. elseif (isset($field->option) || isset($field->optgroup)) {
  370. // Select element found.
  371. $selected = $this->getSelectedItem($field);
  372. if ($selected === FALSE) {
  373. // No item selected so use first item.
  374. $items = $this->getAllOptions($field);
  375. if (!empty($items) && $items[0]['value'] == $value) {
  376. $found = TRUE;
  377. }
  378. }
  379. elseif ($selected == $value) {
  380. $found = TRUE;
  381. }
  382. }
  383. elseif ((string) $field == $value) {
  384. // Text area with correct text.
  385. $found = TRUE;
  386. }
  387. }
  388. }
  389. }
  390. return $this->assertTrue($fields && $found, $message, $group);
  391. }
  392. /**
  393. * Adds mappings to a given configuration.
  394. *
  395. * @param string $id
  396. * ID of the importer.
  397. * @param array $mappings
  398. * An array of mapping arrays. Each mapping array must have a source and
  399. * an target key and can have a unique key.
  400. * @param bool $test_mappings
  401. * (optional) TRUE to automatically test mapping configs. Defaults to TRUE.
  402. */
  403. public function addMappings($id, array $mappings, $test_mappings = TRUE) {
  404. $path = "admin/structure/feeds/$id/mapping";
  405. // Iterate through all mappings and add the mapping via the form.
  406. foreach ($mappings as $i => $mapping) {
  407. if ($test_mappings) {
  408. $current_mapping_key = $this->mappingExists($id, $i, $mapping['source'], $mapping['target']);
  409. $this->assertEqual($current_mapping_key, -1, 'Mapping does not exist before addition.');
  410. }
  411. // Get unique flag and unset it. Otherwise, drupalPost will complain that
  412. // Split up config and mapping.
  413. $config = $mapping;
  414. unset($config['source'], $config['target']);
  415. $mapping = array('source' => $mapping['source'], 'target' => $mapping['target']);
  416. // Add mapping.
  417. $this->drupalPost($path, $mapping, t('Save'));
  418. // If there are other configuration options, set them.
  419. if ($config) {
  420. $this->drupalPostAJAX(NULL, array(), 'mapping_settings_edit_' . $i);
  421. // Set some settings.
  422. $edit = array();
  423. foreach ($config as $key => $value) {
  424. $edit["config[$i][settings][$key]"] = $value;
  425. }
  426. $this->drupalPostAJAX(NULL, $edit, 'mapping_settings_update_' . $i);
  427. $this->drupalPost(NULL, array(), t('Save'));
  428. }
  429. if ($test_mappings) {
  430. $current_mapping_key = $this->mappingExists($id, $i, $mapping['source'], $mapping['target']);
  431. $this->assertTrue($current_mapping_key >= 0, 'Mapping exists after addition.');
  432. }
  433. }
  434. }
  435. /**
  436. * Remove mappings from a given configuration.
  437. *
  438. * @param string $id
  439. * ID of the importer.
  440. * @param array $mappings
  441. * An array of mapping arrays. Each mapping array must have a source and
  442. * a target key and can have a unique key.
  443. * @param bool $test_mappings
  444. * (optional) TRUE to automatically test mapping configs. Defaults to TRUE.
  445. */
  446. public function removeMappings($id, array $mappings, $test_mappings = TRUE) {
  447. $path = "admin/structure/feeds/$id/mapping";
  448. $edit = array();
  449. // Iterate through all mappings and remove via the form.
  450. foreach ($mappings as $i => $mapping) {
  451. if ($test_mappings) {
  452. $current_mapping_key = $this->mappingExists($id, $i, $mapping['source'], $mapping['target']);
  453. $this->assertEqual($current_mapping_key, $i, 'Mapping exists before removal.');
  454. }
  455. $edit["remove_flags[$i]"] = 1;
  456. }
  457. $this->drupalPost($path, $edit, t('Save'));
  458. $this->assertText('Your changes have been saved.');
  459. }
  460. /**
  461. * Gets an array of current mappings from the feeds_importer config.
  462. *
  463. * @param string $id
  464. * ID of the importer.
  465. *
  466. * @return bool|array
  467. * FALSE if the importer has no mappings, or an an array of mappings.
  468. */
  469. public function getCurrentMappings($id) {
  470. $config = db_query("SELECT config FROM {feeds_importer} WHERE id = :id", array(':id' => $id))->fetchField();
  471. $config = unserialize($config);
  472. // We are very specific here. 'mappings' can either be an array or not
  473. // exist.
  474. if (array_key_exists('mappings', $config['processor']['config'])) {
  475. $this->assertTrue(is_array($config['processor']['config']['mappings']), 'Mappings is an array.');
  476. return $config['processor']['config']['mappings'];
  477. }
  478. return FALSE;
  479. }
  480. /**
  481. * Determines if a mapping exists for a given importer.
  482. *
  483. * @param string $id
  484. * ID of the importer.
  485. * @param integer $i
  486. * The key of the mapping.
  487. * @param string $source
  488. * The source field.
  489. * @param string $target
  490. * The target field.
  491. *
  492. * @return integer
  493. * -1 if the mapping doesn't exist, the key of the mapping otherwise.
  494. */
  495. public function mappingExists($id, $i, $source, $target) {
  496. $current_mappings = $this->getCurrentMappings($id);
  497. if ($current_mappings) {
  498. foreach ($current_mappings as $key => $mapping) {
  499. if ($mapping['source'] == $source && $mapping['target'] == $target && $key == $i) {
  500. return $key;
  501. }
  502. }
  503. }
  504. return -1;
  505. }
  506. /**
  507. * Helper function, retrieves node id from a URL.
  508. */
  509. public function getNid($url) {
  510. $matches = array();
  511. preg_match('/node\/(\d+?)$/', $url, $matches);
  512. $nid = $matches[1];
  513. // Test for actual integerness.
  514. $this->assertTrue($nid === (string) (int) $nid, 'Node id is an integer.');
  515. return $nid;
  516. }
  517. /**
  518. * Copies a directory.
  519. */
  520. public function copyDir($source, $dest) {
  521. $result = file_prepare_directory($dest, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
  522. foreach (@scandir($source) as $file) {
  523. if (is_file("$source/$file")) {
  524. $file = file_unmanaged_copy("$source/$file", "$dest/$file");
  525. }
  526. }
  527. }
  528. /**
  529. * Download and extract SimplePIE.
  530. *
  531. * Sets the 'feeds_simplepie_library_dir' variable to the directory where
  532. * SimplePie is downloaded.
  533. */
  534. function downloadExtractSimplePie($version) {
  535. $url = "http://simplepie.org/downloads/simplepie_$version.mini.php";
  536. $filename = 'simplepie.mini.php';
  537. // Avoid downloading the file dozens of times
  538. $library_dir = DRUPAL_ROOT . '/' . $this->originalFileDirectory . '/simpletest/feeds';
  539. $simplepie_library_dir = $library_dir . '/simplepie';
  540. if (!file_exists($library_dir)) {
  541. drupal_mkdir($library_dir);
  542. }
  543. if (!file_exists($simplepie_library_dir)) {
  544. drupal_mkdir($simplepie_library_dir);
  545. }
  546. // Local file name.
  547. $local_file = $simplepie_library_dir . '/' . $filename;
  548. // Begin single threaded code.
  549. if (function_exists('sem_get')) {
  550. $semaphore = sem_get(ftok(__FILE__, 1));
  551. sem_acquire($semaphore);
  552. }
  553. // Download and extact the archive, but only in one thread.
  554. if (!file_exists($local_file)) {
  555. $local_file = system_retrieve_file($url, $local_file, FALSE, FILE_EXISTS_REPLACE);
  556. }
  557. if (function_exists('sem_get')) {
  558. sem_release($semaphore);
  559. }
  560. // End single threaded code.
  561. // Verify that files were successfully extracted.
  562. $this->assertTrue(file_exists($local_file), t('@file found.', array('@file' => $local_file)));
  563. // Set the simpletest library directory.
  564. variable_set('feeds_library_dir', $library_dir);
  565. }
  566. }
  567. /**
  568. * Provides a wrapper for DrupalUnitTestCase for Feeds unit testing.
  569. */
  570. class FeedsUnitTestHelper extends DrupalUnitTestCase {
  571. public function setUp() {
  572. parent::setUp();
  573. // Manually include the feeds module.
  574. // @todo Allow an array of modules from the child class.
  575. drupal_load('module', 'feeds');
  576. }
  577. }
  578. class FeedsUnitTestCase extends FeedsUnitTestHelper {
  579. public static function getInfo() {
  580. return array(
  581. 'name' => 'Unit tests',
  582. 'description' => 'Test basic low-level Feeds module functionality.',
  583. 'group' => 'Feeds',
  584. );
  585. }
  586. /**
  587. * Test valid absolute urls.
  588. *
  589. * @see ValidUrlTestCase
  590. *
  591. * @todo Remove when http://drupal.org/node/1191252 is fixed.
  592. */
  593. function testFeedsValidURL() {
  594. $url_schemes = array('http', 'https', 'ftp', 'feed', 'webcal');
  595. $valid_absolute_urls = array(
  596. 'example.com',
  597. 'www.example.com',
  598. 'ex-ample.com',
  599. '3xampl3.com',
  600. 'example.com/paren(the)sis',
  601. 'example.com/index.html#pagetop',
  602. 'example.com:8080',
  603. 'subdomain.example.com',
  604. 'example.com/index.php?q=node',
  605. 'example.com/index.php?q=node&param=false',
  606. 'user@www.example.com',
  607. 'user:pass@www.example.com:8080/login.php?do=login&style=%23#pagetop',
  608. '127.0.0.1',
  609. 'example.org?',
  610. 'john%20doe:secret:foo@example.org/',
  611. 'example.org/~,$\'*;',
  612. 'caf%C3%A9.example.org',
  613. '[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]:80/index.html',
  614. 'graph.asfdasdfasdf.com/blarg/feed?access_token=133283760145143|tGew8jbxi1ctfVlYh35CPYij1eE',
  615. );
  616. foreach ($url_schemes as $scheme) {
  617. foreach ($valid_absolute_urls as $url) {
  618. $test_url = $scheme . '://' . $url;
  619. $valid_url = feeds_valid_url($test_url, TRUE);
  620. $this->assertTrue($valid_url, t('@url is a valid url.', array('@url' => $test_url)));
  621. }
  622. }
  623. $invalid_ablosule_urls = array(
  624. '',
  625. 'ex!ample.com',
  626. 'ex%ample.com',
  627. );
  628. foreach ($url_schemes as $scheme) {
  629. foreach ($invalid_ablosule_urls as $url) {
  630. $test_url = $scheme . '://' . $url;
  631. $valid_url = feeds_valid_url($test_url, TRUE);
  632. $this->assertFalse($valid_url, t('@url is NOT a valid url.', array('@url' => $test_url)));
  633. }
  634. }
  635. }
  636. }