AtomTest.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  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-2015 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 'Zend/Feed/Reader.php';
  23. /**
  24. * @category Zend
  25. * @package Zend_Feed
  26. * @subpackage UnitTests
  27. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  28. * @license http://framework.zend.com/license/new-bsd New BSD License
  29. * @group Zend_Feed
  30. * @group Zend_Feed_Reader
  31. */
  32. class Zend_Feed_Reader_Entry_AtomTest extends PHPUnit_Framework_TestCase
  33. {
  34. protected $_feedSamplePath = null;
  35. protected $_expectedCats = array();
  36. protected $_expectedCatsDc = array();
  37. public function setup()
  38. {
  39. Zend_Feed_Reader::reset();
  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. * @group ZFR003
  89. */
  90. public function testGetsIdFromAtom03()
  91. {
  92. $feed = Zend_Feed_Reader::importString(
  93. file_get_contents($this->_feedSamplePath . '/id/plain/atom03.xml')
  94. );
  95. $entry = $feed->current();
  96. $this->assertEquals('1', $entry->getId());
  97. }
  98. public function testGetsIdFromAtom10()
  99. {
  100. $feed = Zend_Feed_Reader::importString(
  101. file_get_contents($this->_feedSamplePath . '/id/plain/atom10.xml')
  102. );
  103. $entry = $feed->current();
  104. $this->assertEquals('1', $entry->getId());
  105. }
  106. /**
  107. * Get creation date (Unencoded Text)
  108. */
  109. public function testGetsDateCreatedFromAtom03()
  110. {
  111. $feed = Zend_Feed_Reader::importString(
  112. file_get_contents($this->_feedSamplePath . '/datecreated/plain/atom03.xml')
  113. );
  114. $entry = $feed->current();
  115. $edate = new Zend_Date;
  116. $edate->set('2009-03-07T08:03:50Z', Zend_Date::ISO_8601);
  117. $this->assertTrue($edate->equals($entry->getDateCreated()));
  118. }
  119. public function testGetsDateCreatedFromAtom10()
  120. {
  121. $feed = Zend_Feed_Reader::importString(
  122. file_get_contents($this->_feedSamplePath . '/datecreated/plain/atom10.xml')
  123. );
  124. $entry = $feed->current();
  125. $edate = new Zend_Date;
  126. $edate->set('2009-03-07T08:03:50Z', Zend_Date::ISO_8601);
  127. $this->assertTrue($edate->equals($entry->getDateCreated()));
  128. }
  129. /**
  130. * Get modification date (Unencoded Text)
  131. */
  132. public function testGetsDateModifiedFromAtom03()
  133. {
  134. $feed = Zend_Feed_Reader::importString(
  135. file_get_contents($this->_feedSamplePath . '/datemodified/plain/atom03.xml')
  136. );
  137. $entry = $feed->current();
  138. $edate = new Zend_Date;
  139. $edate->set('2009-03-07T08:03:50Z', Zend_Date::ISO_8601);
  140. $this->assertTrue($edate->equals($entry->getDateModified()));
  141. }
  142. public function testGetsDateModifiedFromAtom10()
  143. {
  144. $feed = Zend_Feed_Reader::importString(
  145. file_get_contents($this->_feedSamplePath . '/datemodified/plain/atom10.xml')
  146. );
  147. $entry = $feed->current();
  148. $edate = new Zend_Date;
  149. $edate->set('2009-03-07T08:03:50Z', Zend_Date::ISO_8601);
  150. $this->assertTrue($edate->equals($entry->getDateModified()));
  151. }
  152. /**
  153. * Get Title (Unencoded Text)
  154. */
  155. public function testGetsTitleFromAtom03()
  156. {
  157. $feed = Zend_Feed_Reader::importString(
  158. file_get_contents($this->_feedSamplePath . '/title/plain/atom03.xml')
  159. );
  160. $entry = $feed->current();
  161. $this->assertEquals('Entry Title', $entry->getTitle());
  162. }
  163. public function testGetsTitleFromAtom10()
  164. {
  165. $feed = Zend_Feed_Reader::importString(
  166. file_get_contents($this->_feedSamplePath . '/title/plain/atom10.xml')
  167. );
  168. $entry = $feed->current();
  169. $this->assertEquals('Entry Title', $entry->getTitle());
  170. }
  171. /**
  172. * Get Authors (Unencoded Text)
  173. */
  174. public function testGetsAuthorsFromAtom03()
  175. {
  176. $feed = Zend_Feed_Reader::importString(
  177. file_get_contents($this->_feedSamplePath . '/author/plain/atom03.xml')
  178. );
  179. $authors = array(
  180. array('email'=>'joe@example.com','name'=>'Joe Bloggs','uri'=>'http://www.example.com'),
  181. array('name'=>'Joe Bloggs','uri'=>'http://www.example.com'),
  182. array('name'=>'Joe Bloggs'),
  183. array('email'=>'joe@example.com','uri'=>'http://www.example.com'),
  184. array('uri'=>'http://www.example.com'),
  185. array('email'=>'joe@example.com')
  186. );
  187. $entry = $feed->current();
  188. $this->assertEquals($authors, (array) $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. array('email'=>'joe@example.com','name'=>'Joe Bloggs','uri'=>'http://www.example.com'),
  197. array('name'=>'Joe Bloggs','uri'=>'http://www.example.com'),
  198. array('name'=>'Joe Bloggs'),
  199. array('email'=>'joe@example.com','uri'=>'http://www.example.com'),
  200. array('uri'=>'http://www.example.com'),
  201. array('email'=>'joe@example.com')
  202. );
  203. $entry = $feed->current();
  204. $this->assertEquals($authors, (array) $entry->getAuthors());
  205. }
  206. /**
  207. * Get Author (Unencoded Text)
  208. */
  209. public function testGetsAuthorFromAtom03()
  210. {
  211. $feed = Zend_Feed_Reader::importString(
  212. file_get_contents($this->_feedSamplePath . '/author/plain/atom03.xml')
  213. );
  214. $entry = $feed->current();
  215. $this->assertEquals(array('name'=>'Joe Bloggs','email'=>'joe@example.com','uri'=>'http://www.example.com'), $entry->getAuthor());
  216. }
  217. public function testGetsAuthorFromAtom10()
  218. {
  219. $feed = Zend_Feed_Reader::importString(
  220. file_get_contents($this->_feedSamplePath . '/author/plain/atom10.xml')
  221. );
  222. $entry = $feed->current();
  223. $this->assertEquals(array('name'=>'Joe Bloggs','email'=>'joe@example.com','uri'=>'http://www.example.com'), $entry->getAuthor());
  224. }
  225. /**
  226. * Get Description (Unencoded Text)
  227. */
  228. public function testGetsDescriptionFromAtom03()
  229. {
  230. $feed = Zend_Feed_Reader::importString(
  231. file_get_contents($this->_feedSamplePath . '/description/plain/atom03.xml')
  232. );
  233. $entry = $feed->current();
  234. $this->assertEquals('Entry Description', $entry->getDescription());
  235. }
  236. public function testGetsDescriptionFromAtom10()
  237. {
  238. $feed = Zend_Feed_Reader::importString(
  239. file_get_contents($this->_feedSamplePath . '/description/plain/atom10.xml')
  240. );
  241. $entry = $feed->current();
  242. $this->assertEquals('Entry Description', $entry->getDescription());
  243. }
  244. /**
  245. * Get enclosure
  246. */
  247. public function testGetsEnclosureFromAtom03()
  248. {
  249. $feed = Zend_Feed_Reader::importString(
  250. file_get_contents($this->_feedSamplePath.'/enclosure/plain/atom03.xml')
  251. );
  252. $entry = $feed->current();
  253. $expected = new stdClass();
  254. $expected->url = 'http://www.example.org/myaudiofile.mp3';
  255. $expected->length = '1234';
  256. $expected->type = 'audio/mpeg';
  257. $this->assertEquals($expected, $entry->getEnclosure());
  258. }
  259. public function testGetsEnclosureFromAtom10()
  260. {
  261. $feed = Zend_Feed_Reader::importString(
  262. file_get_contents($this->_feedSamplePath.'/enclosure/plain/atom10.xml')
  263. );
  264. $entry = $feed->current();
  265. $expected = new stdClass();
  266. $expected->url = 'http://www.example.org/myaudiofile.mp3';
  267. $expected->length = '1234';
  268. $expected->type = 'audio/mpeg';
  269. $this->assertEquals($expected, $entry->getEnclosure());
  270. }
  271. /**
  272. * Get Content (Unencoded Text)
  273. */
  274. public function testGetsContentFromAtom03()
  275. {
  276. $feed = Zend_Feed_Reader::importString(
  277. file_get_contents($this->_feedSamplePath . '/content/plain/atom03.xml')
  278. );
  279. $entry = $feed->current();
  280. $this->assertEquals('Entry Content', $entry->getContent());
  281. }
  282. /**
  283. * TEXT
  284. * @group ZFRATOMCONTENT
  285. */
  286. public function testGetsContentFromAtom10()
  287. {
  288. $feed = Zend_Feed_Reader::importString(
  289. file_get_contents($this->_feedSamplePath . '/content/plain/atom10.xml')
  290. );
  291. $entry = $feed->current();
  292. $this->assertEquals('Entry Content &amp;', $entry->getContent());
  293. }
  294. /**
  295. * HTML Escaped
  296. * @group ZFRATOMCONTENT
  297. */
  298. public function testGetsContentFromAtom10Html()
  299. {
  300. $feed = Zend_Feed_Reader::importString(
  301. file_get_contents($this->_feedSamplePath . '/content/plain/atom10_Html.xml')
  302. );
  303. $entry = $feed->current();
  304. $this->assertEquals('<p>Entry Content &amp;</p>', $entry->getContent());
  305. }
  306. /**
  307. * HTML CDATA Escaped
  308. * @group ZFRATOMCONTENT
  309. */
  310. public function testGetsContentFromAtom10HtmlCdata()
  311. {
  312. $feed = Zend_Feed_Reader::importString(
  313. file_get_contents($this->_feedSamplePath . '/content/plain/atom10_HtmlCdata.xml')
  314. );
  315. $entry = $feed->current();
  316. $this->assertEquals('<p>Entry Content &amp;</p>', $entry->getContent());
  317. }
  318. /**
  319. * XHTML
  320. * @group ZFRATOMCONTENT
  321. */
  322. public function testGetsContentFromAtom10XhtmlNamespaced()
  323. {
  324. $feed = Zend_Feed_Reader::importString(
  325. file_get_contents($this->_feedSamplePath . '/content/plain/atom10_Xhtml.xml')
  326. );
  327. $entry = $feed->current();
  328. $this->assertEquals('<p class="x:"><em>Entry Content &amp;x:</em></p>', $entry->getContent());
  329. }
  330. /**
  331. * Get Link (Unencoded Text)
  332. */
  333. public function testGetsLinkFromAtom03()
  334. {
  335. $feed = Zend_Feed_Reader::importString(
  336. file_get_contents($this->_feedSamplePath . '/link/plain/atom03.xml')
  337. );
  338. $entry = $feed->current();
  339. $this->assertEquals('http://www.example.com/entry', $entry->getLink());
  340. }
  341. public function testGetsLinkFromAtom10()
  342. {
  343. $feed = Zend_Feed_Reader::importString(
  344. file_get_contents($this->_feedSamplePath . '/link/plain/atom10.xml')
  345. );
  346. $entry = $feed->current();
  347. $this->assertEquals('http://www.example.com/entry', $entry->getLink());
  348. }
  349. public function testGetsLinkFromAtom10_WithNoRelAttribute()
  350. {
  351. $feed = Zend_Feed_Reader::importString(
  352. file_get_contents($this->_feedSamplePath . '/link/plain/atom10-norel.xml')
  353. );
  354. $entry = $feed->current();
  355. $this->assertEquals('http://www.example.com/entry', $entry->getLink());
  356. }
  357. public function testGetsLinkFromAtom10_WithRelativeUrl()
  358. {
  359. $feed = Zend_Feed_Reader::importString(
  360. file_get_contents($this->_feedSamplePath . '/link/plain/atom10-relative.xml')
  361. );
  362. $entry = $feed->current();
  363. $this->assertEquals('http://www.example.com/entry', $entry->getLink());
  364. }
  365. /**
  366. * Get Base Uri
  367. */
  368. public function testGetsBaseUriFromAtom10_FromFeedElement()
  369. {
  370. $feed = Zend_Feed_Reader::importString(
  371. file_get_contents($this->_feedSamplePath . '/baseurl/plain/atom10-feedlevel.xml')
  372. );
  373. $entry = $feed->current();
  374. $this->assertEquals('http://www.example.com', $entry->getBaseUrl());
  375. }
  376. public function testGetsBaseUriFromAtom10_FromEntryElement()
  377. {
  378. $feed = Zend_Feed_Reader::importString(
  379. file_get_contents($this->_feedSamplePath . '/baseurl/plain/atom10-entrylevel.xml')
  380. );
  381. $entry = $feed->current();
  382. $this->assertEquals('http://www.example.com/', $entry->getBaseUrl());
  383. }
  384. /**
  385. * Get Comment HTML Link
  386. */
  387. public function testGetsCommentLinkFromAtom03()
  388. {
  389. $feed = Zend_Feed_Reader::importString(
  390. file_get_contents($this->_feedSamplePath . '/commentlink/plain/atom03.xml')
  391. );
  392. $entry = $feed->current();
  393. $this->assertEquals('http://www.example.com/entry/comments', $entry->getCommentLink());
  394. }
  395. public function testGetsCommentLinkFromAtom10()
  396. {
  397. $feed = Zend_Feed_Reader::importString(
  398. file_get_contents($this->_feedSamplePath . '/commentlink/plain/atom10.xml')
  399. );
  400. $entry = $feed->current();
  401. $this->assertEquals('http://www.example.com/entry/comments', $entry->getCommentLink());
  402. }
  403. public function testGetsCommentLinkFromAtom10_RelativeLinks()
  404. {
  405. $feed = Zend_Feed_Reader::importString(
  406. file_get_contents($this->_feedSamplePath . '/commentlink/plain/atom10-relative.xml')
  407. );
  408. $entry = $feed->current();
  409. $this->assertEquals('http://www.example.com/entry/comments', $entry->getCommentLink());
  410. }
  411. /**
  412. * Get category data
  413. */
  414. // Atom 1.0 (Atom 0.3 never supported categories except via Atom 1.0/Dublin Core extensions)
  415. public function testGetsCategoriesFromAtom10()
  416. {
  417. $feed = Zend_Feed_Reader::importString(
  418. file_get_contents($this->_feedSamplePath.'/category/plain/atom10.xml')
  419. );
  420. $entry = $feed->current();
  421. $this->assertEquals($this->_expectedCats, (array) $entry->getCategories());
  422. $this->assertEquals(array('topic1','Cat & Dog'), array_values($entry->getCategories()->getValues()));
  423. }
  424. public function testGetsCategoriesFromAtom03_Atom10Extension()
  425. {
  426. $feed = Zend_Feed_Reader::importString(
  427. file_get_contents($this->_feedSamplePath.'/category/plain/atom03.xml')
  428. );
  429. $entry = $feed->current();
  430. $this->assertEquals($this->_expectedCats, (array) $entry->getCategories());
  431. $this->assertEquals(array('topic1','Cat & Dog'), array_values($entry->getCategories()->getValues()));
  432. }
  433. // DC 1.0/1.1 for Atom 0.3
  434. public function testGetsCategoriesFromAtom03_Dc10()
  435. {
  436. $feed = Zend_Feed_Reader::importString(
  437. file_get_contents($this->_feedSamplePath.'/category/plain/dc10/atom03.xml')
  438. );
  439. $entry = $feed->current();
  440. $this->assertEquals($this->_expectedCatsDc, (array) $entry->getCategories());
  441. $this->assertEquals(array('topic1','topic2'), array_values($entry->getCategories()->getValues()));
  442. }
  443. public function testGetsCategoriesFromAtom03_Dc11()
  444. {
  445. $feed = Zend_Feed_Reader::importString(
  446. file_get_contents($this->_feedSamplePath.'/category/plain/dc11/atom03.xml')
  447. );
  448. $entry = $feed->current();
  449. $this->assertEquals($this->_expectedCatsDc, (array) $entry->getCategories());
  450. $this->assertEquals(array('topic1','topic2'), array_values($entry->getCategories()->getValues()));
  451. }
  452. // No Categories In Entry
  453. public function testGetsCategoriesFromAtom10_None()
  454. {
  455. $feed = Zend_Feed_Reader::importString(
  456. file_get_contents($this->_feedSamplePath.'/category/plain/none/atom10.xml')
  457. );
  458. $entry = $feed->current();
  459. $this->assertEquals(array(), (array) $entry->getCategories());
  460. $this->assertEquals(array(), array_values($entry->getCategories()->getValues()));
  461. }
  462. public function testGetsCategoriesFromAtom03_None()
  463. {
  464. $feed = Zend_Feed_Reader::importString(
  465. file_get_contents($this->_feedSamplePath.'/category/plain/none/atom03.xml')
  466. );
  467. $entry = $feed->current();
  468. $this->assertEquals(array(), (array) $entry->getCategories());
  469. $this->assertEquals(array(), array_values($entry->getCategories()->getValues()));
  470. }
  471. }