common_syndication_parser.test 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. /**
  3. * @file
  4. * Tests for the common syndication parser.
  5. */
  6. /**
  7. * Test cases for common syndication parser library.
  8. *
  9. * @todo Break out into Drupal independent test framework.
  10. * @todo Could I use DrupalUnitTestCase here?
  11. */
  12. class CommonSyndicationParserTestCase extends DrupalWebTestCase {
  13. public static function getInfo() {
  14. return array(
  15. 'name' => 'Common Syndication Parser',
  16. 'description' => 'Unit tests for Common Syndication Parser.',
  17. 'group' => 'Feeds',
  18. );
  19. }
  20. public function setUp() {
  21. parent::setUp(array('feeds', 'feeds_ui', 'ctools', 'job_scheduler'));
  22. feeds_include_library('common_syndication_parser.inc', 'common_syndication_parser');
  23. }
  24. /**
  25. * Dispatch tests, only use one entry point method testX to save time.
  26. */
  27. public function test() {
  28. $this->_testRSS10();
  29. $this->_testRSS2();
  30. $this->_testAtomGeoRSS();
  31. $this->_testAtomGeoRSSWithoutAuthor();
  32. }
  33. /**
  34. * Test RSS 1.0.
  35. */
  36. protected function _testRSS10() {
  37. $string = $this->readFeed('magento.rss1');
  38. $feed = common_syndication_parser_parse($string);
  39. $this->assertEqual($feed['title'], 'Magento Sites Network - A directory listing of Magento Commerce stores');
  40. $this->assertEqual($feed['items'][0]['title'], 'Gezondheidswebwinkel');
  41. $this->assertEqual($feed['items'][0]['url'], 'http://www.magentosites.net/store/2010/04/28/gezondheidswebwinkel/index.html');
  42. $this->assertEqual($feed['items'][1]['url'], 'http://www.magentosites.net/store/2010/04/26/mybobinocom/index.html');
  43. $this->assertEqual($feed['items'][1]['guid'], 'http://www.magentosites.net/node/3472');
  44. $this->assertEqual($feed['items'][2]['guid'], 'http://www.magentosites.net/node/3471');
  45. $this->assertEqual($feed['items'][2]['timestamp'], 1272285294);
  46. }
  47. /**
  48. * Test RSS 2.
  49. */
  50. protected function _testRSS2() {
  51. $string = $this->readFeed('developmentseed.rss2');
  52. $feed = common_syndication_parser_parse($string);
  53. $this->assertEqual($feed['title'], 'Development Seed - Technological Solutions for Progressive Organizations');
  54. $this->assertEqual($feed['items'][0]['title'], 'Open Atrium Translation Workflow: Two Way Translation Updates');
  55. $this->assertEqual($feed['items'][1]['url'], 'http://developmentseed.org/blog/2009/oct/05/week-dc-tech-october-5th-edition');
  56. $this->assertEqual($feed['items'][1]['guid'], '973 at http://developmentseed.org');
  57. $this->assertEqual($feed['items'][2]['guid'], '972 at http://developmentseed.org');
  58. $this->assertEqual($feed['items'][2]['timestamp'], 1254493864);
  59. }
  60. /**
  61. * Test Geo RSS in Atom feed.
  62. */
  63. protected function _testAtomGeoRSS() {
  64. $string = $this->readFeed('earthquake-georss.atom');
  65. $feed = common_syndication_parser_parse($string);
  66. $this->assertEqual($feed['title'], 'USGS M2.5+ Earthquakes');
  67. $this->assertEqual($feed['items'][0]['title'], 'M 2.6, Central Alaska');
  68. $this->assertEqual($feed['items'][1]['url'], 'http://earthquake.usgs.gov/earthquakes/recenteqsww/Quakes/us2010axbz.php');
  69. $this->assertEqual($feed['items'][1]['guid'], 'urn:earthquake-usgs-gov:us:2010axbz');
  70. $this->assertEqual($feed['items'][2]['guid'], 'urn:earthquake-usgs-gov:us:2010axbr');
  71. $this->assertEqual($feed['items'][2]['geolocations'][0]['name'], '-53.1979 -118.0676');
  72. $this->assertEqual($feed['items'][2]['geolocations'][0]['lat'], '-53.1979');
  73. $this->assertEqual($feed['items'][2]['geolocations'][0]['lon'], '-118.0676');
  74. $this->assertEqual($feed['items'][3]['geolocations'][0]['name'], '-43.4371 172.5902');
  75. $this->assertEqual($feed['items'][3]['geolocations'][0]['lat'], '-43.4371');
  76. $this->assertEqual($feed['items'][3]['geolocations'][0]['lon'], '172.5902');
  77. }
  78. /**
  79. * Tests parsing an Atom feed without an author.
  80. */
  81. protected function _testAtomGeoRSSWithoutAuthor() {
  82. $string = $this->readFeed('earthquake-georss-noauthor.atom');
  83. $feed = common_syndication_parser_parse($string);
  84. }
  85. /**
  86. * Helper to read a feed.
  87. */
  88. protected function readFeed($filename) {
  89. $feed = dirname(__FILE__) . '/feeds/' . $filename;
  90. $handle = fopen($feed, 'r');
  91. $string = fread($handle, filesize($feed));
  92. fclose($handle);
  93. return $string;
  94. }
  95. }