ReaderTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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. 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-2010 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. /**
  109. * @group ZF-9723
  110. */
  111. public function testDetectsTypeFromStringOrToRemindPaddyAboutForgettingATestWhichLetsAStupidTypoSurviveUnnoticedForMonths()
  112. {
  113. $feed = '<?xml version="1.0" encoding="utf-8" ?><rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns="http://purl.org/rss/1.0/"><channel></channel></rdf:RDF>';
  114. $type = Zend_Feed_Reader::detectType($feed);
  115. $this->assertEquals(Zend_Feed_Reader::TYPE_RSS_10, $type);
  116. }
  117. public function testGetEncoding()
  118. {
  119. $feed = Zend_Feed_Reader::importString(
  120. file_get_contents(dirname(__FILE__) . '/Reader/Entry/_files/Atom/title/plain/atom10.xml')
  121. );
  122. $this->assertEquals('utf-8', $feed->getEncoding());
  123. $this->assertEquals('utf-8', $feed->current()->getEncoding());
  124. }
  125. public function testImportsFile()
  126. {
  127. try {
  128. $feed = Zend_Feed_Reader::importFile(
  129. dirname(__FILE__) . '/Reader/Entry/_files/Atom/title/plain/atom10.xml'
  130. );
  131. } catch(Exception $e) {
  132. $this->fail($e->getMessage());
  133. }
  134. }
  135. public function testImportsUri()
  136. {
  137. if (!defined('TESTS_ZEND_FEED_READER_ONLINE_ENABLED')
  138. || !constant('TESTS_ZEND_FEED_READER_ONLINE_ENABLED')
  139. ) {
  140. $this->markTestSkipped('testImportsUri() requires a network connection');
  141. return;
  142. }
  143. try {
  144. $feed = Zend_Feed_Reader::import('http://www.planet-php.net/rdf/');
  145. } catch(Exception $e) {
  146. $this->fail($e->getMessage());
  147. }
  148. }
  149. /**
  150. * @group ZF-8328
  151. * @expectedException Zend_Feed_Exception
  152. */
  153. public function testImportsUriAndThrowsExceptionIfNotAFeed()
  154. {
  155. if (!defined('TESTS_ZEND_FEED_READER_ONLINE_ENABLED')
  156. || !constant('TESTS_ZEND_FEED_READER_ONLINE_ENABLED')
  157. ) {
  158. $this->markTestSkipped('testImportsUri() requires a network connection');
  159. return;
  160. }
  161. $feed = Zend_Feed_Reader::import('http://twitter.com/alganet');
  162. }
  163. public function testGetsFeedLinksAsValueObject()
  164. {
  165. if (!defined('TESTS_ZEND_FEED_READER_ONLINE_ENABLED')
  166. || !constant('TESTS_ZEND_FEED_READER_ONLINE_ENABLED')
  167. ) {
  168. $this->markTestSkipped('testGetsFeedLinksAsValueObject() requires a network connection');
  169. return;
  170. }
  171. try {
  172. $links = Zend_Feed_Reader::findFeedLinks('http://www.planet-php.net');
  173. } catch(Exception $e) {
  174. $this->fail($e->getMessage());
  175. }
  176. $this->assertEquals('http://www.planet-php.org/rss/', $links->rss);
  177. }
  178. public function testCompilesLinksAsArrayObject()
  179. {
  180. if (!defined('TESTS_ZEND_FEED_READER_ONLINE_ENABLED')
  181. || !constant('TESTS_ZEND_FEED_READER_ONLINE_ENABLED')
  182. ) {
  183. $this->markTestSkipped('testGetsFeedLinksAsValueObject() requires a network connection');
  184. return;
  185. }
  186. $links = Zend_Feed_Reader::findFeedLinks('http://www.planet-php.net');
  187. $this->assertTrue($links instanceof Zend_Feed_Reader_FeedSet);
  188. $this->assertEquals(array(
  189. 'rel' => 'alternate', 'type' => 'application/rss+xml', 'href' => 'http://www.planet-php.org/rss/'
  190. ), (array) $links->getIterator()->current());
  191. }
  192. public function testFeedSetLoadsFeedObjectWhenFeedArrayKeyAccessed()
  193. {
  194. if (!defined('TESTS_ZEND_FEED_READER_ONLINE_ENABLED')
  195. || !constant('TESTS_ZEND_FEED_READER_ONLINE_ENABLED')
  196. ) {
  197. $this->markTestSkipped('testGetsFeedLinksAsValueObject() requires a network connection');
  198. return;
  199. }
  200. $links = Zend_Feed_Reader::findFeedLinks('http://www.planet-php.net');
  201. $link = $links->getIterator()->current();
  202. $this->assertTrue($link['feed'] instanceof Zend_Feed_Reader_Feed_Rss);
  203. }
  204. public function testZeroCountFeedSetReturnedFromEmptyList()
  205. {
  206. if (!defined('TESTS_ZEND_FEED_READER_ONLINE_ENABLED')
  207. || !constant('TESTS_ZEND_FEED_READER_ONLINE_ENABLED')
  208. ) {
  209. $this->markTestSkipped('testGetsFeedLinksAsValueObject() requires a network connection');
  210. return;
  211. }
  212. $links = Zend_Feed_Reader::findFeedLinks('http://www.example.com');
  213. $this->assertEquals(0, count($links));
  214. }
  215. /**
  216. * @group ZF-8327
  217. */
  218. public function testGetsFeedLinksAndTrimsNewlines()
  219. {
  220. if (!defined('TESTS_ZEND_FEED_READER_ONLINE_ENABLED')
  221. || !constant('TESTS_ZEND_FEED_READER_ONLINE_ENABLED')
  222. ) {
  223. $this->markTestSkipped('testGetsFeedLinksAsValueObject() requires a network connection');
  224. return;
  225. }
  226. try {
  227. $links = Zend_Feed_Reader::findFeedLinks('http://www.infopod.com.br');
  228. } catch(Exception $e) {
  229. $this->fail($e->getMessage());
  230. }
  231. $this->assertEquals('http://feeds.feedburner.com/jonnyken/infoblog', $links->rss);
  232. }
  233. /**
  234. * @group ZF-8330
  235. */
  236. public function testGetsFeedLinksAndNormalisesRelativeUrls()
  237. {
  238. if (!defined('TESTS_ZEND_FEED_READER_ONLINE_ENABLED')
  239. || !constant('TESTS_ZEND_FEED_READER_ONLINE_ENABLED')
  240. ) {
  241. $this->markTestSkipped('testGetsFeedLinksAsValueObject() requires a network connection');
  242. return;
  243. }
  244. try {
  245. $links = Zend_Feed_Reader::findFeedLinks('http://meiobit.com');
  246. } catch(Exception $e) {
  247. $this->fail($e->getMessage());
  248. }
  249. $this->assertEquals('http://meiobit.com/rss.xml', $links->rss);
  250. }
  251. /**
  252. * @group ZF-8330
  253. */
  254. public function testGetsFeedLinksAndNormalisesRelativeUrlsOnUriWithPath()
  255. {
  256. try {
  257. $currClient = Zend_Feed_Reader::getHttpClient();
  258. $testAdapter = new Zend_Http_Client_Adapter_Test();
  259. $testAdapter->setResponse(new Zend_Http_Response(200, array(), '<!DOCTYPE html><html><head><link rel="alternate" type="application/rss+xml" href="../test.rss"><link rel="alternate" type="application/atom+xml" href="/test.atom"></head><body></body></html>'));
  260. Zend_Feed_Reader::setHttpClient(new Zend_Http_Client(null, array('adapter' => $testAdapter)));
  261. $links = Zend_Feed_Reader::findFeedLinks('http://foo/bar');
  262. Zend_Feed_Reader::setHttpClient($currClient);
  263. } catch(Exception $e) {
  264. $this->fail($e->getMessage());
  265. }
  266. $this->assertEquals('http://foo/test.rss', $links->rss);
  267. $this->assertEquals('http://foo/test.atom', $links->atom);
  268. }
  269. public function testAddsPrefixPath()
  270. {
  271. Zend_Feed_Reader::addPrefixPath('A_B_C', '/A/B/C');
  272. $prefixPaths = Zend_Feed_Reader::getPluginLoader()->getPaths();
  273. $this->assertEquals('/A/B/C/', $prefixPaths['A_B_C_'][0]);
  274. }
  275. public function testRegistersUserExtension()
  276. {
  277. try {
  278. Zend_Feed_Reader::addPrefixPath('My_FeedReader_Extension',dirname(__FILE__) . '/Reader/_files/My/Extension');
  279. Zend_Feed_Reader::registerExtension('JungleBooks');
  280. } catch(Exception $e) {
  281. $this->fail($e->getMessage());
  282. }
  283. $this->assertTrue(Zend_Feed_Reader::isRegistered('JungleBooks'));
  284. }
  285. protected function _getTempDirectory()
  286. {
  287. $tmpdir = array();
  288. foreach (array($_ENV, $_SERVER) as $tab) {
  289. foreach (array('TMPDIR', 'TEMP', 'TMP', 'windir', 'SystemRoot') as $key) {
  290. if (isset($tab[$key])) {
  291. if (($key == 'windir') or ($key == 'SystemRoot')) {
  292. $dir = realpath($tab[$key] . '\\temp');
  293. } else {
  294. $dir = realpath($tab[$key]);
  295. }
  296. if ($this->_isGoodTmpDir($dir)) {
  297. return $dir;
  298. }
  299. }
  300. }
  301. }
  302. if (function_exists('sys_get_temp_dir')) {
  303. $dir = sys_get_temp_dir();
  304. if ($this->_isGoodTmpDir($dir)) {
  305. return $dir;
  306. }
  307. }
  308. $tempFile = tempnam(md5(uniqid(rand(), TRUE)), '');
  309. if ($tempFile) {
  310. $dir = realpath(dirname($tempFile));
  311. unlink($tempFile);
  312. if ($this->_isGoodTmpDir($dir)) {
  313. return $dir;
  314. }
  315. }
  316. if ($this->_isGoodTmpDir('/tmp')) {
  317. return '/tmp';
  318. }
  319. if ($this->_isGoodTmpDir('\\temp')) {
  320. return '\\temp';
  321. }
  322. }
  323. protected function _isGoodTmpDir($dir)
  324. {
  325. if (is_readable($dir) && is_writable($dir)) {
  326. return true;
  327. }
  328. return false;
  329. }
  330. }