AtomTest.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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_Entry_AtomTest extends PHPUnit_Framework_TestCase
  34. {
  35. protected $_feedSamplePath = null;
  36. protected $_expectedCats = array();
  37. protected $_expectedCatsDc = array();
  38. public function setup()
  39. {
  40. Zend_Feed_Reader::reset();
  41. if (Zend_Registry::isRegistered('Zend_Locale')) {
  42. $registry = Zend_Registry::getInstance();
  43. unset($registry['Zend_Locale']);
  44. }
  45. $this->_feedSamplePath = dirname(__FILE__) . '/_files/Atom';
  46. $this->_options = Zend_Date::setOptions();
  47. foreach($this->_options as $k=>$v) {
  48. if (is_null($v)) {
  49. unset($this->_options[$k]);
  50. }
  51. }
  52. Zend_Date::setOptions(array('format_type'=>'iso'));
  53. $this->_expectedCats = array(
  54. array(
  55. 'term' => 'topic1',
  56. 'scheme' => 'http://example.com/schema1',
  57. 'label' => 'topic1'
  58. ),
  59. array(
  60. 'term' => 'topic1',
  61. 'scheme' => 'http://example.com/schema2',
  62. 'label' => 'topic1'
  63. ),
  64. array(
  65. 'term' => 'cat_dog',
  66. 'scheme' => 'http://example.com/schema1',
  67. 'label' => 'Cat & Dog'
  68. )
  69. );
  70. $this->_expectedCatsDc = array(
  71. array(
  72. 'term' => 'topic1',
  73. 'scheme' => null,
  74. 'label' => 'topic1'
  75. ),
  76. array(
  77. 'term' => 'topic2',
  78. 'scheme' => null,
  79. 'label' => 'topic2'
  80. )
  81. );
  82. }
  83. public function teardown()
  84. {
  85. Zend_Date::setOptions($this->_options);
  86. }
  87. /**
  88. * Get Id (Unencoded Text)
  89. * @group ZFR003
  90. */
  91. public function testGetsIdFromAtom03()
  92. {
  93. $feed = Zend_Feed_Reader::importString(
  94. file_get_contents($this->_feedSamplePath . '/id/plain/atom03.xml')
  95. );
  96. $entry = $feed->current();
  97. $this->assertEquals('1', $entry->getId());
  98. }
  99. public function testGetsIdFromAtom10()
  100. {
  101. $feed = Zend_Feed_Reader::importString(
  102. file_get_contents($this->_feedSamplePath . '/id/plain/atom10.xml')
  103. );
  104. $entry = $feed->current();
  105. $this->assertEquals('1', $entry->getId());
  106. }
  107. /**
  108. * Get creation date (Unencoded Text)
  109. */
  110. public function testGetsDateCreatedFromAtom03()
  111. {
  112. $feed = Zend_Feed_Reader::importString(
  113. file_get_contents($this->_feedSamplePath . '/datecreated/plain/atom03.xml')
  114. );
  115. $entry = $feed->current();
  116. $edate = new Zend_Date;
  117. $edate->set('2009-03-07T08:03:50Z', Zend_Date::ISO_8601);
  118. $this->assertTrue($edate->equals($entry->getDateCreated()));
  119. }
  120. public function testGetsDateCreatedFromAtom10()
  121. {
  122. $feed = Zend_Feed_Reader::importString(
  123. file_get_contents($this->_feedSamplePath . '/datecreated/plain/atom10.xml')
  124. );
  125. $entry = $feed->current();
  126. $edate = new Zend_Date;
  127. $edate->set('2009-03-07T08:03:50Z', Zend_Date::ISO_8601);
  128. $this->assertTrue($edate->equals($entry->getDateCreated()));
  129. }
  130. /**
  131. * Get modification date (Unencoded Text)
  132. */
  133. public function testGetsDateModifiedFromAtom03()
  134. {
  135. $feed = Zend_Feed_Reader::importString(
  136. file_get_contents($this->_feedSamplePath . '/datemodified/plain/atom03.xml')
  137. );
  138. $entry = $feed->current();
  139. $edate = new Zend_Date;
  140. $edate->set('2009-03-07T08:03:50Z', Zend_Date::ISO_8601);
  141. $this->assertTrue($edate->equals($entry->getDateModified()));
  142. }
  143. public function testGetsDateModifiedFromAtom10()
  144. {
  145. $feed = Zend_Feed_Reader::importString(
  146. file_get_contents($this->_feedSamplePath . '/datemodified/plain/atom10.xml')
  147. );
  148. $entry = $feed->current();
  149. $edate = new Zend_Date;
  150. $edate->set('2009-03-07T08:03:50Z', Zend_Date::ISO_8601);
  151. $this->assertTrue($edate->equals($entry->getDateModified()));
  152. }
  153. /**
  154. * Get Title (Unencoded Text)
  155. */
  156. public function testGetsTitleFromAtom03()
  157. {
  158. $feed = Zend_Feed_Reader::importString(
  159. file_get_contents($this->_feedSamplePath . '/title/plain/atom03.xml')
  160. );
  161. $entry = $feed->current();
  162. $this->assertEquals('Entry Title', $entry->getTitle());
  163. }
  164. public function testGetsTitleFromAtom10()
  165. {
  166. $feed = Zend_Feed_Reader::importString(
  167. file_get_contents($this->_feedSamplePath . '/title/plain/atom10.xml')
  168. );
  169. $entry = $feed->current();
  170. $this->assertEquals('Entry Title', $entry->getTitle());
  171. }
  172. /**
  173. * Get Authors (Unencoded Text)
  174. */
  175. public function testGetsAuthorsFromAtom03()
  176. {
  177. $feed = Zend_Feed_Reader::importString(
  178. file_get_contents($this->_feedSamplePath . '/author/plain/atom03.xml')
  179. );
  180. $authors = array(
  181. array('email'=>'joe@example.com','name'=>'Joe Bloggs','uri'=>'http://www.example.com'),
  182. array('name'=>'Joe Bloggs','uri'=>'http://www.example.com'),
  183. array('name'=>'Joe Bloggs'),
  184. array('email'=>'joe@example.com','uri'=>'http://www.example.com'),
  185. array('uri'=>'http://www.example.com'),
  186. array('email'=>'joe@example.com')
  187. );
  188. $entry = $feed->current();
  189. $this->assertEquals($authors, (array) $entry->getAuthors());
  190. }
  191. public function testGetsAuthorsFromAtom10()
  192. {
  193. $feed = Zend_Feed_Reader::importString(
  194. file_get_contents($this->_feedSamplePath . '/author/plain/atom10.xml')
  195. );
  196. $authors = array(
  197. array('email'=>'joe@example.com','name'=>'Joe Bloggs','uri'=>'http://www.example.com'),
  198. array('name'=>'Joe Bloggs','uri'=>'http://www.example.com'),
  199. array('name'=>'Joe Bloggs'),
  200. array('email'=>'joe@example.com','uri'=>'http://www.example.com'),
  201. array('uri'=>'http://www.example.com'),
  202. array('email'=>'joe@example.com')
  203. );
  204. $entry = $feed->current();
  205. $this->assertEquals($authors, (array) $entry->getAuthors());
  206. }
  207. /**
  208. * Get Author (Unencoded Text)
  209. */
  210. public function testGetsAuthorFromAtom03()
  211. {
  212. $feed = Zend_Feed_Reader::importString(
  213. file_get_contents($this->_feedSamplePath . '/author/plain/atom03.xml')
  214. );
  215. $entry = $feed->current();
  216. $this->assertEquals(array('name'=>'Joe Bloggs','email'=>'joe@example.com','uri'=>'http://www.example.com'), $entry->getAuthor());
  217. }
  218. public function testGetsAuthorFromAtom10()
  219. {
  220. $feed = Zend_Feed_Reader::importString(
  221. file_get_contents($this->_feedSamplePath . '/author/plain/atom10.xml')
  222. );
  223. $entry = $feed->current();
  224. $this->assertEquals(array('name'=>'Joe Bloggs','email'=>'joe@example.com','uri'=>'http://www.example.com'), $entry->getAuthor());
  225. }
  226. /**
  227. * Get Description (Unencoded Text)
  228. */
  229. public function testGetsDescriptionFromAtom03()
  230. {
  231. $feed = Zend_Feed_Reader::importString(
  232. file_get_contents($this->_feedSamplePath . '/description/plain/atom03.xml')
  233. );
  234. $entry = $feed->current();
  235. $this->assertEquals('Entry Description', $entry->getDescription());
  236. }
  237. public function testGetsDescriptionFromAtom10()
  238. {
  239. $feed = Zend_Feed_Reader::importString(
  240. file_get_contents($this->_feedSamplePath . '/description/plain/atom10.xml')
  241. );
  242. $entry = $feed->current();
  243. $this->assertEquals('Entry Description', $entry->getDescription());
  244. }
  245. /**
  246. * Get enclosure
  247. */
  248. public function testGetsEnclosureFromAtom03()
  249. {
  250. $feed = Zend_Feed_Reader::importString(
  251. file_get_contents($this->_feedSamplePath.'/enclosure/plain/atom03.xml')
  252. );
  253. $entry = $feed->current();
  254. $expected = new stdClass();
  255. $expected->url = 'http://www.example.org/myaudiofile.mp3';
  256. $expected->length = '1234';
  257. $expected->type = 'audio/mpeg';
  258. $this->assertEquals($expected, $entry->getEnclosure());
  259. }
  260. public function testGetsEnclosureFromAtom10()
  261. {
  262. $feed = Zend_Feed_Reader::importString(
  263. file_get_contents($this->_feedSamplePath.'/enclosure/plain/atom10.xml')
  264. );
  265. $entry = $feed->current();
  266. $expected = new stdClass();
  267. $expected->url = 'http://www.example.org/myaudiofile.mp3';
  268. $expected->length = '1234';
  269. $expected->type = 'audio/mpeg';
  270. $this->assertEquals($expected, $entry->getEnclosure());
  271. }
  272. /**
  273. * Get Content (Unencoded Text)
  274. */
  275. public function testGetsContentFromAtom03()
  276. {
  277. $feed = Zend_Feed_Reader::importString(
  278. file_get_contents($this->_feedSamplePath . '/content/plain/atom03.xml')
  279. );
  280. $entry = $feed->current();
  281. $this->assertEquals('Entry Content', $entry->getContent());
  282. }
  283. public function testGetsContentFromAtom10()
  284. {
  285. $feed = Zend_Feed_Reader::importString(
  286. file_get_contents($this->_feedSamplePath . '/content/plain/atom10.xml')
  287. );
  288. $entry = $feed->current();
  289. $this->assertEquals('Entry Content', $entry->getContent());
  290. }
  291. /**
  292. * Get Link (Unencoded Text)
  293. */
  294. public function testGetsLinkFromAtom03()
  295. {
  296. $feed = Zend_Feed_Reader::importString(
  297. file_get_contents($this->_feedSamplePath . '/link/plain/atom03.xml')
  298. );
  299. $entry = $feed->current();
  300. $this->assertEquals('http://www.example.com/entry', $entry->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. $entry = $feed->current();
  308. $this->assertEquals('http://www.example.com/entry', $entry->getLink());
  309. }
  310. public function testGetsLinkFromAtom10_WithNoRelAttribute()
  311. {
  312. $feed = Zend_Feed_Reader::importString(
  313. file_get_contents($this->_feedSamplePath . '/link/plain/atom10-norel.xml')
  314. );
  315. $entry = $feed->current();
  316. $this->assertEquals('http://www.example.com/entry', $entry->getLink());
  317. }
  318. public function testGetsLinkFromAtom10_WithRelativeUrl()
  319. {
  320. $feed = Zend_Feed_Reader::importString(
  321. file_get_contents($this->_feedSamplePath . '/link/plain/atom10-relative.xml')
  322. );
  323. $entry = $feed->current();
  324. $this->assertEquals('http://www.example.com/entry', $entry->getLink());
  325. }
  326. /**
  327. * Get Base Uri
  328. */
  329. public function testGetsBaseUriFromAtom10_FromFeedElement()
  330. {
  331. $feed = Zend_Feed_Reader::importString(
  332. file_get_contents($this->_feedSamplePath . '/baseurl/plain/atom10-feedlevel.xml')
  333. );
  334. $entry = $feed->current();
  335. $this->assertEquals('http://www.example.com', $entry->getBaseUrl());
  336. }
  337. public function testGetsBaseUriFromAtom10_FromEntryElement()
  338. {
  339. $feed = Zend_Feed_Reader::importString(
  340. file_get_contents($this->_feedSamplePath . '/baseurl/plain/atom10-entrylevel.xml')
  341. );
  342. $entry = $feed->current();
  343. $this->assertEquals('http://www.example.com/', $entry->getBaseUrl());
  344. }
  345. /**
  346. * Get Comment HTML Link
  347. */
  348. public function testGetsCommentLinkFromAtom03()
  349. {
  350. $feed = Zend_Feed_Reader::importString(
  351. file_get_contents($this->_feedSamplePath . '/commentlink/plain/atom03.xml')
  352. );
  353. $entry = $feed->current();
  354. $this->assertEquals('http://www.example.com/entry/comments', $entry->getCommentLink());
  355. }
  356. public function testGetsCommentLinkFromAtom10()
  357. {
  358. $feed = Zend_Feed_Reader::importString(
  359. file_get_contents($this->_feedSamplePath . '/commentlink/plain/atom10.xml')
  360. );
  361. $entry = $feed->current();
  362. $this->assertEquals('http://www.example.com/entry/comments', $entry->getCommentLink());
  363. }
  364. public function testGetsCommentLinkFromAtom10_RelativeLinks()
  365. {
  366. $feed = Zend_Feed_Reader::importString(
  367. file_get_contents($this->_feedSamplePath . '/commentlink/plain/atom10-relative.xml')
  368. );
  369. $entry = $feed->current();
  370. $this->assertEquals('http://www.example.com/entry/comments', $entry->getCommentLink());
  371. }
  372. /**
  373. * Get category data
  374. */
  375. // Atom 1.0 (Atom 0.3 never supported categories except via Atom 1.0/Dublin Core extensions)
  376. public function testGetsCategoriesFromAtom10()
  377. {
  378. $feed = Zend_Feed_Reader::importString(
  379. file_get_contents($this->_feedSamplePath.'/category/plain/atom10.xml')
  380. );
  381. $entry = $feed->current();
  382. $this->assertEquals($this->_expectedCats, (array) $entry->getCategories());
  383. $this->assertEquals(array('topic1','Cat & Dog'), array_values($entry->getCategories()->getValues()));
  384. }
  385. public function testGetsCategoriesFromAtom03_Atom10Extension()
  386. {
  387. $feed = Zend_Feed_Reader::importString(
  388. file_get_contents($this->_feedSamplePath.'/category/plain/atom03.xml')
  389. );
  390. $entry = $feed->current();
  391. $this->assertEquals($this->_expectedCats, (array) $entry->getCategories());
  392. $this->assertEquals(array('topic1','Cat & Dog'), array_values($entry->getCategories()->getValues()));
  393. }
  394. // DC 1.0/1.1 for Atom 0.3
  395. public function testGetsCategoriesFromAtom03_Dc10()
  396. {
  397. $feed = Zend_Feed_Reader::importString(
  398. file_get_contents($this->_feedSamplePath.'/category/plain/dc10/atom03.xml')
  399. );
  400. $entry = $feed->current();
  401. $this->assertEquals($this->_expectedCatsDc, (array) $entry->getCategories());
  402. $this->assertEquals(array('topic1','topic2'), array_values($entry->getCategories()->getValues()));
  403. }
  404. public function testGetsCategoriesFromAtom03_Dc11()
  405. {
  406. $feed = Zend_Feed_Reader::importString(
  407. file_get_contents($this->_feedSamplePath.'/category/plain/dc11/atom03.xml')
  408. );
  409. $entry = $feed->current();
  410. $this->assertEquals($this->_expectedCatsDc, (array) $entry->getCategories());
  411. $this->assertEquals(array('topic1','topic2'), array_values($entry->getCategories()->getValues()));
  412. }
  413. // No Categories In Entry
  414. public function testGetsCategoriesFromAtom10_None()
  415. {
  416. $feed = Zend_Feed_Reader::importString(
  417. file_get_contents($this->_feedSamplePath.'/category/plain/none/atom10.xml')
  418. );
  419. $entry = $feed->current();
  420. $this->assertEquals(array(), (array) $entry->getCategories());
  421. $this->assertEquals(array(), array_values($entry->getCategories()->getValues()));
  422. }
  423. public function testGetsCategoriesFromAtom03_None()
  424. {
  425. $feed = Zend_Feed_Reader::importString(
  426. file_get_contents($this->_feedSamplePath.'/category/plain/none/atom03.xml')
  427. );
  428. $entry = $feed->current();
  429. $this->assertEquals(array(), (array) $entry->getCategories());
  430. $this->assertEquals(array(), array_values($entry->getCategories()->getValues()));
  431. }
  432. }