SproutCMS

This is the code documentation for the SproutCMS project

source of /sprout/tests/textTest.php

  1. <?php
  2. /*
  3.  * Copyright (C) 2017 Karmabunny Pty Ltd.
  4.  *
  5.  * This file is a part of SproutCMS.
  6.  *
  7.  * SproutCMS is free software: you can redistribute it and/or modify it under the terms
  8.  * of the GNU General Public License as published by the Free Software Foundation, either
  9.  * version 2 of the License, or (at your option) any later version.
  10.  *
  11.  * For more information, visit <http://getsproutcms.com>.
  12.  */
  13.  
  14. use Sprout\Helpers\Text;
  15.  
  16.  
  17. class textTest extends PHPUnit_Framework_TestCase
  18. {
  19.  
  20. public function dataLimitWordsHtml()
  21. {
  22. return array(
  23. array('no tags at all', 5, 'no tags at all'),
  24. array('no tags no tags no tags', 5, 'no tags no tags no...'),
  25.  
  26. array('<p>hi there</p>', 5, '<p>hi there</p>'),
  27. array('<p>hi there hi hi hi</p>', 5, '<p>hi there hi hi hi</p>'),
  28. array('<p>hi there hi hi hi hi</p>', 5, '<p>hi there hi hi hi...</p>'),
  29.  
  30. array('<P>hi there</P>', 5, '<P>hi there</p>'),
  31. array('<P>hi there hi hi hi</P>', 5, '<P>hi there hi hi hi</p>'),
  32. array('<P>hi there hi hi hi hi</P>', 5, '<P>hi there hi hi hi...</p>'),
  33.  
  34. array('<P>hi there</p>', 5, '<P>hi there</p>'),
  35. array('<P>hi there hi hi hi hi</p>', 5, '<P>hi there hi hi hi...</p>'),
  36.  
  37. array('Hi <!-- there --> how are you', 5, 'Hi how are you'),
  38. array('Hi <!-- there --> how are you', 4, 'Hi how are you'),
  39. array('Hi <!-- there --> how are you', 3, 'Hi how are...'),
  40.  
  41. array('Hi <br> how are you', 5, 'Hi <br> how are you'),
  42. array('Hi <br/> how are you', 4, 'Hi <br/> how are you'),
  43. array('Hi <br /> how are you', 3, 'Hi <br /> how are...'),
  44.  
  45. array('Hi <img src=""> how are you', 5, 'Hi <img src=""> how are you'),
  46. array('Hi <img> how are you', 4, 'Hi <img> how are you'),
  47. array('Hi <img src="" /> how are you', 3, 'Hi <img src="" /> how are...'),
  48. array('Hi <img src=""/> how are you', 3, 'Hi <img src=""/> how are...'),
  49. array('Hi <img/> how are you', 3, 'Hi <img/> how are...'),
  50.  
  51. array('Hi <img src="AaA"> how are you', 5, 'Hi <img src="AaA"> how are you'),
  52. array('Hi <ImG src="AaA"> how are you', 5, 'Hi <ImG src="AaA"> how are you'),
  53. array('Hi <a href="AaA"> how</a> are you', 5, 'Hi <a href="AaA"> how</a> are you'),
  54. array('Hi <A href="AaA"> how</a> are you', 5, 'Hi <A href="AaA"> how</a> are you'),
  55. array('Hi <a href="AaA"> how</A> are you', 5, 'Hi <a href="AaA"> how</a> are you'),
  56. array('Hi <a HREF="AaA"> how</a> are you', 5, 'Hi <a HREF="AaA"> how</a> are you'),
  57.  
  58. array("Hi <!--\n there --> how are you", 3, 'Hi how are...'),
  59. array("Hi <!-- there \n--> how are you", 3, 'Hi how are...'),
  60. array("Hi <!----> how are you", 3, 'Hi how are...'),
  61. array("Hi <!--\n--> how are you", 3, 'Hi how are...'),
  62.  
  63. array('<h1>hi there</h1>', 5, '<h1>hi there</h1>'),
  64. array('<h1>hi there hi hi hi</h1>', 5, '<h1>hi there hi hi hi</h1>'),
  65. array('<h1>hi there hi hi hi hi</h1>', 5, '<h1>hi there hi hi hi...</h1>'),
  66.  
  67. array('<p>hi there <b>hi</b> hi hi hi</p>', 5, '<p>hi there <b>hi</b> hi hi...</p>'),
  68. array('<p>hi there hi hi he<b>llo</b></p>', 5, '<p>hi there hi hi he...</p>'),
  69. array('<p>hi there hi hi <b>hi hi</b> hi</p>', 5, '<p>hi there hi hi <b>hi...</b></p>'),
  70. array('<p>hi there hi hi <b>hi</b> hi</p>', 5, '<p>hi there hi hi <b>hi</b>...</p>'),
  71.  
  72. "<ul>\n<li>4 star accommodation</li>\n<li>3 Bedroom</li>\n<li>Queen Bed/Singles/Bunk Bed</li>\n<li>All linen provided</li></ul>",
  73. 6,
  74. "<ul>\n<li>4 star accommodation</li>\n<li>3 Bedroom</li>\n<li>Queen...</li></ul>"
  75. ),
  76. );
  77. }
  78.  
  79.  
  80. /**
  81.   * @dataProvider dataLimitWordsHtml
  82.   **/
  83. public function testLimitWordsHtml($html, $limit, $expect)
  84. {
  85. $out = Text::limitWordsHtml($html, $limit);
  86. $this->assertEquals($expect, $out);
  87. }
  88.  
  89.  
  90. public function dataCensor()
  91. {
  92. return [
  93. [' cat ', ' cat ', false],
  94. [' snaKe ', ' ***** ', false],
  95. ['Sussex University', 'Sus*** University', false],
  96. ['Sussex University', 'Sussex University', true],
  97. ['Clowns clown around with their clowny friends', '*****s ***** around with their *****y friends', false],
  98. ['Clowns clown around with their clowny friends', 'Clowns ***** around with their clowny friends', true],
  99. ['Hate leads to the dark side', '***e leads to the dark side', false],
  100. ['Hate leads to the dark side', '**** leads to the dark side', true],
  101. ['Big deal', '********', false],
  102. ['Big deal', '********', true],
  103. ['Big deals', '********s', false],
  104. ['Big deals', 'Big deals', true],
  105. ['Great wad of cash.', 'Great wad of cash.', false],
  106. ['Great wad of cash/', 'Great wad of cash/', true],
  107. ['Great big wad of cash?', 'Great big wad of cash?', false],
  108. ['Great big wad of cash$', 'Great big wad of cash$', true],
  109. ['Greatbigwad', '***********', false],
  110. ['Greatbigwad', '***********', true],
  111. ];
  112. }
  113.  
  114. /**
  115.   * @dataProvider dataCensor
  116.   */
  117. public function testCensor($original, $expect, $partial)
  118. {
  119. $bad_words = ['sex', 'Snake', '*hat*', 'clown', 'big deal', 'great*wad'];
  120. $out = Text::censor($original, $bad_words, '*', $partial);
  121. $this->assertEquals($expect, $out);
  122. }
  123.  
  124.  
  125. public function dataPlain()
  126. {
  127. return array(
  128. array('Hello world how are you?', 10, 'Hello world how are you?'),
  129. array('Hello world how are you?', 3, 'Hello world how...'),
  130. array('Hello world how are you?', 0, 'Hello world how are you?'),
  131.  
  132. array('<p>Hello <b>world</b> how are you?</p>', 10, 'Hello world how are you?'),
  133. array('<p>Hello <b>world</b> how are you?</p>', 3, 'Hello world how...'),
  134. array('<p>Hello <b>world</b> how are you?</p>', 0, 'Hello world how are you?'),
  135.  
  136. array('<p>Hello<br>How are you?</p>', 10, "Hello\nHow are you?"),
  137. array('<p>Hello<br/>How are you?</p>', 3, "Hello\nHow are..."),
  138. array('<p>Hello<br />How are you?</p>', 0, "Hello\nHow are you?"),
  139. array('<p>Hello<BR>How are you?</p>', 0, "Hello\nHow are you?"),
  140. array('<p>Hello<Br>How are you?</p>', 0, "Hello\nHow are you?"),
  141.  
  142. array("<p>Hello<br>\nHow are you?</p>", 10, "Hello\nHow are you?"),
  143. array("<p>Hello<br/>\nHow are you?</p>", 3, "Hello\nHow are..."),
  144. array("<p>Hello<br />\rHow are you?</p>", 0, "Hello\nHow are you?"),
  145. array("<p>Hello<BR>\nHow are you?</p>", 0, "Hello\nHow are you?"),
  146. array("<p>Hello<Br>\nHow are you?</p>", 0, "Hello\nHow are you?"),
  147.  
  148. array("<p>Hello\n<br>\nHow are you?\r</p>", 10, "Hello\nHow are you?"),
  149. array("<p>Hello\n<br/>\nHow\rare you?\n</p>", 3, "Hello\nHow are..."),
  150. array("<p>Hello\n<br />\nHow are\ryou?\n</p>", 0, "Hello\nHow are you?"),
  151. array("\n<p>\nHello\n<BR>\rHow\nare\nyou?\n</p>\n", 0, "Hello\nHow are you?"),
  152. array("<p>\nHello\n<Br>\nHow are you?</p>", 0, "Hello\nHow are you?"),
  153.  
  154. array('<p>Hello</p><p>How are you?</p>', 10, "Hello\n\nHow are you?"),
  155. array('<p>Hello</p><p>How are you?</p>', 3, "Hello\n\nHow are..."),
  156. array('<p>Hello</p><p>How are you?</p>', 0, "Hello\n\nHow are you?"),
  157. array('<p>Hello</P><P>How are you?</p>', 0, "Hello\n\nHow are you?"),
  158. array('<p class="bob">Hello</p><p class="steve">How are you?</p>', 0, "Hello\n\nHow are you?"),
  159.  
  160. array("<p>Hello</p>\n<p>How \n are you?</p>", 10, "Hello\n\nHow are you?"),
  161. array("<p>Hello</p>\n<p>How \n are you?</p>", 3, "Hello\n\nHow are..."),
  162. array("<p>Hello\n</p>\n<p>How are you?</p>", 0, "Hello\n\nHow are you?"),
  163. array("<p>\nHello</P>\n<P>\nHow are you?</p>", 0, "Hello\n\nHow are you?"),
  164. array("<p class='bob'>Hello</p>\n<p class='steve'>How are you?</p>", 0, "Hello\n\nHow are you?"),
  165. array("<p class=\"bob\">Hello</p>\n<p class=\"steve\">How are you?</p>", 0, "Hello\n\nHow are you?"),
  166.  
  167. array('Hello < how are you?', 10, 'Hello < how are you?'),
  168. array('Hello < how are you?', 3, 'Hello < how...'),
  169. array('Hello < how are you?', 0, 'Hello < how are you?'),
  170.  
  171. array('Hello &amp; how are you?', 10, 'Hello & how are you?'),
  172. array('Hello &amp; how are you?', 3, 'Hello & how...'),
  173. array('Hello &amp; how are you?', 0, 'Hello & how are you?'),
  174.  
  175. array('<p>Hello <b>&amp;</b> how are you?</p>', 10, 'Hello & how are you?'),
  176. array('<p>Hello <b>&amp;</b> how are you?</p>', 3, 'Hello & how...'),
  177. array('<p>Hello <b>&amp;</b> how are you?</p>', 2, 'Hello &...'),
  178. array('<p>Hello <b>&amp;</b> how are you?</p>', 1, 'Hello...'),
  179. array('<p>Hello <b>&amp;</b> how are you?</p>', 0, 'Hello & how are you?'),
  180.  
  181. array('<p>Hello <b>&#5772;</b> how are you?</p>', 10, 'Hello ᚌ how are you?'),
  182. array('<p>Hello <b>&#5772;</b> how are you?</p>', 3, 'Hello ᚌ how...'),
  183. array('<p>Hello <b>&#5772;</b> how are you?</p>', 2, 'Hello ᚌ...'),
  184. array('<p>Hello <b>&#5772;</b> how are you?</p>', 1, 'Hello...'),
  185. array('<p>Hello <b>&#5772;</b> how are you?</p>', 0, 'Hello ᚌ how are you?'),
  186.  
  187. array('Hello ‽ how are you?', 10, 'Hello ‽ how are you?'),
  188. array('Hello ‽ how are you?', 3, 'Hello ‽ how...'),
  189. array('Hello ‽ how are you?', 0, 'Hello ‽ how are you?'),
  190.  
  191. array('<p>Hello</p><style>.p { background: #000; }</style><p>World!</p>'),
  192. array('<p>Hello</p><style media="screen">.p { background: #000; }</style><p>World!</p>'),
  193. array('<p>Hello</p><STYLE>.p { background: #000; }</STYLE><p>World!</p>'),
  194.  
  195. array('<p>Hello</p><script>alert("Hello world!");</script><p>World!</p>'),
  196. array('<p>Hello</p><script src="http://google.com">alert("Hello world!");</script><p>World!</p>'),
  197. array('<p>Hello</p><SCRIPT>alert("Hello world!");</SCRIPT><p>World!</p>'),
  198. );
  199. }
  200.  
  201. /**
  202.   * @dataProvider dataPlain
  203.   **/
  204. public function testPlain($html, $limit, $expect)
  205. {
  206. $out = Text::plain($html, $limit);
  207. $this->assertEquals($expect, $out);
  208. }
  209.  
  210.  
  211. /**
  212.   * Text::richtext
  213.   **/
  214. public function dataRichtext()
  215. {
  216. return array(
  217. array("Hello world", '<p>Hello world</p>'),
  218. array("Hello\nworld", '<p>Hello<br>world</p>'),
  219. array("Hello\r\nworld", '<p>Hello<br>world</p>'),
  220. array("Hello\rworld", '<p>Hello<br>world</p>'),
  221. array("Hello & world", '<p>Hello &amp; world</p>'),
  222. array("Hello <b> world", '<p>Hello &lt;b&gt; world</p>'),
  223. array("Hello &\n& world", '<p>Hello &amp;<br>&amp; world</p>'),
  224. array("Hello &\n\n& world", '<p>Hello &amp;<br><br>&amp; world</p>'),
  225. array(42, '<p>42</p>'),
  226. );
  227. }
  228.  
  229.  
  230. /**
  231.   * Text::richtext
  232.   * @dataProvider dataRichtext
  233.   **/
  234. public function testRichtext($text, $expect)
  235. {
  236. $out = Text::richtext($text);
  237. $this->assertEquals($expect, $out);
  238. }
  239.  
  240.  
  241. /**
  242.   * Text::richtext with a block tag specified
  243.   **/
  244. public function dataRichtextBlockTag()
  245. {
  246. return array(
  247. array("Hello world", 'p', '<p>Hello world</p>'),
  248. array("Hello\nworld", 'div', '<div>Hello<br>world</div>'),
  249. array("Hello\nworld", 'DIV', '<div>Hello<br>world</div>'),
  250. array("Hello\nworld", 'DiV', '<div>Hello<br>world</div>'),
  251. array("Hello world", '', 'Hello world'),
  252. array("Hello\nworld", '', 'Hello<br>world'),
  253. array("Hello world", null, 'Hello world'),
  254. array("Hello\nworld", null, 'Hello<br>world'),
  255. );
  256. }
  257.  
  258.  
  259. /**
  260.   * Text::richtext with a block tag specified
  261.   * @dataProvider dataRichtextBlockTag
  262.   **/
  263. public function testRichtextBlockTag($text, $tag, $expect)
  264. {
  265. $out = Text::richtext($text, $tag);
  266. $this->assertEquals($expect, $out);
  267. }
  268.  
  269.  
  270. /**
  271.   * lower_case -> CamelCaps
  272.   **/
  273. public function dataLcToCamelCaps()
  274. {
  275. return array(
  276. array('', ''),
  277. array('word', 'Word'),
  278. array('Word', 'Word'),
  279. array('one_two', 'OneTwo'),
  280. array('word_1', 'Word1'),
  281. array('1_word', '1Word'),
  282. );
  283. }
  284.  
  285. /**
  286.   * @dataProvider dataLcToCamelCaps
  287.   **/
  288. public function testLcToCamelCaps($in, $expect)
  289. {
  290. $this->assertEquals($expect, Text::lc2camelcaps($in));
  291. }
  292.  
  293. /**
  294.   * lower_case -> camelCase
  295.   **/
  296. public function dataLcToCamelCase()
  297. {
  298. return array(
  299. array('', ''),
  300. array('word', 'word'),
  301. array('Word', 'word'),
  302. array('one_two', 'oneTwo'),
  303. array('word_1', 'word1'),
  304. array('1_word', '1Word'),
  305. );
  306. }
  307.  
  308. /**
  309.   * @dataProvider dataLcToCamelCase
  310.   **/
  311. public function testLcToCamelCase($in, $expect)
  312. {
  313. $this->assertEquals($expect, Text::lc2camelcase($in));
  314. }
  315.  
  316. /**
  317.   * camelCase -> lower_case
  318.   **/
  319. public function dataCamelToLc()
  320. {
  321. return array(
  322. array('', ''),
  323. array('word', 'word'),
  324. array('Word', 'word'),
  325. array('OneTwo', 'one_two'),
  326. array('Word1', 'word_1'),
  327. array('1Word', '1_word'),
  328. array('word1', 'word_1'),
  329. );
  330. }
  331.  
  332. /**
  333.   * @dataProvider dataCamelToLc
  334.   **/
  335. public function testCamelToLc($in, $expect)
  336. {
  337. $this->assertEquals($expect, Text::camel2lc($in));
  338. }
  339.  
  340.  
  341. public function dataLimitedSubsetHtml()
  342. {
  343. return [
  344. ['', ''],
  345. ['Test', 'Test'],
  346. ['Test<ul>Dots</ul>', 'TestDots'],
  347. ['Test<ul>Dots</ul>Test', 'TestDotsTest'],
  348. ['Test<b>Bold</b>Test', 'Test<b>Bold</b>Test'],
  349. ['Test > Test', 'Test &gt; Test'],
  350. ['Test < Test', 'Test &lt; Test'],
  351. ['Test " Test', 'Test &quot; Test'],
  352. ['Test b> Test', 'Test b&gt; Test'],
  353. ['Test <b Test', 'Test &lt;b Test'],
  354. ['Test </b Test', 'Test &lt;/b Test'],
  355. ['Test <a href="xx">Link</a> Test', 'Test <a href="xx">Link</a> Test'],
  356. ];
  357. }
  358.  
  359. /**
  360.   * @dataProvider dataLimitedSubsetHtml
  361.   **/
  362. public function testLimitedSubsetHtml($in, $expect)
  363. {
  364. $this->assertEquals($expect, Text::limitedSubsetHtml($in));
  365. }
  366.  
  367.  
  368. public function dataContainsFormTag()
  369. {
  370. return [
  371. ['', false],
  372. ['<p>form</p>', false],
  373. ['<form action="xx">', true],
  374. ['<form<form yep>', true],
  375. ['<FORM action="xx">', true],
  376. ['<script>console.log("<form>");</script>', false],
  377. ['<script type="text/javascript">console.log("<form>");</script>', false],
  378. ['<script>*:after { content: "<form>" }</script>', false],
  379. ['<style type="text/javascript">*:after { content: "<form>" }</style>', false],
  380. ];
  381. }
  382.  
  383. /**
  384.   * @dataProvider dataContainsFormTag
  385.   **/
  386. public function testContainsFormTag($in, $expect)
  387. {
  388. $this->assertEquals($expect, Text::containsFormTag($in));
  389. }
  390. }
  391.