BasicsTest.php 17 KB

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