AtomTest.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  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. /**
  25. * @category Zend
  26. * @package Zend_Feed
  27. * @subpackage UnitTests
  28. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  29. * @license http://framework.zend.com/license/new-bsd New BSD License
  30. * @group Zend_Feed
  31. * @group Zend_Feed_Reader
  32. */
  33. class Zend_Feed_Reader_Feed_AtomTest extends PHPUnit_Framework_TestCase
  34. {
  35. protected $_feedSamplePath = null;
  36. protected $_options = array();
  37. protected $_expectedCats = array();
  38. protected $_expectedCatsDc = array();
  39. public function setup()
  40. {
  41. Zend_Feed_Reader::reset();
  42. if (Zend_Registry::isRegistered('Zend_Locale')) {
  43. $registry = Zend_Registry::getInstance();
  44. unset($registry['Zend_Locale']);
  45. }
  46. $this->_feedSamplePath = dirname(__FILE__) . '/_files/Atom';
  47. $this->_options = Zend_Date::setOptions();
  48. foreach($this->_options as $k=>$v) {
  49. if (is_null($v)) {
  50. unset($this->_options[$k]);
  51. }
  52. }
  53. Zend_Date::setOptions(array('format_type'=>'iso'));
  54. $this->_expectedCats = array(
  55. array(
  56. 'term' => 'topic1',
  57. 'scheme' => 'http://example.com/schema1',
  58. 'label' => 'topic1'
  59. ),
  60. array(
  61. 'term' => 'topic1',
  62. 'scheme' => 'http://example.com/schema2',
  63. 'label' => 'topic1'
  64. ),
  65. array(
  66. 'term' => 'cat_dog',
  67. 'scheme' => 'http://example.com/schema1',
  68. 'label' => 'Cat & Dog'
  69. )
  70. );
  71. $this->_expectedCatsDc = array(
  72. array(
  73. 'term' => 'topic1',
  74. 'scheme' => null,
  75. 'label' => 'topic1'
  76. ),
  77. array(
  78. 'term' => 'topic2',
  79. 'scheme' => null,
  80. 'label' => 'topic2'
  81. )
  82. );
  83. }
  84. public function teardown()
  85. {
  86. Zend_Date::setOptions($this->_options);
  87. }
  88. /**
  89. * Get Title (Unencoded Text)
  90. */
  91. public function testGetsTitleFromAtom03()
  92. {
  93. $feed = Zend_Feed_Reader::importString(
  94. file_get_contents($this->_feedSamplePath.'/title/plain/atom03.xml')
  95. );
  96. $this->assertEquals('My Title', $feed->getTitle());
  97. }
  98. public function testGetsTitleFromAtom10()
  99. {
  100. $feed = Zend_Feed_Reader::importString(
  101. file_get_contents($this->_feedSamplePath.'/title/plain/atom10.xml')
  102. );
  103. $this->assertEquals('My Title', $feed->getTitle());
  104. }
  105. /**
  106. * Get Authors (Unencoded Text)
  107. */
  108. public function testGetsAuthorArrayFromAtom03()
  109. {
  110. $feed = Zend_Feed_Reader::importString(
  111. file_get_contents($this->_feedSamplePath.'/author/plain/atom03.xml')
  112. );
  113. $authors = array(
  114. array('email'=>'joe@example.com','name'=>'Joe Bloggs','uri'=>'http://www.example.com'),
  115. array('name'=>'Joe Bloggs','uri'=>'http://www.example.com'),
  116. array('name'=>'Joe Bloggs'),
  117. array('email'=>'joe@example.com','uri'=>'http://www.example.com'),
  118. array('uri'=>'http://www.example.com'),
  119. array('email'=>'joe@example.com')
  120. );
  121. $this->assertEquals($authors, (array) $feed->getAuthors());
  122. }
  123. public function testGetsAuthorArrayFromAtom10()
  124. {
  125. $feed = Zend_Feed_Reader::importString(
  126. file_get_contents($this->_feedSamplePath.'/author/plain/atom10.xml')
  127. );
  128. $authors = array(
  129. array('email'=>'joe@example.com','name'=>'Joe Bloggs','uri'=>'http://www.example.com'),
  130. array('name'=>'Joe Bloggs','uri'=>'http://www.example.com'),
  131. array('name'=>'Joe Bloggs'),
  132. array('email'=>'joe@example.com','uri'=>'http://www.example.com'),
  133. array('uri'=>'http://www.example.com'),
  134. array('email'=>'joe@example.com')
  135. );
  136. $this->assertEquals($authors, (array) $feed->getAuthors());
  137. }
  138. /**
  139. * Get Single Author (Unencoded Text)
  140. */
  141. public function testGetsSingleAuthorFromAtom03()
  142. {
  143. $feed = Zend_Feed_Reader::importString(
  144. file_get_contents($this->_feedSamplePath.'/author/plain/atom03.xml')
  145. );
  146. $this->assertEquals(array('name'=>'Joe Bloggs','email'=>'joe@example.com','uri'=>'http://www.example.com'), $feed->getAuthor());
  147. }
  148. public function testGetsSingleAuthorFromAtom10()
  149. {
  150. $feed = Zend_Feed_Reader::importString(
  151. file_get_contents($this->_feedSamplePath.'/author/plain/atom10.xml')
  152. );
  153. $this->assertEquals(array('name'=>'Joe Bloggs','email'=>'joe@example.com','uri'=>'http://www.example.com'), $feed->getAuthor());
  154. }
  155. /**
  156. * Get creation date (Unencoded Text)
  157. */
  158. public function testGetsDateCreatedFromAtom03()
  159. {
  160. $feed = Zend_Feed_Reader::importString(
  161. file_get_contents($this->_feedSamplePath . '/datecreated/plain/atom03.xml')
  162. );
  163. $edate = new Zend_Date;
  164. $edate->set('2009-03-07T08:03:50Z', Zend_Date::ISO_8601);
  165. $this->assertTrue($edate->equals($feed->getDateCreated()));
  166. }
  167. public function testGetsDateCreatedFromAtom10()
  168. {
  169. $feed = Zend_Feed_Reader::importString(
  170. file_get_contents($this->_feedSamplePath . '/datecreated/plain/atom10.xml')
  171. );
  172. $edate = new Zend_Date;
  173. $edate->set('2009-03-07T08:03:50Z', Zend_Date::ISO_8601);
  174. $this->assertTrue($edate->equals($feed->getDateCreated()));
  175. }
  176. /**
  177. * Get modification date (Unencoded Text)
  178. */
  179. public function testGetsDateModifiedFromAtom03()
  180. {
  181. $feed = Zend_Feed_Reader::importString(
  182. file_get_contents($this->_feedSamplePath . '/datemodified/plain/atom03.xml')
  183. );
  184. $edate = new Zend_Date;
  185. $edate->set('2009-03-07T08:03:50Z', Zend_Date::ISO_8601);
  186. $this->assertTrue($edate->equals($feed->getDateModified()));
  187. }
  188. public function testGetsDateModifiedFromAtom10()
  189. {
  190. $feed = Zend_Feed_Reader::importString(
  191. file_get_contents($this->_feedSamplePath . '/datemodified/plain/atom10.xml')
  192. );
  193. $edate = new Zend_Date;
  194. $edate->set('2009-03-07T08:03:50Z', Zend_Date::ISO_8601);
  195. $this->assertTrue($edate->equals($feed->getDateModified()));
  196. }
  197. /**
  198. * Get Last Build Date (Unencoded Text)
  199. */
  200. public function testGetsLastBuildDateAlwaysReturnsNullForAtom()
  201. {
  202. $feed = Zend_Feed_Reader::importString(
  203. file_get_contents($this->_feedSamplePath.'/datemodified/plain/atom10.xml')
  204. );
  205. $this->assertNull($feed->getLastBuildDate());
  206. }
  207. /**
  208. * Get Generator (Unencoded Text)
  209. */
  210. public function testGetsGeneratorFromAtom03()
  211. {
  212. $feed = Zend_Feed_Reader::importString(
  213. file_get_contents($this->_feedSamplePath.'/generator/plain/atom03.xml')
  214. );
  215. $this->assertEquals('Zend_Feed', $feed->getGenerator());
  216. }
  217. public function testGetsGeneratorFromAtom10()
  218. {
  219. $feed = Zend_Feed_Reader::importString(
  220. file_get_contents($this->_feedSamplePath.'/generator/plain/atom10.xml')
  221. );
  222. $this->assertEquals('Zend_Feed', $feed->getGenerator());
  223. }
  224. /**
  225. * Get Copyright (Unencoded Text)
  226. */
  227. public function testGetsCopyrightFromAtom03()
  228. {
  229. $feed = Zend_Feed_Reader::importString(
  230. file_get_contents($this->_feedSamplePath.'/copyright/plain/atom03.xml')
  231. );
  232. $this->assertEquals('Copyright 2008', $feed->getCopyright());
  233. }
  234. public function testGetsCopyrightFromAtom10()
  235. {
  236. $feed = Zend_Feed_Reader::importString(
  237. file_get_contents($this->_feedSamplePath.'/copyright/plain/atom10.xml')
  238. );
  239. $this->assertEquals('Copyright 2008', $feed->getCopyright());
  240. }
  241. /**
  242. * Get Description (Unencoded Text)
  243. */
  244. public function testGetsDescriptionFromAtom03()
  245. {
  246. $feed = Zend_Feed_Reader::importString(
  247. file_get_contents($this->_feedSamplePath.'/description/plain/atom03.xml')
  248. );
  249. $this->assertEquals('My Description', $feed->getDescription());
  250. }
  251. public function testGetsDescriptionFromAtom10()
  252. {
  253. $feed = Zend_Feed_Reader::importString(
  254. file_get_contents($this->_feedSamplePath.'/description/plain/atom10.xml')
  255. );
  256. $this->assertEquals('My Description', $feed->getDescription());
  257. }
  258. /**
  259. * Get Id (Unencoded Text)
  260. */
  261. public function testGetsIdFromAtom03()
  262. {
  263. $feed = Zend_Feed_Reader::importString(
  264. file_get_contents($this->_feedSamplePath.'/id/plain/atom03.xml')
  265. );
  266. $this->assertEquals('123', $feed->getId());
  267. }
  268. public function testGetsIdFromAtom10()
  269. {
  270. $feed = Zend_Feed_Reader::importString(
  271. file_get_contents($this->_feedSamplePath.'/id/plain/atom10.xml')
  272. );
  273. $this->assertEquals('123', $feed->getId());
  274. }
  275. /**
  276. * Get Language (Unencoded Text)
  277. */
  278. public function testGetsLanguageFromAtom03()
  279. {
  280. $feed = Zend_Feed_Reader::importString(
  281. file_get_contents($this->_feedSamplePath.'/language/plain/atom03.xml')
  282. );
  283. $this->assertEquals('en-GB', $feed->getLanguage());
  284. }
  285. public function testGetsLanguageFromAtom10()
  286. {
  287. $feed = Zend_Feed_Reader::importString(
  288. file_get_contents($this->_feedSamplePath.'/language/plain/atom10.xml')
  289. );
  290. $this->assertEquals('en-GB', $feed->getLanguage());
  291. }
  292. /**
  293. * Get Link (Unencoded Text)
  294. */
  295. public function testGetsLinkFromAtom03()
  296. {
  297. $feed = Zend_Feed_Reader::importString(
  298. file_get_contents($this->_feedSamplePath.'/link/plain/atom03.xml')
  299. );
  300. $this->assertEquals('http://www.example.com', $feed->getLink());
  301. }
  302. public function testGetsLinkFromAtom10()
  303. {
  304. $feed = Zend_Feed_Reader::importString(
  305. file_get_contents($this->_feedSamplePath.'/link/plain/atom10.xml')
  306. );
  307. $this->assertEquals('http://www.example.com', $feed->getLink());
  308. }
  309. public function testGetsLinkFromAtom10WithNoRelAttribute()
  310. {
  311. $feed = Zend_Feed_Reader::importString(
  312. file_get_contents($this->_feedSamplePath.'/link/plain/atom10-norel.xml')
  313. );
  314. $this->assertEquals('http://www.example.com', $feed->getLink());
  315. }
  316. public function testGetsLinkFromAtom10WithRelativeUrl()
  317. {
  318. $feed = Zend_Feed_Reader::importString(
  319. file_get_contents($this->_feedSamplePath.'/link/plain/atom10-relative.xml')
  320. );
  321. $this->assertEquals('http://www.example.com', $feed->getLink());
  322. }
  323. /**
  324. * Get Base Uri
  325. */
  326. public function testGetsBaseUriFromAtom10()
  327. {
  328. $feed = Zend_Feed_Reader::importString(
  329. file_get_contents($this->_feedSamplePath.'/feedlink/plain/atom10-relative.xml')
  330. );
  331. $this->assertEquals('http://www.example.com/', $feed->getBaseUrl());
  332. }
  333. /**
  334. * Get Feed Link (Unencoded Text)
  335. */
  336. public function testGetsFeedLinkFromAtom03()
  337. {
  338. $feed = Zend_Feed_Reader::importString(
  339. file_get_contents($this->_feedSamplePath.'/feedlink/plain/atom03.xml')
  340. );
  341. $this->assertEquals('http://www.example.com/feed/atom', $feed->getFeedLink());
  342. }
  343. public function testGetsFeedLinkFromAtom10()
  344. {
  345. $feed = Zend_Feed_Reader::importString(
  346. file_get_contents($this->_feedSamplePath.'/feedlink/plain/atom10.xml')
  347. );
  348. $this->assertEquals('http://www.example.com/feed/atom', $feed->getFeedLink());
  349. }
  350. public function testGetsFeedLinkFromAtom10IfRelativeUri()
  351. {
  352. $feed = Zend_Feed_Reader::importString(
  353. file_get_contents($this->_feedSamplePath.'/feedlink/plain/atom10-relative.xml')
  354. );
  355. $this->assertEquals('http://www.example.com/feed/atom', $feed->getFeedLink());
  356. }
  357. public function testGetsOriginalSourceUriIfFeedLinkNotAvailableFromFeed()
  358. {
  359. $feed = Zend_Feed_Reader::importString(
  360. file_get_contents($this->_feedSamplePath.'/feedlink/plain/atom10_NoFeedLink.xml')
  361. );
  362. $feed->setOriginalSourceUri('http://www.example.com/feed/atom');
  363. $this->assertEquals('http://www.example.com/feed/atom', $feed->getFeedLink());
  364. }
  365. /**
  366. * Get Pubsubhubbub Hubs
  367. */
  368. public function testGetsHubsFromAtom03()
  369. {
  370. $feed = Zend_Feed_Reader::importString(
  371. file_get_contents($this->_feedSamplePath.'/hubs/plain/atom03.xml')
  372. );
  373. $this->assertEquals(array(
  374. 'http://www.example.com/hub1',
  375. 'http://www.example.com/hub2'
  376. ), $feed->getHubs());
  377. }
  378. public function testGetsHubsFromAtom10()
  379. {
  380. $feed = Zend_Feed_Reader::importString(
  381. file_get_contents($this->_feedSamplePath.'/hubs/plain/atom10.xml')
  382. );
  383. $this->assertEquals(array(
  384. 'http://www.example.com/hub1',
  385. 'http://www.example.com/hub2'
  386. ), $feed->getHubs());
  387. }
  388. /**
  389. * Implements Countable
  390. */
  391. public function testCountableInterface()
  392. {
  393. $feed = Zend_Feed_Reader::importString(
  394. file_get_contents($this->_feedSamplePath.'/link/plain/atom10.xml')
  395. );
  396. $this->assertEquals(0, count($feed));
  397. }
  398. /**
  399. * Get category data
  400. */
  401. // Atom 1.0 (Atom 0.3 never supported categories except via Atom 1.0/Dublin Core extensions)
  402. public function testGetsCategoriesFromAtom10()
  403. {
  404. $feed = Zend_Feed_Reader::importString(
  405. file_get_contents($this->_feedSamplePath.'/category/plain/atom10.xml')
  406. );
  407. $this->assertEquals($this->_expectedCats, (array) $feed->getCategories());
  408. $this->assertEquals(array('topic1','Cat & Dog'), array_values($feed->getCategories()->getValues()));
  409. }
  410. public function testGetsCategoriesFromAtom03_Atom10Extension()
  411. {
  412. $feed = Zend_Feed_Reader::importString(
  413. file_get_contents($this->_feedSamplePath.'/category/plain/atom03.xml')
  414. );
  415. $this->assertEquals($this->_expectedCats, (array) $feed->getCategories());
  416. $this->assertEquals(array('topic1','Cat & Dog'), array_values($feed->getCategories()->getValues()));
  417. }
  418. // DC 1.0/1.1 for Atom 0.3
  419. public function testGetsCategoriesFromAtom03_Dc10()
  420. {
  421. $feed = Zend_Feed_Reader::importString(
  422. file_get_contents($this->_feedSamplePath.'/category/plain/dc10/atom03.xml')
  423. );
  424. $this->assertEquals($this->_expectedCatsDc, (array) $feed->getCategories());
  425. $this->assertEquals(array('topic1','topic2'), array_values($feed->getCategories()->getValues()));
  426. }
  427. public function testGetsCategoriesFromAtom03_Dc11()
  428. {
  429. $feed = Zend_Feed_Reader::importString(
  430. file_get_contents($this->_feedSamplePath.'/category/plain/dc11/atom03.xml')
  431. );
  432. $this->assertEquals($this->_expectedCatsDc, (array) $feed->getCategories());
  433. $this->assertEquals(array('topic1','topic2'), array_values($feed->getCategories()->getValues()));
  434. }
  435. // No Categories In Entry
  436. public function testGetsCategoriesFromAtom10_None()
  437. {
  438. $feed = Zend_Feed_Reader::importString(
  439. file_get_contents($this->_feedSamplePath.'/category/plain/none/atom10.xml')
  440. );
  441. $this->assertEquals(array(), (array) $feed->getCategories());
  442. $this->assertEquals(array(), array_values($feed->getCategories()->getValues()));
  443. }
  444. public function testGetsCategoriesFromAtom03_None()
  445. {
  446. $feed = Zend_Feed_Reader::importString(
  447. file_get_contents($this->_feedSamplePath.'/category/plain/none/atom03.xml')
  448. );
  449. $this->assertEquals(array(), (array) $feed->getCategories());
  450. $this->assertEquals(array(), array_values($feed->getCategories()->getValues()));
  451. }
  452. /**
  453. * Get Image (Unencoded Text)
  454. */
  455. public function testGetsImageFromAtom03()
  456. {
  457. $feed = Zend_Feed_Reader::importString(
  458. file_get_contents($this->_feedSamplePath.'/image/plain/atom03.xml')
  459. );
  460. $this->assertEquals(array('uri'=>'http://www.example.com/logo.gif'), $feed->getImage());
  461. }
  462. public function testGetsImageFromAtom10()
  463. {
  464. $feed = Zend_Feed_Reader::importString(
  465. file_get_contents($this->_feedSamplePath.'/image/plain/atom10.xml')
  466. );
  467. $this->assertEquals(array('uri'=>'http://www.example.com/logo.gif'), $feed->getImage());
  468. }
  469. /**
  470. * Get Image (Unencoded Text) When Missing
  471. */
  472. public function testGetsImageFromAtom03_None()
  473. {
  474. $feed = Zend_Feed_Reader::importString(
  475. file_get_contents($this->_feedSamplePath.'/image/plain/none/atom03.xml')
  476. );
  477. $this->assertEquals(null, $feed->getImage());
  478. }
  479. public function testGetsImageFromAtom10_None()
  480. {
  481. $feed = Zend_Feed_Reader::importString(
  482. file_get_contents($this->_feedSamplePath.'/image/plain/none/atom10.xml')
  483. );
  484. $this->assertEquals(null, $feed->getImage());
  485. }
  486. }