ItemTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. <?php
  2. namespace Suin\RSSWriter;
  3. use XoopsUnit\TestCase;
  4. class ItemTest extends TestCase
  5. {
  6. private $channelInterface = '\Suin\RSSWriter\ChannelInterface';
  7. public function testTitle()
  8. {
  9. $title = uniqid();
  10. $item = new Item();
  11. $this->assertSame($item, $item->title($title));
  12. $this->assertAttributeSame($title, 'title', $item);
  13. }
  14. public function testUrl()
  15. {
  16. $url = uniqid();
  17. $item = new Item();
  18. $this->assertSame($item, $item->url($url));
  19. $this->assertAttributeSame($url, 'url', $item);
  20. }
  21. public function testDescription()
  22. {
  23. $description = uniqid();
  24. $item = new Item();
  25. $this->assertSame($item, $item->description($description));
  26. $this->assertAttributeSame($description, 'description', $item);
  27. }
  28. public function testContentEncoded()
  29. {
  30. $item = new Item();
  31. $this->assertSame($item, $item->contentEncoded('<div>contents</div>'));
  32. $this->assertAttributeSame('<div>contents</div>', 'contentEncoded', $item);
  33. $feed = new Feed();
  34. $channel = new Channel();
  35. $item->appendTo($channel);
  36. $channel->appendTo($feed);
  37. $expected = '<?xml version="1.0" encoding="UTF-8"?>
  38. <rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
  39. <channel>
  40. <title/>
  41. <link/>
  42. <description/>
  43. <item>
  44. <title/>
  45. <link/>
  46. <description/>
  47. <content:encoded><![CDATA[<div>contents</div>]]></content:encoded>
  48. </item>
  49. </channel>
  50. </rss>';
  51. $this->assertXmlStringEqualsXmlString($expected, $feed->render());
  52. }
  53. public function testCategory()
  54. {
  55. $category = uniqid();
  56. $item = new Item();
  57. $this->assertSame($item, $item->category($category));
  58. $this->assertAttributeSame([
  59. [$category, null],
  60. ], 'categories', $item);
  61. }
  62. public function testCategory_with_domain()
  63. {
  64. $category = uniqid();
  65. $domain = uniqid();
  66. $item = new Item();
  67. $this->assertSame($item, $item->category($category, $domain));
  68. $this->assertAttributeSame([
  69. [$category, $domain],
  70. ], 'categories', $item);
  71. }
  72. public function testGuid()
  73. {
  74. $guid = uniqid();
  75. $item = new Item();
  76. $this->assertSame($item, $item->guid($guid));
  77. $this->assertAttributeSame($guid, 'guid', $item);
  78. }
  79. public function testGuid_with_permalink()
  80. {
  81. $item = new Item();
  82. $item->guid('guid', true);
  83. $this->assertAttributeSame(true, 'isPermalink', $item);
  84. $item->guid('guid', false);
  85. $this->assertAttributeSame(false, 'isPermalink', $item);
  86. $item->guid('guid'); // default
  87. $this->assertAttributeSame(false, 'isPermalink', $item);
  88. }
  89. public function testPubDate()
  90. {
  91. $pubDate = mt_rand(1000000, 9999999);
  92. $item = new Item();
  93. $this->assertSame($item, $item->pubDate($pubDate));
  94. $this->assertAttributeSame($pubDate, 'pubDate', $item);
  95. }
  96. public function testAppendTo()
  97. {
  98. $item = new Item();
  99. $channel = $this->getMock($this->channelInterface);
  100. $channel->expects($this->once())->method('addItem')->with($item);
  101. $this->assertSame($item, $item->appendTo($channel));
  102. }
  103. public function testEnclosure()
  104. {
  105. $url = uniqid();
  106. $enclosure = ['url' => $url, 'length' => 0, 'type' => 'audio/mpeg'];
  107. $item = new Item();
  108. $this->assertSame($item, $item->enclosure($url));
  109. $this->assertAttributeSame($enclosure, 'enclosure', $item);
  110. }
  111. public function testAuthor()
  112. {
  113. $author = uniqid();
  114. $item = new Item();
  115. $this->assertSame($item, $item->author($author));
  116. $this->assertAttributeSame($author, 'author', $item);
  117. }
  118. public function testPreferCdata()
  119. {
  120. $item = new Item();
  121. $item->title('<h1>title</h1>');
  122. $item->description('<p>description</p>');
  123. // By default, prefer no CDATA on title and description
  124. $actualXml = $item->asXML()->asXML();
  125. $this->assertContains('<title>&lt;h1&gt;title&lt;/h1&gt;</title>', $actualXml);
  126. $this->assertContains('<description>&lt;p&gt;description&lt;/p&gt;</description>', $actualXml);
  127. // Once prefer-cdata is enabled, title and description is wrapped by CDATA
  128. $item->preferCdata(true);
  129. $actualXml = $item->asXML()->asXML();
  130. $this->assertContains('<title><![CDATA[<h1>title</h1>]]></title>', $actualXml);
  131. $this->assertContains('<description><![CDATA[<p>description</p>]]></description>', $actualXml);
  132. // Of course, prefer-cdata can be disabled again
  133. $item->preferCdata(false);
  134. $actualXml = $item->asXML()->asXML();
  135. $this->assertContains('<title>&lt;h1&gt;title&lt;/h1&gt;</title>', $actualXml);
  136. $this->assertContains('<description>&lt;p&gt;description&lt;/p&gt;</description>', $actualXml);
  137. // And like other APIs `preferCdata` is also fluent interface
  138. $obj = $item->preferCdata(true);
  139. $this->assertSame($obj, $item);
  140. }
  141. public function testAsXML()
  142. {
  143. $now = time();
  144. $nowString = date(DATE_RSS, $now);
  145. $data = [
  146. 'title' => "Venice Film Festival Tries to Quit Sinking",
  147. 'url' => 'http://nytimes.com/2004/12/07FEST.html',
  148. 'description' => "Some of the most heated chatter at the Venice Film Festival this week was about the way that the arrival of the stars at the Palazzo del Cinema was being staged.",
  149. 'categories' => [
  150. ["Grateful Dead", null],
  151. ["MSFT", 'http://www.fool.com/cusips'],
  152. ],
  153. 'guid' => "http://inessential.com/2002/09/01.php#a2",
  154. 'isPermalink' => true,
  155. 'pubDate' => $now,
  156. 'enclosure' => [
  157. 'url' => 'http://link-to-audio-file.com/test.mp3',
  158. 'length' => 4992,
  159. 'type' => 'audio/mpeg'
  160. ],
  161. 'author' => 'John Smith'
  162. ];
  163. $item = new Item();
  164. foreach ($data as $key => $value) {
  165. $this->reveal($item)->attr($key, $value);
  166. }
  167. $expect = "
  168. <item>
  169. <title>{$data['title']}</title>
  170. <link>{$data['url']}</link>
  171. <description>{$data['description']}</description>
  172. <category>{$data['categories'][0][0]}</category>
  173. <category domain=\"{$data['categories'][1][1]}\">{$data['categories'][1][0]}</category>
  174. <guid>{$data['guid']}</guid>
  175. <pubDate>{$nowString}</pubDate>
  176. <enclosure url=\"{$data['enclosure']['url']}\" type=\"{$data['enclosure']['type']}\" length=\"{$data['enclosure']['length']}\"/>
  177. <author>{$data['author']}</author>
  178. </item>
  179. ";
  180. $this->assertXmlStringEqualsXmlString($expect, $item->asXML()->asXML());
  181. }
  182. public function testAsXML_false_permalink()
  183. {
  184. $now = time();
  185. $nowString = date(DATE_RSS, $now);
  186. $data = [
  187. 'title' => "Venice Film Festival Tries to Quit Sinking",
  188. 'url' => 'http://nytimes.com/2004/12/07FEST.html',
  189. 'description' => "Some of the most heated chatter at the Venice Film Festival this week was about the way that the arrival of the stars at the Palazzo del Cinema was being staged.",
  190. 'categories' => [
  191. ["Grateful Dead", null],
  192. ["MSFT", 'http://www.fool.com/cusips'],
  193. ],
  194. 'guid' => "http://inessential.com/2002/09/01.php#a2",
  195. 'isPermalink' => false,
  196. 'pubDate' => $now,
  197. 'enclosure' => [
  198. 'url' => 'http://link-to-audio-file.com/test.mp3',
  199. 'length' => 4992,
  200. 'type' => 'audio/mpeg'
  201. ],
  202. 'author' => 'John Smith'
  203. ];
  204. $item = new Item();
  205. foreach ($data as $key => $value) {
  206. $this->reveal($item)->attr($key, $value);
  207. }
  208. $expect = "
  209. <item>
  210. <title>{$data['title']}</title>
  211. <link>{$data['url']}</link>
  212. <description>{$data['description']}</description>
  213. <category>{$data['categories'][0][0]}</category>
  214. <category domain=\"{$data['categories'][1][1]}\">{$data['categories'][1][0]}</category>
  215. <guid isPermaLink=\"false\">{$data['guid']}</guid>
  216. <pubDate>{$nowString}</pubDate>
  217. <enclosure url=\"{$data['enclosure']['url']}\" type=\"{$data['enclosure']['type']}\" length=\"{$data['enclosure']['length']}\"/>
  218. <author>{$data['author']}</author>
  219. </item>
  220. ";
  221. $this->assertXmlStringEqualsXmlString($expect, $item->asXML()->asXML());
  222. }
  223. public function testAsXML_test_Japanese()
  224. {
  225. $data = [
  226. 'title' => "Venice Film Festival",
  227. 'url' => 'http://nytimes.com/2004/12/07FEST.html',
  228. 'description' => "Some of the most heated chatter at the Venice Film Festival this week was about the way that the arrival of the stars at the Palazzo del Cinema was being staged.",
  229. ];
  230. $item = new Item();
  231. foreach ($data as $key => $value) {
  232. $this->reveal($item)->attr($key, $value);
  233. }
  234. $expect = "
  235. <item>
  236. <title>{$data['title']}</title>
  237. <link>{$data['url']}</link>
  238. <description>{$data['description']}</description>
  239. </item>
  240. ";
  241. $this->assertXmlStringEqualsXmlString($expect, $item->asXML()->asXML());
  242. }
  243. public function test_with_amp()
  244. {
  245. $item = new Item();
  246. $item
  247. ->title('test&test')
  248. ->url('url&url')
  249. ->description('desc&desc');
  250. $expect = '<?xml version="1.0" encoding="UTF-8"?>
  251. <item><title>test&amp;test</title><link>url&amp;url</link><description>desc&amp;desc</description></item>
  252. ';
  253. $this->assertSame($expect, $item->asXML()->asXML());
  254. }
  255. public function test_fail_safe_against_invalid_string()
  256. {
  257. $item = new Item();
  258. $item
  259. ->title("test\0test")
  260. ->url("url\0test")
  261. ->description("desc\0desc");
  262. $expect = '<?xml version="1.0" encoding="UTF-8"?>
  263. <item><title>test</title><link>url</link><description>desc</description></item>
  264. ';
  265. $this->assertSame($expect, $item->asXML()->asXML());
  266. }
  267. }