RssViewTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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 = 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($result);
  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);
  143. $this->assertSame('application/rss+xml', $Response->type());
  144. $this->assertTextEquals($expected, $result);
  145. }
  146. /**
  147. * RssViewTest::testSerializeWithUnconfiguredPrefix()
  148. *
  149. * @expectedException RuntimeException
  150. * @return void
  151. */
  152. public function testSerializeWithUnconfiguredPrefix() {
  153. $Request = new CakeRequest();
  154. $Response = new CakeResponse();
  155. $Controller = new Controller($Request, $Response);
  156. $data = array(
  157. 'channel' => array(
  158. 'foo:bar' => 'something',
  159. ),
  160. 'items' => array(
  161. array('title' => 'Title Two'),
  162. )
  163. );
  164. $Controller->set(array('channel' => $data, '_serialize' => 'channel'));
  165. $View = new RssView($Controller);
  166. $result = $View->render(false);
  167. }
  168. /**
  169. * RssViewTest::testSerializeWithArrayLinks()
  170. *
  171. * `'atom:link' => array('@href' => array(...)` becomes
  172. * '@rel' => 'self', '@type' => 'application/rss+xml' automatically set for atom:link
  173. *
  174. * @return void
  175. */
  176. public function testSerializeWithArrayLinks() {
  177. $Request = new CakeRequest();
  178. $Response = new CakeResponse();
  179. $Controller = new Controller($Request, $Response);
  180. $data = array(
  181. 'channel' => array(
  182. 'title' => 'Channel title',
  183. 'link' => 'http://channel.example.org',
  184. 'atom:link' => array('@href' => array('controller' => 'foo', 'action' => 'bar')),
  185. 'description' => 'Channel description',
  186. ),
  187. 'items' => array(
  188. array('title' => 'Title One', 'link' => array('controller' => 'foo', 'action' => 'bar'), 'description' => 'Content one'),
  189. array('title' => 'Title Two', 'link' => array('controller' => 'foo', 'action' => 'bar'), 'description' => 'Content two'),
  190. )
  191. );
  192. $Controller->set(array('channel' => $data, '_serialize' => 'channel'));
  193. $View = new RssView($Controller);
  194. $result = $View->render(false);
  195. $expected = <<<RSS
  196. <?xml version="1.0" encoding="UTF-8"?>
  197. <rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
  198. <channel>
  199. <title>Channel title</title>
  200. <link>http://channel.example.org</link>
  201. <atom:link href="$this->baseUrl/foo/bar" rel="self" type="application/rss+xml"/>
  202. <description>Channel description</description>
  203. <item>
  204. <title>Title One</title>
  205. <link>$this->baseUrl/foo/bar</link>
  206. <description>Content one</description>
  207. </item>
  208. <item>
  209. <title>Title Two</title>
  210. <link>$this->baseUrl/foo/bar</link>
  211. <description>Content two</description>
  212. </item>
  213. </channel>
  214. </rss>
  215. RSS;
  216. //debug($result);
  217. $this->assertSame('application/rss+xml', $Response->type());
  218. $this->assertTextEquals($expected, $result);
  219. }
  220. /**
  221. * RssViewTest::testSerializeWithContent()
  222. *
  223. * @return void
  224. */
  225. public function testSerializeWithContent() {
  226. $Request = new CakeRequest();
  227. $Response = new CakeResponse();
  228. $Controller = new Controller($Request, $Response);
  229. $data = array(
  230. 'channel' => array(
  231. 'title' => 'Channel title',
  232. 'link' => 'http://channel.example.org',
  233. 'guid' => array('url' => 'http://channel.example.org', '@isPermaLink' => 'true'),
  234. 'atom:link' => array('@href' => array('controller' => 'foo', 'action' => 'bar')),
  235. ),
  236. 'items' => array(
  237. array('title' => 'Title One', 'link' => array('controller' => 'foo', 'action' => 'bar'), 'description' => 'Content one',
  238. 'content:encoded' => 'HTML <img src="http://domain.com/some/link/to/image.jpg"/> <b>content</b> one'),
  239. array('title' => 'Title Two', 'link' => array('controller' => 'foo', 'action' => 'bar'), 'description' => 'Content two',
  240. 'content:encoded' => 'HTML <img src="http://domain.com/some/link/to/image.jpg"/> <b>content</b> two'),
  241. )
  242. );
  243. $Controller->set(array('channel' => $data, '_serialize' => 'channel'));
  244. $View = new RssView($Controller);
  245. $result = $View->render(false);
  246. $expected = <<<RSS
  247. <?xml version="1.0" encoding="UTF-8"?>
  248. <rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/" version="2.0">
  249. <channel>
  250. <title>Channel title</title>
  251. <link>http://channel.example.org</link>
  252. <guid isPermaLink="true">http://channel.example.org</guid>
  253. <atom:link href="$this->baseUrl/foo/bar" rel="self" type="application/rss+xml"/>
  254. <description/>
  255. <item>
  256. <title>Title One</title>
  257. <link>$this->baseUrl/foo/bar</link>
  258. <description>Content one</description>
  259. <content:encoded><![CDATA[HTML <img src="http://domain.com/some/link/to/image.jpg"/> <b>content</b> one]]></content:encoded>
  260. </item>
  261. <item>
  262. <title>Title Two</title>
  263. <link>$this->baseUrl/foo/bar</link>
  264. <description>Content two</description>
  265. <content:encoded><![CDATA[HTML <img src="http://domain.com/some/link/to/image.jpg"/> <b>content</b> two]]></content:encoded>
  266. </item>
  267. </channel>
  268. </rss>
  269. RSS;
  270. //debug($output);
  271. $this->assertTextEquals($expected, $result);
  272. }
  273. /**
  274. * RssViewTest::testSerializeWithCustomNamespace()
  275. *
  276. * @return void
  277. */
  278. public function testSerializeWithCustomNamespace() {
  279. $Request = new CakeRequest();
  280. $Response = new CakeResponse();
  281. $Controller = new Controller($Request, $Response);
  282. $data = array(
  283. 'document' => array(
  284. 'namespace' => array(
  285. 'admin' => 'http://webns.net/mvcb/',
  286. 'rdf' => 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'
  287. )
  288. ),
  289. 'channel' => array(
  290. 'title' => 'Channel title',
  291. 'admin:errorReportsTo' => array('@rdf:resource' => 'mailto:me@example.com')
  292. ),
  293. 'items' => array(
  294. array('title' => 'Title One', 'link' => array('controller' => 'foo', 'action' => 'bar')),
  295. )
  296. );
  297. $Controller->set(array('channel' => $data, '_serialize' => 'channel'));
  298. $View = new RssView($Controller);
  299. $result = $View->render(false);
  300. $expected = <<<RSS
  301. <?xml version="1.0" encoding="UTF-8"?>
  302. <rss xmlns:admin="http://webns.net/mvcb/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" version="2.0">
  303. <channel>
  304. <title>Channel title</title>
  305. <admin:errorReportsTo rdf:resource="mailto:me@example.com"/>
  306. <link>$this->baseUrl/</link>
  307. <description/>
  308. <item>
  309. <title>Title One</title>
  310. <link>$this->baseUrl/foo/bar</link>
  311. </item>
  312. </channel>
  313. </rss>
  314. RSS;
  315. //debug($result);
  316. $this->assertTextEquals($expected, $result);
  317. }
  318. /**
  319. * RssViewTest::testSerializeWithImage()
  320. *
  321. * @return void
  322. */
  323. public function testSerializeWithImage() {
  324. $Request = new CakeRequest();
  325. $Response = new CakeResponse();
  326. $Controller = new Controller($Request, $Response);
  327. $url = array('controller' => 'topics', 'action' => 'feed', 'ext' => 'rss');
  328. $data = array(
  329. 'channel' => array(
  330. 'title' => 'Channel title',
  331. 'guid' => array('url' => $url, '@isPermaLink' => 'true'),
  332. 'image' => array(
  333. 'url' => '/img/logo_rss.png',
  334. 'link' => '/'
  335. )
  336. ),
  337. 'items' => array(
  338. array('title' => 'Title One', 'link' => array('controller' => 'foo', 'action' => 'bar')),
  339. )
  340. );
  341. $Controller->set(array('channel' => $data, '_serialize' => 'channel'));
  342. $View = new RssView($Controller);
  343. $result = $View->render(false);
  344. $expected = <<<RSS
  345. <?xml version="1.0" encoding="UTF-8"?>
  346. <rss version="2.0">
  347. <channel>
  348. <title>Channel title</title>
  349. <guid isPermaLink="true">$this->baseUrl/topics/feed.rss</guid>
  350. <image>
  351. <url>$this->baseUrl/img/logo_rss.png</url>
  352. <link>$this->baseUrl/</link>
  353. <title>Channel title</title>
  354. </image>
  355. <link>$this->baseUrl/</link>
  356. <description/>
  357. <item>
  358. <title>Title One</title>
  359. <link>$this->baseUrl/foo/bar</link>
  360. </item>
  361. </channel>
  362. </rss>
  363. RSS;
  364. //debug($result);
  365. $this->assertTextEquals($expected, $result);
  366. }
  367. }