BasicsTest.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  1. <?php
  2. /**
  3. * BasicsTest file
  4. *
  5. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  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://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  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\Filesystem\Folder;
  22. use Cake\Log\Log;
  23. use Cake\Network\Response;
  24. use Cake\TestSuite\TestCase;
  25. require_once CAKE . 'basics.php';
  26. /**
  27. * BasicsTest class
  28. */
  29. class BasicsTest extends TestCase
  30. {
  31. /**
  32. * test the array_diff_key compatibility function.
  33. *
  34. * @return void
  35. */
  36. public function testArrayDiffKey()
  37. {
  38. $one = ['one' => 1, 'two' => 2, 'three' => 3];
  39. $two = ['one' => 'one', 'two' => 'two'];
  40. $result = array_diff_key($one, $two);
  41. $expected = ['three' => 3];
  42. $this->assertEquals($expected, $result);
  43. $one = ['one' => ['value', 'value-two'], 'two' => 2, 'three' => 3];
  44. $two = ['two' => 'two'];
  45. $result = array_diff_key($one, $two);
  46. $expected = ['one' => ['value', 'value-two'], 'three' => 3];
  47. $this->assertEquals($expected, $result);
  48. $one = ['one' => null, 'two' => 2, 'three' => '', 'four' => 0];
  49. $two = ['two' => 'two'];
  50. $result = array_diff_key($one, $two);
  51. $expected = ['one' => null, 'three' => '', 'four' => 0];
  52. $this->assertEquals($expected, $result);
  53. $one = ['minYear' => null, 'maxYear' => null, 'separator' => '-', 'interval' => 1, 'monthNames' => true];
  54. $two = ['minYear' => null, 'maxYear' => null, 'separator' => '-', 'interval' => 1, 'monthNames' => true];
  55. $result = array_diff_key($one, $two);
  56. $this->assertSame([], $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. 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. debug('<div>this-is-a-test</div>', true);
  192. $result = ob_get_clean();
  193. $expectedHtml = <<<EXPECTED
  194. <div class="cake-debug-output">
  195. <span><strong>%s</strong> (line <strong>%d</strong>)</span>
  196. <pre class="cake-debug">
  197. &#039;&lt;div&gt;this-is-a-test&lt;/div&gt;&#039;
  198. </pre>
  199. </div>
  200. EXPECTED;
  201. $expected = sprintf($expectedHtml, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 10);
  202. $this->assertEquals($expected, $result);
  203. ob_start();
  204. debug('<div>this-is-a-test</div>', true, true);
  205. $result = ob_get_clean();
  206. $expected = <<<EXPECTED
  207. <div class="cake-debug-output">
  208. <span><strong>%s</strong> (line <strong>%d</strong>)</span>
  209. <pre class="cake-debug">
  210. &#039;&lt;div&gt;this-is-a-test&lt;/div&gt;&#039;
  211. </pre>
  212. </div>
  213. EXPECTED;
  214. $expected = sprintf($expected, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 10);
  215. $this->assertEquals($expected, $result);
  216. ob_start();
  217. debug('<div>this-is-a-test</div>', true, false);
  218. $result = ob_get_clean();
  219. $expected = <<<EXPECTED
  220. <div class="cake-debug-output">
  221. <pre class="cake-debug">
  222. &#039;&lt;div&gt;this-is-a-test&lt;/div&gt;&#039;
  223. </pre>
  224. </div>
  225. EXPECTED;
  226. $expected = sprintf($expected, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 10);
  227. $this->assertEquals($expected, $result);
  228. ob_start();
  229. debug('<div>this-is-a-test</div>', null);
  230. $result = ob_get_clean();
  231. $expectedHtml = <<<EXPECTED
  232. <div class="cake-debug-output">
  233. <span><strong>%s</strong> (line <strong>%d</strong>)</span>
  234. <pre class="cake-debug">
  235. &#039;&lt;div&gt;this-is-a-test&lt;/div&gt;&#039;
  236. </pre>
  237. </div>
  238. EXPECTED;
  239. $expectedText = <<<EXPECTED
  240. %s (line %d)
  241. ########## DEBUG ##########
  242. '<div>this-is-a-test</div>'
  243. ###########################
  244. EXPECTED;
  245. if (php_sapi_name() === 'cli') {
  246. $expected = sprintf($expectedText, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 18);
  247. } else {
  248. $expected = sprintf($expectedHtml, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 19);
  249. }
  250. $this->assertEquals($expected, $result);
  251. ob_start();
  252. debug('<div>this-is-a-test</div>', null, false);
  253. $result = ob_get_clean();
  254. $expectedHtml = <<<EXPECTED
  255. <div class="cake-debug-output">
  256. <pre class="cake-debug">
  257. &#039;&lt;div&gt;this-is-a-test&lt;/div&gt;&#039;
  258. </pre>
  259. </div>
  260. EXPECTED;
  261. $expectedText = <<<EXPECTED
  262. ########## DEBUG ##########
  263. '<div>this-is-a-test</div>'
  264. ###########################
  265. EXPECTED;
  266. if (php_sapi_name() === 'cli') {
  267. $expected = sprintf($expectedText, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 18);
  268. } else {
  269. $expected = sprintf($expectedHtml, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 19);
  270. }
  271. $this->assertEquals($expected, $result);
  272. ob_start();
  273. debug('<div>this-is-a-test</div>', false);
  274. $result = ob_get_clean();
  275. $expected = <<<EXPECTED
  276. %s (line %d)
  277. ########## DEBUG ##########
  278. '<div>this-is-a-test</div>'
  279. ###########################
  280. EXPECTED;
  281. $expected = sprintf($expected, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 9);
  282. $this->assertEquals($expected, $result);
  283. ob_start();
  284. debug('<div>this-is-a-test</div>', false, true);
  285. $result = ob_get_clean();
  286. $expected = <<<EXPECTED
  287. %s (line %d)
  288. ########## DEBUG ##########
  289. '<div>this-is-a-test</div>'
  290. ###########################
  291. EXPECTED;
  292. $expected = sprintf($expected, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 9);
  293. $this->assertEquals($expected, $result);
  294. ob_start();
  295. debug('<div>this-is-a-test</div>', false, false);
  296. $result = ob_get_clean();
  297. $expected = <<<EXPECTED
  298. ########## DEBUG ##########
  299. '<div>this-is-a-test</div>'
  300. ###########################
  301. EXPECTED;
  302. $expected = sprintf($expected, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 9);
  303. $this->assertEquals($expected, $result);
  304. ob_start();
  305. debug(false, false, false);
  306. $result = ob_get_clean();
  307. $expected = <<<EXPECTED
  308. ########## DEBUG ##########
  309. false
  310. ###########################
  311. EXPECTED;
  312. $expected = sprintf($expected, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 9);
  313. $this->assertEquals($expected, $result);
  314. }
  315. /**
  316. * test pr()
  317. *
  318. * @return void
  319. */
  320. public function testPr()
  321. {
  322. ob_start();
  323. pr(true);
  324. $result = ob_get_clean();
  325. $expected = "\n1\n\n";
  326. $this->assertEquals($expected, $result);
  327. ob_start();
  328. pr(false);
  329. $result = ob_get_clean();
  330. $expected = "\n\n\n";
  331. $this->assertEquals($expected, $result);
  332. ob_start();
  333. pr(null);
  334. $result = ob_get_clean();
  335. $expected = "\n\n\n";
  336. $this->assertEquals($expected, $result);
  337. ob_start();
  338. pr(123);
  339. $result = ob_get_clean();
  340. $expected = "\n123\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('this is a test');
  349. $result = ob_get_clean();
  350. $expected = "\nthis is a test\n\n";
  351. $this->assertEquals($expected, $result);
  352. ob_start();
  353. pr(['this' => 'is', 'a' => 'test', 123 => 456]);
  354. $result = ob_get_clean();
  355. $expected = "\nArray\n(\n [this] => is\n [a] => test\n [123] => 456\n)\n\n";
  356. $this->assertEquals($expected, $result);
  357. }
  358. /**
  359. * test pj()
  360. *
  361. * @return void
  362. */
  363. public function testPj()
  364. {
  365. ob_start();
  366. pj(true);
  367. $result = ob_get_clean();
  368. $expected = "\ntrue\n\n";
  369. $this->assertEquals($expected, $result);
  370. ob_start();
  371. pj(false);
  372. $result = ob_get_clean();
  373. $expected = "\nfalse\n\n";
  374. $this->assertEquals($expected, $result);
  375. ob_start();
  376. pj(null);
  377. $result = ob_get_clean();
  378. $expected = "\nnull\n\n";
  379. $this->assertEquals($expected, $result);
  380. ob_start();
  381. pj(123);
  382. $result = ob_get_clean();
  383. $expected = "\n123\n\n";
  384. $this->assertEquals($expected, $result);
  385. ob_start();
  386. pj('123');
  387. $result = ob_get_clean();
  388. $expected = "\n\"123\"\n\n";
  389. $this->assertEquals($expected, $result);
  390. ob_start();
  391. pj('this is a test');
  392. $result = ob_get_clean();
  393. $expected = "\n\"this is a test\"\n\n";
  394. $this->assertEquals($expected, $result);
  395. ob_start();
  396. pj(['this' => 'is', 'a' => 'test', 123 => 456]);
  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. }