BasicsTest.php 14 KB

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