PageRenderTime 48ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/Cake/Test/Case/BasicsTest.php

http://github.com/cakephp/cakephp
PHP | 943 lines | 636 code | 161 blank | 146 comment | 11 complexity | a5e3b20f443b7d82d61a548c272e98bd MD5 | raw file
Possible License(s): JSON
  1. <?php
  2. /**
  3. * BasicsTest file
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
  8. * Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice
  12. *
  13. * @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
  15. * @package Cake.Test.Case
  16. * @since CakePHP(tm) v 1.2.0.4206
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. require_once CAKE . 'basics.php';
  20. App::uses('Folder', 'Utility');
  21. App::uses('CakeResponse', 'Network');
  22. /**
  23. * BasicsTest class
  24. *
  25. * @package Cake.Test.Case
  26. */
  27. class BasicsTest extends CakeTestCase {
  28. /**
  29. * setUp method
  30. *
  31. * @return void
  32. */
  33. public function setUp() {
  34. App::build(array(
  35. 'locales' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Locale' . DS)
  36. ));
  37. $this->_language = Configure::read('Config.language');
  38. }
  39. /**
  40. * tearDown method
  41. *
  42. * @return void
  43. */
  44. public function tearDown() {
  45. App::build();
  46. Configure::write('Config.language', $this->_language);
  47. }
  48. /**
  49. * test the array_diff_key compatibility function.
  50. *
  51. * @return void
  52. */
  53. public function testArrayDiffKey() {
  54. $one = array('one' => 1, 'two' => 2, 'three' => 3);
  55. $two = array('one' => 'one', 'two' => 'two');
  56. $result = array_diff_key($one, $two);
  57. $expected = array('three' => 3);
  58. $this->assertEquals($expected, $result);
  59. $one = array('one' => array('value', 'value-two'), 'two' => 2, 'three' => 3);
  60. $two = array('two' => 'two');
  61. $result = array_diff_key($one, $two);
  62. $expected = array('one' => array('value', 'value-two'), 'three' => 3);
  63. $this->assertEquals($expected, $result);
  64. $one = array('one' => null, 'two' => 2, 'three' => '', 'four' => 0);
  65. $two = array('two' => 'two');
  66. $result = array_diff_key($one, $two);
  67. $expected = array('one' => null, 'three' => '', 'four' => 0);
  68. $this->assertEquals($expected, $result);
  69. $one = array('minYear' => null, 'maxYear' => null, 'separator' => '-', 'interval' => 1, 'monthNames' => true);
  70. $two = array('minYear' => null, 'maxYear' => null, 'separator' => '-', 'interval' => 1, 'monthNames' => true);
  71. $result = array_diff_key($one, $two);
  72. $this->assertEquals($result, array());
  73. }
  74. /**
  75. * testHttpBase method
  76. *
  77. * @return void
  78. */
  79. public function testEnv() {
  80. $this->skipIf(!function_exists('ini_get') || ini_get('safe_mode') === '1', 'Safe mode is on.');
  81. $__SERVER = $_SERVER;
  82. $__ENV = $_ENV;
  83. $_SERVER['HTTP_HOST'] = 'localhost';
  84. $this->assertEquals(env('HTTP_BASE'), '.localhost');
  85. $_SERVER['HTTP_HOST'] = 'com.ar';
  86. $this->assertEquals(env('HTTP_BASE'), '.com.ar');
  87. $_SERVER['HTTP_HOST'] = 'example.ar';
  88. $this->assertEquals(env('HTTP_BASE'), '.example.ar');
  89. $_SERVER['HTTP_HOST'] = 'example.com';
  90. $this->assertEquals(env('HTTP_BASE'), '.example.com');
  91. $_SERVER['HTTP_HOST'] = 'www.example.com';
  92. $this->assertEquals(env('HTTP_BASE'), '.example.com');
  93. $_SERVER['HTTP_HOST'] = 'subdomain.example.com';
  94. $this->assertEquals(env('HTTP_BASE'), '.example.com');
  95. $_SERVER['HTTP_HOST'] = 'example.com.ar';
  96. $this->assertEquals(env('HTTP_BASE'), '.example.com.ar');
  97. $_SERVER['HTTP_HOST'] = 'www.example.com.ar';
  98. $this->assertEquals(env('HTTP_BASE'), '.example.com.ar');
  99. $_SERVER['HTTP_HOST'] = 'subdomain.example.com.ar';
  100. $this->assertEquals(env('HTTP_BASE'), '.example.com.ar');
  101. $_SERVER['HTTP_HOST'] = 'double.subdomain.example.com';
  102. $this->assertEquals(env('HTTP_BASE'), '.subdomain.example.com');
  103. $_SERVER['HTTP_HOST'] = 'double.subdomain.example.com.ar';
  104. $this->assertEquals(env('HTTP_BASE'), '.subdomain.example.com.ar');
  105. $_SERVER = $_ENV = array();
  106. $_SERVER['SCRIPT_NAME'] = '/a/test/test.php';
  107. $this->assertEquals(env('SCRIPT_NAME'), '/a/test/test.php');
  108. $_SERVER = $_ENV = array();
  109. $_ENV['CGI_MODE'] = 'BINARY';
  110. $_ENV['SCRIPT_URL'] = '/a/test/test.php';
  111. $this->assertEquals(env('SCRIPT_NAME'), '/a/test/test.php');
  112. $_SERVER = $_ENV = array();
  113. $this->assertFalse(env('HTTPS'));
  114. $_SERVER['HTTPS'] = 'on';
  115. $this->assertTrue(env('HTTPS'));
  116. $_SERVER['HTTPS'] = '1';
  117. $this->assertTrue(env('HTTPS'));
  118. $_SERVER['HTTPS'] = 'I am not empty';
  119. $this->assertTrue(env('HTTPS'));
  120. $_SERVER['HTTPS'] = 1;
  121. $this->assertTrue(env('HTTPS'));
  122. $_SERVER['HTTPS'] = 'off';
  123. $this->assertFalse(env('HTTPS'));
  124. $_SERVER['HTTPS'] = false;
  125. $this->assertFalse(env('HTTPS'));
  126. $_SERVER['HTTPS'] = '';
  127. $this->assertFalse(env('HTTPS'));
  128. $_SERVER = array();
  129. $_ENV['SCRIPT_URI'] = 'https://domain.test/a/test.php';
  130. $this->assertTrue(env('HTTPS'));
  131. $_ENV['SCRIPT_URI'] = 'http://domain.test/a/test.php';
  132. $this->assertFalse(env('HTTPS'));
  133. $_SERVER = $_ENV = array();
  134. $this->assertNull(env('TEST_ME'));
  135. $_ENV['TEST_ME'] = 'a';
  136. $this->assertEquals(env('TEST_ME'), 'a');
  137. $_SERVER['TEST_ME'] = 'b';
  138. $this->assertEquals(env('TEST_ME'), 'b');
  139. unset($_ENV['TEST_ME']);
  140. $this->assertEquals(env('TEST_ME'), 'b');
  141. $_SERVER = $__SERVER;
  142. $_ENV = $__ENV;
  143. }
  144. /**
  145. * Test h()
  146. *
  147. * @return void
  148. */
  149. public function testH() {
  150. $string = '<foo>';
  151. $result = h($string);
  152. $this->assertEquals('&lt;foo&gt;', $result);
  153. $in = array('this & that', '<p>Which one</p>');
  154. $result = h($in);
  155. $expected = array('this &amp; that', '&lt;p&gt;Which one&lt;/p&gt;');
  156. $this->assertEquals($expected, $result);
  157. $string = '<foo> & &nbsp;';
  158. $result = h($string);
  159. $this->assertEquals('&lt;foo&gt; &amp; &amp;nbsp;', $result);
  160. $string = '<foo> & &nbsp;';
  161. $result = h($string, false);
  162. $this->assertEquals('&lt;foo&gt; &amp; &nbsp;', $result);
  163. $string = '<foo> & &nbsp;';
  164. $result = h($string, 'UTF-8');
  165. $this->assertEquals('&lt;foo&gt; &amp; &amp;nbsp;', $result);
  166. $arr = array('<foo>', '&nbsp;');
  167. $result = h($arr);
  168. $expected = array(
  169. '&lt;foo&gt;',
  170. '&amp;nbsp;'
  171. );
  172. $this->assertEquals($expected, $result);
  173. $arr = array('<foo>', '&nbsp;');
  174. $result = h($arr, false);
  175. $expected = array(
  176. '&lt;foo&gt;',
  177. '&nbsp;'
  178. );
  179. $this->assertEquals($expected, $result);
  180. $arr = array('f' => '<foo>', 'n' => '&nbsp;');
  181. $result = h($arr, false);
  182. $expected = array(
  183. 'f' => '&lt;foo&gt;',
  184. 'n' => '&nbsp;'
  185. );
  186. $this->assertEquals($expected, $result);
  187. $obj = new stdClass();
  188. $result = h($obj);
  189. $this->assertEquals('(object)stdClass', $result);
  190. $obj = new CakeResponse(array('body' => 'Body content'));
  191. $result = h($obj);
  192. $this->assertEquals('Body content', $result);
  193. }
  194. /**
  195. * Test am()
  196. *
  197. * @return void
  198. */
  199. public function testAm() {
  200. $result = am(array('one', 'two'), 2, 3, 4);
  201. $expected = array('one', 'two', 2, 3, 4);
  202. $this->assertEquals($expected, $result);
  203. $result = am(array('one' => array(2, 3), 'two' => array('foo')), array('one' => array(4, 5)));
  204. $expected = array('one' => array(4, 5), 'two' => array('foo'));
  205. $this->assertEquals($expected, $result);
  206. }
  207. /**
  208. * test cache()
  209. *
  210. * @return void
  211. */
  212. public function testCache() {
  213. $_cacheDisable = Configure::read('Cache.disable');
  214. $this->skipIf($_cacheDisable, 'Cache is disabled, skipping cache() tests.');
  215. Configure::write('Cache.disable', true);
  216. $result = cache('basics_test', 'simple cache write');
  217. $this->assertNull($result);
  218. $result = cache('basics_test');
  219. $this->assertNull($result);
  220. Configure::write('Cache.disable', false);
  221. $result = cache('basics_test', 'simple cache write');
  222. $this->assertTrue((boolean)$result);
  223. $this->assertTrue(file_exists(CACHE . 'basics_test'));
  224. $result = cache('basics_test');
  225. $this->assertEquals($result, 'simple cache write');
  226. @unlink(CACHE . 'basics_test');
  227. cache('basics_test', 'expired', '+1 second');
  228. sleep(2);
  229. $result = cache('basics_test', null, '+1 second');
  230. $this->assertNull($result);
  231. Configure::write('Cache.disable', $_cacheDisable);
  232. }
  233. /**
  234. * test clearCache()
  235. *
  236. * @return void
  237. */
  238. public function testClearCache() {
  239. $cacheOff = Configure::read('Cache.disable');
  240. $this->skipIf($cacheOff, 'Cache is disabled, skipping clearCache() tests.');
  241. cache('views' . DS . 'basics_test.cache', 'simple cache write');
  242. $this->assertTrue(file_exists(CACHE . 'views' . DS . 'basics_test.cache'));
  243. cache('views' . DS . 'basics_test_2.cache', 'simple cache write 2');
  244. $this->assertTrue(file_exists(CACHE . 'views' . DS . 'basics_test_2.cache'));
  245. cache('views' . DS . 'basics_test_3.cache', 'simple cache write 3');
  246. $this->assertTrue(file_exists(CACHE . 'views' . DS . 'basics_test_3.cache'));
  247. $result = clearCache(array('basics_test', 'basics_test_2'), 'views', '.cache');
  248. $this->assertTrue($result);
  249. $this->assertFalse(file_exists(CACHE . 'views' . DS . 'basics_test.cache'));
  250. $this->assertFalse(file_exists(CACHE . 'views' . DS . 'basics_test.cache'));
  251. $this->assertTrue(file_exists(CACHE . 'views' . DS . 'basics_test_3.cache'));
  252. $result = clearCache(null, 'views', '.cache');
  253. $this->assertTrue($result);
  254. $this->assertFalse(file_exists(CACHE . 'views' . DS . 'basics_test_3.cache'));
  255. // Different path from views and with prefix
  256. cache('models' . DS . 'basics_test.cache', 'simple cache write');
  257. $this->assertTrue(file_exists(CACHE . 'models' . DS . 'basics_test.cache'));
  258. cache('models' . DS . 'basics_test_2.cache', 'simple cache write 2');
  259. $this->assertTrue(file_exists(CACHE . 'models' . DS . 'basics_test_2.cache'));
  260. cache('models' . DS . 'basics_test_3.cache', 'simple cache write 3');
  261. $this->assertTrue(file_exists(CACHE . 'models' . DS . 'basics_test_3.cache'));
  262. $result = clearCache('basics', 'models', '.cache');
  263. $this->assertTrue($result);
  264. $this->assertFalse(file_exists(CACHE . 'models' . DS . 'basics_test.cache'));
  265. $this->assertFalse(file_exists(CACHE . 'models' . DS . 'basics_test_2.cache'));
  266. $this->assertFalse(file_exists(CACHE . 'models' . DS . 'basics_test_3.cache'));
  267. // checking if empty files were not removed
  268. $emptyExists = file_exists(CACHE . 'views' . DS . 'empty');
  269. if (!$emptyExists) {
  270. cache('views' . DS . 'empty', '');
  271. }
  272. cache('views' . DS . 'basics_test.php', 'simple cache write');
  273. $this->assertTrue(file_exists(CACHE . 'views' . DS . 'basics_test.php'));
  274. $this->assertTrue(file_exists(CACHE . 'views' . DS . 'empty'));
  275. $result = clearCache();
  276. $this->assertTrue($result);
  277. $this->assertTrue(file_exists(CACHE . 'views' . DS . 'empty'));
  278. $this->assertFalse(file_exists(CACHE . 'views' . DS . 'basics_test.php'));
  279. if (!$emptyExists) {
  280. unlink(CACHE . 'views' . DS . 'empty');
  281. }
  282. }
  283. /**
  284. * test __()
  285. *
  286. * @return void
  287. */
  288. public function test__() {
  289. Configure::write('Config.language', 'rule_1_po');
  290. $result = __('Plural Rule 1');
  291. $expected = 'Plural Rule 1 (translated)';
  292. $this->assertEquals($expected, $result);
  293. $result = __('Plural Rule 1 (from core)');
  294. $expected = 'Plural Rule 1 (from core translated)';
  295. $this->assertEquals($expected, $result);
  296. $result = __('Some string with %s', 'arguments');
  297. $expected = 'Some string with arguments';
  298. $this->assertEquals($expected, $result);
  299. $result = __('Some string with %s %s', 'multiple', 'arguments');
  300. $expected = 'Some string with multiple arguments';
  301. $this->assertEquals($expected, $result);
  302. $result = __('Some string with %s %s', array('multiple', 'arguments'));
  303. $expected = 'Some string with multiple arguments';
  304. $this->assertEquals($expected, $result);
  305. $result = __('Testing %2$s %1$s', 'order', 'different');
  306. $expected = 'Testing different order';
  307. $this->assertEquals($expected, $result);
  308. $result = __('Testing %2$s %1$s', array('order', 'different'));
  309. $expected = 'Testing different order';
  310. $this->assertEquals($expected, $result);
  311. $result = __('Testing %.2f number', 1.2345);
  312. $expected = 'Testing 1.23 number';
  313. $this->assertEquals($expected, $result);
  314. }
  315. /**
  316. * test __n()
  317. *
  318. * @return void
  319. */
  320. public function test__n() {
  321. Configure::write('Config.language', 'rule_1_po');
  322. $result = __n('%d = 1', '%d = 0 or > 1', 0);
  323. $expected = '%d = 0 or > 1 (translated)';
  324. $this->assertEquals($expected, $result);
  325. $result = __n('%d = 1', '%d = 0 or > 1', 1);
  326. $expected = '%d = 1 (translated)';
  327. $this->assertEquals($expected, $result);
  328. $result = __n('%d = 1 (from core)', '%d = 0 or > 1 (from core)', 2);
  329. $expected = '%d = 0 or > 1 (from core translated)';
  330. $this->assertEquals($expected, $result);
  331. $result = __n('%d item.', '%d items.', 1, 1);
  332. $expected = '1 item.';
  333. $this->assertEquals($expected, $result);
  334. $result = __n('%d item for id %s', '%d items for id %s', 2, 2, '1234');
  335. $expected = '2 items for id 1234';
  336. $this->assertEquals($expected, $result);
  337. $result = __n('%d item for id %s', '%d items for id %s', 2, array(2, '1234'));
  338. $expected = '2 items for id 1234';
  339. $this->assertEquals($expected, $result);
  340. }
  341. /**
  342. * test __d()
  343. *
  344. * @return void
  345. */
  346. public function test__d() {
  347. Configure::write('Config.language', 'rule_1_po');
  348. $result = __d('default', 'Plural Rule 1');
  349. $expected = 'Plural Rule 1 (translated)';
  350. $this->assertEquals($expected, $result);
  351. $result = __d('core', 'Plural Rule 1');
  352. $expected = 'Plural Rule 1';
  353. $this->assertEquals($expected, $result);
  354. $result = __d('core', 'Plural Rule 1 (from core)');
  355. $expected = 'Plural Rule 1 (from core translated)';
  356. $this->assertEquals($expected, $result);
  357. $result = __d('core', 'Some string with %s', 'arguments');
  358. $expected = 'Some string with arguments';
  359. $this->assertEquals($expected, $result);
  360. $result = __d('core', 'Some string with %s %s', 'multiple', 'arguments');
  361. $expected = 'Some string with multiple arguments';
  362. $this->assertEquals($expected, $result);
  363. $result = __d('core', 'Some string with %s %s', array('multiple', 'arguments'));
  364. $expected = 'Some string with multiple arguments';
  365. $this->assertEquals($expected, $result);
  366. }
  367. /**
  368. * test __dn()
  369. *
  370. * @return void
  371. */
  372. public function test__dn() {
  373. Configure::write('Config.language', 'rule_1_po');
  374. $result = __dn('default', '%d = 1', '%d = 0 or > 1', 0);
  375. $expected = '%d = 0 or > 1 (translated)';
  376. $this->assertEquals($expected, $result);
  377. $result = __dn('core', '%d = 1', '%d = 0 or > 1', 0);
  378. $expected = '%d = 0 or > 1';
  379. $this->assertEquals($expected, $result);
  380. $result = __dn('core', '%d = 1 (from core)', '%d = 0 or > 1 (from core)', 0);
  381. $expected = '%d = 0 or > 1 (from core translated)';
  382. $this->assertEquals($expected, $result);
  383. $result = __dn('default', '%d = 1', '%d = 0 or > 1', 1);
  384. $expected = '%d = 1 (translated)';
  385. $this->assertEquals($expected, $result);
  386. $result = __dn('core', '%d item.', '%d items.', 1, 1);
  387. $expected = '1 item.';
  388. $this->assertEquals($expected, $result);
  389. $result = __dn('core', '%d item for id %s', '%d items for id %s', 2, 2, '1234');
  390. $expected = '2 items for id 1234';
  391. $this->assertEquals($expected, $result);
  392. $result = __dn('core', '%d item for id %s', '%d items for id %s', 2, array(2, '1234'));
  393. $expected = '2 items for id 1234';
  394. $this->assertEquals($expected, $result);
  395. }
  396. /**
  397. * test __c()
  398. *
  399. * @return void
  400. */
  401. public function test__c() {
  402. Configure::write('Config.language', 'rule_1_po');
  403. $result = __c('Plural Rule 1', 6);
  404. $expected = 'Plural Rule 1 (translated)';
  405. $this->assertEquals($expected, $result);
  406. $result = __c('Plural Rule 1 (from core)', 6);
  407. $expected = 'Plural Rule 1 (from core translated)';
  408. $this->assertEquals($expected, $result);
  409. $result = __c('Some string with %s', 6, 'arguments');
  410. $expected = 'Some string with arguments';
  411. $this->assertEquals($expected, $result);
  412. $result = __c('Some string with %s %s', 6, 'multiple', 'arguments');
  413. $expected = 'Some string with multiple arguments';
  414. $this->assertEquals($expected, $result);
  415. $result = __c('Some string with %s %s', 6, array('multiple', 'arguments'));
  416. $expected = 'Some string with multiple arguments';
  417. $this->assertEquals($expected, $result);
  418. }
  419. /**
  420. * test __dc()
  421. *
  422. * @return void
  423. */
  424. public function test__dc() {
  425. Configure::write('Config.language', 'rule_1_po');
  426. $result = __dc('default', 'Plural Rule 1', 6);
  427. $expected = 'Plural Rule 1 (translated)';
  428. $this->assertEquals($expected, $result);
  429. $result = __dc('default', 'Plural Rule 1 (from core)', 6);
  430. $expected = 'Plural Rule 1 (from core translated)';
  431. $this->assertEquals($expected, $result);
  432. $result = __dc('core', 'Plural Rule 1', 6);
  433. $expected = 'Plural Rule 1';
  434. $this->assertEquals($expected, $result);
  435. $result = __dc('core', 'Plural Rule 1 (from core)', 6);
  436. $expected = 'Plural Rule 1 (from core translated)';
  437. $this->assertEquals($expected, $result);
  438. $result = __dc('core', 'Some string with %s', 6, 'arguments');
  439. $expected = 'Some string with arguments';
  440. $this->assertEquals($expected, $result);
  441. $result = __dc('core', 'Some string with %s %s', 6, 'multiple', 'arguments');
  442. $expected = 'Some string with multiple arguments';
  443. $this->assertEquals($expected, $result);
  444. $result = __dc('core', 'Some string with %s %s', 6, array('multiple', 'arguments'));
  445. $expected = 'Some string with multiple arguments';
  446. $this->assertEquals($expected, $result);
  447. }
  448. /**
  449. * test __dcn()
  450. *
  451. * @return void
  452. */
  453. public function test__dcn() {
  454. Configure::write('Config.language', 'rule_1_po');
  455. $result = __dcn('default', '%d = 1', '%d = 0 or > 1', 0, 6);
  456. $expected = '%d = 0 or > 1 (translated)';
  457. $this->assertEquals($expected, $result);
  458. $result = __dcn('default', '%d = 1 (from core)', '%d = 0 or > 1 (from core)', 1, 6);
  459. $expected = '%d = 1 (from core translated)';
  460. $this->assertEquals($expected, $result);
  461. $result = __dcn('core', '%d = 1', '%d = 0 or > 1', 0, 6);
  462. $expected = '%d = 0 or > 1';
  463. $this->assertEquals($expected, $result);
  464. $result = __dcn('core', '%d item.', '%d items.', 1, 6, 1);
  465. $expected = '1 item.';
  466. $this->assertEquals($expected, $result);
  467. $result = __dcn('core', '%d item for id %s', '%d items for id %s', 2, 6, 2, '1234');
  468. $expected = '2 items for id 1234';
  469. $this->assertEquals($expected, $result);
  470. $result = __dcn('core', '%d item for id %s', '%d items for id %s', 2, 6, array(2, '1234'));
  471. $expected = '2 items for id 1234';
  472. $this->assertEquals($expected, $result);
  473. }
  474. /**
  475. * test LogError()
  476. *
  477. * @return void
  478. */
  479. public function testLogError() {
  480. @unlink(LOGS . 'error.log');
  481. LogError('Testing LogError() basic function');
  482. LogError("Testing with\nmulti-line\nstring");
  483. $result = file_get_contents(LOGS . 'error.log');
  484. $this->assertRegExp('/Error: Testing LogError\(\) basic function/', $result);
  485. $this->assertNotRegExp("/Error: Testing with\nmulti-line\nstring/", $result);
  486. $this->assertRegExp('/Error: Testing with multi-line string/', $result);
  487. }
  488. /**
  489. * test fileExistsInPath()
  490. *
  491. * @return void
  492. */
  493. public function testFileExistsInPath() {
  494. if (!function_exists('ini_set')) {
  495. $this->markTestSkipped('%s ini_set function not available');
  496. }
  497. $_includePath = ini_get('include_path');
  498. $path = TMP . 'basics_test';
  499. $folder1 = $path . DS . 'folder1';
  500. $folder2 = $path . DS . 'folder2';
  501. $file1 = $path . DS . 'file1.php';
  502. $file2 = $folder1 . DS . 'file2.php';
  503. $file3 = $folder1 . DS . 'file3.php';
  504. $file4 = $folder2 . DS . 'file4.php';
  505. new Folder($path, true);
  506. new Folder($folder1, true);
  507. new Folder($folder2, true);
  508. touch($file1);
  509. touch($file2);
  510. touch($file3);
  511. touch($file4);
  512. ini_set('include_path', $path . PATH_SEPARATOR . $folder1);
  513. $this->assertEquals(fileExistsInPath('file1.php'), $file1);
  514. $this->assertEquals(fileExistsInPath('file2.php'), $file2);
  515. $this->assertEquals(fileExistsInPath('folder1' . DS . 'file2.php'), $file2);
  516. $this->assertEquals(fileExistsInPath($file2), $file2);
  517. $this->assertEquals(fileExistsInPath('file3.php'), $file3);
  518. $this->assertEquals(fileExistsInPath($file4), $file4);
  519. $this->assertFalse(fileExistsInPath('file1'));
  520. $this->assertFalse(fileExistsInPath('file4.php'));
  521. $Folder = new Folder($path);
  522. $Folder->delete();
  523. ini_set('include_path', $_includePath);
  524. }
  525. /**
  526. * test convertSlash()
  527. *
  528. * @return void
  529. */
  530. public function testConvertSlash() {
  531. $result = convertSlash('\path\to\location\\');
  532. $expected = '\path\to\location\\';
  533. $this->assertEquals($expected, $result);
  534. $result = convertSlash('/path/to/location/');
  535. $expected = 'path_to_location';
  536. $this->assertEquals($expected, $result);
  537. }
  538. /**
  539. * test debug()
  540. *
  541. * @return void
  542. */
  543. public function testDebug() {
  544. ob_start();
  545. debug('this-is-a-test', false);
  546. $result = ob_get_clean();
  547. $expectedText = <<<EXPECTED
  548. %s (line %d)
  549. ########## DEBUG ##########
  550. this-is-a-test
  551. ###########################
  552. EXPECTED;
  553. $expected = sprintf($expectedText, substr(__FILE__, strlen(ROOT) + 1), __LINE__ - 8);
  554. $this->assertEquals($expected, $result);
  555. ob_start();
  556. debug('<div>this-is-a-test</div>', true);
  557. $result = ob_get_clean();
  558. $expectedHtml = <<<EXPECTED
  559. <div class="cake-debug-output">
  560. <span><strong>%s</strong> (line <strong>%d</strong>)</span>
  561. <pre class="cake-debug">
  562. &lt;div&gt;this-is-a-test&lt;/div&gt;
  563. </pre>
  564. </div>
  565. EXPECTED;
  566. $expected = sprintf($expectedHtml, substr(__FILE__, strlen(ROOT) + 1), __LINE__ - 10);
  567. $this->assertEquals($expected, $result);
  568. ob_start();
  569. debug('<div>this-is-a-test</div>', true, true);
  570. $result = ob_get_clean();
  571. $expected = <<<EXPECTED
  572. <div class="cake-debug-output">
  573. <span><strong>%s</strong> (line <strong>%d</strong>)</span>
  574. <pre class="cake-debug">
  575. &lt;div&gt;this-is-a-test&lt;/div&gt;
  576. </pre>
  577. </div>
  578. EXPECTED;
  579. $expected = sprintf($expected, substr(__FILE__, strlen(ROOT) + 1), __LINE__ - 10);
  580. $this->assertEquals($expected, $result);
  581. ob_start();
  582. debug('<div>this-is-a-test</div>', true, false);
  583. $result = ob_get_clean();
  584. $expected = <<<EXPECTED
  585. <div class="cake-debug-output">
  586. <pre class="cake-debug">
  587. &lt;div&gt;this-is-a-test&lt;/div&gt;
  588. </pre>
  589. </div>
  590. EXPECTED;
  591. $expected = sprintf($expected, substr(__FILE__, strlen(ROOT) + 1), __LINE__ - 10);
  592. $this->assertEquals($expected, $result);
  593. ob_start();
  594. debug('<div>this-is-a-test</div>', null);
  595. $result = ob_get_clean();
  596. $expectedHtml = <<<EXPECTED
  597. <div class="cake-debug-output">
  598. <span><strong>%s</strong> (line <strong>%d</strong>)</span>
  599. <pre class="cake-debug">
  600. &lt;div&gt;this-is-a-test&lt;/div&gt;
  601. </pre>
  602. </div>
  603. EXPECTED;
  604. $expectedText = <<<EXPECTED
  605. %s (line %d)
  606. ########## DEBUG ##########
  607. <div>this-is-a-test</div>
  608. ###########################
  609. EXPECTED;
  610. if (php_sapi_name() == 'cli') {
  611. $expected = sprintf($expectedText, substr(__FILE__, strlen(ROOT) + 1), __LINE__ - 17);
  612. } else {
  613. $expected = sprintf($expectedHtml, substr(__FILE__, strlen(ROOT) + 1), __LINE__ - 19);
  614. }
  615. $this->assertEquals($expected, $result);
  616. ob_start();
  617. debug('<div>this-is-a-test</div>', null, false);
  618. $result = ob_get_clean();
  619. $expectedHtml = <<<EXPECTED
  620. <div class="cake-debug-output">
  621. <pre class="cake-debug">
  622. &lt;div&gt;this-is-a-test&lt;/div&gt;
  623. </pre>
  624. </div>
  625. EXPECTED;
  626. $expectedText = <<<EXPECTED
  627. ########## DEBUG ##########
  628. <div>this-is-a-test</div>
  629. ###########################
  630. EXPECTED;
  631. if (php_sapi_name() == 'cli') {
  632. $expected = sprintf($expectedText, substr(__FILE__, strlen(ROOT) + 1), __LINE__ - 17);
  633. } else {
  634. $expected = sprintf($expectedHtml, substr(__FILE__, strlen(ROOT) + 1), __LINE__ - 19);
  635. }
  636. $this->assertEquals($expected, $result);
  637. ob_start();
  638. debug('<div>this-is-a-test</div>', false);
  639. $result = ob_get_clean();
  640. $expected = <<<EXPECTED
  641. %s (line %d)
  642. ########## DEBUG ##########
  643. <div>this-is-a-test</div>
  644. ###########################
  645. EXPECTED;
  646. $expected = sprintf($expected, substr(__FILE__, strlen(ROOT) + 1), __LINE__ - 8);
  647. $this->assertEquals($expected, $result);
  648. ob_start();
  649. debug('<div>this-is-a-test</div>', false, true);
  650. $result = ob_get_clean();
  651. $expected = <<<EXPECTED
  652. %s (line %d)
  653. ########## DEBUG ##########
  654. <div>this-is-a-test</div>
  655. ###########################
  656. EXPECTED;
  657. $expected = sprintf($expected, substr(__FILE__, strlen(ROOT) + 1), __LINE__ - 8);
  658. $this->assertEquals($expected, $result);
  659. ob_start();
  660. debug('<div>this-is-a-test</div>', false, false);
  661. $result = ob_get_clean();
  662. $expected = <<<EXPECTED
  663. ########## DEBUG ##########
  664. <div>this-is-a-test</div>
  665. ###########################
  666. EXPECTED;
  667. $expected = sprintf($expected, substr(__FILE__, strlen(ROOT) + 1), __LINE__ - 8);
  668. $this->assertEquals($expected, $result);
  669. }
  670. /**
  671. * test pr()
  672. *
  673. * @return void
  674. */
  675. public function testPr() {
  676. ob_start();
  677. pr('this is a test');
  678. $result = ob_get_clean();
  679. $expected = "<pre>this is a test</pre>";
  680. $this->assertEquals($expected, $result);
  681. ob_start();
  682. pr(array('this' => 'is', 'a' => 'test'));
  683. $result = ob_get_clean();
  684. $expected = "<pre>Array\n(\n [this] => is\n [a] => test\n)\n</pre>";
  685. $this->assertEquals($expected, $result);
  686. }
  687. /**
  688. * test stripslashes_deep()
  689. *
  690. * @return void
  691. */
  692. public function testStripslashesDeep() {
  693. $this->skipIf(ini_get('magic_quotes_sybase') === '1', 'magic_quotes_sybase is on.');
  694. $this->assertEquals(stripslashes_deep("tes\'t"), "tes't");
  695. $this->assertEquals(stripslashes_deep('tes\\' . chr(0) .'t'), 'tes' . chr(0) .'t');
  696. $this->assertEquals(stripslashes_deep('tes\"t'), 'tes"t');
  697. $this->assertEquals(stripslashes_deep("tes\'t"), "tes't");
  698. $this->assertEquals(stripslashes_deep('te\\st'), 'test');
  699. $nested = array(
  700. 'a' => "tes\'t",
  701. 'b' => 'tes\\' . chr(0) .'t',
  702. 'c' => array(
  703. 'd' => 'tes\"t',
  704. 'e' => "te\'s\'t",
  705. array('f' => "tes\'t")
  706. ),
  707. 'g' => 'te\\st'
  708. );
  709. $expected = array(
  710. 'a' => "tes't",
  711. 'b' => 'tes' . chr(0) .'t',
  712. 'c' => array(
  713. 'd' => 'tes"t',
  714. 'e' => "te's't",
  715. array('f' => "tes't")
  716. ),
  717. 'g' => 'test'
  718. );
  719. $this->assertEquals(stripslashes_deep($nested), $expected);
  720. }
  721. /**
  722. * test stripslashes_deep() with magic_quotes_sybase on
  723. *
  724. * @return void
  725. */
  726. public function testStripslashesDeepSybase() {
  727. if (!(ini_get('magic_quotes_sybase') === '1')) {
  728. $this->markTestSkipped('magic_quotes_sybase is off');
  729. }
  730. $this->assertEquals(stripslashes_deep("tes\'t"), "tes\'t");
  731. $nested = array(
  732. 'a' => "tes't",
  733. 'b' => "tes''t",
  734. 'c' => array(
  735. 'd' => "tes'''t",
  736. 'e' => "tes''''t",
  737. array('f' => "tes''t")
  738. ),
  739. 'g' => "te'''''st"
  740. );
  741. $expected = array(
  742. 'a' => "tes't",
  743. 'b' => "tes't",
  744. 'c' => array(
  745. 'd' => "tes''t",
  746. 'e' => "tes''t",
  747. array('f' => "tes't")
  748. ),
  749. 'g' => "te'''st"
  750. );
  751. $this->assertEquals(stripslashes_deep($nested), $expected);
  752. }
  753. /**
  754. * test pluginSplit
  755. *
  756. * @return void
  757. */
  758. public function testPluginSplit() {
  759. $result = pluginSplit('Something.else');
  760. $this->assertEquals($result, array('Something', 'else'));
  761. $result = pluginSplit('Something.else.more.dots');
  762. $this->assertEquals($result, array('Something', 'else.more.dots'));
  763. $result = pluginSplit('Somethingelse');
  764. $this->assertEquals($result, array(null, 'Somethingelse'));
  765. $result = pluginSplit('Something.else', true);
  766. $this->assertEquals($result, array('Something.', 'else'));
  767. $result = pluginSplit('Something.else.more.dots', true);
  768. $this->assertEquals($result, array('Something.', 'else.more.dots'));
  769. $result = pluginSplit('Post', false, 'Blog');
  770. $this->assertEquals($result, array('Blog', 'Post'));
  771. $result = pluginSplit('Blog.Post', false, 'Ultimate');
  772. $this->assertEquals($result, array('Blog', 'Post'));
  773. }
  774. }