FunctionsTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * For full copyright and license information, please see the LICENSE.txt
  9. * Redistributions of files must retain the above copyright notice
  10. *
  11. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  12. * @link https://cakephp.org CakePHP(tm) Project
  13. * @since 3.0.0
  14. * @license https://opensource.org/licenses/mit-license.php MIT License
  15. */
  16. namespace Cake\Test\TestCase\Core;
  17. use Cake\Core\Configure;
  18. use Cake\Http\Response;
  19. use Cake\TestSuite\TestCase;
  20. use stdClass;
  21. use function Cake\Core\deprecationWarning;
  22. use function Cake\Core\env;
  23. use function Cake\Core\h;
  24. use function Cake\Core\namespaceSplit;
  25. use function Cake\Core\pluginSplit;
  26. use function Cake\Core\triggerWarning;
  27. /**
  28. * Test cases for functions in Core\functions.php
  29. */
  30. class FunctionsTest extends TestCase
  31. {
  32. /**
  33. * Test cases for env()
  34. */
  35. public function testEnv(): void
  36. {
  37. $_ENV['DOES_NOT_EXIST'] = null;
  38. $this->assertNull(env('DOES_NOT_EXIST'));
  39. $this->assertSame('default', env('DOES_NOT_EXIST', 'default'));
  40. $_ENV['DOES_EXIST'] = 'some value';
  41. $this->assertSame('some value', env('DOES_EXIST'));
  42. $this->assertSame('some value', env('DOES_EXIST', 'default'));
  43. $_ENV['EMPTY_VALUE'] = '';
  44. $this->assertSame('', env('EMPTY_VALUE'));
  45. $this->assertSame('', env('EMPTY_VALUE', 'default'));
  46. $_ENV['ZERO'] = '0';
  47. $this->assertSame('0', env('ZERO'));
  48. $this->assertSame('0', env('ZERO', '1'));
  49. $_ENV['ZERO'] = 0;
  50. $this->assertSame(0, env('ZERO'));
  51. $this->assertSame(0, env('ZERO', 1));
  52. $_ENV['ZERO'] = 0.0;
  53. $this->assertSame(0.0, env('ZERO'));
  54. $this->assertSame(0.0, env('ZERO', 1));
  55. $this->assertSame('', env('DOCUMENT_ROOT'));
  56. $this->assertStringContainsString('phpunit', env('PHP_SELF'));
  57. }
  58. public function testEnv2(): void
  59. {
  60. $this->skipIf(!function_exists('ini_get') || ini_get('safe_mode') === '1', 'Safe mode is on.');
  61. $server = $_SERVER;
  62. $env = $_ENV;
  63. $_SERVER = $_ENV = [];
  64. $_SERVER['SCRIPT_NAME'] = '/a/test/test.php';
  65. $this->assertSame(env('SCRIPT_NAME'), '/a/test/test.php');
  66. $_SERVER = $_ENV = [];
  67. $_ENV['CGI_MODE'] = 'BINARY';
  68. $_ENV['SCRIPT_URL'] = '/a/test/test.php';
  69. $this->assertSame(env('SCRIPT_NAME'), '/a/test/test.php');
  70. $_SERVER = $_ENV = [];
  71. $this->assertFalse(env('HTTPS'));
  72. $_SERVER['HTTPS'] = 'on';
  73. $this->assertTrue(env('HTTPS'));
  74. $_SERVER['HTTPS'] = '1';
  75. $this->assertTrue(env('HTTPS'));
  76. $_SERVER['HTTPS'] = 'I am not empty';
  77. $this->assertTrue(env('HTTPS'));
  78. $_SERVER['HTTPS'] = 1;
  79. $this->assertTrue(env('HTTPS'));
  80. $_SERVER['HTTPS'] = 'off';
  81. $this->assertFalse(env('HTTPS'));
  82. $_SERVER['HTTPS'] = false;
  83. $this->assertFalse(env('HTTPS'));
  84. $_SERVER['HTTPS'] = '';
  85. $this->assertFalse(env('HTTPS'));
  86. $_SERVER = [];
  87. $_ENV['SCRIPT_URI'] = 'https://domain.test/a/test.php';
  88. $this->assertTrue(env('HTTPS'));
  89. $_ENV['SCRIPT_URI'] = 'http://domain.test/a/test.php';
  90. $this->assertFalse(env('HTTPS'));
  91. $_SERVER = $_ENV = [];
  92. $this->assertNull(env('TEST_ME'));
  93. $_ENV['TEST_ME'] = 'a';
  94. $this->assertSame(env('TEST_ME'), 'a');
  95. $_SERVER['TEST_ME'] = 'b';
  96. $this->assertSame(env('TEST_ME'), 'b');
  97. unset($_ENV['TEST_ME']);
  98. $this->assertSame(env('TEST_ME'), 'b');
  99. $_SERVER = $server;
  100. $_ENV = $env;
  101. }
  102. /**
  103. * Test cases for h()
  104. *
  105. * @dataProvider hInputProvider
  106. * @param mixed $value
  107. * @param mixed $expected
  108. */
  109. public function testH($value, $expected): void
  110. {
  111. $result = h($value);
  112. $this->assertSame($expected, $result);
  113. }
  114. public function hInputProvider(): array
  115. {
  116. return [
  117. ['i am clean', 'i am clean'],
  118. ['i "need" escaping', 'i &quot;need&quot; escaping'],
  119. [null, null],
  120. [1, 1],
  121. [1.1, 1.1],
  122. [new stdClass(), '(object)stdClass'],
  123. [new Response(), ''],
  124. [['clean', '"clean-me'], ['clean', '&quot;clean-me']],
  125. ];
  126. }
  127. public function testH2(): void
  128. {
  129. $string = '<foo>';
  130. $result = h($string);
  131. $this->assertSame('&lt;foo&gt;', $result);
  132. $in = ['this & that', '<p>Which one</p>'];
  133. $result = h($in);
  134. $expected = ['this &amp; that', '&lt;p&gt;Which one&lt;/p&gt;'];
  135. $this->assertSame($expected, $result);
  136. $string = '<foo> & &nbsp;';
  137. $result = h($string);
  138. $this->assertSame('&lt;foo&gt; &amp; &amp;nbsp;', $result);
  139. $string = '<foo> & &nbsp;';
  140. $result = h($string, false);
  141. $this->assertSame('&lt;foo&gt; &amp; &nbsp;', $result);
  142. $string = "An invalid\x80string";
  143. $result = h($string);
  144. $this->assertStringContainsString('string', $result);
  145. $arr = ['<foo>', '&nbsp;'];
  146. $result = h($arr);
  147. $expected = [
  148. '&lt;foo&gt;',
  149. '&amp;nbsp;',
  150. ];
  151. $this->assertSame($expected, $result);
  152. $arr = ['<foo>', '&nbsp;'];
  153. $result = h($arr, false);
  154. $expected = [
  155. '&lt;foo&gt;',
  156. '&nbsp;',
  157. ];
  158. $this->assertSame($expected, $result);
  159. $arr = ['f' => '<foo>', 'n' => '&nbsp;'];
  160. $result = h($arr, false);
  161. $expected = [
  162. 'f' => '&lt;foo&gt;',
  163. 'n' => '&nbsp;',
  164. ];
  165. $this->assertSame($expected, $result);
  166. $arr = ['invalid' => "\x99An invalid\x80string", 'good' => 'Good string'];
  167. $result = h($arr);
  168. $this->assertStringContainsString('An invalid', $result['invalid']);
  169. $this->assertSame('Good string', $result['good']);
  170. // Test that boolean values are not converted to strings
  171. $result = h(false);
  172. $this->assertFalse($result);
  173. $arr = ['foo' => false, 'bar' => true];
  174. $result = h($arr);
  175. $this->assertFalse($result['foo']);
  176. $this->assertTrue($result['bar']);
  177. $obj = new stdClass();
  178. $result = h($obj);
  179. $this->assertSame('(object)stdClass', $result);
  180. $obj = new Response(['body' => 'Body content']);
  181. $result = h($obj);
  182. $this->assertSame('Body content', $result);
  183. }
  184. /**
  185. * Test splitting plugin names.
  186. */
  187. public function testPluginSplit(): void
  188. {
  189. $result = pluginSplit('Something.else');
  190. $this->assertSame(['Something', 'else'], $result);
  191. $result = pluginSplit('Something.else.more.dots');
  192. $this->assertSame(['Something', 'else.more.dots'], $result);
  193. $result = pluginSplit('Somethingelse');
  194. $this->assertSame([null, 'Somethingelse'], $result);
  195. $result = pluginSplit('Something.else', true);
  196. $this->assertSame(['Something.', 'else'], $result);
  197. $result = pluginSplit('Something.else.more.dots', true);
  198. $this->assertSame(['Something.', 'else.more.dots'], $result);
  199. $result = pluginSplit('Post', false, 'Blog');
  200. $this->assertSame(['Blog', 'Post'], $result);
  201. $result = pluginSplit('Blog.Post', false, 'Ultimate');
  202. $this->assertSame(['Blog', 'Post'], $result);
  203. }
  204. /**
  205. * test namespaceSplit
  206. */
  207. public function testNamespaceSplit(): void
  208. {
  209. $result = namespaceSplit('Something');
  210. $this->assertSame(['', 'Something'], $result);
  211. $result = namespaceSplit('\Something');
  212. $this->assertSame(['', 'Something'], $result);
  213. $result = namespaceSplit('Cake\Something');
  214. $this->assertSame(['Cake', 'Something'], $result);
  215. $result = namespaceSplit('Cake\Test\Something');
  216. $this->assertSame(['Cake\Test', 'Something'], $result);
  217. }
  218. /**
  219. * Test error messages coming out when deprecated level is on, manually setting the stack frame
  220. */
  221. public function testDeprecationWarningEnabled(): void
  222. {
  223. $error = $this->captureError(E_ALL, function (): void {
  224. deprecationWarning('5.0.0', 'This is going away', 2);
  225. });
  226. $this->assertMatchesRegularExpression(
  227. '/This is going away\n(.*?)[\/\\\]FunctionsTest.php, line\: \d+/',
  228. $error->getMessage()
  229. );
  230. }
  231. /**
  232. * Test error messages coming out when deprecated level is on, not setting the stack frame manually
  233. */
  234. public function testDeprecationWarningEnabledDefaultFrame(): void
  235. {
  236. $error = $this->captureError(E_ALL, function (): void {
  237. deprecationWarning('5.0.0', 'This is going away too');
  238. });
  239. $this->assertMatchesRegularExpression(
  240. '/This is going away too\n(.*?)[\/\\\]TestCase.php, line\: \d+/',
  241. $error->getMessage()
  242. );
  243. }
  244. /**
  245. * Test no error when deprecation matches ignore paths.
  246. */
  247. public function testDeprecationWarningPathDisabled(): void
  248. {
  249. $this->expectNotToPerformAssertions();
  250. Configure::write('Error.ignoredDeprecationPaths', ['src/TestSuite/*']);
  251. $this->withErrorReporting(E_ALL, function (): void {
  252. deprecationWarning('5.0.0', 'This will be gone soon');
  253. });
  254. }
  255. /**
  256. * Test no error when deprecated level is off.
  257. */
  258. public function testDeprecationWarningLevelDisabled(): void
  259. {
  260. $this->expectNotToPerformAssertions();
  261. $this->withErrorReporting(E_ALL ^ E_USER_DEPRECATED, function (): void {
  262. deprecationWarning('5.0.0', 'This is leaving');
  263. });
  264. }
  265. /**
  266. * Test error messages coming out when warning level is on.
  267. */
  268. public function testTriggerWarningEnabled(): void
  269. {
  270. $error = $this->captureError(E_ALL, function (): void {
  271. triggerWarning('This will be gone one day ' . uniqid());
  272. });
  273. $this->assertMatchesRegularExpression(
  274. '/This will be gone one day \w+ - (.*?)[\/\\\]TestCase.php, line\: \d+/',
  275. $error->getMessage()
  276. );
  277. }
  278. /**
  279. * Test no error when warning level is off.
  280. */
  281. public function testTriggerWarningLevelDisabled(): void
  282. {
  283. $this->withErrorReporting(E_ALL ^ E_USER_WARNING, function (): void {
  284. triggerWarning('This was a mistake.');
  285. $this->assertTrue(true);
  286. });
  287. }
  288. }