BasicsTest.php 18 KB

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