ReaderTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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-2009 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. require_once 'PHPUnit/Framework/TestCase.php';
  23. require_once 'Zend/Feed/Reader.php';
  24. require_once 'Zend/Cache.php';
  25. /**
  26. * @category Zend
  27. * @package Zend_Feed
  28. * @subpackage UnitTests
  29. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  30. * @license http://framework.zend.com/license/new-bsd New BSD License
  31. * @group Zend_Feed
  32. * @group Zend_Feed_Reader
  33. */
  34. class Zend_Feed_ReaderTest extends PHPUnit_Framework_TestCase
  35. {
  36. protected $_feedSamplePath = null;
  37. public function setup()
  38. {
  39. $this->_feedSamplePath = dirname(__FILE__) . '/Reader/_files';
  40. }
  41. public function tearDown()
  42. {
  43. Zend_Feed_Reader::reset();
  44. }
  45. public function testDetectsFeedIsRss20()
  46. {
  47. $feed = Zend_Feed_Reader::importString(
  48. file_get_contents($this->_feedSamplePath.'/Reader/rss20.xml'));
  49. $type = Zend_Feed_Reader::detectType($feed);
  50. $this->assertEquals(Zend_Feed_Reader::TYPE_RSS_20, $type);
  51. }
  52. public function testDetectsFeedIsRss094()
  53. {
  54. $feed = Zend_Feed_Reader::importString(
  55. file_get_contents($this->_feedSamplePath.'/Reader/rss094.xml'));
  56. $type = Zend_Feed_Reader::detectType($feed);
  57. $this->assertEquals(Zend_Feed_Reader::TYPE_RSS_094, $type);
  58. }
  59. public function testDetectsFeedIsRss093()
  60. {
  61. $feed = Zend_Feed_Reader::importString(
  62. file_get_contents($this->_feedSamplePath.'/Reader/rss093.xml'));
  63. $type = Zend_Feed_Reader::detectType($feed);
  64. $this->assertEquals(Zend_Feed_Reader::TYPE_RSS_093, $type);
  65. }
  66. public function testDetectsFeedIsRss092()
  67. {
  68. $feed = Zend_Feed_Reader::importString(
  69. file_get_contents($this->_feedSamplePath.'/Reader/rss092.xml'));
  70. $type = Zend_Feed_Reader::detectType($feed);
  71. $this->assertEquals(Zend_Feed_Reader::TYPE_RSS_092, $type);
  72. }
  73. public function testDetectsFeedIsRss091()
  74. {
  75. $feed = Zend_Feed_Reader::importString(
  76. file_get_contents($this->_feedSamplePath.'/Reader/rss091.xml'));
  77. $type = Zend_Feed_Reader::detectType($feed);
  78. $this->assertEquals(Zend_Feed_Reader::TYPE_RSS_091, $type);
  79. }
  80. public function testDetectsFeedIsRss10()
  81. {
  82. $feed = Zend_Feed_Reader::importString(
  83. file_get_contents($this->_feedSamplePath.'/Reader/rss10.xml'));
  84. $type = Zend_Feed_Reader::detectType($feed);
  85. $this->assertEquals(Zend_Feed_Reader::TYPE_RSS_10, $type);
  86. }
  87. public function testDetectsFeedIsRss090()
  88. {
  89. $feed = Zend_Feed_Reader::importString(
  90. file_get_contents($this->_feedSamplePath.'/Reader/rss090.xml'));
  91. $type = Zend_Feed_Reader::detectType($feed);
  92. $this->assertEquals(Zend_Feed_Reader::TYPE_RSS_090, $type);
  93. }
  94. public function testDetectsFeedIsAtom10()
  95. {
  96. $feed = Zend_Feed_Reader::importString(
  97. file_get_contents($this->_feedSamplePath.'/Reader/atom10.xml'));
  98. $type = Zend_Feed_Reader::detectType($feed);
  99. $this->assertEquals(Zend_Feed_Reader::TYPE_ATOM_10, $type);
  100. }
  101. public function testDetectsFeedIsAtom03()
  102. {
  103. $feed = Zend_Feed_Reader::importString(
  104. file_get_contents($this->_feedSamplePath.'/Reader/atom03.xml'));
  105. $type = Zend_Feed_Reader::detectType($feed);
  106. $this->assertEquals(Zend_Feed_Reader::TYPE_ATOM_03, $type);
  107. }
  108. public function testGetEncoding()
  109. {
  110. $feed = Zend_Feed_Reader::importString(
  111. file_get_contents(dirname(__FILE__) . '/Reader/Entry/_files/Atom/title/plain/atom10.xml')
  112. );
  113. $this->assertEquals('utf-8', $feed->getEncoding());
  114. $this->assertEquals('utf-8', $feed->current()->getEncoding());
  115. }
  116. public function testImportsFile()
  117. {
  118. try {
  119. $feed = Zend_Feed_Reader::importFile(
  120. dirname(__FILE__) . '/Reader/Entry/_files/Atom/title/plain/atom10.xml'
  121. );
  122. } catch(Exception $e) {
  123. $this->fail($e->getMessage());
  124. }
  125. }
  126. public function testImportsUri()
  127. {
  128. if (!defined('TESTS_ZEND_FEED_READER_ONLINE_ENABLED')
  129. || !constant('TESTS_ZEND_FEED_READER_ONLINE_ENABLED')
  130. ) {
  131. $this->markTestSkipped('testImportsUri() requires a network connection');
  132. return;
  133. }
  134. try {
  135. $feed = Zend_Feed_Reader::import('http://www.planet-php.net/rdf/');
  136. } catch(Exception $e) {
  137. $this->fail($e->getMessage());
  138. }
  139. }
  140. public function testGetsFeedLinksAsValueObject()
  141. {
  142. if (!defined('TESTS_ZEND_FEED_READER_ONLINE_ENABLED')
  143. || !constant('TESTS_ZEND_FEED_READER_ONLINE_ENABLED')
  144. ) {
  145. $this->markTestSkipped('testGetsFeedLinksAsValueObject() requires a network connection');
  146. return;
  147. }
  148. try {
  149. $links = Zend_Feed_Reader::findFeedLinks('http://www.planet-php.net');
  150. } catch(Exception $e) {
  151. $this->fail($e->getMessage());
  152. }
  153. $this->assertEquals('http://www.planet-php.org/rss/', $links->rss);
  154. }
  155. public function testCompilesLinksAsArrayObject()
  156. {
  157. if (!defined('TESTS_ZEND_FEED_READER_ONLINE_ENABLED')
  158. || !constant('TESTS_ZEND_FEED_READER_ONLINE_ENABLED')
  159. ) {
  160. $this->markTestSkipped('testGetsFeedLinksAsValueObject() requires a network connection');
  161. return;
  162. }
  163. $links = Zend_Feed_Reader::findFeedLinks('http://www.planet-php.net');
  164. $this->assertTrue($links instanceof Zend_Feed_Reader_FeedSet);
  165. $this->assertEquals(array(
  166. 'rel' => 'alternate', 'type' => 'application/rss+xml', 'href' => 'http://www.planet-php.org/rss/'
  167. ), (array) $links->getIterator()->current());
  168. }
  169. public function testFeedSetLoadsFeedObjectWhenFeedArrayKeyAccessed()
  170. {
  171. if (!defined('TESTS_ZEND_FEED_READER_ONLINE_ENABLED')
  172. || !constant('TESTS_ZEND_FEED_READER_ONLINE_ENABLED')
  173. ) {
  174. $this->markTestSkipped('testGetsFeedLinksAsValueObject() requires a network connection');
  175. return;
  176. }
  177. $links = Zend_Feed_Reader::findFeedLinks('http://www.planet-php.net');
  178. $link = $links->getIterator()->current();
  179. $this->assertTrue($link['feed'] instanceof Zend_Feed_Reader_Feed_Rss);
  180. }
  181. public function testZeroCountFeedSetReturnedFromEmptyList()
  182. {
  183. if (!defined('TESTS_ZEND_FEED_READER_ONLINE_ENABLED')
  184. || !constant('TESTS_ZEND_FEED_READER_ONLINE_ENABLED')
  185. ) {
  186. $this->markTestSkipped('testGetsFeedLinksAsValueObject() requires a network connection');
  187. return;
  188. }
  189. $links = Zend_Feed_Reader::findFeedLinks('http://www.example.com');
  190. $this->assertEquals(0, count($links));
  191. }
  192. /**
  193. * @group ZF-8327
  194. */
  195. public function testGetsFeedLinksAndTrimsNewlines()
  196. {
  197. if (!defined('TESTS_ZEND_FEED_READER_ONLINE_ENABLED')
  198. || !constant('TESTS_ZEND_FEED_READER_ONLINE_ENABLED')
  199. ) {
  200. $this->markTestSkipped('testGetsFeedLinksAsValueObject() requires a network connection');
  201. return;
  202. }
  203. try {
  204. $links = Zend_Feed_Reader::findFeedLinks('http://www.infopod.com.br');
  205. } catch(Exception $e) {
  206. $this->fail($e->getMessage());
  207. }
  208. $this->assertEquals('http://feeds.feedburner.com/jonnyken/infoblog', $links->rss);
  209. }
  210. public function testAddsPrefixPath()
  211. {
  212. Zend_Feed_Reader::addPrefixPath('A_B_C', '/A/B/C');
  213. $prefixPaths = Zend_Feed_Reader::getPluginLoader()->getPaths();
  214. $this->assertEquals('/A/B/C/', $prefixPaths['A_B_C_'][0]);
  215. }
  216. public function testRegistersUserExtension()
  217. {
  218. try {
  219. Zend_Feed_Reader::addPrefixPath('My_FeedReader_Extension',dirname(__FILE__) . '/Reader/_files/My/Extension');
  220. Zend_Feed_Reader::registerExtension('JungleBooks');
  221. } catch(Exception $e) {
  222. $this->fail($e->getMessage());
  223. }
  224. $this->assertTrue(Zend_Feed_Reader::isRegistered('JungleBooks'));
  225. }
  226. /** Test condition for ZF-7486 (Could Not Replicate) **/
  227. public function testRepeatedFeedImportsWithCacheEnabledDirectOrIndirectDoNotCreateARedeclarationOfAbstractClassFatalError()
  228. {
  229. if (!defined('TESTS_ZEND_FEED_READER_ONLINE_ENABLED')
  230. || !constant('TESTS_ZEND_FEED_READER_ONLINE_ENABLED')
  231. ) {
  232. $this->markTestSkipped('testRepeatedFeedImportsDoNotCreateARedeclarationOfAbstractClassFatalError() requires a network connection');
  233. return;
  234. }
  235. $frontendOptions = array(
  236. 'lifetime' => 7200,
  237. 'automatic_serialization' => true
  238. );
  239. $backendOptions = array(
  240. 'cache_dir' => $this->_getTempDirectory()
  241. );
  242. $cache = Zend_Cache::factory('Core','File',$frontendOptions,$backendOptions);
  243. Zend_Feed_Reader::setCache($cache);
  244. $feed = Zend_Feed_Reader::import('http://www.planet-php.net/rss/');
  245. $cache->save($feed, 'feed1');
  246. $feed2 = Zend_Feed_Reader::import('http://www.planet-php.net/rdf/');
  247. $cache->save($feed, 'feed2');
  248. $feed3 = Zend_Feed_Reader::import('http://www.planet-php.net/atom/');
  249. $cache->save($feed, 'feed3');
  250. $feed4 = Zend_Feed_Reader::import('http://www.phpdeveloper.org/feed');
  251. $cache->save($feed, 'feed4');
  252. $feedXmlFromCache = $cache->load('Zend_Feed_Reader_' . md5('http://www.planet-php.net/rss/'));
  253. $feedFromCache = Zend_Feed_Reader::importString($feedXmlFromCache);
  254. $this->assertTrue($feedFromCache instanceof Zend_Feed_Reader_FeedAbstract);
  255. $feedFromCache2 = $cache->load('feed4');
  256. $this->assertTrue($feedFromCache2 instanceof Zend_Feed_Reader_FeedAbstract);
  257. }
  258. protected function _getTempDirectory()
  259. {
  260. $tmpdir = array();
  261. foreach (array($_ENV, $_SERVER) as $tab) {
  262. foreach (array('TMPDIR', 'TEMP', 'TMP', 'windir', 'SystemRoot') as $key) {
  263. if (isset($tab[$key])) {
  264. if (($key == 'windir') or ($key == 'SystemRoot')) {
  265. $dir = realpath($tab[$key] . '\\temp');
  266. } else {
  267. $dir = realpath($tab[$key]);
  268. }
  269. if ($this->_isGoodTmpDir($dir)) {
  270. return $dir;
  271. }
  272. }
  273. }
  274. }
  275. if (function_exists('sys_get_temp_dir')) {
  276. $dir = sys_get_temp_dir();
  277. if ($this->_isGoodTmpDir($dir)) {
  278. return $dir;
  279. }
  280. }
  281. $tempFile = tempnam(md5(uniqid(rand(), TRUE)), '');
  282. if ($tempFile) {
  283. $dir = realpath(dirname($tempFile));
  284. unlink($tempFile);
  285. if ($this->_isGoodTmpDir($dir)) {
  286. return $dir;
  287. }
  288. }
  289. if ($this->_isGoodTmpDir('/tmp')) {
  290. return '/tmp';
  291. }
  292. if ($this->_isGoodTmpDir('\\temp')) {
  293. return '\\temp';
  294. }
  295. }
  296. protected function _isGoodTmpDir($dir)
  297. {
  298. if (is_readable($dir) && is_writable($dir)) {
  299. return true;
  300. }
  301. return false;
  302. }
  303. }