BasicsTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * BasicsTest file
  5. *
  6. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  7. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  8. *
  9. * Licensed under The MIT License
  10. * For full copyright and license information, please see the LICENSE.txt
  11. * Redistributions of files must retain the above copyright notice
  12. *
  13. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  14. * @link https://cakephp.org CakePHP(tm) Project
  15. * @since 1.2.0
  16. * @license https://opensource.org/licenses/mit-license.php MIT License
  17. */
  18. namespace Cake\Test\TestCase;
  19. use Cake\Collection\Collection;
  20. use Cake\Error\Debugger;
  21. use Cake\Event\EventManager;
  22. use Cake\Http\Response;
  23. use Cake\TestSuite\TestCase;
  24. use stdClass;
  25. require_once CAKE . 'basics.php';
  26. /**
  27. * BasicsTest class
  28. */
  29. class BasicsTest extends TestCase
  30. {
  31. /**
  32. * test the array_diff_key compatibility function.
  33. */
  34. public function testArrayDiffKey(): void
  35. {
  36. $one = ['one' => 1, 'two' => 2, 'three' => 3];
  37. $two = ['one' => 'one', 'two' => 'two'];
  38. $result = array_diff_key($one, $two);
  39. $expected = ['three' => 3];
  40. $this->assertSame($expected, $result);
  41. $one = ['one' => ['value', 'value-two'], 'two' => 2, 'three' => 3];
  42. $two = ['two' => 'two'];
  43. $result = array_diff_key($one, $two);
  44. $expected = ['one' => ['value', 'value-two'], 'three' => 3];
  45. $this->assertSame($expected, $result);
  46. $one = ['one' => null, 'two' => 2, 'three' => '', 'four' => 0];
  47. $two = ['two' => 'two'];
  48. $result = array_diff_key($one, $two);
  49. $expected = ['one' => null, 'three' => '', 'four' => 0];
  50. $this->assertSame($expected, $result);
  51. $one = ['minYear' => null, 'maxYear' => null, 'separator' => '-', 'interval' => 1, 'monthNames' => true];
  52. $two = ['minYear' => null, 'maxYear' => null, 'separator' => '-', 'interval' => 1, 'monthNames' => true];
  53. $result = array_diff_key($one, $two);
  54. $this->assertSame([], $result);
  55. $one = ['minYear' => null, 'maxYear' => null, 'separator' => '-', 'interval' => 1, 'monthNames' => true];
  56. $two = [];
  57. $result = array_diff_key($one, $two);
  58. $this->assertSame($one, $result);
  59. }
  60. /**
  61. * testHttpBase method
  62. */
  63. public function testEnv(): void
  64. {
  65. $this->skipIf(!function_exists('ini_get') || ini_get('safe_mode') === '1', 'Safe mode is on.');
  66. $server = $_SERVER;
  67. $env = $_ENV;
  68. $_SERVER = $_ENV = [];
  69. $_SERVER['SCRIPT_NAME'] = '/a/test/test.php';
  70. $this->assertSame(env('SCRIPT_NAME'), '/a/test/test.php');
  71. $_SERVER = $_ENV = [];
  72. $_ENV['CGI_MODE'] = 'BINARY';
  73. $_ENV['SCRIPT_URL'] = '/a/test/test.php';
  74. $this->assertSame(env('SCRIPT_NAME'), '/a/test/test.php');
  75. $_SERVER = $_ENV = [];
  76. $this->assertFalse(env('HTTPS'));
  77. $_SERVER['HTTPS'] = 'on';
  78. $this->assertTrue(env('HTTPS'));
  79. $_SERVER['HTTPS'] = '1';
  80. $this->assertTrue(env('HTTPS'));
  81. $_SERVER['HTTPS'] = 'I am not empty';
  82. $this->assertTrue(env('HTTPS'));
  83. $_SERVER['HTTPS'] = 1;
  84. $this->assertTrue(env('HTTPS'));
  85. $_SERVER['HTTPS'] = 'off';
  86. $this->assertFalse(env('HTTPS'));
  87. $_SERVER['HTTPS'] = false;
  88. $this->assertFalse(env('HTTPS'));
  89. $_SERVER['HTTPS'] = '';
  90. $this->assertFalse(env('HTTPS'));
  91. $_SERVER = [];
  92. $_ENV['SCRIPT_URI'] = 'https://domain.test/a/test.php';
  93. $this->assertTrue(env('HTTPS'));
  94. $_ENV['SCRIPT_URI'] = 'http://domain.test/a/test.php';
  95. $this->assertFalse(env('HTTPS'));
  96. $_SERVER = $_ENV = [];
  97. $this->assertNull(env('TEST_ME'));
  98. $_ENV['TEST_ME'] = 'a';
  99. $this->assertSame(env('TEST_ME'), 'a');
  100. $_SERVER['TEST_ME'] = 'b';
  101. $this->assertSame(env('TEST_ME'), 'b');
  102. unset($_ENV['TEST_ME']);
  103. $this->assertSame(env('TEST_ME'), 'b');
  104. $_SERVER = $server;
  105. $_ENV = $env;
  106. }
  107. /**
  108. * Test h()
  109. */
  110. public function testH(): void
  111. {
  112. $string = '<foo>';
  113. $result = h($string);
  114. $this->assertSame('&lt;foo&gt;', $result);
  115. $in = ['this & that', '<p>Which one</p>'];
  116. $result = h($in);
  117. $expected = ['this &amp; that', '&lt;p&gt;Which one&lt;/p&gt;'];
  118. $this->assertSame($expected, $result);
  119. $string = '<foo> & &nbsp;';
  120. $result = h($string);
  121. $this->assertSame('&lt;foo&gt; &amp; &amp;nbsp;', $result);
  122. $string = '<foo> & &nbsp;';
  123. $result = h($string, false);
  124. $this->assertSame('&lt;foo&gt; &amp; &nbsp;', $result);
  125. $string = "An invalid\x80string";
  126. $result = h($string);
  127. $this->assertStringContainsString('string', $result);
  128. $arr = ['<foo>', '&nbsp;'];
  129. $result = h($arr);
  130. $expected = [
  131. '&lt;foo&gt;',
  132. '&amp;nbsp;',
  133. ];
  134. $this->assertSame($expected, $result);
  135. $arr = ['<foo>', '&nbsp;'];
  136. $result = h($arr, false);
  137. $expected = [
  138. '&lt;foo&gt;',
  139. '&nbsp;',
  140. ];
  141. $this->assertSame($expected, $result);
  142. $arr = ['f' => '<foo>', 'n' => '&nbsp;'];
  143. $result = h($arr, false);
  144. $expected = [
  145. 'f' => '&lt;foo&gt;',
  146. 'n' => '&nbsp;',
  147. ];
  148. $this->assertSame($expected, $result);
  149. $arr = ['invalid' => "\x99An invalid\x80string", 'good' => 'Good string'];
  150. $result = h($arr);
  151. $this->assertStringContainsString('An invalid', $result['invalid']);
  152. $this->assertSame('Good string', $result['good']);
  153. // Test that boolean values are not converted to strings
  154. $result = h(false);
  155. $this->assertFalse($result);
  156. $arr = ['foo' => false, 'bar' => true];
  157. $result = h($arr);
  158. $this->assertFalse($result['foo']);
  159. $this->assertTrue($result['bar']);
  160. $obj = new stdClass();
  161. $result = h($obj);
  162. $this->assertSame('(object)stdClass', $result);
  163. $obj = new Response(['body' => 'Body content']);
  164. $result = h($obj);
  165. $this->assertSame('Body content', $result);
  166. }
  167. /**
  168. * test debug()
  169. */
  170. public function testDebug(): void
  171. {
  172. ob_start();
  173. $this->assertSame('this-is-a-test', debug('this-is-a-test', false));
  174. $result = ob_get_clean();
  175. $expectedText = <<<EXPECTED
  176. %s (line %d)
  177. ########## DEBUG ##########
  178. 'this-is-a-test'
  179. ###########################
  180. EXPECTED;
  181. $expected = sprintf($expectedText, Debugger::trimPath(__FILE__), __LINE__ - 9);
  182. $this->assertSame($expected, $result);
  183. ob_start();
  184. $value = '<div>this-is-a-test</div>';
  185. $this->assertSame($value, debug($value, true));
  186. $result = ob_get_clean();
  187. $this->assertStringContainsString('<div class="cake-debug-output', $result);
  188. $this->assertStringContainsString('this-is-a-test', $result);
  189. ob_start();
  190. debug('<div>this-is-a-test</div>', true, true);
  191. $result = ob_get_clean();
  192. $expected = <<<EXPECTED
  193. <div class="cake-debug-output cake-debug" style="direction:ltr">
  194. <span><strong>%s</strong> (line <strong>%d</strong>)</span>
  195. EXPECTED;
  196. $expected = sprintf($expected, Debugger::trimPath(__FILE__), __LINE__ - 6);
  197. $this->assertStringContainsString($expected, $result);
  198. ob_start();
  199. debug('<div>this-is-a-test</div>', true, false);
  200. $result = ob_get_clean();
  201. $this->assertStringNotContainsString('(line', $result);
  202. }
  203. /**
  204. * test pr()
  205. */
  206. public function testPr(): void
  207. {
  208. ob_start();
  209. $this->assertTrue(pr(true));
  210. $result = ob_get_clean();
  211. $expected = "\n1\n\n";
  212. $this->assertSame($expected, $result);
  213. ob_start();
  214. $this->assertFalse(pr(false));
  215. $result = ob_get_clean();
  216. $expected = "\n\n\n";
  217. $this->assertSame($expected, $result);
  218. ob_start();
  219. $this->assertNull(pr(null));
  220. $result = ob_get_clean();
  221. $expected = "\n\n\n";
  222. $this->assertSame($expected, $result);
  223. ob_start();
  224. $this->assertSame(123, pr(123));
  225. $result = ob_get_clean();
  226. $expected = "\n123\n\n";
  227. $this->assertSame($expected, $result);
  228. ob_start();
  229. pr('123');
  230. $result = ob_get_clean();
  231. $expected = "\n123\n\n";
  232. $this->assertSame($expected, $result);
  233. ob_start();
  234. pr('this is a test');
  235. $result = ob_get_clean();
  236. $expected = "\nthis is a test\n\n";
  237. $this->assertSame($expected, $result);
  238. ob_start();
  239. pr(['this' => 'is', 'a' => 'test', 123 => 456]);
  240. $result = ob_get_clean();
  241. $expected = "\nArray\n(\n [this] => is\n [a] => test\n [123] => 456\n)\n\n";
  242. $this->assertSame($expected, $result);
  243. }
  244. /**
  245. * test pj()
  246. */
  247. public function testPj(): void
  248. {
  249. ob_start();
  250. $this->assertTrue(pj(true));
  251. $result = ob_get_clean();
  252. $expected = "\ntrue\n\n";
  253. $this->assertSame($expected, $result);
  254. ob_start();
  255. $this->assertFalse(pj(false));
  256. $result = ob_get_clean();
  257. $expected = "\nfalse\n\n";
  258. $this->assertSame($expected, $result);
  259. ob_start();
  260. $this->assertNull(pj(null));
  261. $result = ob_get_clean();
  262. $expected = "\nnull\n\n";
  263. $this->assertSame($expected, $result);
  264. ob_start();
  265. $this->assertSame(123, pj(123));
  266. $result = ob_get_clean();
  267. $expected = "\n123\n\n";
  268. $this->assertSame($expected, $result);
  269. ob_start();
  270. pj('123');
  271. $result = ob_get_clean();
  272. $expected = "\n\"123\"\n\n";
  273. $this->assertSame($expected, $result);
  274. ob_start();
  275. pj('this is a test');
  276. $result = ob_get_clean();
  277. $expected = "\n\"this is a test\"\n\n";
  278. $this->assertSame($expected, $result);
  279. ob_start();
  280. $value = ['this' => 'is', 'a' => 'test', 123 => 456];
  281. $this->assertSame($value, pj($value));
  282. $result = ob_get_clean();
  283. $expected = "\n{\n \"this\": \"is\",\n \"a\": \"test\",\n \"123\": 456\n}\n\n";
  284. $this->assertSame($expected, $result);
  285. }
  286. /**
  287. * Test splitting plugin names.
  288. */
  289. public function testPluginSplit(): void
  290. {
  291. $result = pluginSplit('Something.else');
  292. $this->assertSame(['Something', 'else'], $result);
  293. $result = pluginSplit('Something.else.more.dots');
  294. $this->assertSame(['Something', 'else.more.dots'], $result);
  295. $result = pluginSplit('Somethingelse');
  296. $this->assertSame([null, 'Somethingelse'], $result);
  297. $result = pluginSplit('Something.else', true);
  298. $this->assertSame(['Something.', 'else'], $result);
  299. $result = pluginSplit('Something.else.more.dots', true);
  300. $this->assertSame(['Something.', 'else.more.dots'], $result);
  301. $result = pluginSplit('Post', false, 'Blog');
  302. $this->assertSame(['Blog', 'Post'], $result);
  303. $result = pluginSplit('Blog.Post', false, 'Ultimate');
  304. $this->assertSame(['Blog', 'Post'], $result);
  305. }
  306. /**
  307. * test namespaceSplit
  308. */
  309. public function testNamespaceSplit(): void
  310. {
  311. $result = namespaceSplit('Something');
  312. $this->assertSame(['', 'Something'], $result);
  313. $result = namespaceSplit('\Something');
  314. $this->assertSame(['', 'Something'], $result);
  315. $result = namespaceSplit('Cake\Something');
  316. $this->assertSame(['Cake', 'Something'], $result);
  317. $result = namespaceSplit('Cake\Test\Something');
  318. $this->assertSame(['Cake\Test', 'Something'], $result);
  319. }
  320. /**
  321. * Tests that the stackTrace() method is a shortcut for Debugger::trace()
  322. */
  323. public function testStackTrace(): void
  324. {
  325. ob_start();
  326. // phpcs:ignore
  327. stackTrace(); $expected = Debugger::trace();
  328. $result = ob_get_clean();
  329. $this->assertSame($expected, $result);
  330. $opts = ['args' => true];
  331. ob_start();
  332. // phpcs:ignore
  333. stackTrace($opts); $expected = Debugger::trace($opts);
  334. $result = ob_get_clean();
  335. $this->assertSame($expected, $result);
  336. $opts = ['format' => 'array'];
  337. $trace = Debugger::trace($opts);
  338. $this->assertEmpty(array_column($trace, 'args'));
  339. }
  340. /**
  341. * Tests that the collection() method is a shortcut for new Collection
  342. */
  343. public function testCollection(): void
  344. {
  345. $items = [1, 2, 3];
  346. $collection = collection($items);
  347. $this->assertInstanceOf(Collection::class, $collection);
  348. $this->assertSame($items, $collection->toArray());
  349. }
  350. /**
  351. * Test that works in tandem with testEventManagerReset2 to
  352. * test the EventManager reset.
  353. *
  354. * The return value is passed to testEventManagerReset2 as
  355. * an arguments.
  356. */
  357. public function testEventManagerReset1(): EventManager
  358. {
  359. $eventManager = EventManager::instance();
  360. $this->assertInstanceOf(EventManager::class, $eventManager);
  361. return $eventManager;
  362. }
  363. /**
  364. * Test if the EventManager is reset between tests.
  365. *
  366. * @depends testEventManagerReset1
  367. */
  368. public function testEventManagerReset2(EventManager $prevEventManager): void
  369. {
  370. $this->assertInstanceOf(EventManager::class, $prevEventManager);
  371. $this->assertNotSame($prevEventManager, EventManager::instance());
  372. }
  373. }