RssViewTest.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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. 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 = $this->baseUrl = php_sapi_name() == 'cli' ? 'http://localhost' : HTTP_BASE;
  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 = array(
  53. 'channel' => array(
  54. 'title' => 'Channel title',
  55. 'link' => 'http://channel.example.org',
  56. 'description' => 'Channel description'
  57. ),
  58. 'items' => array(
  59. array('title' => 'Title One', 'link' => 'http://example.org/one', 'author' => 'one@example.org', 'description' => 'Content one'),
  60. array('title' => 'Title Two', 'link' => 'http://example.org/two', 'author' => 'two@example.org', 'description' => 'Content two'),
  61. ));
  62. $Controller->set(array('channel' => $data, '_serialize' => 'channel'));
  63. $View = new RssView($Controller);
  64. $result = $View->render(false);
  65. $expected = <<<RSS
  66. <?xml version="1.0" encoding="UTF-8"?>
  67. <rss version="2.0">
  68. <channel>
  69. <title>Channel title</title>
  70. <link>http://channel.example.org</link>
  71. <description>Channel description</description>
  72. <item>
  73. <title>Title One</title>
  74. <link>http://example.org/one</link>
  75. <author>one@example.org</author>
  76. <description>Content one</description>
  77. </item>
  78. <item>
  79. <title>Title Two</title>
  80. <link>http://example.org/two</link>
  81. <author>two@example.org</author>
  82. <description>Content two</description>
  83. </item>
  84. </channel>
  85. </rss>
  86. RSS;
  87. //debug($output); ob_flush();
  88. $this->assertSame('application/rss+xml', $Response->type());
  89. $this->assertTextEquals($expected, $result);
  90. }
  91. /**
  92. * RssViewTest::testSerialize()
  93. *
  94. * @return void
  95. */
  96. public function testSerializeWithPrefixes() {
  97. $Request = new CakeRequest();
  98. $Response = new CakeResponse();
  99. $Controller = new Controller($Request, $Response);
  100. $time = time();
  101. $data = array(
  102. 'channel' => array(
  103. 'title' => 'Channel title',
  104. 'link' => 'http://channel.example.org',
  105. 'description' => 'Channel description',
  106. 'sy:updatePeriod' => 'hourly',
  107. 'sy:updateFrequency' => 1
  108. ),
  109. 'items' => array(
  110. array('title' => 'Title One', 'link' => 'http://example.org/one', 'dc:creator' => 'Author One', 'pubDate' => $time),
  111. array('title' => 'Title Two', 'link' => 'http://example.org/two', 'dc:creator' => 'Author Two', 'pubDate' => $time),
  112. )
  113. );
  114. $Controller->set(array('channel' => $data, '_serialize' => 'channel'));
  115. $View = new RssView($Controller);
  116. $result = $View->render(false);
  117. $time = date('r', $time);
  118. $expected = <<<RSS
  119. <?xml version="1.0" encoding="UTF-8"?>
  120. <rss xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">
  121. <channel>
  122. <title>Channel title</title>
  123. <link>http://channel.example.org</link>
  124. <description>Channel description</description>
  125. <sy:updatePeriod>hourly</sy:updatePeriod>
  126. <sy:updateFrequency>1</sy:updateFrequency>
  127. <item>
  128. <title>Title One</title>
  129. <link>http://example.org/one</link>
  130. <dc:creator>Author One</dc:creator>
  131. <pubDate>$time</pubDate>
  132. </item>
  133. <item>
  134. <title>Title Two</title>
  135. <link>http://example.org/two</link>
  136. <dc:creator>Author Two</dc:creator>
  137. <pubDate>$time</pubDate>
  138. </item>
  139. </channel>
  140. </rss>
  141. RSS;
  142. //debug($result); ob_flush();
  143. $this->assertSame('application/rss+xml', $Response->type());
  144. $this->assertTextEquals($expected, $result);
  145. }
  146. /**
  147. * RssViewTest::testSerializeWithArrayLinks()
  148. *
  149. * `'atom:link' => array('@href' => array(...)` becomes
  150. * '@rel' => 'self', '@type' => 'application/rss+xml' automatically set for atom:link
  151. *
  152. * @return void
  153. */
  154. public function testSerializeWithArrayLinks() {
  155. $Request = new CakeRequest();
  156. $Response = new CakeResponse();
  157. $Controller = new Controller($Request, $Response);
  158. $data = array(
  159. 'channel' => array(
  160. 'title' => 'Channel title',
  161. 'link' => 'http://channel.example.org',
  162. 'atom:link' => array('@href' => array('controller' => 'foo', 'action' => 'bar')),
  163. 'description' => 'Channel description',
  164. ),
  165. 'items' => array(
  166. array('title' => 'Title One', 'link' => array('controller' => 'foo', 'action' => 'bar'), 'description' => 'Content one'),
  167. array('title' => 'Title Two', 'link' => array('controller' => 'foo', 'action' => 'bar'), 'description' => 'Content two'),
  168. )
  169. );
  170. $Controller->set(array('channel' => $data, '_serialize' => 'channel'));
  171. $View = new RssView($Controller);
  172. $result = $View->render(false);
  173. $expected = <<<RSS
  174. <?xml version="1.0" encoding="UTF-8"?>
  175. <rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
  176. <channel>
  177. <title>Channel title</title>
  178. <link>http://channel.example.org</link>
  179. <atom:link href="$this->baseUrl/foo/bar" rel="self" type="application/rss+xml"/>
  180. <description>Channel description</description>
  181. <item>
  182. <title>Title One</title>
  183. <link>$this->baseUrl/foo/bar</link>
  184. <description>Content one</description>
  185. </item>
  186. <item>
  187. <title>Title Two</title>
  188. <link>$this->baseUrl/foo/bar</link>
  189. <description>Content two</description>
  190. </item>
  191. </channel>
  192. </rss>
  193. RSS;
  194. //debug($result); ob_flush();
  195. $this->assertSame('application/rss+xml', $Response->type());
  196. $this->assertTextEquals($expected, $result);
  197. }
  198. /**
  199. * RssViewTest::testSerializeWithContent()
  200. *
  201. * @return void
  202. */
  203. public function testSerializeWithContent() {
  204. $Request = new CakeRequest();
  205. $Response = new CakeResponse();
  206. $Controller = new Controller($Request, $Response);
  207. $data = array(
  208. 'channel' => array(
  209. 'title' => 'Channel title',
  210. 'link' => 'http://channel.example.org',
  211. 'guid' => array('url' => 'http://channel.example.org', '@isPermaLink' => 'true'),
  212. 'atom:link' => array('@href' => array('controller' => 'foo', 'action' => 'bar')),
  213. ),
  214. 'items' => array(
  215. array('title' => 'Title One', 'link' => array('controller' => 'foo', 'action' => 'bar'), 'description' => 'Content one',
  216. 'content:encoded' => 'HTML <img src="http://domain.com/some/link/to/image.jpg"/> <b>content</b> one'),
  217. array('title' => 'Title Two', 'link' => array('controller' => 'foo', 'action' => 'bar'), 'description' => 'Content two',
  218. 'content:encoded' => 'HTML <img src="http://domain.com/some/link/to/image.jpg"/> <b>content</b> two'),
  219. )
  220. );
  221. $Controller->set(array('channel' => $data, '_serialize' => 'channel'));
  222. $View = new RssView($Controller);
  223. $result = $View->render(false);
  224. $expected = <<<RSS
  225. <?xml version="1.0" encoding="UTF-8"?>
  226. <rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/" version="2.0">
  227. <channel>
  228. <title>Channel title</title>
  229. <link>http://channel.example.org</link>
  230. <guid isPermaLink="true">http://channel.example.org</guid>
  231. <atom:link href="$this->baseUrl/foo/bar" rel="self" type="application/rss+xml"/>
  232. <description/>
  233. <item>
  234. <title>Title One</title>
  235. <link>$this->baseUrl/foo/bar</link>
  236. <description>Content one</description>
  237. <content:encoded><![CDATA[HTML <img src="http://domain.com/some/link/to/image.jpg"/> <b>content</b> one]]></content:encoded>
  238. </item>
  239. <item>
  240. <title>Title Two</title>
  241. <link>$this->baseUrl/foo/bar</link>
  242. <description>Content two</description>
  243. <content:encoded><![CDATA[HTML <img src="http://domain.com/some/link/to/image.jpg"/> <b>content</b> two]]></content:encoded>
  244. </item>
  245. </channel>
  246. </rss>
  247. RSS;
  248. //debug($output); ob_flush();
  249. $this->assertTextEquals($expected, $result);
  250. }
  251. /**
  252. * RssViewTest::testSerializeWithCustomNamespace()
  253. *
  254. * @return void
  255. */
  256. public function testSerializeWithCustomNamespace() {
  257. $Request = new CakeRequest();
  258. $Response = new CakeResponse();
  259. $Controller = new Controller($Request, $Response);
  260. $data = array(
  261. 'document' => array(
  262. 'namespace' => array(
  263. 'admin' => 'http://webns.net/mvcb/',
  264. 'rdf' => 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'
  265. )
  266. ),
  267. 'channel' => array(
  268. 'title' => 'Channel title',
  269. 'admin:errorReportsTo' => array('@rdf:resource' => 'mailto:me@example.com')
  270. ),
  271. 'items' => array(
  272. array('title' => 'Title One', 'link' => array('controller' => 'foo', 'action' => 'bar')),
  273. )
  274. );
  275. $Controller->set(array('channel' => $data, '_serialize' => 'channel'));
  276. $View = new RssView($Controller);
  277. $result = $View->render(false);
  278. $expected = <<<RSS
  279. <?xml version="1.0" encoding="UTF-8"?>
  280. <rss xmlns:admin="http://webns.net/mvcb/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" version="2.0">
  281. <channel>
  282. <title>Channel title</title>
  283. <admin:errorReportsTo rdf:resource="mailto:me@example.com"/>
  284. <description/>
  285. <item>
  286. <title>Title One</title>
  287. <link>$this->baseUrl/foo/bar</link>
  288. </item>
  289. </channel>
  290. </rss>
  291. RSS;
  292. //debug($result); ob_flush();
  293. $this->assertTextEquals($expected, $result);
  294. }
  295. }