AtomTest.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  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 Generator (Unencoded Text)
  199. */
  200. public function testGetsGeneratorFromAtom03()
  201. {
  202. $feed = Zend_Feed_Reader::importString(
  203. file_get_contents($this->_feedSamplePath.'/generator/plain/atom03.xml')
  204. );
  205. $this->assertEquals('Zend_Feed', $feed->getGenerator());
  206. }
  207. public function testGetsGeneratorFromAtom10()
  208. {
  209. $feed = Zend_Feed_Reader::importString(
  210. file_get_contents($this->_feedSamplePath.'/generator/plain/atom10.xml')
  211. );
  212. $this->assertEquals('Zend_Feed', $feed->getGenerator());
  213. }
  214. /**
  215. * Get Copyright (Unencoded Text)
  216. */
  217. public function testGetsCopyrightFromAtom03()
  218. {
  219. $feed = Zend_Feed_Reader::importString(
  220. file_get_contents($this->_feedSamplePath.'/copyright/plain/atom03.xml')
  221. );
  222. $this->assertEquals('Copyright 2008', $feed->getCopyright());
  223. }
  224. public function testGetsCopyrightFromAtom10()
  225. {
  226. $feed = Zend_Feed_Reader::importString(
  227. file_get_contents($this->_feedSamplePath.'/copyright/plain/atom10.xml')
  228. );
  229. $this->assertEquals('Copyright 2008', $feed->getCopyright());
  230. }
  231. /**
  232. * Get Description (Unencoded Text)
  233. */
  234. public function testGetsDescriptionFromAtom03()
  235. {
  236. $feed = Zend_Feed_Reader::importString(
  237. file_get_contents($this->_feedSamplePath.'/description/plain/atom03.xml')
  238. );
  239. $this->assertEquals('My Description', $feed->getDescription());
  240. }
  241. public function testGetsDescriptionFromAtom10()
  242. {
  243. $feed = Zend_Feed_Reader::importString(
  244. file_get_contents($this->_feedSamplePath.'/description/plain/atom10.xml')
  245. );
  246. $this->assertEquals('My Description', $feed->getDescription());
  247. }
  248. /**
  249. * Get Id (Unencoded Text)
  250. */
  251. public function testGetsIdFromAtom03()
  252. {
  253. $feed = Zend_Feed_Reader::importString(
  254. file_get_contents($this->_feedSamplePath.'/id/plain/atom03.xml')
  255. );
  256. $this->assertEquals('123', $feed->getId());
  257. }
  258. public function testGetsIdFromAtom10()
  259. {
  260. $feed = Zend_Feed_Reader::importString(
  261. file_get_contents($this->_feedSamplePath.'/id/plain/atom10.xml')
  262. );
  263. $this->assertEquals('123', $feed->getId());
  264. }
  265. /**
  266. * Get Language (Unencoded Text)
  267. */
  268. public function testGetsLanguageFromAtom03()
  269. {
  270. $feed = Zend_Feed_Reader::importString(
  271. file_get_contents($this->_feedSamplePath.'/language/plain/atom03.xml')
  272. );
  273. $this->assertEquals('en-GB', $feed->getLanguage());
  274. }
  275. public function testGetsLanguageFromAtom10()
  276. {
  277. $feed = Zend_Feed_Reader::importString(
  278. file_get_contents($this->_feedSamplePath.'/language/plain/atom10.xml')
  279. );
  280. $this->assertEquals('en-GB', $feed->getLanguage());
  281. }
  282. /**
  283. * Get Link (Unencoded Text)
  284. */
  285. public function testGetsLinkFromAtom03()
  286. {
  287. $feed = Zend_Feed_Reader::importString(
  288. file_get_contents($this->_feedSamplePath.'/link/plain/atom03.xml')
  289. );
  290. $this->assertEquals('http://www.example.com', $feed->getLink());
  291. }
  292. public function testGetsLinkFromAtom10()
  293. {
  294. $feed = Zend_Feed_Reader::importString(
  295. file_get_contents($this->_feedSamplePath.'/link/plain/atom10.xml')
  296. );
  297. $this->assertEquals('http://www.example.com', $feed->getLink());
  298. }
  299. public function testGetsLinkFromAtom10WithNoRelAttribute()
  300. {
  301. $feed = Zend_Feed_Reader::importString(
  302. file_get_contents($this->_feedSamplePath.'/link/plain/atom10-norel.xml')
  303. );
  304. $this->assertEquals('http://www.example.com', $feed->getLink());
  305. }
  306. public function testGetsLinkFromAtom10WithRelativeUrl()
  307. {
  308. $feed = Zend_Feed_Reader::importString(
  309. file_get_contents($this->_feedSamplePath.'/link/plain/atom10-relative.xml')
  310. );
  311. $this->assertEquals('http://www.example.com', $feed->getLink());
  312. }
  313. /**
  314. * Get Base Uri
  315. */
  316. public function testGetsBaseUriFromAtom10()
  317. {
  318. $feed = Zend_Feed_Reader::importString(
  319. file_get_contents($this->_feedSamplePath.'/feedlink/plain/atom10-relative.xml')
  320. );
  321. $this->assertEquals('http://www.example.com/', $feed->getBaseUrl());
  322. }
  323. /**
  324. * Get Feed Link (Unencoded Text)
  325. */
  326. public function testGetsFeedLinkFromAtom03()
  327. {
  328. $feed = Zend_Feed_Reader::importString(
  329. file_get_contents($this->_feedSamplePath.'/feedlink/plain/atom03.xml')
  330. );
  331. $this->assertEquals('http://www.example.com/feed/atom', $feed->getFeedLink());
  332. }
  333. public function testGetsFeedLinkFromAtom10()
  334. {
  335. $feed = Zend_Feed_Reader::importString(
  336. file_get_contents($this->_feedSamplePath.'/feedlink/plain/atom10.xml')
  337. );
  338. $this->assertEquals('http://www.example.com/feed/atom', $feed->getFeedLink());
  339. }
  340. public function testGetsFeedLinkFromAtom10IfRelativeUri()
  341. {
  342. $feed = Zend_Feed_Reader::importString(
  343. file_get_contents($this->_feedSamplePath.'/feedlink/plain/atom10-relative.xml')
  344. );
  345. $this->assertEquals('http://www.example.com/feed/atom', $feed->getFeedLink());
  346. }
  347. /**
  348. * Get Pubsubhubbub Hubs
  349. */
  350. public function testGetsHubsFromAtom03()
  351. {
  352. $feed = Zend_Feed_Reader::importString(
  353. file_get_contents($this->_feedSamplePath.'/hubs/plain/atom03.xml')
  354. );
  355. $this->assertEquals(array(
  356. 'http://www.example.com/hub1',
  357. 'http://www.example.com/hub2'
  358. ), $feed->getHubs());
  359. }
  360. public function testGetsHubsFromAtom10()
  361. {
  362. $feed = Zend_Feed_Reader::importString(
  363. file_get_contents($this->_feedSamplePath.'/hubs/plain/atom10.xml')
  364. );
  365. $this->assertEquals(array(
  366. 'http://www.example.com/hub1',
  367. 'http://www.example.com/hub2'
  368. ), $feed->getHubs());
  369. }
  370. /**
  371. * Implements Countable
  372. */
  373. public function testCountableInterface()
  374. {
  375. $feed = Zend_Feed_Reader::importString(
  376. file_get_contents($this->_feedSamplePath.'/link/plain/atom10.xml')
  377. );
  378. $this->assertEquals(0, count($feed));
  379. }
  380. /**
  381. * Get category data
  382. */
  383. // Atom 1.0 (Atom 0.3 never supported categories except via Atom 1.0/Dublin Core extensions)
  384. public function testGetsCategoriesFromAtom10()
  385. {
  386. $feed = Zend_Feed_Reader::importString(
  387. file_get_contents($this->_feedSamplePath.'/category/plain/atom10.xml')
  388. );
  389. $this->assertEquals($this->_expectedCats, (array) $feed->getCategories());
  390. $this->assertEquals(array('topic1','Cat & Dog'), array_values($feed->getCategories()->getValues()));
  391. }
  392. public function testGetsCategoriesFromAtom03_Atom10Extension()
  393. {
  394. $feed = Zend_Feed_Reader::importString(
  395. file_get_contents($this->_feedSamplePath.'/category/plain/atom03.xml')
  396. );
  397. $this->assertEquals($this->_expectedCats, (array) $feed->getCategories());
  398. $this->assertEquals(array('topic1','Cat & Dog'), array_values($feed->getCategories()->getValues()));
  399. }
  400. // DC 1.0/1.1 for Atom 0.3
  401. public function testGetsCategoriesFromAtom03_Dc10()
  402. {
  403. $feed = Zend_Feed_Reader::importString(
  404. file_get_contents($this->_feedSamplePath.'/category/plain/dc10/atom03.xml')
  405. );
  406. $this->assertEquals($this->_expectedCatsDc, (array) $feed->getCategories());
  407. $this->assertEquals(array('topic1','topic2'), array_values($feed->getCategories()->getValues()));
  408. }
  409. public function testGetsCategoriesFromAtom03_Dc11()
  410. {
  411. $feed = Zend_Feed_Reader::importString(
  412. file_get_contents($this->_feedSamplePath.'/category/plain/dc11/atom03.xml')
  413. );
  414. $this->assertEquals($this->_expectedCatsDc, (array) $feed->getCategories());
  415. $this->assertEquals(array('topic1','topic2'), array_values($feed->getCategories()->getValues()));
  416. }
  417. // No Categories In Entry
  418. public function testGetsCategoriesFromAtom10_None()
  419. {
  420. $feed = Zend_Feed_Reader::importString(
  421. file_get_contents($this->_feedSamplePath.'/category/plain/none/atom10.xml')
  422. );
  423. $this->assertEquals(array(), (array) $feed->getCategories());
  424. $this->assertEquals(array(), array_values($feed->getCategories()->getValues()));
  425. }
  426. public function testGetsCategoriesFromAtom03_None()
  427. {
  428. $feed = Zend_Feed_Reader::importString(
  429. file_get_contents($this->_feedSamplePath.'/category/plain/none/atom03.xml')
  430. );
  431. $this->assertEquals(array(), (array) $feed->getCategories());
  432. $this->assertEquals(array(), array_values($feed->getCategories()->getValues()));
  433. }
  434. }