PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/TestCase/BasicsTest.php

https://github.com/binondord/cakephp
PHP | 589 lines | 394 code | 97 blank | 98 comment | 5 complexity | a8810fb44633f2b31dc5a04d02e2c2a2 MD5 | raw file
  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. }
  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. 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. debug('<div>this-is-a-test</div>', true);
  193. $result = ob_get_clean();
  194. $expectedHtml = <<<EXPECTED
  195. <div class="cake-debug-output">
  196. <span><strong>%s</strong> (line <strong>%d</strong>)</span>
  197. <pre class="cake-debug">
  198. &#039;&lt;div&gt;this-is-a-test&lt;/div&gt;&#039;
  199. </pre>
  200. </div>
  201. EXPECTED;
  202. $expected = sprintf($expectedHtml, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 10);
  203. $this->assertEquals($expected, $result);
  204. ob_start();
  205. debug('<div>this-is-a-test</div>', true, true);
  206. $result = ob_get_clean();
  207. $expected = <<<EXPECTED
  208. <div class="cake-debug-output">
  209. <span><strong>%s</strong> (line <strong>%d</strong>)</span>
  210. <pre class="cake-debug">
  211. &#039;&lt;div&gt;this-is-a-test&lt;/div&gt;&#039;
  212. </pre>
  213. </div>
  214. EXPECTED;
  215. $expected = sprintf($expected, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 10);
  216. $this->assertEquals($expected, $result);
  217. ob_start();
  218. debug('<div>this-is-a-test</div>', true, false);
  219. $result = ob_get_clean();
  220. $expected = <<<EXPECTED
  221. <div class="cake-debug-output">
  222. <pre class="cake-debug">
  223. &#039;&lt;div&gt;this-is-a-test&lt;/div&gt;&#039;
  224. </pre>
  225. </div>
  226. EXPECTED;
  227. $expected = sprintf($expected, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 10);
  228. $this->assertEquals($expected, $result);
  229. ob_start();
  230. debug('<div>this-is-a-test</div>', null);
  231. $result = ob_get_clean();
  232. $expectedHtml = <<<EXPECTED
  233. <div class="cake-debug-output">
  234. <span><strong>%s</strong> (line <strong>%d</strong>)</span>
  235. <pre class="cake-debug">
  236. &#039;&lt;div&gt;this-is-a-test&lt;/div&gt;&#039;
  237. </pre>
  238. </div>
  239. EXPECTED;
  240. $expectedText = <<<EXPECTED
  241. %s (line %d)
  242. ########## DEBUG ##########
  243. '<div>this-is-a-test</div>'
  244. ###########################
  245. EXPECTED;
  246. if (PHP_SAPI === 'cli') {
  247. $expected = sprintf($expectedText, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 18);
  248. } else {
  249. $expected = sprintf($expectedHtml, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 19);
  250. }
  251. $this->assertEquals($expected, $result);
  252. ob_start();
  253. debug('<div>this-is-a-test</div>', null, false);
  254. $result = ob_get_clean();
  255. $expectedHtml = <<<EXPECTED
  256. <div class="cake-debug-output">
  257. <pre class="cake-debug">
  258. &#039;&lt;div&gt;this-is-a-test&lt;/div&gt;&#039;
  259. </pre>
  260. </div>
  261. EXPECTED;
  262. $expectedText = <<<EXPECTED
  263. ########## DEBUG ##########
  264. '<div>this-is-a-test</div>'
  265. ###########################
  266. EXPECTED;
  267. if (PHP_SAPI === 'cli') {
  268. $expected = sprintf($expectedText, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 18);
  269. } else {
  270. $expected = sprintf($expectedHtml, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 19);
  271. }
  272. $this->assertEquals($expected, $result);
  273. ob_start();
  274. debug('<div>this-is-a-test</div>', false);
  275. $result = ob_get_clean();
  276. $expected = <<<EXPECTED
  277. %s (line %d)
  278. ########## DEBUG ##########
  279. '<div>this-is-a-test</div>'
  280. ###########################
  281. EXPECTED;
  282. $expected = sprintf($expected, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 9);
  283. $this->assertEquals($expected, $result);
  284. ob_start();
  285. debug('<div>this-is-a-test</div>', false, true);
  286. $result = ob_get_clean();
  287. $expected = <<<EXPECTED
  288. %s (line %d)
  289. ########## DEBUG ##########
  290. '<div>this-is-a-test</div>'
  291. ###########################
  292. EXPECTED;
  293. $expected = sprintf($expected, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 9);
  294. $this->assertEquals($expected, $result);
  295. ob_start();
  296. debug('<div>this-is-a-test</div>', false, false);
  297. $result = ob_get_clean();
  298. $expected = <<<EXPECTED
  299. ########## DEBUG ##########
  300. '<div>this-is-a-test</div>'
  301. ###########################
  302. EXPECTED;
  303. $expected = sprintf($expected, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 9);
  304. $this->assertEquals($expected, $result);
  305. ob_start();
  306. debug(false, false, false);
  307. $result = ob_get_clean();
  308. $expected = <<<EXPECTED
  309. ########## DEBUG ##########
  310. false
  311. ###########################
  312. EXPECTED;
  313. $expected = sprintf($expected, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 9);
  314. $this->assertEquals($expected, $result);
  315. }
  316. /**
  317. * test pr()
  318. *
  319. * @return void
  320. */
  321. public function testPr()
  322. {
  323. ob_start();
  324. pr(true);
  325. $result = ob_get_clean();
  326. $expected = "\n1\n\n";
  327. $this->assertEquals($expected, $result);
  328. ob_start();
  329. pr(false);
  330. $result = ob_get_clean();
  331. $expected = "\n\n\n";
  332. $this->assertEquals($expected, $result);
  333. ob_start();
  334. pr(null);
  335. $result = ob_get_clean();
  336. $expected = "\n\n\n";
  337. $this->assertEquals($expected, $result);
  338. ob_start();
  339. pr(123);
  340. $result = ob_get_clean();
  341. $expected = "\n123\n\n";
  342. $this->assertEquals($expected, $result);
  343. ob_start();
  344. pr('123');
  345. $result = ob_get_clean();
  346. $expected = "\n123\n\n";
  347. $this->assertEquals($expected, $result);
  348. ob_start();
  349. pr('this is a test');
  350. $result = ob_get_clean();
  351. $expected = "\nthis is a test\n\n";
  352. $this->assertEquals($expected, $result);
  353. ob_start();
  354. pr(['this' => 'is', 'a' => 'test', 123 => 456]);
  355. $result = ob_get_clean();
  356. $expected = "\nArray\n(\n [this] => is\n [a] => test\n [123] => 456\n)\n\n";
  357. $this->assertEquals($expected, $result);
  358. }
  359. /**
  360. * test pj()
  361. *
  362. * @return void
  363. */
  364. public function testPj()
  365. {
  366. ob_start();
  367. pj(true);
  368. $result = ob_get_clean();
  369. $expected = "\ntrue\n\n";
  370. $this->assertEquals($expected, $result);
  371. ob_start();
  372. pj(false);
  373. $result = ob_get_clean();
  374. $expected = "\nfalse\n\n";
  375. $this->assertEquals($expected, $result);
  376. ob_start();
  377. pj(null);
  378. $result = ob_get_clean();
  379. $expected = "\nnull\n\n";
  380. $this->assertEquals($expected, $result);
  381. ob_start();
  382. pj(123);
  383. $result = ob_get_clean();
  384. $expected = "\n123\n\n";
  385. $this->assertEquals($expected, $result);
  386. ob_start();
  387. pj('123');
  388. $result = ob_get_clean();
  389. $expected = "\n\"123\"\n\n";
  390. $this->assertEquals($expected, $result);
  391. ob_start();
  392. pj('this is a test');
  393. $result = ob_get_clean();
  394. $expected = "\n\"this is a test\"\n\n";
  395. $this->assertEquals($expected, $result);
  396. ob_start();
  397. pj(['this' => 'is', 'a' => 'test', 123 => 456]);
  398. $result = ob_get_clean();
  399. $expected = "\n{\n \"this\": \"is\",\n \"a\": \"test\",\n \"123\": 456\n}\n\n";
  400. $this->assertEquals($expected, $result);
  401. }
  402. /**
  403. * Test splitting plugin names.
  404. *
  405. * @return void
  406. */
  407. public function testPluginSplit()
  408. {
  409. $result = pluginSplit('Something.else');
  410. $this->assertEquals(['Something', 'else'], $result);
  411. $result = pluginSplit('Something.else.more.dots');
  412. $this->assertEquals(['Something', 'else.more.dots'], $result);
  413. $result = pluginSplit('Somethingelse');
  414. $this->assertEquals([null, 'Somethingelse'], $result);
  415. $result = pluginSplit('Something.else', true);
  416. $this->assertEquals(['Something.', 'else'], $result);
  417. $result = pluginSplit('Something.else.more.dots', true);
  418. $this->assertEquals(['Something.', 'else.more.dots'], $result);
  419. $result = pluginSplit('Post', false, 'Blog');
  420. $this->assertEquals(['Blog', 'Post'], $result);
  421. $result = pluginSplit('Blog.Post', false, 'Ultimate');
  422. $this->assertEquals(['Blog', 'Post'], $result);
  423. }
  424. /**
  425. * test namespaceSplit
  426. *
  427. * @return void
  428. */
  429. public function testNamespaceSplit()
  430. {
  431. $result = namespaceSplit('Something');
  432. $this->assertEquals(['', 'Something'], $result);
  433. $result = namespaceSplit('\Something');
  434. $this->assertEquals(['', 'Something'], $result);
  435. $result = namespaceSplit('Cake\Something');
  436. $this->assertEquals(['Cake', 'Something'], $result);
  437. $result = namespaceSplit('Cake\Test\Something');
  438. $this->assertEquals(['Cake\Test', 'Something'], $result);
  439. }
  440. /**
  441. * Tests that the stackTrace() method is a shortcut for Debugger::trace()
  442. *
  443. * @return void
  444. */
  445. public function testStackTrace()
  446. {
  447. ob_start();
  448. list($r, $expected) = [stackTrace(), \Cake\Error\Debugger::trace()];
  449. $result = ob_get_clean();
  450. $this->assertEquals($expected, $result);
  451. $opts = ['args' => true];
  452. ob_start();
  453. list($r, $expected) = [stackTrace($opts), \Cake\Error\Debugger::trace($opts)];
  454. $result = ob_get_clean();
  455. $this->assertEquals($expected, $result);
  456. }
  457. /**
  458. * Tests that the collection() method is a shortcut for new Collection
  459. *
  460. * @return void
  461. */
  462. public function testCollection()
  463. {
  464. $items = [1, 2, 3];
  465. $collection = collection($items);
  466. $this->assertInstanceOf('Cake\Collection\Collection', $collection);
  467. $this->assertSame($items, $collection->toArray());
  468. }
  469. /**
  470. * Test that works in tandem with testEventManagerReset2 to
  471. * test the EventManager reset.
  472. *
  473. * The return value is passed to testEventManagerReset2 as
  474. * an arguments.
  475. *
  476. * @return \Cake\Event\EventManager
  477. */
  478. public function testEventManagerReset1()
  479. {
  480. return EventManager::instance();
  481. }
  482. /**
  483. * Test if the EventManager is reset between tests.
  484. *
  485. * @depends testEventManagerReset1
  486. * @return void
  487. */
  488. public function testEventManagerReset2($prevEventManager)
  489. {
  490. $this->assertNotSame($prevEventManager, EventManager::instance());
  491. }
  492. }