RssViewTest.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  1. <?php
  2. /**
  3. * PHP 5
  4. *
  5. * Licensed under The MIT License
  6. * For full copyright and license information, please see the LICENSE.txt
  7. * Redistributions of files must retain the above copyright notice
  8. *
  9. * @author Mark Scherer
  10. * @license http://opensource.org/licenses/mit-license.php MIT
  11. */
  12. App::uses('Controller', 'Controller');
  13. App::uses('CakeRequest', 'Network');
  14. App::uses('CakeResponse', 'Network');
  15. App::uses('RssView', 'Tools.View');
  16. /**
  17. * RssViewTest
  18. *
  19. */
  20. class RssViewTest extends CakeTestCase {
  21. public $Rss;
  22. public $baseUrl;
  23. /**
  24. * RssViewTest::setUp()
  25. *
  26. * @return void
  27. */
  28. public function setUp() {
  29. parent::setUp();
  30. $this->Rss = new RssView();
  31. $this->baseUrl = trim(Router::url('/', true), '/');
  32. }
  33. /**
  34. * TestTime method
  35. *
  36. * @return void
  37. */
  38. public function testTime() {
  39. $now = time();
  40. $time = $this->Rss->time($now);
  41. $this->assertEquals(date('r', $now), $time);
  42. }
  43. /**
  44. * RssViewTest::testSerialize()
  45. *
  46. * @return void
  47. */
  48. public function testSerialize() {
  49. $Request = new CakeRequest();
  50. $Response = new CakeResponse();
  51. $Controller = new Controller($Request, $Response);
  52. $data = [
  53. 'channel' => [
  54. 'title' => 'Channel title',
  55. 'link' => 'http://channel.example.org',
  56. 'description' => 'Channel description'
  57. ],
  58. 'items' => [
  59. ['title' => 'Title One', 'link' => 'http://example.org/one',
  60. 'author' => 'one@example.org', 'description' => 'Content one',
  61. 'source' => ['url' => 'http://foo.bar']],
  62. ['title' => 'Title Two', 'link' => 'http://example.org/two',
  63. 'author' => 'two@example.org', 'description' => 'Content two',
  64. 'source' => ['url' => 'http://foo.bar', 'content' => 'Foo bar']],
  65. ]
  66. ];
  67. $Controller->set(['channel' => $data, '_serialize' => 'channel']);
  68. $View = new RssView($Controller);
  69. $result = $View->render(false);
  70. $expected = <<<RSS
  71. <?xml version="1.0" encoding="UTF-8"?>
  72. <rss version="2.0">
  73. <channel>
  74. <title>Channel title</title>
  75. <link>http://channel.example.org</link>
  76. <description>Channel description</description>
  77. <item>
  78. <title>Title One</title>
  79. <link>http://example.org/one</link>
  80. <author>one@example.org</author>
  81. <description>Content one</description>
  82. <source url="http://foo.bar">http://foo.bar</source>
  83. </item>
  84. <item>
  85. <title>Title Two</title>
  86. <link>http://example.org/two</link>
  87. <author>two@example.org</author>
  88. <description>Content two</description>
  89. <source url="http://foo.bar">Foo bar</source>
  90. </item>
  91. </channel>
  92. </rss>
  93. RSS;
  94. $this->assertSame('application/rss+xml', $Response->type());
  95. $this->assertTextEquals($expected, $result);
  96. }
  97. /**
  98. * RssViewTest::testSerialize()
  99. *
  100. * @return void
  101. */
  102. public function testSerializeWithPrefixes() {
  103. $Request = new CakeRequest();
  104. $Response = new CakeResponse();
  105. $Controller = new Controller($Request, $Response);
  106. $time = time();
  107. $data = [
  108. 'channel' => [
  109. 'title' => 'Channel title',
  110. 'link' => 'http://channel.example.org',
  111. 'description' => 'Channel description',
  112. 'sy:updatePeriod' => 'hourly',
  113. 'sy:updateFrequency' => 1
  114. ],
  115. 'items' => [
  116. ['title' => 'Title One', 'link' => 'http://example.org/one',
  117. 'dc:creator' => 'Author One', 'pubDate' => $time],
  118. ['title' => 'Title Two', 'link' => 'http://example.org/two',
  119. 'dc:creator' => 'Author Two', 'pubDate' => $time,
  120. 'source' => 'http://foo.bar'],
  121. ]
  122. ];
  123. $Controller->set(['channel' => $data, '_serialize' => 'channel']);
  124. $View = new RssView($Controller);
  125. $result = $View->render(false);
  126. $time = date('r', $time);
  127. $expected = <<<RSS
  128. <?xml version="1.0" encoding="UTF-8"?>
  129. <rss xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">
  130. <channel>
  131. <title>Channel title</title>
  132. <link>http://channel.example.org</link>
  133. <description>Channel description</description>
  134. <sy:updatePeriod>hourly</sy:updatePeriod>
  135. <sy:updateFrequency>1</sy:updateFrequency>
  136. <item>
  137. <title>Title One</title>
  138. <link>http://example.org/one</link>
  139. <dc:creator>Author One</dc:creator>
  140. <pubDate>$time</pubDate>
  141. </item>
  142. <item>
  143. <title>Title Two</title>
  144. <link>http://example.org/two</link>
  145. <dc:creator>Author Two</dc:creator>
  146. <pubDate>$time</pubDate>
  147. <source url="http://foo.bar">http://foo.bar</source>
  148. </item>
  149. </channel>
  150. </rss>
  151. RSS;
  152. //debug($result);
  153. $this->assertSame('application/rss+xml', $Response->type());
  154. $this->assertTextEquals($expected, $result);
  155. }
  156. /**
  157. * RssViewTest::testSerializeWithUnconfiguredPrefix()
  158. *
  159. * @expectedException RuntimeException
  160. * @return void
  161. */
  162. public function testSerializeWithUnconfiguredPrefix() {
  163. $Request = new CakeRequest();
  164. $Response = new CakeResponse();
  165. $Controller = new Controller($Request, $Response);
  166. $data = [
  167. 'channel' => [
  168. 'foo:bar' => 'something',
  169. ],
  170. 'items' => [
  171. ['title' => 'Title Two'],
  172. ]
  173. ];
  174. $Controller->set(['channel' => $data, '_serialize' => 'channel']);
  175. $View = new RssView($Controller);
  176. $result = $View->render(false);
  177. }
  178. /**
  179. * RssViewTest::testSerializeWithArrayLinks()
  180. *
  181. * `'atom:link' => array('@href' => array(...)` becomes
  182. * '@rel' => 'self', '@type' => 'application/rss+xml' automatically set for atom:link
  183. *
  184. * @return void
  185. */
  186. public function testSerializeWithArrayLinks() {
  187. $Request = new CakeRequest();
  188. $Response = new CakeResponse();
  189. $Controller = new Controller($Request, $Response);
  190. $data = [
  191. 'channel' => [
  192. 'title' => 'Channel title',
  193. 'link' => 'http://channel.example.org',
  194. 'atom:link' => ['@href' => ['controller' => 'foo', 'action' => 'bar']],
  195. 'description' => 'Channel description',
  196. ],
  197. 'items' => [
  198. ['title' => 'Title One', 'link' => ['controller' => 'foo', 'action' => 'bar'], 'description' => 'Content one'],
  199. ['title' => 'Title Two', 'link' => ['controller' => 'foo', 'action' => 'bar'], 'description' => 'Content two'],
  200. ]
  201. ];
  202. $Controller->set(['channel' => $data, '_serialize' => 'channel']);
  203. $View = new RssView($Controller);
  204. $result = $View->render(false);
  205. $expected = <<<RSS
  206. <?xml version="1.0" encoding="UTF-8"?>
  207. <rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
  208. <channel>
  209. <title>Channel title</title>
  210. <link>http://channel.example.org</link>
  211. <atom:link href="$this->baseUrl/foo/bar" rel="self" type="application/rss+xml"/>
  212. <description>Channel description</description>
  213. <item>
  214. <title>Title One</title>
  215. <link>$this->baseUrl/foo/bar</link>
  216. <description>Content one</description>
  217. </item>
  218. <item>
  219. <title>Title Two</title>
  220. <link>$this->baseUrl/foo/bar</link>
  221. <description>Content two</description>
  222. </item>
  223. </channel>
  224. </rss>
  225. RSS;
  226. //debug($result);
  227. $this->assertSame('application/rss+xml', $Response->type());
  228. $this->assertTextEquals($expected, $result);
  229. }
  230. /**
  231. * RssViewTest::testSerializeWithContent()
  232. *
  233. * @return void
  234. */
  235. public function testSerializeWithContent() {
  236. $Request = new CakeRequest();
  237. $Response = new CakeResponse();
  238. $Controller = new Controller($Request, $Response);
  239. $data = [
  240. 'channel' => [
  241. 'title' => 'Channel title',
  242. 'link' => 'http://channel.example.org',
  243. 'guid' => ['url' => 'http://channel.example.org', '@isPermaLink' => 'true'],
  244. 'atom:link' => ['@href' => ['controller' => 'foo', 'action' => 'bar']],
  245. ],
  246. 'items' => [
  247. ['title' => 'Title One', 'link' => ['controller' => 'foo', 'action' => 'bar'], 'description' => 'Content one',
  248. 'content:encoded' => 'HTML <img src="http://domain.com/some/link/to/image.jpg"/> <b>content</b> one'],
  249. ['title' => 'Title Two', 'link' => ['controller' => 'foo', 'action' => 'bar'], 'description' => 'Content two',
  250. 'content:encoded' => 'HTML <img src="http://domain.com/some/link/to/image.jpg"/> <b>content</b> two'],
  251. ]
  252. ];
  253. $Controller->set(['channel' => $data, '_serialize' => 'channel']);
  254. $View = new RssView($Controller);
  255. $result = $View->render(false);
  256. $expected = <<<RSS
  257. <?xml version="1.0" encoding="UTF-8"?>
  258. <rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/" version="2.0">
  259. <channel>
  260. <title>Channel title</title>
  261. <link>http://channel.example.org</link>
  262. <guid isPermaLink="true">http://channel.example.org</guid>
  263. <atom:link href="$this->baseUrl/foo/bar" rel="self" type="application/rss+xml"/>
  264. <description/>
  265. <item>
  266. <title>Title One</title>
  267. <link>$this->baseUrl/foo/bar</link>
  268. <description>Content one</description>
  269. <content:encoded><![CDATA[HTML <img src="http://domain.com/some/link/to/image.jpg"/> <b>content</b> one]]></content:encoded>
  270. </item>
  271. <item>
  272. <title>Title Two</title>
  273. <link>$this->baseUrl/foo/bar</link>
  274. <description>Content two</description>
  275. <content:encoded><![CDATA[HTML <img src="http://domain.com/some/link/to/image.jpg"/> <b>content</b> two]]></content:encoded>
  276. </item>
  277. </channel>
  278. </rss>
  279. RSS;
  280. //debug($output);
  281. $this->assertTextEquals($expected, $result);
  282. }
  283. /**
  284. * RssViewTest::testSerializeWithCustomNamespace()
  285. *
  286. * @return void
  287. */
  288. public function testSerializeWithCustomNamespace() {
  289. $Request = new CakeRequest();
  290. $Response = new CakeResponse();
  291. $Controller = new Controller($Request, $Response);
  292. $data = [
  293. 'document' => [
  294. 'namespace' => [
  295. 'admin' => 'http://webns.net/mvcb/',
  296. 'rdf' => 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'
  297. ]
  298. ],
  299. 'channel' => [
  300. 'title' => 'Channel title',
  301. 'admin:errorReportsTo' => ['@rdf:resource' => 'mailto:me@example.com']
  302. ],
  303. 'items' => [
  304. ['title' => 'Title One', 'link' => ['controller' => 'foo', 'action' => 'bar']],
  305. ]
  306. ];
  307. $Controller->set(['channel' => $data, '_serialize' => 'channel']);
  308. $View = new RssView($Controller);
  309. $result = $View->render(false);
  310. $expected = <<<RSS
  311. <?xml version="1.0" encoding="UTF-8"?>
  312. <rss xmlns:admin="http://webns.net/mvcb/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" version="2.0">
  313. <channel>
  314. <title>Channel title</title>
  315. <admin:errorReportsTo rdf:resource="mailto:me@example.com"/>
  316. <link>$this->baseUrl/</link>
  317. <description/>
  318. <item>
  319. <title>Title One</title>
  320. <link>$this->baseUrl/foo/bar</link>
  321. </item>
  322. </channel>
  323. </rss>
  324. RSS;
  325. //debug($result);
  326. $this->assertTextEquals($expected, $result);
  327. }
  328. /**
  329. * RssViewTest::testSerializeWithImage()
  330. *
  331. * @return void
  332. */
  333. public function testSerializeWithImage() {
  334. $Request = new CakeRequest();
  335. $Response = new CakeResponse();
  336. $Controller = new Controller($Request, $Response);
  337. $url = ['controller' => 'topics', 'action' => 'feed', 'ext' => 'rss'];
  338. $data = [
  339. 'channel' => [
  340. 'title' => 'Channel title',
  341. 'guid' => ['url' => $url, '@isPermaLink' => 'true'],
  342. 'image' => [
  343. 'url' => '/img/logo_rss.png',
  344. 'link' => '/'
  345. ]
  346. ],
  347. 'items' => [
  348. ['title' => 'Title One', 'link' => ['controller' => 'foo', 'action' => 'bar']],
  349. ]
  350. ];
  351. $Controller->set(['channel' => $data, '_serialize' => 'channel']);
  352. $View = new RssView($Controller);
  353. $result = $View->render(false);
  354. $expected = <<<RSS
  355. <?xml version="1.0" encoding="UTF-8"?>
  356. <rss version="2.0">
  357. <channel>
  358. <title>Channel title</title>
  359. <guid isPermaLink="true">$this->baseUrl/topics/feed.rss</guid>
  360. <image>
  361. <url>$this->baseUrl/img/logo_rss.png</url>
  362. <link>$this->baseUrl/</link>
  363. <title>Channel title</title>
  364. </image>
  365. <link>$this->baseUrl/</link>
  366. <description/>
  367. <item>
  368. <title>Title One</title>
  369. <link>$this->baseUrl/foo/bar</link>
  370. </item>
  371. </channel>
  372. </rss>
  373. RSS;
  374. $this->assertTextEquals($expected, $result);
  375. }
  376. /**
  377. * RssViewTest::testSerializeWithCategories()
  378. *
  379. * @return void
  380. */
  381. public function testSerializeWithCategories() {
  382. $Request = new CakeRequest();
  383. $Response = new CakeResponse();
  384. $Controller = new Controller($Request, $Response);
  385. $data = [
  386. 'channel' => [
  387. 'title' => 'Channel title',
  388. 'link' => 'http://channel.example.org',
  389. 'category' => 'IT/Internet/Web development & more',
  390. ],
  391. 'items' => [
  392. ['title' => 'Title One', 'link' => ['controller' => 'foo', 'action' => 'bar'], 'description' => 'Content one',
  393. 'category' => 'Internet'],
  394. ['title' => 'Title Two', 'link' => ['controller' => 'foo', 'action' => 'bar'], 'description' => 'Content two',
  395. 'category' => ['News', 'Tutorial'],
  396. 'comments' => ['controller' => 'foo', 'action' => 'bar', 'ext' => 'rss']],
  397. ]
  398. ];
  399. $Controller->set(['channel' => $data, '_serialize' => 'channel']);
  400. $View = new RssView($Controller);
  401. $result = $View->render(false);
  402. $expected = <<<RSS
  403. <?xml version="1.0" encoding="UTF-8"?>
  404. <rss version="2.0">
  405. <channel>
  406. <title>Channel title</title>
  407. <link>http://channel.example.org</link>
  408. <category>IT/Internet/Web development &amp; more</category>
  409. <description/>
  410. <item>
  411. <title>Title One</title>
  412. <link>$this->baseUrl/foo/bar</link>
  413. <description>Content one</description>
  414. <category>Internet</category>
  415. </item>
  416. <item>
  417. <title>Title Two</title>
  418. <link>$this->baseUrl/foo/bar</link>
  419. <description>Content two</description>
  420. <category>News</category>
  421. <category>Tutorial</category>
  422. <comments>$this->baseUrl/foo/bar.rss</comments>
  423. </item>
  424. </channel>
  425. </rss>
  426. RSS;
  427. $this->assertTextEquals($expected, $result);
  428. }
  429. /**
  430. * RssViewTest::testSerializeWithEnclosure()
  431. *
  432. * @return void
  433. */
  434. public function testSerializeWithEnclosure() {
  435. $Request = new CakeRequest();
  436. $Response = new CakeResponse();
  437. $Controller = new Controller($Request, $Response);
  438. $data = [
  439. 'channel' => [
  440. 'title' => 'Channel title',
  441. 'link' => 'http://channel.example.org',
  442. ],
  443. 'items' => [
  444. ['title' => 'Title One', 'link' => ['controller' => 'foo', 'action' => 'bar'], 'description' => 'Content one',
  445. 'enclosure' => ['url' => 'http://www.example.com/media/3d.wmv', 'length' => 78645, 'type' => 'video/wmv']],
  446. ]
  447. ];
  448. $Controller->set(['channel' => $data, '_serialize' => 'channel']);
  449. $View = new RssView($Controller);
  450. $result = $View->render(false);
  451. $expected = <<<RSS
  452. <?xml version="1.0" encoding="UTF-8"?>
  453. <rss version="2.0">
  454. <channel>
  455. <title>Channel title</title>
  456. <link>http://channel.example.org</link>
  457. <description/>
  458. <item>
  459. <title>Title One</title>
  460. <link>$this->baseUrl/foo/bar</link>
  461. <description>Content one</description>
  462. <enclosure url="http://www.example.com/media/3d.wmv" length="78645" type="video/wmv"/>
  463. </item>
  464. </channel>
  465. </rss>
  466. RSS;
  467. $this->assertTextEquals($expected, $result);
  468. }
  469. /**
  470. * RssViewTest::testSerializeWithCustomTags()
  471. *
  472. * @return void
  473. */
  474. public function testSerializeWithCustomTags() {
  475. $Request = new CakeRequest();
  476. $Response = new CakeResponse();
  477. $Controller = new Controller($Request, $Response);
  478. $data = [
  479. 'channel' => [
  480. 'title' => 'Channel title',
  481. 'link' => 'http://channel.example.org',
  482. ],
  483. 'items' => [
  484. ['title' => 'Title One', 'link' => ['controller' => 'foo', 'action' => 'bar'], 'description' => 'Content one',
  485. 'foo' => ['@url' => 'http://www.example.com/media/3d.wmv', '@length' => 78645, '@type' => 'video/wmv']],
  486. ]
  487. ];
  488. $Controller->set(['channel' => $data, '_serialize' => 'channel']);
  489. $View = new RssView($Controller);
  490. $result = $View->render(false);
  491. $expected = <<<RSS
  492. <?xml version="1.0" encoding="UTF-8"?>
  493. <rss version="2.0">
  494. <channel>
  495. <title>Channel title</title>
  496. <link>http://channel.example.org</link>
  497. <description/>
  498. <item>
  499. <title>Title One</title>
  500. <link>$this->baseUrl/foo/bar</link>
  501. <description>Content one</description>
  502. <foo url="http://www.example.com/media/3d.wmv" length="78645" type="video/wmv"/>
  503. </item>
  504. </channel>
  505. </rss>
  506. RSS;
  507. $this->assertTextEquals($expected, $result);
  508. }
  509. /**
  510. * RssViewTest::testSerializeWithSpecialChars()
  511. *
  512. * @return void
  513. */
  514. public function testSerializeWithSpecialChars() {
  515. $Request = new CakeRequest();
  516. $Response = new CakeResponse();
  517. $Controller = new Controller($Request, $Response);
  518. $data = [
  519. 'channel' => [
  520. 'title' => 'Channel title with äöü umlauts and <!> special chars',
  521. 'link' => 'http://channel.example.org',
  522. ],
  523. 'items' => [
  524. [
  525. 'title' => 'A <unsafe title',
  526. 'link' => ['controller' => 'foo', 'action' => 'bar'],
  527. 'description' => 'My content "&" and <other> stuff here should also be escaped safely'],
  528. ]
  529. ];
  530. $Controller->set(['channel' => $data, '_serialize' => 'channel']);
  531. $View = new RssView($Controller);
  532. $result = $View->render(false);
  533. $expected = <<<RSS
  534. <?xml version="1.0" encoding="UTF-8"?>
  535. <rss version="2.0">
  536. <channel>
  537. <title>Channel title with äöü umlauts and &lt;!&gt; special chars</title>
  538. <link>http://channel.example.org</link>
  539. <description/>
  540. <item>
  541. <title>A &lt;unsafe title</title>
  542. <link>$this->baseUrl/foo/bar</link>
  543. <description>My content "&amp;" and &lt;other&gt; stuff here should also be escaped safely</description>
  544. </item>
  545. </channel>
  546. </rss>
  547. RSS;
  548. $this->assertTextEquals($expected, $result);
  549. }
  550. }