Smilies.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. <?php
  2. /**
  3. * @group formatting
  4. * @group emoji
  5. */
  6. class Tests_Formatting_Smilies extends WP_UnitTestCase {
  7. /**
  8. * Basic Test Content DataProvider
  9. *
  10. * array ( input_txt, converted_output_txt)
  11. */
  12. public function get_smilies_input_output() {
  13. $includes_path = includes_url( 'images/smilies/' );
  14. return array(
  15. array(
  16. 'Lorem ipsum dolor sit amet mauris ;-) Praesent gravida sodales. :lol: Vivamus nec diam in faucibus eu, bibendum varius nec, imperdiet purus est, at augue at lacus malesuada elit dapibus a, :eek: mauris. Cras mauris viverra elit. Nam laoreet viverra. Pellentesque tortor. Nam libero ante, porta urna ut turpis. Nullam wisi magna, :mrgreen: tincidunt nec, sagittis non, fringilla enim. Nam consectetuer nec, ullamcorper pede eu dui odio consequat vel, vehicula tortor quis pede turpis cursus quis, egestas ipsum ultricies ut, eleifend velit. Mauris vestibulum iaculis. Sed in nunc. Vivamus elit porttitor egestas. Mauris purus :?:',
  17. "Lorem ipsum dolor sit amet mauris \xf0\x9f\x98\x89 Praesent gravida sodales. \xf0\x9f\x98\x86 Vivamus nec diam in faucibus eu, bibendum varius nec, imperdiet purus est, at augue at lacus malesuada elit dapibus a, \xf0\x9f\x98\xae mauris. Cras mauris viverra elit. Nam laoreet viverra. Pellentesque tortor. Nam libero ante, porta urna ut turpis. Nullam wisi magna, <img src=\"${includes_path}mrgreen.png\" alt=\":mrgreen:\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" /> tincidunt nec, sagittis non, fringilla enim. Nam consectetuer nec, ullamcorper pede eu dui odio consequat vel, vehicula tortor quis pede turpis cursus quis, egestas ipsum ultricies ut, eleifend velit. Mauris vestibulum iaculis. Sed in nunc. Vivamus elit porttitor egestas. Mauris purus \xe2\x9d\x93",
  18. ),
  19. array(
  20. '<strong>Welcome to the jungle!</strong> We got fun n games! :) We got everything you want 8-) <em>Honey we know the names :)</em>',
  21. "<strong>Welcome to the jungle!</strong> We got fun n games! \xf0\x9f\x99\x82 We got everything you want \xf0\x9f\x98\x8e <em>Honey we know the names \xf0\x9f\x99\x82</em>",
  22. ),
  23. array(
  24. "<strong;)>a little bit of this\na little bit:other: of that :D\n:D a little bit of good\nyeah with a little bit of bad8O",
  25. "<strong;)>a little bit of this\na little bit:other: of that \xf0\x9f\x98\x80\n\xf0\x9f\x98\x80 a little bit of good\nyeah with a little bit of bad8O",
  26. ),
  27. array(
  28. '<strong style="here comes the sun :-D">and I say it\'s allright:D:D',
  29. '<strong style="here comes the sun :-D">and I say it\'s allright:D:D',
  30. ),
  31. array(
  32. '<!-- Woo-hoo, I\'m a comment, baby! :x > -->',
  33. '<!-- Woo-hoo, I\'m a comment, baby! :x > -->',
  34. ),
  35. array(
  36. ':?:P:?::-x:mrgreen:::',
  37. ':?:P:?::-x:mrgreen:::',
  38. ),
  39. );
  40. }
  41. /**
  42. * @dataProvider get_smilies_input_output
  43. *
  44. * Basic Validation Test to confirm that smilies are converted to image
  45. * when use_smilies = 1 and not when use_smilies = 0
  46. */
  47. function test_convert_standard_smilies( $in_txt, $converted_txt ) {
  48. // Standard smilies, use_smilies: ON.
  49. update_option( 'use_smilies', 1 );
  50. smilies_init();
  51. $this->assertSame( $converted_txt, convert_smilies( $in_txt ) );
  52. // Standard smilies, use_smilies: OFF.
  53. update_option( 'use_smilies', 0 );
  54. $this->assertSame( $in_txt, convert_smilies( $in_txt ) );
  55. }
  56. /**
  57. * Custom Smilies Test Content DataProvider
  58. *
  59. * array ( input_txt, converted_output_txt)
  60. */
  61. public function get_custom_smilies_input_output() {
  62. $includes_path = includes_url( 'images/smilies/' );
  63. return array(
  64. array(
  65. 'Peter Brian Gabriel (born 13 February 1950) is a British singer, musician, and songwriter who rose to fame as the lead vocalist and flautist of the progressive rock group Genesis. :monkey:',
  66. 'Peter Brian Gabriel (born 13 February 1950) is a British singer, musician, and songwriter who rose to fame as the lead vocalist and flautist of the progressive rock group Genesis. <img src="' . $includes_path . 'icon_shock_the_monkey.gif" alt=":monkey:" class="wp-smiley" style="height: 1em; max-height: 1em;" />',
  67. ),
  68. array(
  69. 'Star Wars Jedi Knight :arrow: Jedi Academy is a first and third-person shooter action game set in the Star Wars universe. It was developed by Raven Software and published, distributed and marketed by LucasArts in North America and by Activision in the rest of the world. :nervou:',
  70. 'Star Wars Jedi Knight <img src="' . $includes_path . 'icon_arrow.gif" alt=":arrow:" class="wp-smiley" style="height: 1em; max-height: 1em;" /> Jedi Academy is a first and third-person shooter action game set in the Star Wars universe. It was developed by Raven Software and published, distributed and marketed by LucasArts in North America and by Activision in the rest of the world. <img src="' . $includes_path . 'icon_nervou.gif" alt=":nervou:" class="wp-smiley" style="height: 1em; max-height: 1em;" />',
  71. ),
  72. array(
  73. ':arrow: monkey: Lorem ipsum dolor sit amet enim. Etiam ullam :PP <br />corper. Suspendisse a pellentesque dui, non felis.<a> :arrow: :arrow</a>',
  74. '<img src="' . $includes_path . 'icon_arrow.gif" alt=":arrow:" class="wp-smiley" style="height: 1em; max-height: 1em;" /> monkey: Lorem ipsum dolor sit amet enim. Etiam ullam <img src="' . $includes_path . 'icon_tongue.gif" alt=":PP" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <br />corper. Suspendisse a pellentesque dui, non felis.<a> <img src="' . $includes_path . 'icon_arrow.gif" alt=":arrow:" class="wp-smiley" style="height: 1em; max-height: 1em;" /> :arrow</a>',
  75. ),
  76. );
  77. }
  78. /**
  79. * @dataProvider get_custom_smilies_input_output
  80. *
  81. * Validate Custom Smilies are converted to images when use_smilies = 1
  82. */
  83. function test_convert_custom_smilies( $in_txt, $converted_txt ) {
  84. global $wpsmiliestrans;
  85. // Custom smilies, use_smilies: ON.
  86. update_option( 'use_smilies', 1 );
  87. if ( ! isset( $wpsmiliestrans ) ) {
  88. smilies_init();
  89. }
  90. $trans_orig = $wpsmiliestrans; // Save original translations array.
  91. $wpsmiliestrans = array(
  92. ':PP' => 'icon_tongue.gif',
  93. ':arrow:' => 'icon_arrow.gif',
  94. ':monkey:' => 'icon_shock_the_monkey.gif',
  95. ':nervou:' => 'icon_nervou.gif',
  96. );
  97. smilies_init();
  98. $this->assertSame( $converted_txt, convert_smilies( $in_txt ) );
  99. // Standard smilies, use_smilies: OFF.
  100. update_option( 'use_smilies', 0 );
  101. $this->assertSame( $in_txt, convert_smilies( $in_txt ) );
  102. $wpsmiliestrans = $trans_orig; // Reset original translations array.
  103. }
  104. /**
  105. * DataProvider of HTML elements/tags that smilie matches should be ignored in
  106. */
  107. public function get_smilies_ignore_tags() {
  108. return array(
  109. array( 'pre' ),
  110. array( 'code' ),
  111. array( 'script' ),
  112. array( 'style' ),
  113. array( 'textarea' ),
  114. );
  115. }
  116. /**
  117. * Validate Conversion of Smilies is ignored in pre-determined tags
  118. * pre, code, script, style
  119. *
  120. * @ticket 16448
  121. * @dataProvider get_smilies_ignore_tags
  122. */
  123. public function test_ignore_smilies_in_tags( $element ) {
  124. $includes_path = includes_url( 'images/smilies/' );
  125. $in_str = 'Do we ingore smilies ;-) in ' . $element . ' tags <' . $element . ' class="foo">My Content Here :?: </' . $element . '>';
  126. $exp_str = "Do we ingore smilies \xf0\x9f\x98\x89 in $element tags <$element class=\"foo\">My Content Here :?: </$element>";
  127. // Standard smilies, use_smilies: ON.
  128. update_option( 'use_smilies', 1 );
  129. smilies_init();
  130. $this->assertSame( $exp_str, convert_smilies( $in_str ) );
  131. // Standard smilies, use_smilies: OFF.
  132. update_option( 'use_smilies', 0 );
  133. }
  134. /**
  135. * DataProvider of Smilie Combinations
  136. */
  137. public function get_smilies_combinations() {
  138. $includes_path = includes_url( 'images/smilies/' );
  139. return array(
  140. array(
  141. '8-O :-(',
  142. "\xf0\x9f\x98\xaf \xf0\x9f\x99\x81",
  143. ),
  144. array(
  145. '8-) 8-O',
  146. "\xf0\x9f\x98\x8e \xf0\x9f\x98\xaf",
  147. ),
  148. array(
  149. '8-) 8O',
  150. "\xf0\x9f\x98\x8e \xf0\x9f\x98\xaf",
  151. ),
  152. array(
  153. '8-) :-(',
  154. "\xf0\x9f\x98\x8e \xf0\x9f\x99\x81",
  155. ),
  156. array(
  157. '8-) :twisted:',
  158. "\xf0\x9f\x98\x8e \xf0\x9f\x98\x88",
  159. ),
  160. array(
  161. '8O :twisted: :( :? :roll: :mrgreen:',
  162. "\xf0\x9f\x98\xaf \xf0\x9f\x98\x88 \xf0\x9f\x99\x81 \xf0\x9f\x98\x95 \xf0\x9f\x99\x84 <img src=\"{$includes_path}mrgreen.png\" alt=\":mrgreen:\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" />",
  163. ),
  164. );
  165. }
  166. /**
  167. * Validate Combinations of Smilies separated by single space
  168. * are converted correctly
  169. *
  170. * @ticket 20124
  171. * @dataProvider get_smilies_combinations
  172. */
  173. public function test_smilies_combinations( $in_txt, $converted_txt ) {
  174. // Custom smilies, use_smilies: ON.
  175. update_option( 'use_smilies', 1 );
  176. smilies_init();
  177. $this->assertSame( $converted_txt, convert_smilies( $in_txt ) );
  178. // Custom smilies, use_smilies: OFF.
  179. update_option( 'use_smilies', 0 );
  180. $this->assertSame( $in_txt, convert_smilies( $in_txt ) );
  181. }
  182. /**
  183. * DataProvider of Single Smilies input and converted output
  184. */
  185. public function get_single_smilies_input_output() {
  186. $includes_path = includes_url( 'images/smilies/' );
  187. return array(
  188. array(
  189. '8-O :-(',
  190. '8-O :-(',
  191. ),
  192. array(
  193. '8O :) additional text here :)',
  194. '8O <img src="' . $includes_path . 'simple-smile.png" alt=":)" class="wp-smiley" style="height: 1em; max-height: 1em;" /> additional text here <img src="' . $includes_path . 'simple-smile.png" alt=":)" class="wp-smiley" style="height: 1em; max-height: 1em;" />',
  195. ),
  196. array(
  197. ':) :) :) :)',
  198. '<img src="' . $includes_path . 'simple-smile.png" alt=":)" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <img src="' . $includes_path . 'simple-smile.png" alt=":)" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <img src="' . $includes_path . 'simple-smile.png" alt=":)" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <img src="' . $includes_path . 'simple-smile.png" alt=":)" class="wp-smiley" style="height: 1em; max-height: 1em;" />',
  199. ),
  200. );
  201. }
  202. /**
  203. * Validate Smilies are converted for single smilie in
  204. * the $wpsmiliestrans global array
  205. *
  206. * @ticket 25303
  207. * @dataProvider get_single_smilies_input_output
  208. */
  209. public function test_single_smilies_in_wpsmiliestrans( $in_txt, $converted_txt ) {
  210. global $wpsmiliestrans;
  211. // Standard smilies, use_smilies: ON.
  212. update_option( 'use_smilies', 1 );
  213. if ( ! isset( $wpsmiliestrans ) ) {
  214. smilies_init();
  215. }
  216. $orig_trans = $wpsmiliestrans; // Save original tranlations array.
  217. $wpsmiliestrans = array(
  218. ':)' => 'simple-smile.png',
  219. );
  220. smilies_init();
  221. $this->assertSame( $converted_txt, convert_smilies( $in_txt ) );
  222. // Standard smilies, use_smilies: OFF.
  223. update_option( 'use_smilies', 0 );
  224. $this->assertSame( $in_txt, convert_smilies( $in_txt ) );
  225. $wpsmiliestrans = $orig_trans; // Reset original translations array.
  226. }
  227. public function get_spaces_around_smilies() {
  228. $nbsp = "\xC2\xA0";
  229. return array(
  230. array(
  231. 'test :) smile',
  232. "test \xf0\x9f\x99\x82 smile",
  233. ),
  234. array(
  235. 'test &nbsp;:)&nbsp;smile',
  236. "test &nbsp;\xf0\x9f\x99\x82&nbsp;smile",
  237. ),
  238. array(
  239. "test {$nbsp}:){$nbsp}smile",
  240. "test {$nbsp}\xf0\x9f\x99\x82{$nbsp}smile",
  241. ),
  242. );
  243. }
  244. /**
  245. * Check that $wp_smiliessearch pattern will match smilies
  246. * between spaces, but never capture those spaces.
  247. *
  248. * Further check that spaces aren't randomly deleted
  249. * or added when replacing the text with an image.
  250. *
  251. * @ticket 22692
  252. * @dataProvider get_spaces_around_smilies
  253. */
  254. function test_spaces_around_smilies( $in_txt, $converted_txt ) {
  255. // Standard smilies, use_smilies: ON.
  256. update_option( 'use_smilies', 1 );
  257. smilies_init();
  258. $this->assertSame( $converted_txt, convert_smilies( $in_txt ) );
  259. // Standard smilies, use_smilies: OFF.
  260. update_option( 'use_smilies', 0 );
  261. }
  262. /**
  263. * Test to ensure smilies can be removed with a filter
  264. *
  265. * @ticket 35905
  266. */
  267. public function test_smilies_filter_removes_smilies() {
  268. add_filter( 'smilies', array( $this, '_filter_remove_smilies' ) );
  269. smilies_init();
  270. remove_filter( 'smilies', array( $this, '_filter_remove_smilies' ) );
  271. $txt = ':oops: I did it again';
  272. $this->assertSame( $txt, convert_smilies( $txt ) );
  273. }
  274. /**
  275. * Test to ensure smilies can be added with a filter
  276. *
  277. * @ticket 35905
  278. */
  279. public function test_smilies_filter_adds_smilies() {
  280. add_filter( 'smilies', array( $this, '_filter_add_smilies' ) );
  281. smilies_init();
  282. remove_filter( 'smilies', array( $this, '_filter_add_smilies' ) );
  283. $txt = 'You played with my <3';
  284. $expected_txt = 'You played with my \xe2\x9d\xa4';
  285. $this->assertSame( $expected_txt, convert_smilies( $txt ) );
  286. }
  287. public function _filter_remove_smilies( $wpsmiliestrans ) {
  288. unset( $wpsmiliestrans[':oops:'] );
  289. return $wpsmiliestrans;
  290. }
  291. public function _filter_add_smilies( $wpsmiliestrans ) {
  292. $wpsmiliestrans['<3'] = '\xe2\x9d\xa4';
  293. return $wpsmiliestrans;
  294. }
  295. }