AtomTest.php 16 KB

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