BasicsTest.php 17 KB

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