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.
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
26 require_once dirname(__FILE__
) . '/../../TestHelper.php';
31 require_once 'Zend/Feed.php';
34 * @see Zend_Feed_Builder
36 require_once 'Zend/Feed/Builder.php';
39 * @see Zend_Http_Client_Adapter_Test
41 require_once 'Zend/Http/Client/Adapter/Test.php';
44 * @see Zend_Http_Client
46 require_once 'Zend/Http/Client.php';
51 * @subpackage UnitTests
52 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
53 * @license http://framework.zend.com/license/new-bsd New BSD License
56 class Zend_Feed_ImportTest
extends PHPUnit_Framework_TestCase
62 * HTTP client test adapter
64 * @var Zend_Http_Client_Adapter_Test
68 public function setUp()
70 $this->_adapter
= new Zend_Http_Client_Adapter_Test();
71 Zend_Feed
::setHttpClient(new Zend_Http_Client(null, array('adapter' => $this->_adapter
)));
72 $this->_client
= Zend_Feed
::getHttpClient();
73 $this->_feedDir
= dirname(__FILE__
) . '/_files';
77 * Test an atom feed generated by google's Blogger platform
79 public function testAtomGoogle()
81 $this->_importAtomValid('AtomTestGoogle.xml');
85 * Test an atom feed generated by mozillaZine.org
87 public function testAtomMozillazine()
89 $this->_importAtomValid('AtomTestMozillazine.xml');
93 * Test an atom feed generated by O'Reilly
95 public function testAtomOReilly()
97 $this->_importAtomValid('AtomTestOReilly.xml');
101 * Test an atom feed generated by PlanetPHP
103 public function testAtomPlanetPHP()
105 $this->_importAtomValid('AtomTestPlanetPHP.xml');
109 * Test a small atom feed
111 public function testAtomSample1()
113 $this->_importAtomValid('AtomTestSample1.xml');
117 * Test a small atom feed without any entries
119 public function testAtomSample2()
121 $this->_importAtomValid('AtomTestSample2.xml');
125 * Test an atom feed with a </entry> tag missing
127 public function testAtomSample3()
129 $this->_importInvalid('AtomTestSample3.xml');
133 * Test an atom feed with links within entries
135 public function testAtomSample4()
137 $this->_importAtomValid('AtomTestSample4.xml');
141 * Test a RSS feed generated by UserLand Frontier v9.5
143 public function testRssHarvardLaw()
145 $this->_importRssValid('RssTestHarvardLaw.xml');
149 * Test a RSS feed generated by PlanetPHP
151 public function testRssPlanetPHP()
153 $this->_importRssValid('RssTestPlanetPHP.xml');
157 * Test a RSS feed generated by Slashdot
159 public function testRssSlashdot()
161 $this->_importRssValid('RssTestSlashdot.xml');
165 * Test a RSS feed generated by CNN
167 public function testRssCNN()
169 $this->_importRssValid('RssTestCNN.xml');
173 * Test a valid RSS 0.91 sample
175 public function testRss091Sample1()
177 $this->_importRssValid('RssTest091Sample1.xml');
181 * Test a valid RSS 0.91 sample
183 public function testRss092Sample1()
185 $this->_importRssValid('RssTest092Sample1.xml');
189 * Test a valid RSS 1.0 sample
191 public function testRss100Sample1()
193 $feed = $this->_importRssValid('RssTest100Sample1.xml');
194 $this->assertEquals(2, $feed->count());
198 * Test a valid RSS 1.0 sample with some extensions in it
200 public function testRss100Sample2()
202 $feed = $this->_importRssValid('RssTest100Sample2.xml');
203 $this->assertEquals(1, $feed->count());
207 * Test a valid RSS 2.0 sample
209 public function testRss200Sample1()
211 $this->_importRssValid('RssTest200Sample1.xml');
215 * Test the import of a RSS feed from an array
217 public function testRssImportFullArray()
219 $feed = Zend_Feed
::importArray($this->_getFullArray(), 'rss');
220 $this->assertType('Zend_Feed_Rss', $feed);
224 * Test the import of a RSS feed from an array
227 public function testRssImportSetsIsPermaLinkAsFalseIfGuidNotAUri()
229 $feed = Zend_Feed
::importArray($this->_getFullArray(), 'rss');
230 $entry = $feed->current();
231 $this->assertEquals('false', $entry->guid
['isPermaLink']);
235 * Test the import of a RSS feed from an array
237 public function testAtomImportFullArray()
239 $feed = Zend_Feed
::importArray($this->_getFullArray(), 'atom');
243 * Test the import of a RSS feed from a builder
245 public function testRssImportFullBuilder()
247 $feed = Zend_Feed
::importBuilder(new Zend_Feed_Builder($this->_getFullArray()), 'rss');
248 $this->assertType('Zend_Feed_Rss', $feed);
252 * Test the import of a full iTunes RSS feed from a builder
254 public function testRssImportFulliTunesBuilder()
256 $array = $this->_getFullArray();
257 $array['itunes']['author'] = 'iTunes Author';
258 $array['itunes']['owner'] = array('name' => 'iTunes Owner',
259 'email' => 'itunes@example.com');
260 $array['itunes']['image'] = 'http://www.example/itunes.png';
261 $array['itunes']['subtitle'] = 'iTunes subtitle';
262 $array['itunes']['summary'] = 'iTunes summary';
263 $array['itunes']['explicit'] = 'clean';
264 $array['itunes']['block'] = 'no';
265 $array['itunes']['new-feed-url'] = 'http://www.example/itunes.xml';
266 $feed = Zend_Feed
::importBuilder(new Zend_Feed_Builder($array), 'rss');
267 $this->assertType('Zend_Feed_Rss', $feed);
271 * Test the import of an Atom feed from a builder
273 public function testAtomImportFullBuilder()
275 $feed = Zend_Feed
::importBuilder(new Zend_Feed_Builder($this->_getFullArray()), 'atom');
280 * Test the import of an Atom feed from a builder
282 public function testAtomImportFullBuilderValid()
284 $feed = Zend_Feed
::importBuilder(new Zend_Feed_Builder($this->_getFullArray()), 'atom');
286 $feed = Zend_Feed
::importString($feed->saveXml());
287 $this->assertType('Zend_Feed_Atom', $feed);
291 * Check the validity of the builder import (rss)
293 public function testRssImportFullBuilderValid()
295 $feed = Zend_Feed
::importBuilder(new Zend_Feed_Builder($this->_getFullArray()), 'rss');
296 $this->assertType('Zend_Feed_Rss', $feed);
297 $feed = Zend_Feed
::importString($feed->saveXml());
298 $this->assertType('Zend_Feed_Rss', $feed);
302 * Test the return of a link() call (atom)
304 public function testAtomGetLink()
306 $feed = Zend_Feed
::importBuilder(new Zend_Feed_Builder($this->_getFullArray()), 'atom');
307 $this->assertType('Zend_Feed_Atom', $feed);
308 $feed = Zend_Feed
::importString($feed->saveXml());
309 $this->assertType('Zend_Feed_Atom', $feed);
310 $href = $feed->link('self');
311 $this->assertEquals('http://www.example.com', $href);
315 * Imports an invalid feed and ensure everything works as expected
316 * even if XDebug is running (ZF-2590).
318 public function testImportInvalidIsXdebugAware()
320 if (!function_exists('xdebug_is_enabled')) {
321 $this->markTestIncomplete('XDebug not installed');
324 $response = new Zend_Http_Response(200, array(), '');
325 $this->_adapter
->setResponse($response);
328 $feed = Zend_Feed
::import('http://localhost');
329 $this->fail('Expected Zend_Feed_Exception not thrown');
330 } catch (Zend_Feed_Exception
$e) {
331 $this->assertType('Zend_Feed_Exception', $e);
332 $this->assertRegExp('/(XDebug is running|Empty string)/', $e->getMessage());
337 * Returns the array used by Zend_Feed::importArray
338 * and Zend_Feed::importBuilder tests
342 protected function _getFullArray()
344 $array = array('title' => 'Title of the feed',
345 'link' => 'http://www.example.com',
346 'description' => 'Description of the feed',
347 'author' => 'Olivier Sirven',
348 'email' => 'olivier@elma.fr',
349 'webmaster' => 'olivier@elma.fr',
350 'charset' => 'iso-8859-15',
351 'lastUpdate' => time(),
352 'published' => strtotime('2007-02-27'),
353 'copyright' => 'Common Creative',
354 'image' => 'http://www.example/images/icon.png',
357 'rating' => ' (PICS-1.1 "http://www.gcf.org/v2.5" labels
358 on "1994.11.05T08:15-0500"
359 exp "1995.12.31T23:59-0000"
360 for "http://www.greatdocs.com/foo.html"
361 by "George Sanderson, Jr."
362 ratings (suds 0.5 density 0 color/hue 1))',
363 'cloud' => array('domain' => 'rpc.sys.com',
365 'registerProcedure' => 'webServices.pingMe',
366 'protocol' => 'xml-rpc'),
367 'textInput' => array('title' => 'subscribe',
368 'description' => 'enter your email address to subscribe by mail',
370 'link' => 'http://www.example.com/subscribe'),
371 'skipHours' => array(1, 13, 17),
372 'skipDays' => array('Saturday', 'Sunday'),
373 'itunes' => array('block' => 'no',
374 'keywords' => 'example,itunes,podcast',
375 'category' => array(array('main' => 'Technology',
377 array('main' => 'Music'))),
378 'entries' => array(array('guid' => time(),
379 'title' => 'First article',
380 'link' => 'http://www.example.com',
381 'description' => 'First article description',
382 'content' => 'First article <strong>content</strong>',
383 'lastUpdate' => time(),
384 'comments' => 'http://www.example.com/#comments',
385 'commentRss' => 'http://www.example.com/comments.xml',
386 'source' => array('title' => 'Original title',
387 'url' => 'http://www.domain.com'),
388 'category' => array(array('term' => 'test category',
389 'scheme' => 'http://www.example.com/scheme'),
390 array('term' => 'another category')
392 'enclosure' => array(array('url' => 'http://www.example.com/podcast.mp3',
393 'type' => 'audio/mpeg',
394 'length' => '12216320'
396 array('url' => 'http://www.example.com/podcast2.mp3',
397 'type' => 'audio/mpeg',
398 'length' => '1221632'
402 array('title' => 'Second article',
403 'link' => 'http://www.example.com/two',
404 'description' => 'Second article description',
405 'content' => 'Second article <strong>content</strong>',
406 'lastUpdate' => time(),
407 'comments' => 'http://www.example.com/two/#comments',
408 'category' => array(array('term' => 'test category')),
416 * Import an invalid atom feed
418 protected function _importAtomValid($filename)
420 $response = new Zend_Http_Response(200, array(), file_get_contents("$this->_feedDir/$filename"));
421 $this->_adapter
->setResponse($response);
423 $feed = Zend_Feed
::import('http://localhost');
424 $this->assertType('Zend_Feed_Atom', $feed);
428 * Import a valid rss feed
430 protected function _importRssValid($filename)
432 $response = new Zend_Http_Response(200, array(), file_get_contents("$this->_feedDir/$filename"));
433 $this->_adapter
->setResponse($response);
435 $feed = Zend_Feed
::import('http://localhost');
436 $this->assertType('Zend_Feed_Rss', $feed);
441 * Imports an invalid feed
443 protected function _importInvalid($filename)
445 $response = new Zend_Http_Response(200, array(), file_get_contents("$this->_feedDir/$filename"));
446 $this->_adapter
->setResponse($response);
449 $feed = Zend_Feed
::import('http://localhost');
450 $this->fail('Expected Zend_Feed_Exception not thrown');
451 } catch (Zend_Feed_Exception
$e) {
452 $this->assertType('Zend_Feed_Exception', $e);
459 public function testFindFeedsIncludesUriAsArrayKey()
461 if (!defined('TESTS_ZEND_FEED_READER_ONLINE_ENABLED')
462 ||
!constant('TESTS_ZEND_FEED_READER_ONLINE_ENABLED')
464 $this->markTestSkipped('testFindFeedsIncludesUriAsArrayKey() requires a network connection');
467 Zend_Feed
::setHttpClient(new Zend_Http_Client
);
468 $feeds = Zend_Feed
::findFeeds('http://www.planet-php.net');
469 $this->assertEquals(array(
470 'http://www.planet-php.org:80/rss/', 'http://www.planet-php.org:80/rdf/'
471 ), array_keys($feeds));