ImportTest.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Feed
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * Test helper
  24. */
  25. require_once dirname(__FILE__) . '/../../TestHelper.php';
  26. /**
  27. * @see Zend_Feed
  28. */
  29. require_once 'Zend/Feed.php';
  30. /**
  31. * @see Zend_Feed_Builder
  32. */
  33. require_once 'Zend/Feed/Builder.php';
  34. /**
  35. * @see Zend_Http_Client_Adapter_Test
  36. */
  37. require_once 'Zend/Http/Client/Adapter/Test.php';
  38. /**
  39. * @see Zend_Http_Client
  40. */
  41. require_once 'Zend/Http/Client.php';
  42. /**
  43. * @category Zend
  44. * @package Zend_Feed
  45. * @subpackage UnitTests
  46. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  47. * @license http://framework.zend.com/license/new-bsd New BSD License
  48. * @group Zend_Feed
  49. */
  50. class Zend_Feed_ImportTest extends PHPUnit_Framework_TestCase
  51. {
  52. protected $_client;
  53. protected $_feedDir;
  54. /**
  55. * HTTP client test adapter
  56. *
  57. * @var Zend_Http_Client_Adapter_Test
  58. */
  59. protected $_adapter;
  60. public function setUp()
  61. {
  62. $this->_adapter = new Zend_Http_Client_Adapter_Test();
  63. Zend_Feed::setHttpClient(new Zend_Http_Client(null, array('adapter' => $this->_adapter)));
  64. $this->_client = Zend_Feed::getHttpClient();
  65. $this->_feedDir = dirname(__FILE__) . '/_files';
  66. }
  67. /**
  68. * Test an atom feed generated by google's Blogger platform
  69. */
  70. public function testAtomGoogle()
  71. {
  72. $this->_importAtomValid('AtomTestGoogle.xml');
  73. }
  74. /**
  75. * Test an atom feed generated by mozillaZine.org
  76. */
  77. public function testAtomMozillazine()
  78. {
  79. $this->_importAtomValid('AtomTestMozillazine.xml');
  80. }
  81. /**
  82. * Test an atom feed generated by O'Reilly
  83. */
  84. public function testAtomOReilly()
  85. {
  86. $this->_importAtomValid('AtomTestOReilly.xml');
  87. }
  88. /**
  89. * Test an atom feed generated by PlanetPHP
  90. */
  91. public function testAtomPlanetPHP()
  92. {
  93. $this->_importAtomValid('AtomTestPlanetPHP.xml');
  94. }
  95. /**
  96. * Test a small atom feed
  97. */
  98. public function testAtomSample1()
  99. {
  100. $this->_importAtomValid('AtomTestSample1.xml');
  101. }
  102. /**
  103. * Test a small atom feed without any entries
  104. */
  105. public function testAtomSample2()
  106. {
  107. $this->_importAtomValid('AtomTestSample2.xml');
  108. }
  109. /**
  110. * Test an atom feed with a </entry> tag missing
  111. */
  112. public function testAtomSample3()
  113. {
  114. $this->_importInvalid('AtomTestSample3.xml');
  115. }
  116. /**
  117. * Test an atom feed with links within entries
  118. */
  119. public function testAtomSample4()
  120. {
  121. $this->_importAtomValid('AtomTestSample4.xml');
  122. }
  123. /**
  124. * Test a RSS feed generated by UserLand Frontier v9.5
  125. */
  126. public function testRssHarvardLaw()
  127. {
  128. $this->_importRssValid('RssTestHarvardLaw.xml');
  129. }
  130. /**
  131. * Test a RSS feed generated by PlanetPHP
  132. */
  133. public function testRssPlanetPHP()
  134. {
  135. $this->_importRssValid('RssTestPlanetPHP.xml');
  136. }
  137. /**
  138. * Test a RSS feed generated by Slashdot
  139. */
  140. public function testRssSlashdot()
  141. {
  142. $this->_importRssValid('RssTestSlashdot.xml');
  143. }
  144. /**
  145. * Test a RSS feed generated by CNN
  146. */
  147. public function testRssCNN()
  148. {
  149. $this->_importRssValid('RssTestCNN.xml');
  150. }
  151. /**
  152. * Test a valid RSS 0.91 sample
  153. */
  154. public function testRss091Sample1()
  155. {
  156. $this->_importRssValid('RssTest091Sample1.xml');
  157. }
  158. /**
  159. * Test a valid RSS 0.91 sample
  160. */
  161. public function testRss092Sample1()
  162. {
  163. $this->_importRssValid('RssTest092Sample1.xml');
  164. }
  165. /**
  166. * Test a valid RSS 1.0 sample
  167. */
  168. public function testRss100Sample1()
  169. {
  170. $feed = $this->_importRssValid('RssTest100Sample1.xml');
  171. $this->assertEquals(2, $feed->count());
  172. }
  173. /**
  174. * Test a valid RSS 1.0 sample with some extensions in it
  175. */
  176. public function testRss100Sample2()
  177. {
  178. $feed = $this->_importRssValid('RssTest100Sample2.xml');
  179. $this->assertEquals(1, $feed->count());
  180. }
  181. /**
  182. * Test a valid RSS 2.0 sample
  183. */
  184. public function testRss200Sample1()
  185. {
  186. $this->_importRssValid('RssTest200Sample1.xml');
  187. }
  188. /**
  189. * Test the import of a RSS feed from an array
  190. */
  191. public function testRssImportFullArray()
  192. {
  193. $feed = Zend_Feed::importArray($this->_getFullArray(), 'rss');
  194. $this->assertType('Zend_Feed_Rss', $feed);
  195. }
  196. /**
  197. * Test the import of a RSS feed from an array
  198. * @group ZF-5833
  199. */
  200. public function testRssImportSetsIsPermaLinkAsFalseIfGuidNotAUri()
  201. {
  202. $feed = Zend_Feed::importArray($this->_getFullArray(), 'rss');
  203. $entry = $feed->current();
  204. $this->assertEquals('false', $entry->guid['isPermaLink']);
  205. }
  206. /**
  207. * Test the import of a RSS feed from an array
  208. */
  209. public function testAtomImportFullArray()
  210. {
  211. $feed = Zend_Feed::importArray($this->_getFullArray(), 'atom');
  212. }
  213. /**
  214. * Test the import of a RSS feed from a builder
  215. */
  216. public function testRssImportFullBuilder()
  217. {
  218. $feed = Zend_Feed::importBuilder(new Zend_Feed_Builder($this->_getFullArray()), 'rss');
  219. $this->assertType('Zend_Feed_Rss', $feed);
  220. }
  221. /**
  222. * Test the import of a full iTunes RSS feed from a builder
  223. */
  224. public function testRssImportFulliTunesBuilder()
  225. {
  226. $array = $this->_getFullArray();
  227. $array['itunes']['author'] = 'iTunes Author';
  228. $array['itunes']['owner'] = array('name' => 'iTunes Owner',
  229. 'email' => 'itunes@example.com');
  230. $array['itunes']['image'] = 'http://www.example/itunes.png';
  231. $array['itunes']['subtitle'] = 'iTunes subtitle';
  232. $array['itunes']['summary'] = 'iTunes summary';
  233. $array['itunes']['explicit'] = 'clean';
  234. $array['itunes']['block'] = 'no';
  235. $array['itunes']['new-feed-url'] = 'http://www.example/itunes.xml';
  236. $feed = Zend_Feed::importBuilder(new Zend_Feed_Builder($array), 'rss');
  237. $this->assertType('Zend_Feed_Rss', $feed);
  238. }
  239. /**
  240. * Test the import of an Atom feed from a builder
  241. */
  242. public function testAtomImportFullBuilder()
  243. {
  244. $feed = Zend_Feed::importBuilder(new Zend_Feed_Builder($this->_getFullArray()), 'atom');
  245. }
  246. /**
  247. * Test the import of an Atom feed from a builder
  248. */
  249. public function testAtomImportFullBuilderValid()
  250. {
  251. $feed = Zend_Feed::importBuilder(new Zend_Feed_Builder($this->_getFullArray()), 'atom');
  252. $feed = Zend_Feed::importString($feed->saveXml());
  253. $this->assertType('Zend_Feed_Atom', $feed);
  254. }
  255. /**
  256. * Check the validity of the builder import (rss)
  257. */
  258. public function testRssImportFullBuilderValid()
  259. {
  260. $feed = Zend_Feed::importBuilder(new Zend_Feed_Builder($this->_getFullArray()), 'rss');
  261. $this->assertType('Zend_Feed_Rss', $feed);
  262. $feed = Zend_Feed::importString($feed->saveXml());
  263. $this->assertType('Zend_Feed_Rss', $feed);
  264. }
  265. /**
  266. * Test the return of a link() call (atom)
  267. */
  268. public function testAtomGetLink()
  269. {
  270. $feed = Zend_Feed::importBuilder(new Zend_Feed_Builder($this->_getFullArray()), 'atom');
  271. $this->assertType('Zend_Feed_Atom', $feed);
  272. $feed = Zend_Feed::importString($feed->saveXml());
  273. $this->assertType('Zend_Feed_Atom', $feed);
  274. $href = $feed->link('self');
  275. $this->assertEquals('http://www.example.com', $href);
  276. }
  277. /**
  278. * Imports an invalid feed and ensure everything works as expected
  279. * even if XDebug is running (ZF-2590).
  280. */
  281. public function testImportInvalidIsXdebugAware()
  282. {
  283. if (!function_exists('xdebug_is_enabled')) {
  284. $this->markTestIncomplete('XDebug not installed');
  285. }
  286. $response = new Zend_Http_Response(200, array(), '');
  287. $this->_adapter->setResponse($response);
  288. try {
  289. $feed = Zend_Feed::import('http://localhost');
  290. $this->fail('Expected Zend_Feed_Exception not thrown');
  291. } catch (Zend_Feed_Exception $e) {
  292. $this->assertType('Zend_Feed_Exception', $e);
  293. $this->assertRegExp('/(XDebug is running|Empty string)/', $e->getMessage());
  294. }
  295. }
  296. /**
  297. * Returns the array used by Zend_Feed::importArray
  298. * and Zend_Feed::importBuilder tests
  299. *
  300. * @return array
  301. */
  302. protected function _getFullArray()
  303. {
  304. $array = array('title' => 'Title of the feed',
  305. 'link' => 'http://www.example.com',
  306. 'description' => 'Description of the feed',
  307. 'author' => 'Olivier Sirven',
  308. 'email' => 'olivier@elma.fr',
  309. 'webmaster' => 'olivier@elma.fr',
  310. 'charset' => 'iso-8859-15',
  311. 'lastUpdate' => time(),
  312. 'published' => strtotime('2007-02-27'),
  313. 'copyright' => 'Common Creative',
  314. 'image' => 'http://www.example/images/icon.png',
  315. 'language' => 'en',
  316. 'ttl' => 60,
  317. 'rating' => ' (PICS-1.1 "http://www.gcf.org/v2.5" labels
  318. on "1994.11.05T08:15-0500"
  319. exp "1995.12.31T23:59-0000"
  320. for "http://www.greatdocs.com/foo.html"
  321. by "George Sanderson, Jr."
  322. ratings (suds 0.5 density 0 color/hue 1))',
  323. 'cloud' => array('domain' => 'rpc.sys.com',
  324. 'path' => '/rpc',
  325. 'registerProcedure' => 'webServices.pingMe',
  326. 'protocol' => 'xml-rpc'),
  327. 'textInput' => array('title' => 'subscribe',
  328. 'description' => 'enter your email address to subscribe by mail',
  329. 'name' => 'email',
  330. 'link' => 'http://www.example.com/subscribe'),
  331. 'skipHours' => array(1, 13, 17),
  332. 'skipDays' => array('Saturday', 'Sunday'),
  333. 'itunes' => array('block' => 'no',
  334. 'keywords' => 'example,itunes,podcast',
  335. 'category' => array(array('main' => 'Technology',
  336. 'sub' => 'Gadgets'),
  337. array('main' => 'Music'))),
  338. 'entries' => array(array('guid' => time(),
  339. 'title' => 'First article',
  340. 'link' => 'http://www.example.com',
  341. 'description' => 'First article description',
  342. 'content' => 'First article <strong>content</strong>',
  343. 'lastUpdate' => time(),
  344. 'comments' => 'http://www.example.com/#comments',
  345. 'commentRss' => 'http://www.example.com/comments.xml',
  346. 'source' => array('title' => 'Original title',
  347. 'url' => 'http://www.domain.com'),
  348. 'category' => array(array('term' => 'test category',
  349. 'scheme' => 'http://www.example.com/scheme'),
  350. array('term' => 'another category')
  351. ),
  352. 'enclosure' => array(array('url' => 'http://www.example.com/podcast.mp3',
  353. 'type' => 'audio/mpeg',
  354. 'length' => '12216320'
  355. ),
  356. array('url' => 'http://www.example.com/podcast2.mp3',
  357. 'type' => 'audio/mpeg',
  358. 'length' => '1221632'
  359. )
  360. )
  361. ),
  362. array('title' => 'Second article',
  363. 'link' => 'http://www.example.com/two',
  364. 'description' => 'Second article description',
  365. 'content' => 'Second article <strong>content</strong>',
  366. 'lastUpdate' => time(),
  367. 'comments' => 'http://www.example.com/two/#comments',
  368. 'category' => array(array('term' => 'test category')),
  369. )
  370. )
  371. );
  372. return $array;
  373. }
  374. /**
  375. * Import an invalid atom feed
  376. */
  377. protected function _importAtomValid($filename)
  378. {
  379. $response = new Zend_Http_Response(200, array(), file_get_contents("$this->_feedDir/$filename"));
  380. $this->_adapter->setResponse($response);
  381. $feed = Zend_Feed::import('http://localhost');
  382. $this->assertType('Zend_Feed_Atom', $feed);
  383. }
  384. /**
  385. * Import a valid rss feed
  386. */
  387. protected function _importRssValid($filename)
  388. {
  389. $response = new Zend_Http_Response(200, array(), file_get_contents("$this->_feedDir/$filename"));
  390. $this->_adapter->setResponse($response);
  391. $feed = Zend_Feed::import('http://localhost');
  392. $this->assertType('Zend_Feed_Rss', $feed);
  393. return $feed;
  394. }
  395. /**
  396. * Imports an invalid feed
  397. */
  398. protected function _importInvalid($filename)
  399. {
  400. $response = new Zend_Http_Response(200, array(), file_get_contents("$this->_feedDir/$filename"));
  401. $this->_adapter->setResponse($response);
  402. try {
  403. $feed = Zend_Feed::import('http://localhost');
  404. $this->fail('Expected Zend_Feed_Exception not thrown');
  405. } catch (Zend_Feed_Exception $e) {
  406. $this->assertType('Zend_Feed_Exception', $e);
  407. }
  408. }
  409. /**
  410. * @issue ZF-5903
  411. */
  412. public function testFindFeedsIncludesUriAsArrayKey()
  413. {
  414. if (!defined('TESTS_ZEND_FEED_READER_ONLINE_ENABLED')
  415. || !constant('TESTS_ZEND_FEED_READER_ONLINE_ENABLED')
  416. ) {
  417. $this->markTestSkipped('testFindFeedsIncludesUriAsArrayKey() requires a network connection');
  418. return;
  419. }
  420. Zend_Feed::setHttpClient(new Zend_Http_Client);
  421. $feeds = Zend_Feed::findFeeds('http://www.planet-php.net');
  422. $this->assertEquals(array(
  423. 'http://www.planet-php.org:80/rss/', 'http://www.planet-php.org:80/rdf/'
  424. ), array_keys($feeds));
  425. }
  426. }