RssViewTest.php 17 KB

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