AtomTest.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  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. /**
  284. * TEXT
  285. * @group ZFRATOMCONTENT
  286. */
  287. public function testGetsContentFromAtom10()
  288. {
  289. $feed = Zend_Feed_Reader::importString(
  290. file_get_contents($this->_feedSamplePath . '/content/plain/atom10.xml')
  291. );
  292. $entry = $feed->current();
  293. $this->assertEquals('Entry Content &amp;', $entry->getContent());
  294. }
  295. /**
  296. * HTML Escaped
  297. * @group ZFRATOMCONTENT
  298. */
  299. public function testGetsContentFromAtom10Html()
  300. {
  301. $feed = Zend_Feed_Reader::importString(
  302. file_get_contents($this->_feedSamplePath . '/content/plain/atom10_Html.xml')
  303. );
  304. $entry = $feed->current();
  305. $this->assertEquals('<p>Entry Content &amp;</p>', $entry->getContent());
  306. }
  307. /**
  308. * HTML CDATA Escaped
  309. * @group ZFRATOMCONTENT
  310. */
  311. public function testGetsContentFromAtom10HtmlCdata()
  312. {
  313. $feed = Zend_Feed_Reader::importString(
  314. file_get_contents($this->_feedSamplePath . '/content/plain/atom10_HtmlCdata.xml')
  315. );
  316. $entry = $feed->current();
  317. $this->assertEquals('<p>Entry Content &amp;</p>', $entry->getContent());
  318. }
  319. /**
  320. * XHTML
  321. * @group ZFRATOMCONTENT
  322. */
  323. public function testGetsContentFromAtom10XhtmlNamespaced()
  324. {
  325. $feed = Zend_Feed_Reader::importString(
  326. file_get_contents($this->_feedSamplePath . '/content/plain/atom10_Xhtml.xml')
  327. );
  328. $entry = $feed->current();
  329. $this->assertEquals('<p class="x:"><em>Entry Content &amp;x:</em></p>', $entry->getContent());
  330. }
  331. /**
  332. * Get Link (Unencoded Text)
  333. */
  334. public function testGetsLinkFromAtom03()
  335. {
  336. $feed = Zend_Feed_Reader::importString(
  337. file_get_contents($this->_feedSamplePath . '/link/plain/atom03.xml')
  338. );
  339. $entry = $feed->current();
  340. $this->assertEquals('http://www.example.com/entry', $entry->getLink());
  341. }
  342. public function testGetsLinkFromAtom10()
  343. {
  344. $feed = Zend_Feed_Reader::importString(
  345. file_get_contents($this->_feedSamplePath . '/link/plain/atom10.xml')
  346. );
  347. $entry = $feed->current();
  348. $this->assertEquals('http://www.example.com/entry', $entry->getLink());
  349. }
  350. public function testGetsLinkFromAtom10_WithNoRelAttribute()
  351. {
  352. $feed = Zend_Feed_Reader::importString(
  353. file_get_contents($this->_feedSamplePath . '/link/plain/atom10-norel.xml')
  354. );
  355. $entry = $feed->current();
  356. $this->assertEquals('http://www.example.com/entry', $entry->getLink());
  357. }
  358. public function testGetsLinkFromAtom10_WithRelativeUrl()
  359. {
  360. $feed = Zend_Feed_Reader::importString(
  361. file_get_contents($this->_feedSamplePath . '/link/plain/atom10-relative.xml')
  362. );
  363. $entry = $feed->current();
  364. $this->assertEquals('http://www.example.com/entry', $entry->getLink());
  365. }
  366. /**
  367. * Get Base Uri
  368. */
  369. public function testGetsBaseUriFromAtom10_FromFeedElement()
  370. {
  371. $feed = Zend_Feed_Reader::importString(
  372. file_get_contents($this->_feedSamplePath . '/baseurl/plain/atom10-feedlevel.xml')
  373. );
  374. $entry = $feed->current();
  375. $this->assertEquals('http://www.example.com', $entry->getBaseUrl());
  376. }
  377. public function testGetsBaseUriFromAtom10_FromEntryElement()
  378. {
  379. $feed = Zend_Feed_Reader::importString(
  380. file_get_contents($this->_feedSamplePath . '/baseurl/plain/atom10-entrylevel.xml')
  381. );
  382. $entry = $feed->current();
  383. $this->assertEquals('http://www.example.com/', $entry->getBaseUrl());
  384. }
  385. /**
  386. * Get Comment HTML Link
  387. */
  388. public function testGetsCommentLinkFromAtom03()
  389. {
  390. $feed = Zend_Feed_Reader::importString(
  391. file_get_contents($this->_feedSamplePath . '/commentlink/plain/atom03.xml')
  392. );
  393. $entry = $feed->current();
  394. $this->assertEquals('http://www.example.com/entry/comments', $entry->getCommentLink());
  395. }
  396. public function testGetsCommentLinkFromAtom10()
  397. {
  398. $feed = Zend_Feed_Reader::importString(
  399. file_get_contents($this->_feedSamplePath . '/commentlink/plain/atom10.xml')
  400. );
  401. $entry = $feed->current();
  402. $this->assertEquals('http://www.example.com/entry/comments', $entry->getCommentLink());
  403. }
  404. public function testGetsCommentLinkFromAtom10_RelativeLinks()
  405. {
  406. $feed = Zend_Feed_Reader::importString(
  407. file_get_contents($this->_feedSamplePath . '/commentlink/plain/atom10-relative.xml')
  408. );
  409. $entry = $feed->current();
  410. $this->assertEquals('http://www.example.com/entry/comments', $entry->getCommentLink());
  411. }
  412. /**
  413. * Get category data
  414. */
  415. // Atom 1.0 (Atom 0.3 never supported categories except via Atom 1.0/Dublin Core extensions)
  416. public function testGetsCategoriesFromAtom10()
  417. {
  418. $feed = Zend_Feed_Reader::importString(
  419. file_get_contents($this->_feedSamplePath.'/category/plain/atom10.xml')
  420. );
  421. $entry = $feed->current();
  422. $this->assertEquals($this->_expectedCats, (array) $entry->getCategories());
  423. $this->assertEquals(array('topic1','Cat & Dog'), array_values($entry->getCategories()->getValues()));
  424. }
  425. public function testGetsCategoriesFromAtom03_Atom10Extension()
  426. {
  427. $feed = Zend_Feed_Reader::importString(
  428. file_get_contents($this->_feedSamplePath.'/category/plain/atom03.xml')
  429. );
  430. $entry = $feed->current();
  431. $this->assertEquals($this->_expectedCats, (array) $entry->getCategories());
  432. $this->assertEquals(array('topic1','Cat & Dog'), array_values($entry->getCategories()->getValues()));
  433. }
  434. // DC 1.0/1.1 for Atom 0.3
  435. public function testGetsCategoriesFromAtom03_Dc10()
  436. {
  437. $feed = Zend_Feed_Reader::importString(
  438. file_get_contents($this->_feedSamplePath.'/category/plain/dc10/atom03.xml')
  439. );
  440. $entry = $feed->current();
  441. $this->assertEquals($this->_expectedCatsDc, (array) $entry->getCategories());
  442. $this->assertEquals(array('topic1','topic2'), array_values($entry->getCategories()->getValues()));
  443. }
  444. public function testGetsCategoriesFromAtom03_Dc11()
  445. {
  446. $feed = Zend_Feed_Reader::importString(
  447. file_get_contents($this->_feedSamplePath.'/category/plain/dc11/atom03.xml')
  448. );
  449. $entry = $feed->current();
  450. $this->assertEquals($this->_expectedCatsDc, (array) $entry->getCategories());
  451. $this->assertEquals(array('topic1','topic2'), array_values($entry->getCategories()->getValues()));
  452. }
  453. // No Categories In Entry
  454. public function testGetsCategoriesFromAtom10_None()
  455. {
  456. $feed = Zend_Feed_Reader::importString(
  457. file_get_contents($this->_feedSamplePath.'/category/plain/none/atom10.xml')
  458. );
  459. $entry = $feed->current();
  460. $this->assertEquals(array(), (array) $entry->getCategories());
  461. $this->assertEquals(array(), array_values($entry->getCategories()->getValues()));
  462. }
  463. public function testGetsCategoriesFromAtom03_None()
  464. {
  465. $feed = Zend_Feed_Reader::importString(
  466. file_get_contents($this->_feedSamplePath.'/category/plain/none/atom03.xml')
  467. );
  468. $entry = $feed->current();
  469. $this->assertEquals(array(), (array) $entry->getCategories());
  470. $this->assertEquals(array(), array_values($entry->getCategories()->getValues()));
  471. }
  472. }