ImportTest.php 16 KB

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