SproutCMS

This is the code documentation for the SproutCMS project

source of /sprout/tests/encHelperTest.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\Enc;
  15. use Sprout\Helpers\Itemlist;
  16.  
  17.  
  18. /**
  19. * Test suite
  20. **/
  21. class encHelperTest extends PHPUnit_Framework_TestCase
  22. {
  23.  
  24. public function testCleanfunky()
  25. {
  26. $this->assertEquals(Enc::cleanfunky('a'), 'a');
  27. $this->assertEquals(Enc::cleanfunky('&'), '&');
  28. $this->assertEquals(Enc::cleanfunky('ê'), 'ê');
  29.  
  30. // UTF-8 chars
  31. $this->assertEquals("\303\252", 'ê');
  32. $this->assertEquals(Enc::cleanfunky("\303\252"), 'ê');
  33. $this->assertEquals("\xC3\xAA", 'ê');
  34. $this->assertEquals(Enc::cleanfunky("\xC3\xAA"), 'ê');
  35.  
  36. // Spaces (border case to 0x1F)
  37. $this->assertEquals(Enc::cleanfunky("\x20"), ' ');
  38. $this->assertEquals(Enc::cleanfunky(' '), ' ');
  39.  
  40. // Some disallowed stuff
  41. $this->assertEquals(Enc::cleanfunky("\x1A"), '');
  42. $this->assertEquals(Enc::cleanfunky("\0"), '');
  43. $this->assertEquals(Enc::cleanfunky("\x1F"), '');
  44.  
  45. // Some allowed stuff
  46. $this->assertEquals(Enc::cleanfunky("\t"), "\t");
  47. $this->assertEquals(Enc::cleanfunky("\n"), "\n");
  48. $this->assertEquals(Enc::cleanfunky("\r"), "\r");
  49.  
  50. // Non-string inputs
  51. $this->assertEquals(Enc::cleanfunky(null), '');
  52. $this->assertEquals(Enc::cleanfunky(array()), '');
  53. $this->assertEquals(Enc::cleanfunky(new Itemlist), '');
  54. $this->assertEquals(Enc::cleanfunky(new stdClass), '');
  55. $this->assertEquals(Enc::cleanfunky(123), '123');
  56. $this->assertEquals(Enc::cleanfunky(123.45), '123.45');
  57. $this->assertEquals(Enc::cleanfunky(123e12), '123e12');
  58. }
  59.  
  60. public function testHtml()
  61. {
  62. $this->assertEquals(Enc::html('a'), 'a');
  63. $this->assertEquals(Enc::html('&'), '&amp;');
  64. $this->assertEquals(Enc::html('"'), '&quot;');
  65. $this->assertEquals(Enc::html('>'), '&gt;');
  66. $this->assertEquals(Enc::html('<'), '&lt;');
  67. $this->assertEquals(Enc::html('ê'), 'ê');
  68. $this->assertEquals(Enc::html("\0"), '');
  69. $this->assertEquals(Enc::html("\x1A"), '');
  70. $this->assertEquals(Enc::html(array()), '');
  71.  
  72. $this->assertEquals('&amp;amp;', Enc::html('&amp;'));
  73. $this->assertEquals('&amp;quot;', Enc::html('&quot;'));
  74. $this->assertEquals('&amp;gt;', Enc::html('&gt;'));
  75. $this->assertEquals('&amp;lt;', Enc::html('&lt;'));
  76. }
  77.  
  78. public function htmlNoDup()
  79. {
  80. $this->assertEquals(Enc::htmlNoDup('a'), 'a');
  81. $this->assertEquals(Enc::htmlNoDup('&'), '&amp;');
  82. $this->assertEquals(Enc::htmlNoDup('"'), '&quot;');
  83. $this->assertEquals(Enc::htmlNoDup('>'), '&gt;');
  84. $this->assertEquals(Enc::htmlNoDup('<'), '&lt;');
  85. $this->assertEquals(Enc::htmlNoDup('ê'), 'ê');
  86. $this->assertEquals(Enc::htmlNoDup("\0"), '');
  87. $this->assertEquals(Enc::htmlNoDup("\x1A"), '');
  88. $this->assertEquals(Enc::htmlNoDup(array()), '');
  89.  
  90. $this->assertEquals('&amp;', Enc::htmlNoDup('&amp;'));
  91. $this->assertEquals('&quot;', Enc::htmlNoDup('&quot;'));
  92. $this->assertEquals('&gt;', Enc::htmlNoDup('&gt;'));
  93. $this->assertEquals('&lt;', Enc::htmlNoDup('&lt;'));
  94. }
  95.  
  96. public function textXml()
  97. {
  98. $this->assertEquals(Enc::xml('a'), 'a');
  99. $this->assertEquals(Enc::xml('&'), '&amp;');
  100. $this->assertEquals(Enc::xml('"'), '&quot;');
  101. $this->assertEquals(Enc::xml('>'), '&gt;');
  102. $this->assertEquals(Enc::xml('<'), '&lt;');
  103. $this->assertEquals(Enc::xml('&amp;'), '&amp;amp;');
  104. $this->assertEquals(Enc::xml('ê'), 'ê');
  105. $this->assertEquals(Enc::xml("\0"), '');
  106. $this->assertEquals(Enc::xml("\x1A"), '');
  107. $this->assertEquals(Enc::xml(array()), '');
  108. }
  109.  
  110. public function textUrl()
  111. {
  112. $this->assertEquals(Enc::url('a'), 'a');
  113. $this->assertEquals(Enc::url('1'), '1');
  114. $this->assertEquals(Enc::url('hello?'), 'hello%3F');
  115. $this->assertEquals(Enc::url('hello how are you'), 'hello+how+are+you');
  116. $this->assertEquals(Enc::url('ê'), '%C3%AA');
  117. $this->assertEquals(Enc::url("\0"), '');
  118. $this->assertEquals(Enc::url("\x1A"), '');
  119. $this->assertEquals(Enc::url(array()), '');
  120. }
  121.  
  122. public function testId()
  123. {
  124. $this->assertEquals(Enc::id('a'), 'a');
  125. $this->assertEquals(Enc::id('&'), '');
  126. $this->assertEquals(Enc::id('"'), '');
  127. $this->assertEquals(Enc::id('hey ya'), 'hey_ya');
  128. $this->assertEquals(Enc::id('ê'), '');
  129. $this->assertEquals(Enc::id('lowercase_id'), 'lowercase_id');
  130. $this->assertEquals(Enc::id('UPPERCASE_ID'), 'UPPERCASE_ID');
  131. $this->assertEquals(Enc::id('MIXED_case_ID'), 'MIXED_case_ID');
  132. $this->assertEquals(Enc::id('test--dashes'), 'test--dashes');
  133. $this->assertEquals(Enc::id('underscore_test'), 'underscore_test');
  134. $this->assertEquals(Enc::id('double__underscore__test'), 'double_underscore_test');
  135. $this->assertEquals(Enc::id('trailing_underscore_test_'), 'trailing_underscore_test');
  136. $this->assertEquals(Enc::id('no_symbols~!@#$%^&*()<>.,?/\'":;|\\]}[{`_no_symbols'), 'no_symbols_no_symbols');
  137. $this->assertEquals(Enc::id("\0"), '');
  138. $this->assertEquals(Enc::id("\x1A"), '');
  139. $this->assertEquals(Enc::id(array()), '');
  140. }
  141.  
  142. public function testJs()
  143. {
  144. $this->assertEquals(Enc::js('a'), 'a');
  145. $this->assertEquals(Enc::js('"'), '\\"');
  146. $this->assertEquals(Enc::js('\''), '\\\'');
  147. $this->assertEquals(Enc::js('\\'), '\\\\');
  148. $this->assertEquals(Enc::js('hey ya'), 'hey ya');
  149. $this->assertEquals(Enc::js('ê'), 'ê');
  150. $this->assertEquals(Enc::js("\n"), '\n');
  151. $this->assertEquals(Enc::js("\0"), '');
  152. $this->assertEquals(Enc::js("\x1A"), '');
  153. $this->assertEquals(Enc::js(array()), '');
  154. }
  155.  
  156. public function testHttpfield()
  157. {
  158. $this->assertEquals(Enc::httpfield('a'), 'a');
  159. $this->assertEquals(Enc::httpfield('&'), '');
  160. $this->assertEquals(Enc::httpfield('"'), '');
  161. $this->assertEquals(Enc::httpfield(':-.'), ':-.');
  162. $this->assertEquals(Enc::httpfield('[]'), '');
  163. $this->assertEquals(Enc::httpfield('hey ya'), 'hey_ya');
  164. $this->assertEquals(Enc::httpfield('ê'), '');
  165. $this->assertEquals(Enc::httpfield("\0"), '');
  166. $this->assertEquals(Enc::httpfield("\x1A"), '');
  167. $this->assertEquals(Enc::httpfield(array()), '');
  168. }
  169.  
  170. public function testUrlname()
  171. {
  172. // General conformance
  173. $this->assertEquals(Enc::urlname('a'), 'a');
  174. $this->assertEquals(Enc::urlname('"'), '');
  175. $this->assertEquals(Enc::urlname(':-.[]'), '');
  176. $this->assertEquals(Enc::urlname('ê'), '');
  177. $this->assertEquals(Enc::urlname("\0"), '');
  178. $this->assertEquals(Enc::urlname("\x1A"), '');
  179. $this->assertEquals(Enc::urlname(array()), '');
  180.  
  181. // Space character tests
  182. $this->assertEquals(Enc::urlname('hey ya', '_'), 'hey_ya');
  183. $this->assertEquals(Enc::urlname('hey ya', '-'), 'hey-ya');
  184.  
  185. // Special case of '&' => 'and'
  186. $this->assertEquals(Enc::urlname(' '), '');
  187. $this->assertEquals(Enc::urlname('&'), '');
  188. $this->assertEquals(Enc::urlname(' & '), '');
  189. $this->assertEquals(Enc::urlname(' hello world ', '_'), 'hello_world');
  190. $this->assertEquals(Enc::urlname(' hello world ', '_'), 'hello_world');
  191. $this->assertEquals(Enc::urlname(' hello & world ', '_'), 'hello_and_world');
  192. $this->assertEquals(Enc::urlname(' hello world & ', '_'), 'hello_world');
  193. $this->assertEquals(Enc::urlname(' & hello world & ', '_'), 'hello_world');
  194. }
  195.  
  196. public function testJsdate()
  197. {
  198. // The mysql path through the helper
  199. $this->assertEquals(Enc::jsdate('1988-05-07'), 'new Date(1988, 5 - 1, 7)');
  200. $this->assertEquals(Enc::jsdate('1988-05-07', 'mysql'), 'new Date(1988, 5 - 1, 7)');
  201. $this->assertNull(Enc::jsdate(array(7,5,1988), 'mysql'));
  202.  
  203. // the array path through the helper
  204. $this->assertEquals(Enc::jsdate(array(7,5,1988), 'array'), 'new Date(1988, 5 - 1, 7)');
  205. $this->assertNull(Enc::jsdate('1988-05-07', 'array'));
  206.  
  207. // Something else
  208. $this->assertNull(Enc::jsdate('goog', 'goog'));
  209.  
  210. // 2-digit years
  211. $this->assertEquals(Enc::jsdate('88-05-07', 'mysql'), 'new Date(1988, 5 - 1, 7)');
  212. $this->assertEquals(Enc::jsdate(array(7,5,88), 'array'), 'new Date(1988, 5 - 1, 7)');
  213. $this->assertEquals(Enc::jsdate('20-05-07', 'mysql'), 'new Date(2020, 5 - 1, 7)');
  214. $this->assertEquals(Enc::jsdate(array(7,5,20), 'array'), 'new Date(2020, 5 - 1, 7)');
  215. $this->assertEquals(Enc::jsdate('49-05-07', 'mysql'), 'new Date(2049, 5 - 1, 7)');
  216. $this->assertEquals(Enc::jsdate(array(7,5,49), 'array'), 'new Date(2049, 5 - 1, 7)');
  217. $this->assertEquals(Enc::jsdate('50-05-07', 'mysql'), 'new Date(1950, 5 - 1, 7)');
  218. $this->assertEquals(Enc::jsdate(array(7,5,50), 'array'), 'new Date(1950, 5 - 1, 7)');
  219. }
  220. }
  221.