BasicsTest.php 17 KB

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