AtomTest.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  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-2009 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-2009 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. 0 => 'joe@example.com (Joe Bloggs)',
  182. 1 => 'Joe Bloggs',
  183. 3 => 'joe@example.com',
  184. 4 => 'http://www.example.com',
  185. 6 => 'jane@example.com (Jane Bloggs)'
  186. );
  187. $entry = $feed->current();
  188. $this->assertEquals($authors, $entry->getAuthors());
  189. }
  190. public function testGetsAuthorsFromAtom10()
  191. {
  192. $feed = Zend_Feed_Reader::importString(
  193. file_get_contents($this->_feedSamplePath . '/author/plain/atom10.xml')
  194. );
  195. $authors = array(
  196. 0 => 'joe@example.com (Joe Bloggs)',
  197. 1 => 'Joe Bloggs',
  198. 3 => 'joe@example.com',
  199. 4 => 'http://www.example.com',
  200. 6 => 'jane@example.com (Jane Bloggs)'
  201. );
  202. $entry = $feed->current();
  203. $this->assertEquals($authors, $entry->getAuthors());
  204. }
  205. /**
  206. * Get Author (Unencoded Text)
  207. */
  208. public function testGetsAuthorFromAtom03()
  209. {
  210. $feed = Zend_Feed_Reader::importString(
  211. file_get_contents($this->_feedSamplePath . '/author/plain/atom03.xml')
  212. );
  213. $entry = $feed->current();
  214. $this->assertEquals('joe@example.com (Joe Bloggs)', $entry->getAuthor());
  215. }
  216. public function testGetsAuthorFromAtom10()
  217. {
  218. $feed = Zend_Feed_Reader::importString(
  219. file_get_contents($this->_feedSamplePath . '/author/plain/atom10.xml')
  220. );
  221. $entry = $feed->current();
  222. $this->assertEquals('joe@example.com (Joe Bloggs)', $entry->getAuthor());
  223. }
  224. /**
  225. * Get Description (Unencoded Text)
  226. */
  227. public function testGetsDescriptionFromAtom03()
  228. {
  229. $feed = Zend_Feed_Reader::importString(
  230. file_get_contents($this->_feedSamplePath . '/description/plain/atom03.xml')
  231. );
  232. $entry = $feed->current();
  233. $this->assertEquals('Entry Description', $entry->getDescription());
  234. }
  235. public function testGetsDescriptionFromAtom10()
  236. {
  237. $feed = Zend_Feed_Reader::importString(
  238. file_get_contents($this->_feedSamplePath . '/description/plain/atom10.xml')
  239. );
  240. $entry = $feed->current();
  241. $this->assertEquals('Entry Description', $entry->getDescription());
  242. }
  243. /**
  244. * Get enclosure
  245. */
  246. public function testGetsEnclosureFromAtom03()
  247. {
  248. $feed = Zend_Feed_Reader::importString(
  249. file_get_contents($this->_feedSamplePath.'/enclosure/plain/atom03.xml')
  250. );
  251. $entry = $feed->current();
  252. $expected = new stdClass();
  253. $expected->url = 'http://www.example.org/myaudiofile.mp3';
  254. $expected->length = '1234';
  255. $expected->type = 'audio/mpeg';
  256. $this->assertEquals($expected, $entry->getEnclosure());
  257. }
  258. public function testGetsEnclosureFromAtom10()
  259. {
  260. $feed = Zend_Feed_Reader::importString(
  261. file_get_contents($this->_feedSamplePath.'/enclosure/plain/atom10.xml')
  262. );
  263. $entry = $feed->current();
  264. $expected = new stdClass();
  265. $expected->url = 'http://www.example.org/myaudiofile.mp3';
  266. $expected->length = '1234';
  267. $expected->type = 'audio/mpeg';
  268. $this->assertEquals($expected, $entry->getEnclosure());
  269. }
  270. /**
  271. * Get Content (Unencoded Text)
  272. */
  273. public function testGetsContentFromAtom03()
  274. {
  275. $feed = Zend_Feed_Reader::importString(
  276. file_get_contents($this->_feedSamplePath . '/content/plain/atom03.xml')
  277. );
  278. $entry = $feed->current();
  279. $this->assertEquals('Entry Content', $entry->getContent());
  280. }
  281. public function testGetsContentFromAtom10()
  282. {
  283. $feed = Zend_Feed_Reader::importString(
  284. file_get_contents($this->_feedSamplePath . '/content/plain/atom10.xml')
  285. );
  286. $entry = $feed->current();
  287. $this->assertEquals('Entry Content', $entry->getContent());
  288. }
  289. /**
  290. * Get Link (Unencoded Text)
  291. */
  292. public function testGetsLinkFromAtom03()
  293. {
  294. $feed = Zend_Feed_Reader::importString(
  295. file_get_contents($this->_feedSamplePath . '/link/plain/atom03.xml')
  296. );
  297. $entry = $feed->current();
  298. $this->assertEquals('http://www.example.com/entry', $entry->getLink());
  299. }
  300. public function testGetsLinkFromAtom10()
  301. {
  302. $feed = Zend_Feed_Reader::importString(
  303. file_get_contents($this->_feedSamplePath . '/link/plain/atom10.xml')
  304. );
  305. $entry = $feed->current();
  306. $this->assertEquals('http://www.example.com/entry', $entry->getLink());
  307. }
  308. public function testGetsLinkFromAtom10_WithNoRelAttribute()
  309. {
  310. $feed = Zend_Feed_Reader::importString(
  311. file_get_contents($this->_feedSamplePath . '/link/plain/atom10-norel.xml')
  312. );
  313. $entry = $feed->current();
  314. $this->assertEquals('http://www.example.com/entry', $entry->getLink());
  315. }
  316. public function testGetsLinkFromAtom10_WithRelativeUrl()
  317. {
  318. $feed = Zend_Feed_Reader::importString(
  319. file_get_contents($this->_feedSamplePath . '/link/plain/atom10-relative.xml')
  320. );
  321. $entry = $feed->current();
  322. $this->assertEquals('http://www.example.com/entry', $entry->getLink());
  323. }
  324. /**
  325. * Get Base Uri
  326. */
  327. public function testGetsBaseUriFromAtom10_FromFeedElement()
  328. {
  329. $feed = Zend_Feed_Reader::importString(
  330. file_get_contents($this->_feedSamplePath . '/baseurl/plain/atom10-feedlevel.xml')
  331. );
  332. $entry = $feed->current();
  333. $this->assertEquals('http://www.example.com', $entry->getBaseUrl());
  334. }
  335. public function testGetsBaseUriFromAtom10_FromEntryElement()
  336. {
  337. $feed = Zend_Feed_Reader::importString(
  338. file_get_contents($this->_feedSamplePath . '/baseurl/plain/atom10-entrylevel.xml')
  339. );
  340. $entry = $feed->current();
  341. $this->assertEquals('http://www.example.com/', $entry->getBaseUrl());
  342. }
  343. /**
  344. * Get Comment HTML Link
  345. */
  346. public function testGetsCommentLinkFromAtom03()
  347. {
  348. $feed = Zend_Feed_Reader::importString(
  349. file_get_contents($this->_feedSamplePath . '/commentlink/plain/atom03.xml')
  350. );
  351. $entry = $feed->current();
  352. $this->assertEquals('http://www.example.com/entry/comments', $entry->getCommentLink());
  353. }
  354. public function testGetsCommentLinkFromAtom10()
  355. {
  356. $feed = Zend_Feed_Reader::importString(
  357. file_get_contents($this->_feedSamplePath . '/commentlink/plain/atom10.xml')
  358. );
  359. $entry = $feed->current();
  360. $this->assertEquals('http://www.example.com/entry/comments', $entry->getCommentLink());
  361. }
  362. public function testGetsCommentLinkFromAtom10_RelativeLinks()
  363. {
  364. $feed = Zend_Feed_Reader::importString(
  365. file_get_contents($this->_feedSamplePath . '/commentlink/plain/atom10-relative.xml')
  366. );
  367. $entry = $feed->current();
  368. $this->assertEquals('http://www.example.com/entry/comments', $entry->getCommentLink());
  369. }
  370. /**
  371. * Get category data
  372. */
  373. // Atom 1.0 (Atom 0.3 never supported categories except via Atom 1.0/Dublin Core extensions)
  374. public function testGetsCategoriesFromAtom10()
  375. {
  376. $feed = Zend_Feed_Reader::importString(
  377. file_get_contents($this->_feedSamplePath.'/category/plain/atom10.xml')
  378. );
  379. $entry = $feed->current();
  380. $this->assertEquals($this->_expectedCats, (array) $entry->getCategories());
  381. $this->assertEquals(array('topic1','Cat & Dog'), array_values($entry->getCategories()->getValues()));
  382. }
  383. public function testGetsCategoriesFromAtom03_Atom10Extension()
  384. {
  385. $feed = Zend_Feed_Reader::importString(
  386. file_get_contents($this->_feedSamplePath.'/category/plain/atom03.xml')
  387. );
  388. $entry = $feed->current();
  389. $this->assertEquals($this->_expectedCats, (array) $entry->getCategories());
  390. $this->assertEquals(array('topic1','Cat & Dog'), array_values($entry->getCategories()->getValues()));
  391. }
  392. // DC 1.0/1.1 for Atom 0.3
  393. public function testGetsCategoriesFromAtom03_Dc10()
  394. {
  395. $feed = Zend_Feed_Reader::importString(
  396. file_get_contents($this->_feedSamplePath.'/category/plain/dc10/atom03.xml')
  397. );
  398. $entry = $feed->current();
  399. $this->assertEquals($this->_expectedCatsDc, (array) $entry->getCategories());
  400. $this->assertEquals(array('topic1','topic2'), array_values($entry->getCategories()->getValues()));
  401. }
  402. public function testGetsCategoriesFromAtom03_Dc11()
  403. {
  404. $feed = Zend_Feed_Reader::importString(
  405. file_get_contents($this->_feedSamplePath.'/category/plain/dc11/atom03.xml')
  406. );
  407. $entry = $feed->current();
  408. $this->assertEquals($this->_expectedCatsDc, (array) $entry->getCategories());
  409. $this->assertEquals(array('topic1','topic2'), array_values($entry->getCategories()->getValues()));
  410. }
  411. // No Categories In Entry
  412. public function testGetsCategoriesFromAtom10_None()
  413. {
  414. $feed = Zend_Feed_Reader::importString(
  415. file_get_contents($this->_feedSamplePath.'/category/plain/none/atom10.xml')
  416. );
  417. $entry = $feed->current();
  418. $this->assertEquals(array(), (array) $entry->getCategories());
  419. $this->assertEquals(array(), array_values($entry->getCategories()->getValues()));
  420. }
  421. public function testGetsCategoriesFromAtom03_None()
  422. {
  423. $feed = Zend_Feed_Reader::importString(
  424. file_get_contents($this->_feedSamplePath.'/category/plain/none/atom03.xml')
  425. );
  426. $entry = $feed->current();
  427. $this->assertEquals(array(), (array) $entry->getCategories());
  428. $this->assertEquals(array(), array_values($entry->getCategories()->getValues()));
  429. }
  430. }