ReaderTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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. 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. /**
  141. * @group ZF-8328
  142. * @expectedException Zend_Feed_Exception
  143. */
  144. public function testImportsUriAndThrowsExceptionIfNotAFeed()
  145. {
  146. if (!defined('TESTS_ZEND_FEED_READER_ONLINE_ENABLED')
  147. || !constant('TESTS_ZEND_FEED_READER_ONLINE_ENABLED')
  148. ) {
  149. $this->markTestSkipped('testImportsUri() requires a network connection');
  150. return;
  151. }
  152. $feed = Zend_Feed_Reader::import('http://twitter.com/alganet');
  153. }
  154. public function testGetsFeedLinksAsValueObject()
  155. {
  156. if (!defined('TESTS_ZEND_FEED_READER_ONLINE_ENABLED')
  157. || !constant('TESTS_ZEND_FEED_READER_ONLINE_ENABLED')
  158. ) {
  159. $this->markTestSkipped('testGetsFeedLinksAsValueObject() requires a network connection');
  160. return;
  161. }
  162. try {
  163. $links = Zend_Feed_Reader::findFeedLinks('http://www.planet-php.net');
  164. } catch(Exception $e) {
  165. $this->fail($e->getMessage());
  166. }
  167. $this->assertEquals('http://www.planet-php.org/rss/', $links->rss);
  168. }
  169. public function testCompilesLinksAsArrayObject()
  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. $this->assertTrue($links instanceof Zend_Feed_Reader_FeedSet);
  179. $this->assertEquals(array(
  180. 'rel' => 'alternate', 'type' => 'application/rss+xml', 'href' => 'http://www.planet-php.org/rss/'
  181. ), (array) $links->getIterator()->current());
  182. }
  183. public function testFeedSetLoadsFeedObjectWhenFeedArrayKeyAccessed()
  184. {
  185. if (!defined('TESTS_ZEND_FEED_READER_ONLINE_ENABLED')
  186. || !constant('TESTS_ZEND_FEED_READER_ONLINE_ENABLED')
  187. ) {
  188. $this->markTestSkipped('testGetsFeedLinksAsValueObject() requires a network connection');
  189. return;
  190. }
  191. $links = Zend_Feed_Reader::findFeedLinks('http://www.planet-php.net');
  192. $link = $links->getIterator()->current();
  193. $this->assertTrue($link['feed'] instanceof Zend_Feed_Reader_Feed_Rss);
  194. }
  195. public function testZeroCountFeedSetReturnedFromEmptyList()
  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. $links = Zend_Feed_Reader::findFeedLinks('http://www.example.com');
  204. $this->assertEquals(0, count($links));
  205. }
  206. /**
  207. * @group ZF-8327
  208. */
  209. public function testGetsFeedLinksAndTrimsNewlines()
  210. {
  211. if (!defined('TESTS_ZEND_FEED_READER_ONLINE_ENABLED')
  212. || !constant('TESTS_ZEND_FEED_READER_ONLINE_ENABLED')
  213. ) {
  214. $this->markTestSkipped('testGetsFeedLinksAsValueObject() requires a network connection');
  215. return;
  216. }
  217. try {
  218. $links = Zend_Feed_Reader::findFeedLinks('http://www.infopod.com.br');
  219. } catch(Exception $e) {
  220. $this->fail($e->getMessage());
  221. }
  222. $this->assertEquals('http://feeds.feedburner.com/jonnyken/infoblog', $links->rss);
  223. }
  224. /**
  225. * @group ZF-8330
  226. */
  227. public function testGetsFeedLinksAndNormalisesRelativeUrls()
  228. {
  229. if (!defined('TESTS_ZEND_FEED_READER_ONLINE_ENABLED')
  230. || !constant('TESTS_ZEND_FEED_READER_ONLINE_ENABLED')
  231. ) {
  232. $this->markTestSkipped('testGetsFeedLinksAsValueObject() requires a network connection');
  233. return;
  234. }
  235. try {
  236. $links = Zend_Feed_Reader::findFeedLinks('http://meiobit.com');
  237. } catch(Exception $e) {
  238. $this->fail($e->getMessage());
  239. }
  240. $this->assertEquals('http://meiobit.com/rss.xml', $links->rss);
  241. }
  242. /**
  243. * @group ZF-8330
  244. */
  245. public function testGetsFeedLinksAndNormalisesRelativeUrlsOnUriWithPath()
  246. {
  247. try {
  248. $currClient = Zend_Feed_Reader::getHttpClient();
  249. $testAdapter = new Zend_Http_Client_Adapter_Test();
  250. $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>'));
  251. Zend_Feed_Reader::setHttpClient(new Zend_Http_Client(null, array('adapter' => $testAdapter)));
  252. $links = Zend_Feed_Reader::findFeedLinks('http://foo/bar');
  253. Zend_Feed_Reader::setHttpClient($currClient);
  254. } catch(Exception $e) {
  255. $this->fail($e->getMessage());
  256. }
  257. $this->assertEquals('http://foo/test.rss', $links->rss);
  258. $this->assertEquals('http://foo/test.atom', $links->atom);
  259. }
  260. public function testAddsPrefixPath()
  261. {
  262. Zend_Feed_Reader::addPrefixPath('A_B_C', '/A/B/C');
  263. $prefixPaths = Zend_Feed_Reader::getPluginLoader()->getPaths();
  264. $this->assertEquals('/A/B/C/', $prefixPaths['A_B_C_'][0]);
  265. }
  266. public function testRegistersUserExtension()
  267. {
  268. try {
  269. Zend_Feed_Reader::addPrefixPath('My_FeedReader_Extension',dirname(__FILE__) . '/Reader/_files/My/Extension');
  270. Zend_Feed_Reader::registerExtension('JungleBooks');
  271. } catch(Exception $e) {
  272. $this->fail($e->getMessage());
  273. }
  274. $this->assertTrue(Zend_Feed_Reader::isRegistered('JungleBooks'));
  275. }
  276. protected function _getTempDirectory()
  277. {
  278. $tmpdir = array();
  279. foreach (array($_ENV, $_SERVER) as $tab) {
  280. foreach (array('TMPDIR', 'TEMP', 'TMP', 'windir', 'SystemRoot') as $key) {
  281. if (isset($tab[$key])) {
  282. if (($key == 'windir') or ($key == 'SystemRoot')) {
  283. $dir = realpath($tab[$key] . '\\temp');
  284. } else {
  285. $dir = realpath($tab[$key]);
  286. }
  287. if ($this->_isGoodTmpDir($dir)) {
  288. return $dir;
  289. }
  290. }
  291. }
  292. }
  293. if (function_exists('sys_get_temp_dir')) {
  294. $dir = sys_get_temp_dir();
  295. if ($this->_isGoodTmpDir($dir)) {
  296. return $dir;
  297. }
  298. }
  299. $tempFile = tempnam(md5(uniqid(rand(), TRUE)), '');
  300. if ($tempFile) {
  301. $dir = realpath(dirname($tempFile));
  302. unlink($tempFile);
  303. if ($this->_isGoodTmpDir($dir)) {
  304. return $dir;
  305. }
  306. }
  307. if ($this->_isGoodTmpDir('/tmp')) {
  308. return '/tmp';
  309. }
  310. if ($this->_isGoodTmpDir('\\temp')) {
  311. return '\\temp';
  312. }
  313. }
  314. protected function _isGoodTmpDir($dir)
  315. {
  316. if (is_readable($dir) && is_writable($dir)) {
  317. return true;
  318. }
  319. return false;
  320. }
  321. }