| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394 |
- <?php
- /**
- * Zend Framework
- *
- * LICENSE
- *
- * This source file is subject to the new BSD license that is bundled
- * with this package in the file LICENSE.txt.
- * It is also available through the world-wide-web at this URL:
- * http://framework.zend.com/license/new-bsd
- * If you did not receive a copy of the license and are unable to
- * obtain it through the world-wide-web, please send an email
- * to license@zend.com so we can send you a copy immediately.
- *
- * @category Zend
- * @package Zend_Feed
- * @subpackage UnitTests
- * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
- * @license http://framework.zend.com/license/new-bsd New BSD License
- * @version $Id$
- */
- require_once 'Zend/Feed/Reader.php';
- require_once 'Zend/Cache.php';
- /**
- * @category Zend
- * @package Zend_Feed
- * @subpackage UnitTests
- * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
- * @license http://framework.zend.com/license/new-bsd New BSD License
- * @group Zend_Feed
- * @group Zend_Feed_Reader
- */
- class Zend_Feed_ReaderTest extends PHPUnit_Framework_TestCase
- {
- protected $_feedSamplePath = null;
- public function setup()
- {
- $this->_feedSamplePath = dirname(__FILE__) . '/Reader/_files';
- }
- public function tearDown()
- {
- Zend_Feed_Reader::reset();
- }
- public function testDetectsFeedIsRss20()
- {
- $feed = Zend_Feed_Reader::importString(
- file_get_contents($this->_feedSamplePath.'/Reader/rss20.xml'));
- $type = Zend_Feed_Reader::detectType($feed);
- $this->assertEquals(Zend_Feed_Reader::TYPE_RSS_20, $type);
- }
- public function testDetectsFeedIsRss094()
- {
- $feed = Zend_Feed_Reader::importString(
- file_get_contents($this->_feedSamplePath.'/Reader/rss094.xml'));
- $type = Zend_Feed_Reader::detectType($feed);
- $this->assertEquals(Zend_Feed_Reader::TYPE_RSS_094, $type);
- }
- public function testDetectsFeedIsRss093()
- {
- $feed = Zend_Feed_Reader::importString(
- file_get_contents($this->_feedSamplePath.'/Reader/rss093.xml'));
- $type = Zend_Feed_Reader::detectType($feed);
- $this->assertEquals(Zend_Feed_Reader::TYPE_RSS_093, $type);
- }
- public function testDetectsFeedIsRss092()
- {
- $feed = Zend_Feed_Reader::importString(
- file_get_contents($this->_feedSamplePath.'/Reader/rss092.xml'));
- $type = Zend_Feed_Reader::detectType($feed);
- $this->assertEquals(Zend_Feed_Reader::TYPE_RSS_092, $type);
- }
- public function testDetectsFeedIsRss091()
- {
- $feed = Zend_Feed_Reader::importString(
- file_get_contents($this->_feedSamplePath.'/Reader/rss091.xml'));
- $type = Zend_Feed_Reader::detectType($feed);
- $this->assertEquals(Zend_Feed_Reader::TYPE_RSS_091, $type);
- }
- public function testDetectsFeedIsRss10()
- {
- $feed = Zend_Feed_Reader::importString(
- file_get_contents($this->_feedSamplePath.'/Reader/rss10.xml'));
- $type = Zend_Feed_Reader::detectType($feed);
- $this->assertEquals(Zend_Feed_Reader::TYPE_RSS_10, $type);
- }
- public function testDetectsFeedIsRss090()
- {
- $feed = Zend_Feed_Reader::importString(
- file_get_contents($this->_feedSamplePath.'/Reader/rss090.xml'));
- $type = Zend_Feed_Reader::detectType($feed);
- $this->assertEquals(Zend_Feed_Reader::TYPE_RSS_090, $type);
- }
- public function testDetectsFeedIsAtom10()
- {
- $feed = Zend_Feed_Reader::importString(
- file_get_contents($this->_feedSamplePath.'/Reader/atom10.xml'));
- $type = Zend_Feed_Reader::detectType($feed);
- $this->assertEquals(Zend_Feed_Reader::TYPE_ATOM_10, $type);
- }
- public function testDetectsFeedIsAtom03()
- {
- $feed = Zend_Feed_Reader::importString(
- file_get_contents($this->_feedSamplePath.'/Reader/atom03.xml'));
- $type = Zend_Feed_Reader::detectType($feed);
- $this->assertEquals(Zend_Feed_Reader::TYPE_ATOM_03, $type);
- }
- /**
- * @group ZF-9723
- */
- public function testDetectsTypeFromStringOrToRemindPaddyAboutForgettingATestWhichLetsAStupidTypoSurviveUnnoticedForMonths()
- {
- $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>';
- $type = Zend_Feed_Reader::detectType($feed);
- $this->assertEquals(Zend_Feed_Reader::TYPE_RSS_10, $type);
- }
- public function testGetEncoding()
- {
- $feed = Zend_Feed_Reader::importString(
- file_get_contents(dirname(__FILE__) . '/Reader/Entry/_files/Atom/title/plain/atom10.xml')
- );
- $this->assertEquals('utf-8', $feed->getEncoding());
- $this->assertEquals('utf-8', $feed->current()->getEncoding());
- }
- public function testImportsFile()
- {
- try {
- $feed = Zend_Feed_Reader::importFile(
- dirname(__FILE__) . '/Reader/Entry/_files/Atom/title/plain/atom10.xml'
- );
- } catch(Exception $e) {
- $this->fail($e->getMessage());
- }
- }
- public function testImportsUri()
- {
- if (!defined('TESTS_ZEND_FEED_READER_ONLINE_ENABLED')
- || !constant('TESTS_ZEND_FEED_READER_ONLINE_ENABLED')
- ) {
- $this->markTestSkipped('testImportsUri() requires a network connection');
- return;
- }
- try {
- $feed = Zend_Feed_Reader::import('http://www.planet-php.net/rdf/');
- } catch(Exception $e) {
- $this->fail($e->getMessage());
- }
- }
-
- /**
- * @group ZF-8328
- * @expectedException Zend_Feed_Exception
- */
- public function testImportsUriAndThrowsExceptionIfNotAFeed()
- {
- if (!defined('TESTS_ZEND_FEED_READER_ONLINE_ENABLED')
- || !constant('TESTS_ZEND_FEED_READER_ONLINE_ENABLED')
- ) {
- $this->markTestSkipped('testImportsUri() requires a network connection');
- return;
- }
- $feed = Zend_Feed_Reader::import('http://twitter.com/alganet');
- }
- public function testGetsFeedLinksAsValueObject()
- {
- if (!defined('TESTS_ZEND_FEED_READER_ONLINE_ENABLED')
- || !constant('TESTS_ZEND_FEED_READER_ONLINE_ENABLED')
- ) {
- $this->markTestSkipped('testGetsFeedLinksAsValueObject() requires a network connection');
- return;
- }
- try {
- $links = Zend_Feed_Reader::findFeedLinks('http://www.planet-php.net');
- } catch(Exception $e) {
- $this->fail($e->getMessage());
- }
- $this->assertEquals('http://www.planet-php.org/rss/', $links->rss);
- }
- public function testCompilesLinksAsArrayObject()
- {
- if (!defined('TESTS_ZEND_FEED_READER_ONLINE_ENABLED')
- || !constant('TESTS_ZEND_FEED_READER_ONLINE_ENABLED')
- ) {
- $this->markTestSkipped('testGetsFeedLinksAsValueObject() requires a network connection');
- return;
- }
- $links = Zend_Feed_Reader::findFeedLinks('http://www.planet-php.net');
- $this->assertTrue($links instanceof Zend_Feed_Reader_FeedSet);
- $this->assertEquals(array(
- 'rel' => 'alternate', 'type' => 'application/rss+xml', 'href' => 'http://www.planet-php.org/rss/'
- ), (array) $links->getIterator()->current());
- }
- public function testFeedSetLoadsFeedObjectWhenFeedArrayKeyAccessed()
- {
- if (!defined('TESTS_ZEND_FEED_READER_ONLINE_ENABLED')
- || !constant('TESTS_ZEND_FEED_READER_ONLINE_ENABLED')
- ) {
- $this->markTestSkipped('testGetsFeedLinksAsValueObject() requires a network connection');
- return;
- }
- $links = Zend_Feed_Reader::findFeedLinks('http://www.planet-php.net');
- $link = $links->getIterator()->current();
- $this->assertTrue($link['feed'] instanceof Zend_Feed_Reader_Feed_Rss);
- }
- public function testZeroCountFeedSetReturnedFromEmptyList()
- {
- if (!defined('TESTS_ZEND_FEED_READER_ONLINE_ENABLED')
- || !constant('TESTS_ZEND_FEED_READER_ONLINE_ENABLED')
- ) {
- $this->markTestSkipped('testGetsFeedLinksAsValueObject() requires a network connection');
- return;
- }
- $links = Zend_Feed_Reader::findFeedLinks('http://www.example.com');
- $this->assertEquals(0, count($links));
- }
-
- /**
- * @group ZF-8327
- */
- public function testGetsFeedLinksAndTrimsNewlines()
- {
- if (!defined('TESTS_ZEND_FEED_READER_ONLINE_ENABLED')
- || !constant('TESTS_ZEND_FEED_READER_ONLINE_ENABLED')
- ) {
- $this->markTestSkipped('testGetsFeedLinksAsValueObject() requires a network connection');
- return;
- }
- try {
- $links = Zend_Feed_Reader::findFeedLinks('http://www.infopod.com.br');
- } catch(Exception $e) {
- $this->fail($e->getMessage());
- }
- $this->assertEquals('http://feeds.feedburner.com/jonnyken/infoblog', $links->rss);
- }
-
- /**
- * @group ZF-8330
- */
- public function testGetsFeedLinksAndNormalisesRelativeUrls()
- {
- if (!defined('TESTS_ZEND_FEED_READER_ONLINE_ENABLED')
- || !constant('TESTS_ZEND_FEED_READER_ONLINE_ENABLED')
- ) {
- $this->markTestSkipped('testGetsFeedLinksAsValueObject() requires a network connection');
- return;
- }
- try {
- $links = Zend_Feed_Reader::findFeedLinks('http://meiobit.com');
- } catch(Exception $e) {
- $this->fail($e->getMessage());
- }
- $this->assertEquals('http://meiobit.com/rss.xml', $links->rss);
- }
-
- /**
- * @group ZF-8330
- */
- public function testGetsFeedLinksAndNormalisesRelativeUrlsOnUriWithPath()
- {
- try {
- $currClient = Zend_Feed_Reader::getHttpClient();
- $testAdapter = new Zend_Http_Client_Adapter_Test();
- $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>'));
- Zend_Feed_Reader::setHttpClient(new Zend_Http_Client(null, array('adapter' => $testAdapter)));
- $links = Zend_Feed_Reader::findFeedLinks('http://foo/bar');
- Zend_Feed_Reader::setHttpClient($currClient);
- } catch(Exception $e) {
- $this->fail($e->getMessage());
- }
- $this->assertEquals('http://foo/test.rss', $links->rss);
- $this->assertEquals('http://foo/test.atom', $links->atom);
- }
- public function testAddsPrefixPath()
- {
- Zend_Feed_Reader::addPrefixPath('A_B_C', '/A/B/C');
- $prefixPaths = Zend_Feed_Reader::getPluginLoader()->getPaths();
- $this->assertEquals('/A/B/C/', $prefixPaths['A_B_C_'][0]);
- }
- public function testRegistersUserExtension()
- {
- try {
- Zend_Feed_Reader::addPrefixPath('My_FeedReader_Extension',dirname(__FILE__) . '/Reader/_files/My/Extension');
- Zend_Feed_Reader::registerExtension('JungleBooks');
- } catch(Exception $e) {
- $this->fail($e->getMessage());
- }
- $this->assertTrue(Zend_Feed_Reader::isRegistered('JungleBooks'));
- }
-
- /**
- * @group ZF-11184
- */
- public function testImportingUriWithEmptyResponseBodyTriggersException()
- {
- $currClient = Zend_Feed_Reader::getHttpClient();
- $testAdapter = new Zend_Http_Client_Adapter_Test();
- $testAdapter->setResponse(new Zend_Http_Response(200,array(),''));
- Zend_Feed_Reader::setHttpClient(new Zend_Http_Client(null, array(
- 'adapter'=>$testAdapter
- )));
-
- $this->setExpectedException('Zend_Feed_Exception', 'Feed failed to load');
- $result = Zend_Feed_Reader::import('http://www.example.com');
- }
- public function testXxePreventionOnFeedParsing()
- {
- $string = file_get_contents($this->_feedSamplePath.'/Reader/xxe-atom10.xml');
- $string = str_replace('XXE_URI', $this->_feedSamplePath.'/Reader/xxe-info.txt', $string);
- $this->setExpectedException('Zend_Feed_Exception');
- $feed = Zend_Feed_Reader::importString($string);
- }
-
- protected function _getTempDirectory()
- {
- $tmpdir = array();
- foreach (array($_ENV, $_SERVER) as $tab) {
- foreach (array('TMPDIR', 'TEMP', 'TMP', 'windir', 'SystemRoot') as $key) {
- if (isset($tab[$key])) {
- if (($key == 'windir') or ($key == 'SystemRoot')) {
- $dir = realpath($tab[$key] . '\\temp');
- } else {
- $dir = realpath($tab[$key]);
- }
- if ($this->_isGoodTmpDir($dir)) {
- return $dir;
- }
- }
- }
- }
- if (function_exists('sys_get_temp_dir')) {
- $dir = sys_get_temp_dir();
- if ($this->_isGoodTmpDir($dir)) {
- return $dir;
- }
- }
- $tempFile = tempnam(md5(uniqid(rand(), TRUE)), '');
- if ($tempFile) {
- $dir = realpath(dirname($tempFile));
- unlink($tempFile);
- if ($this->_isGoodTmpDir($dir)) {
- return $dir;
- }
- }
- if ($this->_isGoodTmpDir('/tmp')) {
- return '/tmp';
- }
- if ($this->_isGoodTmpDir('\\temp')) {
- return '\\temp';
- }
- }
- protected function _isGoodTmpDir($dir)
- {
- if (is_readable($dir) && is_writable($dir)) {
- return true;
- }
- return false;
- }
- }
|