ImportTest.php 15 KB

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