BasicsTest.php 17 KB

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