BasicsTest.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  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\Event\EventManager;
  21. use Cake\Http\Response;
  22. use Cake\TestSuite\TestCase;
  23. require_once CAKE . 'basics.php';
  24. /**
  25. * BasicsTest class
  26. */
  27. class BasicsTest extends TestCase
  28. {
  29. /**
  30. * test the array_diff_key compatibility function.
  31. *
  32. * @return void
  33. */
  34. public function testArrayDiffKey()
  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. * @return void
  64. */
  65. public function testEnv()
  66. {
  67. $this->skipIf(!function_exists('ini_get') || ini_get('safe_mode') === '1', 'Safe mode is on.');
  68. $server = $_SERVER;
  69. $env = $_ENV;
  70. $_SERVER = $_ENV = [];
  71. $_SERVER['SCRIPT_NAME'] = '/a/test/test.php';
  72. $this->assertSame(env('SCRIPT_NAME'), '/a/test/test.php');
  73. $_SERVER = $_ENV = [];
  74. $_ENV['CGI_MODE'] = 'BINARY';
  75. $_ENV['SCRIPT_URL'] = '/a/test/test.php';
  76. $this->assertSame(env('SCRIPT_NAME'), '/a/test/test.php');
  77. $_SERVER = $_ENV = [];
  78. $this->assertFalse(env('HTTPS'));
  79. $_SERVER['HTTPS'] = 'on';
  80. $this->assertTrue(env('HTTPS'));
  81. $_SERVER['HTTPS'] = '1';
  82. $this->assertTrue(env('HTTPS'));
  83. $_SERVER['HTTPS'] = 'I am not empty';
  84. $this->assertTrue(env('HTTPS'));
  85. $_SERVER['HTTPS'] = 1;
  86. $this->assertTrue(env('HTTPS'));
  87. $_SERVER['HTTPS'] = 'off';
  88. $this->assertFalse(env('HTTPS'));
  89. $_SERVER['HTTPS'] = false;
  90. $this->assertFalse(env('HTTPS'));
  91. $_SERVER['HTTPS'] = '';
  92. $this->assertFalse(env('HTTPS'));
  93. $_SERVER = [];
  94. $_ENV['SCRIPT_URI'] = 'https://domain.test/a/test.php';
  95. $this->assertTrue(env('HTTPS'));
  96. $_ENV['SCRIPT_URI'] = 'http://domain.test/a/test.php';
  97. $this->assertFalse(env('HTTPS'));
  98. $_SERVER = $_ENV = [];
  99. $this->assertNull(env('TEST_ME'));
  100. $_ENV['TEST_ME'] = 'a';
  101. $this->assertSame(env('TEST_ME'), 'a');
  102. $_SERVER['TEST_ME'] = 'b';
  103. $this->assertSame(env('TEST_ME'), 'b');
  104. unset($_ENV['TEST_ME']);
  105. $this->assertSame(env('TEST_ME'), 'b');
  106. $_SERVER = $server;
  107. $_ENV = $env;
  108. }
  109. /**
  110. * Test h()
  111. *
  112. * @return void
  113. */
  114. public function testH()
  115. {
  116. $string = '<foo>';
  117. $result = h($string);
  118. $this->assertSame('&lt;foo&gt;', $result);
  119. $in = ['this & that', '<p>Which one</p>'];
  120. $result = h($in);
  121. $expected = ['this &amp; that', '&lt;p&gt;Which one&lt;/p&gt;'];
  122. $this->assertSame($expected, $result);
  123. $string = '<foo> & &nbsp;';
  124. $result = h($string);
  125. $this->assertSame('&lt;foo&gt; &amp; &amp;nbsp;', $result);
  126. $string = '<foo> & &nbsp;';
  127. $result = h($string, false);
  128. $this->assertSame('&lt;foo&gt; &amp; &nbsp;', $result);
  129. $string = "An invalid\x80string";
  130. $result = h($string);
  131. $this->assertStringContainsString('string', $result);
  132. $arr = ['<foo>', '&nbsp;'];
  133. $result = h($arr);
  134. $expected = [
  135. '&lt;foo&gt;',
  136. '&amp;nbsp;',
  137. ];
  138. $this->assertSame($expected, $result);
  139. $arr = ['<foo>', '&nbsp;'];
  140. $result = h($arr, false);
  141. $expected = [
  142. '&lt;foo&gt;',
  143. '&nbsp;',
  144. ];
  145. $this->assertSame($expected, $result);
  146. $arr = ['f' => '<foo>', 'n' => '&nbsp;'];
  147. $result = h($arr, false);
  148. $expected = [
  149. 'f' => '&lt;foo&gt;',
  150. 'n' => '&nbsp;',
  151. ];
  152. $this->assertSame($expected, $result);
  153. $arr = ['invalid' => "\x99An invalid\x80string", 'good' => 'Good string'];
  154. $result = h($arr);
  155. $this->assertStringContainsString('An invalid', $result['invalid']);
  156. $this->assertSame('Good string', $result['good']);
  157. // Test that boolean values are not converted to strings
  158. $result = h(false);
  159. $this->assertFalse($result);
  160. $arr = ['foo' => false, 'bar' => true];
  161. $result = h($arr);
  162. $this->assertFalse($result['foo']);
  163. $this->assertTrue($result['bar']);
  164. $obj = new \stdClass();
  165. $result = h($obj);
  166. $this->assertSame('(object)stdClass', $result);
  167. $obj = new Response(['body' => 'Body content']);
  168. $result = h($obj);
  169. $this->assertSame('Body content', $result);
  170. }
  171. /**
  172. * test debug()
  173. *
  174. * @return void
  175. */
  176. public function testDebug()
  177. {
  178. ob_start();
  179. $this->assertSame('this-is-a-test', debug('this-is-a-test', false));
  180. $result = ob_get_clean();
  181. $expectedText = <<<EXPECTED
  182. %s (line %d)
  183. ########## DEBUG ##########
  184. 'this-is-a-test'
  185. ###########################
  186. EXPECTED;
  187. $expected = sprintf($expectedText, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 9);
  188. $this->assertSame($expected, $result);
  189. ob_start();
  190. $value = '<div>this-is-a-test</div>';
  191. $this->assertSame($value, debug($value, true));
  192. $result = ob_get_clean();
  193. $expectedHtml = <<<EXPECTED
  194. <div class="cake-debug-output" style="direction:ltr">
  195. <span><strong>%s</strong> (line <strong>%d</strong>)</span>
  196. <pre class="cake-debug">
  197. &#039;&lt;div&gt;this-is-a-test&lt;/div&gt;&#039;
  198. </pre>
  199. </div>
  200. EXPECTED;
  201. $expected = sprintf($expectedHtml, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 10);
  202. $this->assertSame($expected, $result);
  203. ob_start();
  204. debug('<div>this-is-a-test</div>', true, true);
  205. $result = ob_get_clean();
  206. $expected = <<<EXPECTED
  207. <div class="cake-debug-output" style="direction:ltr">
  208. <span><strong>%s</strong> (line <strong>%d</strong>)</span>
  209. <pre class="cake-debug">
  210. &#039;&lt;div&gt;this-is-a-test&lt;/div&gt;&#039;
  211. </pre>
  212. </div>
  213. EXPECTED;
  214. $expected = sprintf($expected, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 10);
  215. $this->assertSame($expected, $result);
  216. ob_start();
  217. debug('<div>this-is-a-test</div>', true, false);
  218. $result = ob_get_clean();
  219. $expected = <<<EXPECTED
  220. <div class="cake-debug-output" style="direction:ltr">
  221. <pre class="cake-debug">
  222. &#039;&lt;div&gt;this-is-a-test&lt;/div&gt;&#039;
  223. </pre>
  224. </div>
  225. EXPECTED;
  226. $expected = sprintf($expected, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 10);
  227. $this->assertSame($expected, $result);
  228. ob_start();
  229. debug('<div>this-is-a-test</div>', null);
  230. $result = ob_get_clean();
  231. $expectedHtml = <<<EXPECTED
  232. <div class="cake-debug-output" style="direction:ltr">
  233. <span><strong>%s</strong> (line <strong>%d</strong>)</span>
  234. <pre class="cake-debug">
  235. &#039;&lt;div&gt;this-is-a-test&lt;/div&gt;&#039;
  236. </pre>
  237. </div>
  238. EXPECTED;
  239. $expectedText = <<<EXPECTED
  240. %s (line %d)
  241. ########## DEBUG ##########
  242. '<div>this-is-a-test</div>'
  243. ###########################
  244. EXPECTED;
  245. if ((PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg')) {
  246. $expected = sprintf($expectedText, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 18);
  247. } else {
  248. $expected = sprintf($expectedHtml, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 19);
  249. }
  250. $this->assertSame($expected, $result);
  251. ob_start();
  252. debug('<div>this-is-a-test</div>', null, false);
  253. $result = ob_get_clean();
  254. $expectedHtml = <<<EXPECTED
  255. <div class="cake-debug-output" style="direction:ltr">
  256. <pre class="cake-debug">
  257. &#039;&lt;div&gt;this-is-a-test&lt;/div&gt;&#039;
  258. </pre>
  259. </div>
  260. EXPECTED;
  261. $expectedText = <<<EXPECTED
  262. ########## DEBUG ##########
  263. '<div>this-is-a-test</div>'
  264. ###########################
  265. EXPECTED;
  266. if ((PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg')) {
  267. $expected = sprintf($expectedText, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 18);
  268. } else {
  269. $expected = sprintf($expectedHtml, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 19);
  270. }
  271. $this->assertSame($expected, $result);
  272. ob_start();
  273. debug('<div>this-is-a-test</div>', false);
  274. $result = ob_get_clean();
  275. $expected = <<<EXPECTED
  276. %s (line %d)
  277. ########## DEBUG ##########
  278. '<div>this-is-a-test</div>'
  279. ###########################
  280. EXPECTED;
  281. $expected = sprintf($expected, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 9);
  282. $this->assertSame($expected, $result);
  283. ob_start();
  284. debug('<div>this-is-a-test</div>', false, true);
  285. $result = ob_get_clean();
  286. $expected = <<<EXPECTED
  287. %s (line %d)
  288. ########## DEBUG ##########
  289. '<div>this-is-a-test</div>'
  290. ###########################
  291. EXPECTED;
  292. $expected = sprintf($expected, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 9);
  293. $this->assertSame($expected, $result);
  294. ob_start();
  295. debug('<div>this-is-a-test</div>', false, false);
  296. $result = ob_get_clean();
  297. $expected = <<<EXPECTED
  298. ########## DEBUG ##########
  299. '<div>this-is-a-test</div>'
  300. ###########################
  301. EXPECTED;
  302. $expected = sprintf($expected, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 9);
  303. $this->assertSame($expected, $result);
  304. ob_start();
  305. $this->assertFalse(debug(false, false, false));
  306. $result = ob_get_clean();
  307. $expected = <<<EXPECTED
  308. ########## DEBUG ##########
  309. false
  310. ###########################
  311. EXPECTED;
  312. $expected = sprintf($expected, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 9);
  313. $this->assertSame($expected, $result);
  314. }
  315. /**
  316. * test pr()
  317. *
  318. * @return void
  319. */
  320. public function testPr()
  321. {
  322. ob_start();
  323. $this->assertTrue(pr(true));
  324. $result = ob_get_clean();
  325. $expected = "\n1\n\n";
  326. $this->assertSame($expected, $result);
  327. ob_start();
  328. $this->assertFalse(pr(false));
  329. $result = ob_get_clean();
  330. $expected = "\n\n\n";
  331. $this->assertSame($expected, $result);
  332. ob_start();
  333. $this->assertNull(pr(null));
  334. $result = ob_get_clean();
  335. $expected = "\n\n\n";
  336. $this->assertSame($expected, $result);
  337. ob_start();
  338. $this->assertSame(123, pr(123));
  339. $result = ob_get_clean();
  340. $expected = "\n123\n\n";
  341. $this->assertSame($expected, $result);
  342. ob_start();
  343. pr('123');
  344. $result = ob_get_clean();
  345. $expected = "\n123\n\n";
  346. $this->assertSame($expected, $result);
  347. ob_start();
  348. pr('this is a test');
  349. $result = ob_get_clean();
  350. $expected = "\nthis is a test\n\n";
  351. $this->assertSame($expected, $result);
  352. ob_start();
  353. pr(['this' => 'is', 'a' => 'test', 123 => 456]);
  354. $result = ob_get_clean();
  355. $expected = "\nArray\n(\n [this] => is\n [a] => test\n [123] => 456\n)\n\n";
  356. $this->assertSame($expected, $result);
  357. }
  358. /**
  359. * test pj()
  360. *
  361. * @return void
  362. */
  363. public function testPj()
  364. {
  365. ob_start();
  366. $this->assertTrue(pj(true));
  367. $result = ob_get_clean();
  368. $expected = "\ntrue\n\n";
  369. $this->assertSame($expected, $result);
  370. ob_start();
  371. $this->assertFalse(pj(false));
  372. $result = ob_get_clean();
  373. $expected = "\nfalse\n\n";
  374. $this->assertSame($expected, $result);
  375. ob_start();
  376. $this->assertNull(pj(null));
  377. $result = ob_get_clean();
  378. $expected = "\nnull\n\n";
  379. $this->assertSame($expected, $result);
  380. ob_start();
  381. $this->assertSame(123, pj(123));
  382. $result = ob_get_clean();
  383. $expected = "\n123\n\n";
  384. $this->assertSame($expected, $result);
  385. ob_start();
  386. pj('123');
  387. $result = ob_get_clean();
  388. $expected = "\n\"123\"\n\n";
  389. $this->assertSame($expected, $result);
  390. ob_start();
  391. pj('this is a test');
  392. $result = ob_get_clean();
  393. $expected = "\n\"this is a test\"\n\n";
  394. $this->assertSame($expected, $result);
  395. ob_start();
  396. $value = ['this' => 'is', 'a' => 'test', 123 => 456];
  397. $this->assertSame($value, pj($value));
  398. $result = ob_get_clean();
  399. $expected = "\n{\n \"this\": \"is\",\n \"a\": \"test\",\n \"123\": 456\n}\n\n";
  400. $this->assertSame($expected, $result);
  401. }
  402. /**
  403. * Test splitting plugin names.
  404. *
  405. * @return void
  406. */
  407. public function testPluginSplit()
  408. {
  409. $result = pluginSplit('Something.else');
  410. $this->assertSame(['Something', 'else'], $result);
  411. $result = pluginSplit('Something.else.more.dots');
  412. $this->assertSame(['Something', 'else.more.dots'], $result);
  413. $result = pluginSplit('Somethingelse');
  414. $this->assertSame([null, 'Somethingelse'], $result);
  415. $result = pluginSplit('Something.else', true);
  416. $this->assertSame(['Something.', 'else'], $result);
  417. $result = pluginSplit('Something.else.more.dots', true);
  418. $this->assertSame(['Something.', 'else.more.dots'], $result);
  419. $result = pluginSplit('Post', false, 'Blog');
  420. $this->assertSame(['Blog', 'Post'], $result);
  421. $result = pluginSplit('Blog.Post', false, 'Ultimate');
  422. $this->assertSame(['Blog', 'Post'], $result);
  423. }
  424. /**
  425. * test namespaceSplit
  426. *
  427. * @return void
  428. */
  429. public function testNamespaceSplit()
  430. {
  431. $result = namespaceSplit('Something');
  432. $this->assertSame(['', 'Something'], $result);
  433. $result = namespaceSplit('\Something');
  434. $this->assertSame(['', 'Something'], $result);
  435. $result = namespaceSplit('Cake\Something');
  436. $this->assertSame(['Cake', 'Something'], $result);
  437. $result = namespaceSplit('Cake\Test\Something');
  438. $this->assertSame(['Cake\Test', 'Something'], $result);
  439. }
  440. /**
  441. * Tests that the stackTrace() method is a shortcut for Debugger::trace()
  442. *
  443. * @return void
  444. */
  445. public function testStackTrace()
  446. {
  447. ob_start();
  448. [$r, $expected] = [stackTrace(), \Cake\Error\Debugger::trace()];
  449. $result = ob_get_clean();
  450. $this->assertSame($expected, $result);
  451. $opts = ['args' => true];
  452. ob_start();
  453. [$r, $expected] = [stackTrace($opts), \Cake\Error\Debugger::trace($opts)];
  454. $result = ob_get_clean();
  455. $this->assertSame($expected, $result);
  456. }
  457. /**
  458. * Tests that the collection() method is a shortcut for new Collection
  459. *
  460. * @return void
  461. */
  462. public function testCollection()
  463. {
  464. $items = [1, 2, 3];
  465. $collection = collection($items);
  466. $this->assertInstanceOf(Collection::class, $collection);
  467. $this->assertSame($items, $collection->toArray());
  468. }
  469. /**
  470. * Test that works in tandem with testEventManagerReset2 to
  471. * test the EventManager reset.
  472. *
  473. * The return value is passed to testEventManagerReset2 as
  474. * an arguments.
  475. *
  476. * @return \Cake\Event\EventManager
  477. */
  478. public function testEventManagerReset1()
  479. {
  480. $eventManager = EventManager::instance();
  481. $this->assertInstanceOf(EventManager::class, $eventManager);
  482. return $eventManager;
  483. }
  484. /**
  485. * Test if the EventManager is reset between tests.
  486. *
  487. * @depends testEventManagerReset1
  488. * @return void
  489. */
  490. public function testEventManagerReset2($prevEventManager)
  491. {
  492. $this->assertInstanceOf(EventManager::class, $prevEventManager);
  493. $this->assertNotSame($prevEventManager, EventManager::instance());
  494. }
  495. }