RssViewTest.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. <?php
  2. /**
  3. * PHP 5
  4. *
  5. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  6. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  7. *
  8. * Licensed under The MIT License
  9. * For full copyright and license information, please see the LICENSE.txt
  10. * Redistributions of files must retain the above copyright notice
  11. *
  12. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  13. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  14. * @package Cake.Test.Case.View
  15. * @since CakePHP(tm) v 2.5.0
  16. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  17. */
  18. App::uses('Controller', 'Controller');
  19. App::uses('CakeRequest', 'Network');
  20. App::uses('CakeResponse', 'Network');
  21. App::uses('RssView', 'Tools.View');
  22. /**
  23. * RssViewTest
  24. *
  25. */
  26. class RssViewTest extends CakeTestCase {
  27. public $Rss;
  28. public function setUp() {
  29. parent::setUp();
  30. //Configure::write('debug', 0);
  31. $this->Rss = new RssView();
  32. $this->baseUrl = HTTP_BASE;
  33. }
  34. /**
  35. * testTime method
  36. *
  37. * @return void
  38. */
  39. public function testTime() {
  40. $now = time();
  41. $time = $this->Rss->time($now);
  42. $this->assertEquals(date('r', $now), $time);
  43. }
  44. /**
  45. * RssViewTest::testSerialize()
  46. *
  47. * @return void
  48. */
  49. public function testSerialize() {
  50. $Request = new CakeRequest();
  51. $Response = new CakeResponse();
  52. $Controller = new Controller($Request, $Response);
  53. $data = array(
  54. 'channel' => array(
  55. 'title' => 'Channel title',
  56. 'link' => 'http://channel.example.org',
  57. 'description' => 'Channel description'
  58. ),
  59. 'items' => array(
  60. array('title' => 'Title One', 'link' => 'http://example.org/one', 'author' => 'one@example.org', 'description' => 'Content one'),
  61. array('title' => 'Title Two', 'link' => 'http://example.org/two', 'author' => 'two@example.org', 'description' => 'Content two'),
  62. ));
  63. $Controller->set(array('channel' => $data, '_serialize' => 'channel'));
  64. $View = new RssView($Controller);
  65. $result = $View->render(false);
  66. $expected = <<<RSS
  67. <?xml version="1.0" encoding="UTF-8"?>
  68. <rss version="2.0">
  69. <channel>
  70. <title>Channel title</title>
  71. <link>http://channel.example.org</link>
  72. <description>Channel description</description>
  73. <item>
  74. <title>Title One</title>
  75. <link>http://example.org/one</link>
  76. <author>one@example.org</author>
  77. <description>Content one</description>
  78. </item>
  79. <item>
  80. <title>Title Two</title>
  81. <link>http://example.org/two</link>
  82. <author>two@example.org</author>
  83. <description>Content two</description>
  84. </item>
  85. </channel>
  86. </rss>
  87. RSS;
  88. //debug($output); ob_flush();
  89. $this->assertSame('application/rss+xml', $Response->type());
  90. $this->assertTextEquals($expected, $result);
  91. }
  92. /**
  93. * RssViewTest::testSerialize()
  94. *
  95. * @return void
  96. */
  97. public function testSerializeWithPrefixes() {
  98. $Request = new CakeRequest();
  99. $Response = new CakeResponse();
  100. $Controller = new Controller($Request, $Response);
  101. $time = time();
  102. $data = array(
  103. 'channel' => array(
  104. 'title' => 'Channel title',
  105. 'link' => 'http://channel.example.org',
  106. 'description' => 'Channel description',
  107. 'sy:updatePeriod' => 'hourly',
  108. 'sy:updateFrequency' => 1
  109. ),
  110. 'items' => array(
  111. array('title' => 'Title One', 'link' => 'http://example.org/one', 'dc:creator' => 'Author One', 'pubDate' => $time),
  112. array('title' => 'Title Two', 'link' => 'http://example.org/two', 'dc:creator' => 'Author Two', 'pubDate' => $time),
  113. )
  114. );
  115. $Controller->set(array('channel' => $data, '_serialize' => 'channel'));
  116. $View = new RssView($Controller);
  117. $result = $View->render(false);
  118. $time = date('r', $time);
  119. $expected = <<<RSS
  120. <?xml version="1.0" encoding="UTF-8"?>
  121. <rss xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">
  122. <channel>
  123. <title>Channel title</title>
  124. <link>http://channel.example.org</link>
  125. <description>Channel description</description>
  126. <sy:updatePeriod>hourly</sy:updatePeriod>
  127. <sy:updateFrequency>1</sy:updateFrequency>
  128. <item>
  129. <title>Title One</title>
  130. <link>http://example.org/one</link>
  131. <dc:creator>Author One</dc:creator>
  132. <pubDate>$time</pubDate>
  133. </item>
  134. <item>
  135. <title>Title Two</title>
  136. <link>http://example.org/two</link>
  137. <dc:creator>Author Two</dc:creator>
  138. <pubDate>$time</pubDate>
  139. </item>
  140. </channel>
  141. </rss>
  142. RSS;
  143. //debug($result); ob_flush();
  144. $this->assertSame('application/rss+xml', $Response->type());
  145. $this->assertTextEquals($expected, $result);
  146. }
  147. /**
  148. * RssViewTest::testSerializeWithArrayLinks()
  149. *
  150. * `'atom:link' => array('@href' => array(...)` becomes
  151. * '@rel' => 'self', '@type' => 'application/rss+xml' automatically set for atom:link
  152. *
  153. * @return void
  154. */
  155. public function testSerializeWithArrayLinks() {
  156. $Request = new CakeRequest();
  157. $Response = new CakeResponse();
  158. $Controller = new Controller($Request, $Response);
  159. $data = array(
  160. 'channel' => array(
  161. 'title' => 'Channel title',
  162. 'link' => 'http://channel.example.org',
  163. 'atom:link' => array('@href' => array('controller' => 'foo', 'action' => 'bar')),
  164. 'description' => 'Channel description',
  165. ),
  166. 'items' => array(
  167. array('title' => 'Title One', 'link' => array('controller' => 'foo', 'action' => 'bar'), 'description' => 'Content one'),
  168. array('title' => 'Title Two', 'link' => array('controller' => 'foo', 'action' => 'bar'), 'description' => 'Content two'),
  169. )
  170. );
  171. $Controller->set(array('channel' => $data, '_serialize' => 'channel'));
  172. $View = new RssView($Controller);
  173. $result = $View->render(false);
  174. $expected = <<<RSS
  175. <?xml version="1.0" encoding="UTF-8"?>
  176. <rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
  177. <channel>
  178. <title>Channel title</title>
  179. <link>http://channel.example.org</link>
  180. <atom:link href="$this->baseUrl/foo/bar" rel="self" type="application/rss+xml"/>
  181. <description>Channel description</description>
  182. <item>
  183. <title>Title One</title>
  184. <link>$this->baseUrl/foo/bar</link>
  185. <description>Content one</description>
  186. </item>
  187. <item>
  188. <title>Title Two</title>
  189. <link>$this->baseUrl/foo/bar</link>
  190. <description>Content two</description>
  191. </item>
  192. </channel>
  193. </rss>
  194. RSS;
  195. //debug($result); ob_flush();
  196. $this->assertSame('application/rss+xml', $Response->type());
  197. $this->assertTextEquals($expected, $result);
  198. }
  199. /**
  200. * RssViewTest::testSerializeWithContent()
  201. *
  202. * @return void
  203. */
  204. public function testSerializeWithContent() {
  205. $Request = new CakeRequest();
  206. $Response = new CakeResponse();
  207. $Controller = new Controller($Request, $Response);
  208. $data = array(
  209. 'channel' => array(
  210. 'title' => 'Channel title',
  211. 'link' => 'http://channel.example.org',
  212. 'guid' => array('url' => 'http://channel.example.org', '@isPermaLink' => 'true'),
  213. 'atom:link' => array('@href' => array('controller' => 'foo', 'action' => 'bar')),
  214. ),
  215. 'items' => array(
  216. array('title' => 'Title One', 'link' => array('controller' => 'foo', 'action' => 'bar'), 'description' => 'Content one',
  217. 'content:encoded' => 'HTML <img src="http://domain.com/some/link/to/image.jpg"/> <b>content</b> one'),
  218. array('title' => 'Title Two', 'link' => array('controller' => 'foo', 'action' => 'bar'), 'description' => 'Content two',
  219. 'content:encoded' => 'HTML <img src="http://domain.com/some/link/to/image.jpg"/> <b>content</b> two'),
  220. )
  221. );
  222. $Controller->set(array('channel' => $data, '_serialize' => 'channel'));
  223. $View = new RssView($Controller);
  224. $result = $View->render(false);
  225. $expected = <<<RSS
  226. <?xml version="1.0" encoding="UTF-8"?>
  227. <rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/" version="2.0">
  228. <channel>
  229. <title>Channel title</title>
  230. <link>http://channel.example.org</link>
  231. <guid isPermaLink="true">http://channel.example.org</guid>
  232. <atom:link href="$this->baseUrl/foo/bar" rel="self" type="application/rss+xml"/>
  233. <description/>
  234. <item>
  235. <title>Title One</title>
  236. <link>$this->baseUrl/foo/bar</link>
  237. <description>Content one</description>
  238. <content:encoded><![CDATA[HTML <img src="http://domain.com/some/link/to/image.jpg"/> <b>content</b> one]]></content:encoded>
  239. </item>
  240. <item>
  241. <title>Title Two</title>
  242. <link>$this->baseUrl/foo/bar</link>
  243. <description>Content two</description>
  244. <content:encoded><![CDATA[HTML <img src="http://domain.com/some/link/to/image.jpg"/> <b>content</b> two]]></content:encoded>
  245. </item>
  246. </channel>
  247. </rss>
  248. RSS;
  249. //debug($output); ob_flush();
  250. $this->assertTextEquals($expected, $result);
  251. }
  252. /**
  253. * RssViewTest::testSerializeWithCustomNamespace()
  254. *
  255. * @return void
  256. */
  257. public function testSerializeWithCustomNamespace() {
  258. $Request = new CakeRequest();
  259. $Response = new CakeResponse();
  260. $Controller = new Controller($Request, $Response);
  261. $data = array(
  262. 'document' => array(
  263. 'namespace' => array(
  264. 'admin' => 'http://webns.net/mvcb/',
  265. 'rdf' => 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'
  266. )
  267. ),
  268. 'channel' => array(
  269. 'title' => 'Channel title',
  270. 'admin:errorReportsTo' => array('@rdf:resource' => 'mailto:me@example.com')
  271. ),
  272. 'items' => array(
  273. array('title' => 'Title One', 'link' => array('controller' => 'foo', 'action' => 'bar')),
  274. )
  275. );
  276. $Controller->set(array('channel' => $data, '_serialize' => 'channel'));
  277. $View = new RssView($Controller);
  278. $result = $View->render(false);
  279. $expected = <<<RSS
  280. <?xml version="1.0" encoding="UTF-8"?>
  281. <rss xmlns:admin="http://webns.net/mvcb/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" version="2.0">
  282. <channel>
  283. <title>Channel title</title>
  284. <admin:errorReportsTo rdf:resource="mailto:me@example.com"/>
  285. <description/>
  286. <item>
  287. <title>Title One</title>
  288. <link>$this->baseUrl/foo/bar</link>
  289. </item>
  290. </channel>
  291. </rss>
  292. RSS;
  293. //debug($result); ob_flush();
  294. $this->assertTextEquals($expected, $result);
  295. }
  296. }