BasicsTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  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\Log\Log;
  22. use Cake\Network\Response;
  23. use Cake\TestSuite\TestCase;
  24. use Cake\Utility\Folder;
  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['HTTP_HOST'] = 'localhost';
  66. $this->assertEquals(env('HTTP_BASE'), '.localhost');
  67. $_SERVER['HTTP_HOST'] = 'com.ar';
  68. $this->assertEquals(env('HTTP_BASE'), '.com.ar');
  69. $_SERVER['HTTP_HOST'] = 'example.ar';
  70. $this->assertEquals(env('HTTP_BASE'), '.example.ar');
  71. $_SERVER['HTTP_HOST'] = 'example.com';
  72. $this->assertEquals(env('HTTP_BASE'), '.example.com');
  73. $_SERVER['HTTP_HOST'] = 'www.example.com';
  74. $this->assertEquals(env('HTTP_BASE'), '.example.com');
  75. $_SERVER['HTTP_HOST'] = 'subdomain.example.com';
  76. $this->assertEquals(env('HTTP_BASE'), '.example.com');
  77. $_SERVER['HTTP_HOST'] = 'example.com.ar';
  78. $this->assertEquals(env('HTTP_BASE'), '.example.com.ar');
  79. $_SERVER['HTTP_HOST'] = 'www.example.com.ar';
  80. $this->assertEquals(env('HTTP_BASE'), '.example.com.ar');
  81. $_SERVER['HTTP_HOST'] = 'subdomain.example.com.ar';
  82. $this->assertEquals(env('HTTP_BASE'), '.example.com.ar');
  83. $_SERVER['HTTP_HOST'] = 'double.subdomain.example.com';
  84. $this->assertEquals(env('HTTP_BASE'), '.subdomain.example.com');
  85. $_SERVER['HTTP_HOST'] = 'double.subdomain.example.com.ar';
  86. $this->assertEquals(env('HTTP_BASE'), '.subdomain.example.com.ar');
  87. $_SERVER = $_ENV = array();
  88. $_SERVER['SCRIPT_NAME'] = '/a/test/test.php';
  89. $this->assertEquals(env('SCRIPT_NAME'), '/a/test/test.php');
  90. $_SERVER = $_ENV = array();
  91. $_ENV['CGI_MODE'] = 'BINARY';
  92. $_ENV['SCRIPT_URL'] = '/a/test/test.php';
  93. $this->assertEquals(env('SCRIPT_NAME'), '/a/test/test.php');
  94. $_SERVER = $_ENV = array();
  95. $this->assertFalse(env('HTTPS'));
  96. $_SERVER['HTTPS'] = 'on';
  97. $this->assertTrue(env('HTTPS'));
  98. $_SERVER['HTTPS'] = '1';
  99. $this->assertTrue(env('HTTPS'));
  100. $_SERVER['HTTPS'] = 'I am not empty';
  101. $this->assertTrue(env('HTTPS'));
  102. $_SERVER['HTTPS'] = 1;
  103. $this->assertTrue(env('HTTPS'));
  104. $_SERVER['HTTPS'] = 'off';
  105. $this->assertFalse(env('HTTPS'));
  106. $_SERVER['HTTPS'] = false;
  107. $this->assertFalse(env('HTTPS'));
  108. $_SERVER['HTTPS'] = '';
  109. $this->assertFalse(env('HTTPS'));
  110. $_SERVER = array();
  111. $_ENV['SCRIPT_URI'] = 'https://domain.test/a/test.php';
  112. $this->assertTrue(env('HTTPS'));
  113. $_ENV['SCRIPT_URI'] = 'http://domain.test/a/test.php';
  114. $this->assertFalse(env('HTTPS'));
  115. $_SERVER = $_ENV = array();
  116. $this->assertNull(env('TEST_ME'));
  117. $_ENV['TEST_ME'] = 'a';
  118. $this->assertEquals(env('TEST_ME'), 'a');
  119. $_SERVER['TEST_ME'] = 'b';
  120. $this->assertEquals(env('TEST_ME'), 'b');
  121. unset($_ENV['TEST_ME']);
  122. $this->assertEquals(env('TEST_ME'), 'b');
  123. $_SERVER = $server;
  124. $_ENV = $env;
  125. }
  126. /**
  127. * Test h()
  128. *
  129. * @return void
  130. */
  131. public function testH() {
  132. $string = '<foo>';
  133. $result = h($string);
  134. $this->assertEquals('&lt;foo&gt;', $result);
  135. $in = array('this & that', '<p>Which one</p>');
  136. $result = h($in);
  137. $expected = array('this &amp; that', '&lt;p&gt;Which one&lt;/p&gt;');
  138. $this->assertEquals($expected, $result);
  139. $string = '<foo> & &nbsp;';
  140. $result = h($string);
  141. $this->assertEquals('&lt;foo&gt; &amp; &amp;nbsp;', $result);
  142. $string = '<foo> & &nbsp;';
  143. $result = h($string, false);
  144. $this->assertEquals('&lt;foo&gt; &amp; &nbsp;', $result);
  145. $string = '<foo> & &nbsp;';
  146. $result = h($string, 'UTF-8');
  147. $this->assertEquals('&lt;foo&gt; &amp; &amp;nbsp;', $result);
  148. $string = "An invalid\x80string";
  149. $result = h($string);
  150. $this->assertContains('string', $result);
  151. $arr = array('<foo>', '&nbsp;');
  152. $result = h($arr);
  153. $expected = array(
  154. '&lt;foo&gt;',
  155. '&amp;nbsp;'
  156. );
  157. $this->assertEquals($expected, $result);
  158. $arr = array('<foo>', '&nbsp;');
  159. $result = h($arr, false);
  160. $expected = array(
  161. '&lt;foo&gt;',
  162. '&nbsp;'
  163. );
  164. $this->assertEquals($expected, $result);
  165. $arr = array('f' => '<foo>', 'n' => '&nbsp;');
  166. $result = h($arr, false);
  167. $expected = array(
  168. 'f' => '&lt;foo&gt;',
  169. 'n' => '&nbsp;'
  170. );
  171. $this->assertEquals($expected, $result);
  172. $arr = array('invalid' => "\x99An invalid\x80string", 'good' => 'Good string');
  173. $result = h($arr);
  174. $this->assertContains('An invalid', $result['invalid']);
  175. $this->assertEquals('Good string', $result['good']);
  176. // Test that boolean values are not converted to strings
  177. $result = h(false);
  178. $this->assertFalse($result);
  179. $arr = array('foo' => false, 'bar' => true);
  180. $result = h($arr);
  181. $this->assertFalse($result['foo']);
  182. $this->assertTrue($result['bar']);
  183. $obj = new \stdClass();
  184. $result = h($obj);
  185. $this->assertEquals('(object)stdClass', $result);
  186. $obj = new Response(array('body' => 'Body content'));
  187. $result = h($obj);
  188. $this->assertEquals('Body content', $result);
  189. }
  190. /**
  191. * test debug()
  192. *
  193. * @return void
  194. */
  195. public function testDebug() {
  196. ob_start();
  197. debug('this-is-a-test', false);
  198. $result = ob_get_clean();
  199. $expectedText = <<<EXPECTED
  200. %s (line %d)
  201. ########## DEBUG ##########
  202. 'this-is-a-test'
  203. ###########################
  204. EXPECTED;
  205. $expected = sprintf($expectedText, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 9);
  206. $this->assertEquals($expected, $result);
  207. ob_start();
  208. debug('<div>this-is-a-test</div>', true);
  209. $result = ob_get_clean();
  210. $expectedHtml = <<<EXPECTED
  211. <div class="cake-debug-output">
  212. <span><strong>%s</strong> (line <strong>%d</strong>)</span>
  213. <pre class="cake-debug">
  214. &#039;&lt;div&gt;this-is-a-test&lt;/div&gt;&#039;
  215. </pre>
  216. </div>
  217. EXPECTED;
  218. $expected = sprintf($expectedHtml, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 10);
  219. $this->assertEquals($expected, $result);
  220. ob_start();
  221. debug('<div>this-is-a-test</div>', true, true);
  222. $result = ob_get_clean();
  223. $expected = <<<EXPECTED
  224. <div class="cake-debug-output">
  225. <span><strong>%s</strong> (line <strong>%d</strong>)</span>
  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>', true, false);
  235. $result = ob_get_clean();
  236. $expected = <<<EXPECTED
  237. <div class="cake-debug-output">
  238. <pre class="cake-debug">
  239. &#039;&lt;div&gt;this-is-a-test&lt;/div&gt;&#039;
  240. </pre>
  241. </div>
  242. EXPECTED;
  243. $expected = sprintf($expected, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 10);
  244. $this->assertEquals($expected, $result);
  245. ob_start();
  246. debug('<div>this-is-a-test</div>', null);
  247. $result = ob_get_clean();
  248. $expectedHtml = <<<EXPECTED
  249. <div class="cake-debug-output">
  250. <span><strong>%s</strong> (line <strong>%d</strong>)</span>
  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. %s (line %d)
  258. ########## DEBUG ##########
  259. '<div>this-is-a-test</div>'
  260. ###########################
  261. EXPECTED;
  262. if (php_sapi_name() === 'cli') {
  263. $expected = sprintf($expectedText, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 18);
  264. } else {
  265. $expected = sprintf($expectedHtml, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 19);
  266. }
  267. $this->assertEquals($expected, $result);
  268. ob_start();
  269. debug('<div>this-is-a-test</div>', null, false);
  270. $result = ob_get_clean();
  271. $expectedHtml = <<<EXPECTED
  272. <div class="cake-debug-output">
  273. <pre class="cake-debug">
  274. &#039;&lt;div&gt;this-is-a-test&lt;/div&gt;&#039;
  275. </pre>
  276. </div>
  277. EXPECTED;
  278. $expectedText = <<<EXPECTED
  279. ########## DEBUG ##########
  280. '<div>this-is-a-test</div>'
  281. ###########################
  282. EXPECTED;
  283. if (php_sapi_name() === 'cli') {
  284. $expected = sprintf($expectedText, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 18);
  285. } else {
  286. $expected = sprintf($expectedHtml, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 19);
  287. }
  288. $this->assertEquals($expected, $result);
  289. ob_start();
  290. debug('<div>this-is-a-test</div>', false);
  291. $result = ob_get_clean();
  292. $expected = <<<EXPECTED
  293. %s (line %d)
  294. ########## DEBUG ##########
  295. '<div>this-is-a-test</div>'
  296. ###########################
  297. EXPECTED;
  298. $expected = sprintf($expected, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 9);
  299. $this->assertEquals($expected, $result);
  300. ob_start();
  301. debug('<div>this-is-a-test</div>', false, true);
  302. $result = ob_get_clean();
  303. $expected = <<<EXPECTED
  304. %s (line %d)
  305. ########## DEBUG ##########
  306. '<div>this-is-a-test</div>'
  307. ###########################
  308. EXPECTED;
  309. $expected = sprintf($expected, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 9);
  310. $this->assertEquals($expected, $result);
  311. ob_start();
  312. debug('<div>this-is-a-test</div>', false, false);
  313. $result = ob_get_clean();
  314. $expected = <<<EXPECTED
  315. ########## DEBUG ##########
  316. '<div>this-is-a-test</div>'
  317. ###########################
  318. EXPECTED;
  319. $expected = sprintf($expected, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 9);
  320. $this->assertEquals($expected, $result);
  321. ob_start();
  322. debug(false, false, false);
  323. $result = ob_get_clean();
  324. $expected = <<<EXPECTED
  325. ########## DEBUG ##########
  326. false
  327. ###########################
  328. EXPECTED;
  329. $expected = sprintf($expected, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 9);
  330. $this->assertEquals($expected, $result);
  331. }
  332. /**
  333. * test pr()
  334. *
  335. * @return void
  336. */
  337. public function testPr() {
  338. $this->skipIf(php_sapi_name() === 'cli', 'Skipping web test in cli mode');
  339. ob_start();
  340. pr('this is a test');
  341. $result = ob_get_clean();
  342. $expected = "<pre>this is a test</pre>";
  343. $this->assertEquals($expected, $result);
  344. ob_start();
  345. pr(array('this' => 'is', 'a' => 'test'));
  346. $result = ob_get_clean();
  347. $expected = "<pre>Array\n(\n [this] => is\n [a] => test\n)\n</pre>";
  348. $this->assertEquals($expected, $result);
  349. }
  350. /**
  351. * test pr()
  352. *
  353. * @return void
  354. */
  355. public function testPrCli() {
  356. $this->skipIf(php_sapi_name() != 'cli', 'Skipping cli test in web mode');
  357. ob_start();
  358. pr('this is a test');
  359. $result = ob_get_clean();
  360. $expected = "\nthis is a test\n";
  361. $this->assertEquals($expected, $result);
  362. ob_start();
  363. pr(array('this' => 'is', 'a' => 'test'));
  364. $result = ob_get_clean();
  365. $expected = "\nArray\n(\n [this] => is\n [a] => test\n)\n\n";
  366. $this->assertEquals($expected, $result);
  367. }
  368. /**
  369. * Test splitting plugin names.
  370. *
  371. * @return void
  372. */
  373. public function testPluginSplit() {
  374. $result = pluginSplit('Something.else');
  375. $this->assertEquals(array('Something', 'else'), $result);
  376. $result = pluginSplit('Something.else.more.dots');
  377. $this->assertEquals(array('Something', 'else.more.dots'), $result);
  378. $result = pluginSplit('Somethingelse');
  379. $this->assertEquals(array(null, 'Somethingelse'), $result);
  380. $result = pluginSplit('Something.else', true);
  381. $this->assertEquals(array('Something.', 'else'), $result);
  382. $result = pluginSplit('Something.else.more.dots', true);
  383. $this->assertEquals(array('Something.', 'else.more.dots'), $result);
  384. $result = pluginSplit('Post', false, 'Blog');
  385. $this->assertEquals(array('Blog', 'Post'), $result);
  386. $result = pluginSplit('Blog.Post', false, 'Ultimate');
  387. $this->assertEquals(array('Blog', 'Post'), $result);
  388. }
  389. /**
  390. * test namespaceSplit
  391. *
  392. * @return void
  393. */
  394. public function testNamespaceSplit() {
  395. $result = namespaceSplit('Something');
  396. $this->assertEquals(array('', 'Something'), $result);
  397. $result = namespaceSplit('\Something');
  398. $this->assertEquals(array('', 'Something'), $result);
  399. $result = namespaceSplit('Cake\Something');
  400. $this->assertEquals(array('Cake', 'Something'), $result);
  401. $result = namespaceSplit('Cake\Test\Something');
  402. $this->assertEquals(array('Cake\Test', 'Something'), $result);
  403. }
  404. /**
  405. * Tests that the stackTrace() method is a shortcut for Debugger::trace()
  406. *
  407. * @return void
  408. */
  409. public function testStackTrace() {
  410. ob_start();
  411. list($r, $expected) = [stackTrace(), \Cake\Utility\Debugger::trace()];
  412. $result = ob_get_clean();
  413. $this->assertEquals($expected, $result);
  414. $opts = ['args' => true];
  415. ob_start();
  416. list($r, $expected) = [stackTrace($opts), \Cake\Utility\Debugger::trace($opts)];
  417. $result = ob_get_clean();
  418. $this->assertEquals($expected, $result);
  419. }
  420. /**
  421. * Tests that the collection() method is a shortcut for new Collection
  422. *
  423. * @return void
  424. */
  425. public function testCollection() {
  426. $items = [1, 2, 3];
  427. $collection = collection($items);
  428. $this->assertInstanceOf('Cake\Collection\Collection', $collection);
  429. $this->assertSame($items, $collection->toArray());
  430. }
  431. }