PageRenderTime 47ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/fouzia23chowdhury/cakephpCRUD
PHP | 1197 lines | 802 code | 211 blank | 184 comment | 14 complexity | e1abfc8365d914c86c6d2779ceb48d01 MD5 | raw file
  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. * @package Cake.Test.Case
  15. * @since CakePHP(tm) v 1.2.0.4206
  16. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  17. */
  18. require_once CAKE . 'basics.php';
  19. App::uses('Folder', 'Utility');
  20. App::uses('CakeResponse', 'Network');
  21. App::uses('Debugger', 'Utility');
  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. parent::setUp();
  35. App::build(array(
  36. 'Locale' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Locale' . DS)
  37. ));
  38. }
  39. /**
  40. * test the array_diff_key compatibility function.
  41. *
  42. * @return void
  43. */
  44. public function testArrayDiffKey() {
  45. $one = array('one' => 1, 'two' => 2, 'three' => 3);
  46. $two = array('one' => 'one', 'two' => 'two');
  47. $result = array_diff_key($one, $two);
  48. $expected = array('three' => 3);
  49. $this->assertEquals($expected, $result);
  50. $one = array('one' => array('value', 'value-two'), 'two' => 2, 'three' => 3);
  51. $two = array('two' => 'two');
  52. $result = array_diff_key($one, $two);
  53. $expected = array('one' => array('value', 'value-two'), 'three' => 3);
  54. $this->assertEquals($expected, $result);
  55. $one = array('one' => null, 'two' => 2, 'three' => '', 'four' => 0);
  56. $two = array('two' => 'two');
  57. $result = array_diff_key($one, $two);
  58. $expected = array('one' => null, 'three' => '', 'four' => 0);
  59. $this->assertEquals($expected, $result);
  60. $one = array('minYear' => null, 'maxYear' => null, 'separator' => '-', 'interval' => 1, 'monthNames' => true);
  61. $two = array('minYear' => null, 'maxYear' => null, 'separator' => '-', 'interval' => 1, 'monthNames' => true);
  62. $result = array_diff_key($one, $two);
  63. $this->assertSame(array(), $result);
  64. }
  65. /**
  66. * testHttpBase method
  67. *
  68. * @return void
  69. */
  70. public function testEnv() {
  71. $this->skipIf(!function_exists('ini_get') || ini_get('safe_mode') === '1', 'Safe mode is on.');
  72. $server = $_SERVER;
  73. $env = $_ENV;
  74. $_SERVER['HTTP_HOST'] = 'localhost';
  75. $this->assertEquals(env('HTTP_BASE'), '.localhost');
  76. $_SERVER['HTTP_HOST'] = 'com.ar';
  77. $this->assertEquals(env('HTTP_BASE'), '.com.ar');
  78. $_SERVER['HTTP_HOST'] = 'example.ar';
  79. $this->assertEquals(env('HTTP_BASE'), '.example.ar');
  80. $_SERVER['HTTP_HOST'] = 'example.com';
  81. $this->assertEquals(env('HTTP_BASE'), '.example.com');
  82. $_SERVER['HTTP_HOST'] = 'www.example.com';
  83. $this->assertEquals(env('HTTP_BASE'), '.example.com');
  84. $_SERVER['HTTP_HOST'] = 'subdomain.example.com';
  85. $this->assertEquals(env('HTTP_BASE'), '.example.com');
  86. $_SERVER['HTTP_HOST'] = 'example.com.ar';
  87. $this->assertEquals(env('HTTP_BASE'), '.example.com.ar');
  88. $_SERVER['HTTP_HOST'] = 'www.example.com.ar';
  89. $this->assertEquals(env('HTTP_BASE'), '.example.com.ar');
  90. $_SERVER['HTTP_HOST'] = 'subdomain.example.com.ar';
  91. $this->assertEquals(env('HTTP_BASE'), '.example.com.ar');
  92. $_SERVER['HTTP_HOST'] = 'double.subdomain.example.com';
  93. $this->assertEquals(env('HTTP_BASE'), '.subdomain.example.com');
  94. $_SERVER['HTTP_HOST'] = 'double.subdomain.example.com.ar';
  95. $this->assertEquals(env('HTTP_BASE'), '.subdomain.example.com.ar');
  96. $_SERVER = $_ENV = array();
  97. $_SERVER['SCRIPT_NAME'] = '/a/test/test.php';
  98. $this->assertEquals(env('SCRIPT_NAME'), '/a/test/test.php');
  99. $_SERVER = $_ENV = array();
  100. $_ENV['CGI_MODE'] = 'BINARY';
  101. $_ENV['SCRIPT_URL'] = '/a/test/test.php';
  102. $this->assertEquals(env('SCRIPT_NAME'), '/a/test/test.php');
  103. $_SERVER = $_ENV = array();
  104. $this->assertFalse(env('HTTPS'));
  105. $_SERVER['HTTPS'] = 'on';
  106. $this->assertTrue(env('HTTPS'));
  107. $_SERVER['HTTPS'] = '1';
  108. $this->assertTrue(env('HTTPS'));
  109. $_SERVER['HTTPS'] = 'I am not empty';
  110. $this->assertTrue(env('HTTPS'));
  111. $_SERVER['HTTPS'] = 1;
  112. $this->assertTrue(env('HTTPS'));
  113. $_SERVER['HTTPS'] = 'off';
  114. $this->assertFalse(env('HTTPS'));
  115. $_SERVER['HTTPS'] = false;
  116. $this->assertFalse(env('HTTPS'));
  117. $_SERVER['HTTPS'] = '';
  118. $this->assertFalse(env('HTTPS'));
  119. $_SERVER = array();
  120. $_ENV['SCRIPT_URI'] = 'https://domain.test/a/test.php';
  121. $this->assertTrue(env('HTTPS'));
  122. $_ENV['SCRIPT_URI'] = 'http://domain.test/a/test.php';
  123. $this->assertFalse(env('HTTPS'));
  124. $_SERVER = $_ENV = array();
  125. $this->assertNull(env('TEST_ME'));
  126. $_ENV['TEST_ME'] = 'a';
  127. $this->assertEquals(env('TEST_ME'), 'a');
  128. $_SERVER['TEST_ME'] = 'b';
  129. $this->assertEquals(env('TEST_ME'), 'b');
  130. unset($_ENV['TEST_ME']);
  131. $this->assertEquals(env('TEST_ME'), 'b');
  132. $_SERVER = $server;
  133. $_ENV = $env;
  134. }
  135. /**
  136. * Test h()
  137. *
  138. * @return void
  139. */
  140. public function testH() {
  141. $string = '<foo>';
  142. $result = h($string);
  143. $this->assertEquals('&lt;foo&gt;', $result);
  144. $in = array('this & that', '<p>Which one</p>');
  145. $result = h($in);
  146. $expected = array('this &amp; that', '&lt;p&gt;Which one&lt;/p&gt;');
  147. $this->assertEquals($expected, $result);
  148. $string = '<foo> & &nbsp;';
  149. $result = h($string);
  150. $this->assertEquals('&lt;foo&gt; &amp; &amp;nbsp;', $result);
  151. $string = '<foo> & &nbsp;';
  152. $result = h($string, false);
  153. $this->assertEquals('&lt;foo&gt; &amp; &nbsp;', $result);
  154. $string = '<foo> & &nbsp;';
  155. $result = h($string, 'UTF-8');
  156. $this->assertEquals('&lt;foo&gt; &amp; &amp;nbsp;', $result);
  157. $arr = array('<foo>', '&nbsp;');
  158. $result = h($arr);
  159. $expected = array(
  160. '&lt;foo&gt;',
  161. '&amp;nbsp;'
  162. );
  163. $this->assertEquals($expected, $result);
  164. $arr = array('<foo>', '&nbsp;');
  165. $result = h($arr, false);
  166. $expected = array(
  167. '&lt;foo&gt;',
  168. '&nbsp;'
  169. );
  170. $this->assertEquals($expected, $result);
  171. $arr = array('f' => '<foo>', 'n' => '&nbsp;');
  172. $result = h($arr, false);
  173. $expected = array(
  174. 'f' => '&lt;foo&gt;',
  175. 'n' => '&nbsp;'
  176. );
  177. $this->assertEquals($expected, $result);
  178. // Test that boolean values are not converted to strings
  179. $result = h(false);
  180. $this->assertFalse($result);
  181. $arr = array('foo' => false, 'bar' => true);
  182. $result = h($arr);
  183. $this->assertFalse($result['foo']);
  184. $this->assertTrue($result['bar']);
  185. $obj = new stdClass();
  186. $result = h($obj);
  187. $this->assertEquals('(object)stdClass', $result);
  188. $obj = new CakeResponse(array('body' => 'Body content'));
  189. $result = h($obj);
  190. $this->assertEquals('Body content', $result);
  191. }
  192. /**
  193. * Test am()
  194. *
  195. * @return void
  196. */
  197. public function testAm() {
  198. $result = am(array('one', 'two'), 2, 3, 4);
  199. $expected = array('one', 'two', 2, 3, 4);
  200. $this->assertEquals($expected, $result);
  201. $result = am(array('one' => array(2, 3), 'two' => array('foo')), array('one' => array(4, 5)));
  202. $expected = array('one' => array(4, 5), 'two' => array('foo'));
  203. $this->assertEquals($expected, $result);
  204. }
  205. /**
  206. * test cache()
  207. *
  208. * @return void
  209. */
  210. public function testCache() {
  211. $_cacheDisable = Configure::read('Cache.disable');
  212. $this->skipIf($_cacheDisable, 'Cache is disabled, skipping cache() tests.');
  213. Configure::write('Cache.disable', true);
  214. $result = cache('basics_test', 'simple cache write');
  215. $this->assertNull($result);
  216. $result = cache('basics_test');
  217. $this->assertNull($result);
  218. Configure::write('Cache.disable', false);
  219. $result = cache('basics_test', 'simple cache write');
  220. $this->assertTrue((bool)$result);
  221. $this->assertTrue(file_exists(CACHE . 'basics_test'));
  222. $result = cache('basics_test');
  223. $this->assertEquals('simple cache write', $result);
  224. if (file_exists(CACHE . 'basics_test')) {
  225. unlink(CACHE . 'basics_test');
  226. }
  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 testTranslate() {
  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 and a null argument', null);
  303. $expected = 'Some string with %s and a null argument';
  304. $this->assertEquals($expected, $result);
  305. $result = __('Some string with multiple %s%s, first being null', null, 'arguments');
  306. $expected = 'Some string with multiple arguments, first being null';
  307. $this->assertEquals($expected, $result);
  308. $result = __('Some string with %s %s', array('multiple', 'arguments'));
  309. $expected = 'Some string with multiple arguments';
  310. $this->assertEquals($expected, $result);
  311. $result = __('Testing %2$s %1$s', 'order', 'different');
  312. $expected = 'Testing different order';
  313. $this->assertEquals($expected, $result);
  314. $result = __('Testing %2$s %1$s', array('order', 'different'));
  315. $expected = 'Testing different order';
  316. $this->assertEquals($expected, $result);
  317. $result = __('Testing %.2f number', 1.2345);
  318. $expected = 'Testing 1.23 number';
  319. $this->assertEquals($expected, $result);
  320. }
  321. /**
  322. * testTranslatePercent
  323. *
  324. * @return void
  325. */
  326. public function testTranslatePercent() {
  327. $result = __('%s are 100% real fruit', 'Apples');
  328. $expected = 'Apples are 100% real fruit';
  329. $this->assertEquals($expected, $result, 'Percent sign at end of word should be considered literal');
  330. $result = __('%s are %d% real fruit', 'Apples', 100);
  331. $expected = 'Apples are 100% real fruit';
  332. $this->assertEquals($expected, $result, 'A digit marker should not be misinterpreted');
  333. $result = __('%s are %s% real fruit', 'Apples', 100);
  334. $expected = 'Apples are 100% real fruit';
  335. $this->assertEquals($expected, $result, 'A string marker should not be misinterpreted');
  336. $result = __('%nonsense %s', 'Apples');
  337. $expected = '%nonsense Apples';
  338. $this->assertEquals($expected, $result, 'A percent sign at the start of the string should be considered literal');
  339. $result = __('%s are awesome%', 'Apples');
  340. $expected = 'Apples are awesome%';
  341. $this->assertEquals($expected, $result, 'A percent sign at the end of the string should be considered literal');
  342. $result = __('%2$d %1$s entered the bowl', 'Apples', 2);
  343. $expected = '2 Apples entered the bowl';
  344. $this->assertEquals($expected, $result, 'Positional replacement markers should not be misinterpreted');
  345. $result = __('%.2f% of all %s agree', 99.44444, 'Cats');
  346. $expected = '99.44% of all Cats agree';
  347. $this->assertEquals($expected, $result, 'significant-digit placeholder should not be misinterpreted');
  348. }
  349. /**
  350. * testTranslateWithFormatSpecifiers
  351. *
  352. * @return void
  353. */
  354. public function testTranslateWithFormatSpecifiers() {
  355. $expected = 'Check, one, two, three';
  356. $result = __('Check, %+10s, three', 'one, two');
  357. $this->assertEquals($expected, $result);
  358. $expected = 'Check, +1, two, three';
  359. $result = __('Check, %+5d, two, three', 1);
  360. $this->assertEquals($expected, $result);
  361. $expected = 'Check, @@one, two, three';
  362. $result = __('Check, %\'@+10s, three', 'one, two');
  363. $this->assertEquals($expected, $result);
  364. $expected = 'Check, one, two , three';
  365. $result = __('Check, %-10s, three', 'one, two');
  366. $this->assertEquals($expected, $result);
  367. $expected = 'Check, one, two##, three';
  368. $result = __('Check, %\'#-10s, three', 'one, two');
  369. $this->assertEquals($expected, $result);
  370. $expected = 'Check, one, two, three';
  371. $result = __d('default', 'Check, %+10s, three', 'one, two');
  372. $this->assertEquals($expected, $result);
  373. $expected = 'Check, @@one, two, three';
  374. $result = __d('default', 'Check, %\'@+10s, three', 'one, two');
  375. $this->assertEquals($expected, $result);
  376. $expected = 'Check, one, two , three';
  377. $result = __d('default', 'Check, %-10s, three', 'one, two');
  378. $this->assertEquals($expected, $result);
  379. $expected = 'Check, one, two##, three';
  380. $result = __d('default', 'Check, %\'#-10s, three', 'one, two');
  381. $this->assertEquals($expected, $result);
  382. }
  383. /**
  384. * testTranslateDomainPluralWithFormatSpecifiers
  385. *
  386. * @return void
  387. */
  388. public function testTranslateDomainPluralWithFormatSpecifiers() {
  389. $result = __dn('core', '%+5d item.', '%+5d items.', 1, 1);
  390. $expected = ' +1 item.';
  391. $this->assertEquals($expected, $result);
  392. $result = __dn('core', '%-5d item.', '%-5d items.', 10, 10);
  393. $expected = '10 items.';
  394. $this->assertEquals($expected, $result);
  395. $result = __dn('core', '%\'#+5d item.', '%\'*+5d items.', 1, 1);
  396. $expected = '###+1 item.';
  397. $this->assertEquals($expected, $result);
  398. $result = __dn('core', '%\'#+5d item.', '%\'*+5d items.', 90, 90);
  399. $expected = '**+90 items.';
  400. $this->assertEquals($expected, $result);
  401. $result = __dn('core', '%\'#+5d item.', '%\'*+5d items.', 9000, 9000);
  402. $expected = '+9000 items.';
  403. $this->assertEquals($expected, $result);
  404. }
  405. /**
  406. * test testTranslatePluralWithFormatSpecifiers
  407. *
  408. * @return void
  409. */
  410. public function testTranslatePluralWithFormatSpecifiers() {
  411. Configure::write('Config.language', 'rule_1_po');
  412. $result = __n('%-5d = 1', '%-5d = 0 or > 1', 10);
  413. $expected = '%-5d = 0 or > 1 (translated)';
  414. $this->assertEquals($expected, $result);
  415. }
  416. /**
  417. * test testTranslateDomainCategoryWithFormatSpecifiers
  418. *
  419. * @return void
  420. */
  421. public function testTranslateDomainCategoryWithFormatSpecifiers() {
  422. Configure::write('Config.language', 'rule_1_po');
  423. $result = __dc('default', '%+10s world', 6, 'hello');
  424. $expected = ' hello world';
  425. $this->assertEquals($expected, $result);
  426. $result = __dc('default', '%-10s world', 6, 'hello');
  427. $expected = 'hello world';
  428. $this->assertEquals($expected, $result);
  429. $result = __dc('default', '%\'@-10s world', 6, 'hello');
  430. $expected = 'hello@@@@@ world';
  431. $this->assertEquals($expected, $result);
  432. }
  433. /**
  434. * test testTranslateDomainCategoryPluralWithFormatSpecifiers
  435. *
  436. * @return void
  437. */
  438. public function testTranslateDomainCategoryPluralWithFormatSpecifiers() {
  439. Configure::write('Config.language', 'rule_1_po');
  440. $result = __dcn('default', '%-5d = 1', '%-5d = 0 or > 1', 0, 6);
  441. $expected = '%-5d = 0 or > 1 (translated)';
  442. $this->assertEquals($expected, $result);
  443. $result = __dcn('default', '%-5d = 1', '%-5d = 0 or > 1', 1, 6);
  444. $expected = '%-5d = 1 (translated)';
  445. $this->assertEquals($expected, $result);
  446. }
  447. /**
  448. * test testTranslateCategoryWithFormatSpecifiers
  449. *
  450. * @return void
  451. */
  452. public function testTranslateCategoryWithFormatSpecifiers() {
  453. $result = __c('Some string with %+10s', 6, 'arguments');
  454. $expected = 'Some string with arguments';
  455. $this->assertEquals($expected, $result);
  456. $result = __c('Some string with %-10s: args', 6, 'arguments');
  457. $expected = 'Some string with arguments : args';
  458. $this->assertEquals($expected, $result);
  459. $result = __c('Some string with %\'*-10s: args', 6, 'arguments');
  460. $expected = 'Some string with arguments*: args';
  461. $this->assertEquals($expected, $result);
  462. }
  463. /**
  464. * test __n()
  465. *
  466. * @return void
  467. */
  468. public function testTranslatePlural() {
  469. Configure::write('Config.language', 'rule_1_po');
  470. $result = __n('%d = 1', '%d = 0 or > 1', 0);
  471. $expected = '%d = 0 or > 1 (translated)';
  472. $this->assertEquals($expected, $result);
  473. $result = __n('%d = 1', '%d = 0 or > 1', 1);
  474. $expected = '%d = 1 (translated)';
  475. $this->assertEquals($expected, $result);
  476. $result = __n('%d = 1 (from core)', '%d = 0 or > 1 (from core)', 2);
  477. $expected = '%d = 0 or > 1 (from core translated)';
  478. $this->assertEquals($expected, $result);
  479. $result = __n('%d item.', '%d items.', 1, 1);
  480. $expected = '1 item.';
  481. $this->assertEquals($expected, $result);
  482. $result = __n('%d item for id %s', '%d items for id %s', 2, 2, '1234');
  483. $expected = '2 items for id 1234';
  484. $this->assertEquals($expected, $result);
  485. $result = __n('%d item for id %s', '%d items for id %s', 2, array(2, '1234'));
  486. $expected = '2 items for id 1234';
  487. $this->assertEquals($expected, $result);
  488. }
  489. /**
  490. * test __d()
  491. *
  492. * @return void
  493. */
  494. public function testTranslateDomain() {
  495. Configure::write('Config.language', 'rule_1_po');
  496. $result = __d('default', 'Plural Rule 1');
  497. $expected = 'Plural Rule 1 (translated)';
  498. $this->assertEquals($expected, $result);
  499. $result = __d('core', 'Plural Rule 1');
  500. $expected = 'Plural Rule 1';
  501. $this->assertEquals($expected, $result);
  502. $result = __d('core', 'Plural Rule 1 (from core)');
  503. $expected = 'Plural Rule 1 (from core translated)';
  504. $this->assertEquals($expected, $result);
  505. $result = __d('core', 'Some string with %s', 'arguments');
  506. $expected = 'Some string with arguments';
  507. $this->assertEquals($expected, $result);
  508. $result = __d('core', 'Some string with %s %s', 'multiple', 'arguments');
  509. $expected = 'Some string with multiple arguments';
  510. $this->assertEquals($expected, $result);
  511. $result = __d('core', 'Some string with %s %s', array('multiple', 'arguments'));
  512. $expected = 'Some string with multiple arguments';
  513. $this->assertEquals($expected, $result);
  514. }
  515. /**
  516. * test __dn()
  517. *
  518. * @return void
  519. */
  520. public function testTranslateDomainPlural() {
  521. Configure::write('Config.language', 'rule_1_po');
  522. $result = __dn('default', '%d = 1', '%d = 0 or > 1', 0);
  523. $expected = '%d = 0 or > 1 (translated)';
  524. $this->assertEquals($expected, $result);
  525. $result = __dn('core', '%d = 1', '%d = 0 or > 1', 0);
  526. $expected = '%d = 0 or > 1';
  527. $this->assertEquals($expected, $result);
  528. $result = __dn('core', '%d = 1 (from core)', '%d = 0 or > 1 (from core)', 0);
  529. $expected = '%d = 0 or > 1 (from core translated)';
  530. $this->assertEquals($expected, $result);
  531. $result = __dn('default', '%d = 1', '%d = 0 or > 1', 1);
  532. $expected = '%d = 1 (translated)';
  533. $this->assertEquals($expected, $result);
  534. $result = __dn('core', '%d item.', '%d items.', 1, 1);
  535. $expected = '1 item.';
  536. $this->assertEquals($expected, $result);
  537. $result = __dn('core', '%d item for id %s', '%d items for id %s', 2, 2, '1234');
  538. $expected = '2 items for id 1234';
  539. $this->assertEquals($expected, $result);
  540. $result = __dn('core', '%d item for id %s', '%d items for id %s', 2, array(2, '1234'));
  541. $expected = '2 items for id 1234';
  542. $this->assertEquals($expected, $result);
  543. }
  544. /**
  545. * test __c()
  546. *
  547. * @return void
  548. */
  549. public function testTranslateCategory() {
  550. Configure::write('Config.language', 'rule_1_po');
  551. $result = __c('Plural Rule 1', 6);
  552. $expected = 'Plural Rule 1 (translated)';
  553. $this->assertEquals($expected, $result);
  554. $result = __c('Plural Rule 1 (from core)', 6);
  555. $expected = 'Plural Rule 1 (from core translated)';
  556. $this->assertEquals($expected, $result);
  557. $result = __c('Some string with %s', 6, 'arguments');
  558. $expected = 'Some string with arguments';
  559. $this->assertEquals($expected, $result);
  560. $result = __c('Some string with %s %s', 6, 'multiple', 'arguments');
  561. $expected = 'Some string with multiple arguments';
  562. $this->assertEquals($expected, $result);
  563. $result = __c('Some string with %s %s', 6, array('multiple', 'arguments'));
  564. $expected = 'Some string with multiple arguments';
  565. $this->assertEquals($expected, $result);
  566. }
  567. /**
  568. * test __dc()
  569. *
  570. * @return void
  571. */
  572. public function testTranslateDomainCategory() {
  573. Configure::write('Config.language', 'rule_1_po');
  574. $result = __dc('default', 'Plural Rule 1', 6);
  575. $expected = 'Plural Rule 1 (translated)';
  576. $this->assertEquals($expected, $result);
  577. $result = __dc('default', 'Plural Rule 1 (from core)', 6);
  578. $expected = 'Plural Rule 1 (from core translated)';
  579. $this->assertEquals($expected, $result);
  580. $result = __dc('core', 'Plural Rule 1', 6);
  581. $expected = 'Plural Rule 1';
  582. $this->assertEquals($expected, $result);
  583. $result = __dc('core', 'Plural Rule 1 (from core)', 6);
  584. $expected = 'Plural Rule 1 (from core translated)';
  585. $this->assertEquals($expected, $result);
  586. $result = __dc('core', 'Some string with %s', 6, 'arguments');
  587. $expected = 'Some string with arguments';
  588. $this->assertEquals($expected, $result);
  589. $result = __dc('core', 'Some string with %s %s', 6, 'multiple', 'arguments');
  590. $expected = 'Some string with multiple arguments';
  591. $this->assertEquals($expected, $result);
  592. $result = __dc('core', 'Some string with %s %s', 6, array('multiple', 'arguments'));
  593. $expected = 'Some string with multiple arguments';
  594. $this->assertEquals($expected, $result);
  595. }
  596. /**
  597. * test __dcn()
  598. *
  599. * @return void
  600. */
  601. public function testTranslateDomainCategoryPlural() {
  602. Configure::write('Config.language', 'rule_1_po');
  603. $result = __dcn('default', '%d = 1', '%d = 0 or > 1', 0, 6);
  604. $expected = '%d = 0 or > 1 (translated)';
  605. $this->assertEquals($expected, $result);
  606. $result = __dcn('default', '%d = 1 (from core)', '%d = 0 or > 1 (from core)', 1, 6);
  607. $expected = '%d = 1 (from core translated)';
  608. $this->assertEquals($expected, $result);
  609. $result = __dcn('core', '%d = 1', '%d = 0 or > 1', 0, 6);
  610. $expected = '%d = 0 or > 1';
  611. $this->assertEquals($expected, $result);
  612. $result = __dcn('core', '%d item.', '%d items.', 1, 6, 1);
  613. $expected = '1 item.';
  614. $this->assertEquals($expected, $result);
  615. $result = __dcn('core', '%d item for id %s', '%d items for id %s', 2, 6, 2, '1234');
  616. $expected = '2 items for id 1234';
  617. $this->assertEquals($expected, $result);
  618. $result = __dcn('core', '%d item for id %s', '%d items for id %s', 2, 6, array(2, '1234'));
  619. $expected = '2 items for id 1234';
  620. $this->assertEquals($expected, $result);
  621. }
  622. /**
  623. * test LogError()
  624. *
  625. * @return void
  626. */
  627. public function testLogError() {
  628. if (file_exists(LOGS . 'error.log')) {
  629. unlink(LOGS . 'error.log');
  630. }
  631. // disable stderr output for this test
  632. if (CakeLog::stream('stderr')) {
  633. CakeLog::disable('stderr');
  634. }
  635. LogError('Testing LogError() basic function');
  636. LogError("Testing with\nmulti-line\nstring");
  637. if (CakeLog::stream('stderr')) {
  638. CakeLog::enable('stderr');
  639. }
  640. $result = file_get_contents(LOGS . 'error.log');
  641. $this->assertRegExp('/Error: Testing LogError\(\) basic function/', $result);
  642. $this->assertNotRegExp("/Error: Testing with\nmulti-line\nstring/", $result);
  643. $this->assertRegExp('/Error: Testing with multi-line string/', $result);
  644. }
  645. /**
  646. * test fileExistsInPath()
  647. *
  648. * @return void
  649. */
  650. public function testFileExistsInPath() {
  651. if (!function_exists('ini_set')) {
  652. $this->markTestSkipped('%s ini_set function not available');
  653. }
  654. $_includePath = ini_get('include_path');
  655. $path = TMP . 'basics_test';
  656. $folder1 = $path . DS . 'folder1';
  657. $folder2 = $path . DS . 'folder2';
  658. $file1 = $path . DS . 'file1.php';
  659. $file2 = $folder1 . DS . 'file2.php';
  660. $file3 = $folder1 . DS . 'file3.php';
  661. $file4 = $folder2 . DS . 'file4.php';
  662. new Folder($path, true);
  663. new Folder($folder1, true);
  664. new Folder($folder2, true);
  665. touch($file1);
  666. touch($file2);
  667. touch($file3);
  668. touch($file4);
  669. ini_set('include_path', $path . PATH_SEPARATOR . $folder1);
  670. $this->assertEquals(fileExistsInPath('file1.php'), $file1);
  671. $this->assertEquals(fileExistsInPath('file2.php'), $file2);
  672. $this->assertEquals(fileExistsInPath('folder1' . DS . 'file2.php'), $file2);
  673. $this->assertEquals(fileExistsInPath($file2), $file2);
  674. $this->assertEquals(fileExistsInPath('file3.php'), $file3);
  675. $this->assertEquals(fileExistsInPath($file4), $file4);
  676. $this->assertFalse(fileExistsInPath('file1'));
  677. $this->assertFalse(fileExistsInPath('file4.php'));
  678. $Folder = new Folder($path);
  679. $Folder->delete();
  680. ini_set('include_path', $_includePath);
  681. }
  682. /**
  683. * test convertSlash()
  684. *
  685. * @return void
  686. */
  687. public function testConvertSlash() {
  688. $result = convertSlash('\path\to\location\\');
  689. $expected = '\path\to\location\\';
  690. $this->assertEquals($expected, $result);
  691. $result = convertSlash('/path/to/location/');
  692. $expected = 'path_to_location';
  693. $this->assertEquals($expected, $result);
  694. }
  695. /**
  696. * test debug()
  697. *
  698. * @return void
  699. */
  700. public function testDebug() {
  701. ob_start();
  702. debug('this-is-a-test', false);
  703. $result = ob_get_clean();
  704. $expectedText = <<<EXPECTED
  705. %s (line %d)
  706. ########## DEBUG ##########
  707. 'this-is-a-test'
  708. ###########################
  709. EXPECTED;
  710. $expected = sprintf($expectedText, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 9);
  711. $this->assertEquals($expected, $result);
  712. ob_start();
  713. debug('<div>this-is-a-test</div>', true);
  714. $result = ob_get_clean();
  715. $expectedHtml = <<<EXPECTED
  716. <div class="cake-debug-output">
  717. <span><strong>%s</strong> (line <strong>%d</strong>)</span>
  718. <pre class="cake-debug">
  719. &#039;&lt;div&gt;this-is-a-test&lt;/div&gt;&#039;
  720. </pre>
  721. </div>
  722. EXPECTED;
  723. $expected = sprintf($expectedHtml, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 10);
  724. $this->assertEquals($expected, $result);
  725. ob_start();
  726. debug('<div>this-is-a-test</div>', true, true);
  727. $result = ob_get_clean();
  728. $expected = <<<EXPECTED
  729. <div class="cake-debug-output">
  730. <span><strong>%s</strong> (line <strong>%d</strong>)</span>
  731. <pre class="cake-debug">
  732. &#039;&lt;div&gt;this-is-a-test&lt;/div&gt;&#039;
  733. </pre>
  734. </div>
  735. EXPECTED;
  736. $expected = sprintf($expected, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 10);
  737. $this->assertEquals($expected, $result);
  738. ob_start();
  739. debug('<div>this-is-a-test</div>', true, false);
  740. $result = ob_get_clean();
  741. $expected = <<<EXPECTED
  742. <div class="cake-debug-output">
  743. <pre class="cake-debug">
  744. &#039;&lt;div&gt;this-is-a-test&lt;/div&gt;&#039;
  745. </pre>
  746. </div>
  747. EXPECTED;
  748. $expected = sprintf($expected, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 10);
  749. $this->assertEquals($expected, $result);
  750. ob_start();
  751. debug('<div>this-is-a-test</div>', null);
  752. $result = ob_get_clean();
  753. $expectedHtml = <<<EXPECTED
  754. <div class="cake-debug-output">
  755. <span><strong>%s</strong> (line <strong>%d</strong>)</span>
  756. <pre class="cake-debug">
  757. &#039;&lt;div&gt;this-is-a-test&lt;/div&gt;&#039;
  758. </pre>
  759. </div>
  760. EXPECTED;
  761. $expectedText = <<<EXPECTED
  762. %s (line %d)
  763. ########## DEBUG ##########
  764. '<div>this-is-a-test</div>'
  765. ###########################
  766. EXPECTED;
  767. if (php_sapi_name() === 'cli') {
  768. $expected = sprintf($expectedText, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 18);
  769. } else {
  770. $expected = sprintf($expectedHtml, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 20);
  771. }
  772. $this->assertEquals($expected, $result);
  773. ob_start();
  774. debug('<div>this-is-a-test</div>', null, false);
  775. $result = ob_get_clean();
  776. $expectedHtml = <<<EXPECTED
  777. <div class="cake-debug-output">
  778. <pre class="cake-debug">
  779. &#039;&lt;div&gt;this-is-a-test&lt;/div&gt;&#039;
  780. </pre>
  781. </div>
  782. EXPECTED;
  783. $expectedText = <<<EXPECTED
  784. ########## DEBUG ##########
  785. '<div>this-is-a-test</div>'
  786. ###########################
  787. EXPECTED;
  788. if (php_sapi_name() === 'cli') {
  789. $expected = sprintf($expectedText, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 18);
  790. } else {
  791. $expected = sprintf($expectedHtml, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 19);
  792. }
  793. $this->assertEquals($expected, $result);
  794. ob_start();
  795. debug('<div>this-is-a-test</div>', false);
  796. $result = ob_get_clean();
  797. $expected = <<<EXPECTED
  798. %s (line %d)
  799. ########## DEBUG ##########
  800. '<div>this-is-a-test</div>'
  801. ###########################
  802. EXPECTED;
  803. $expected = sprintf($expected, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 9);
  804. $this->assertEquals($expected, $result);
  805. ob_start();
  806. debug('<div>this-is-a-test</div>', false, true);
  807. $result = ob_get_clean();
  808. $expected = <<<EXPECTED
  809. %s (line %d)
  810. ########## DEBUG ##########
  811. '<div>this-is-a-test</div>'
  812. ###########################
  813. EXPECTED;
  814. $expected = sprintf($expected, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 9);
  815. $this->assertEquals($expected, $result);
  816. ob_start();
  817. debug('<div>this-is-a-test</div>', false, false);
  818. $result = ob_get_clean();
  819. $expected = <<<EXPECTED
  820. ########## DEBUG ##########
  821. '<div>this-is-a-test</div>'
  822. ###########################
  823. EXPECTED;
  824. $expected = sprintf($expected, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 9);
  825. $this->assertEquals($expected, $result);
  826. ob_start();
  827. debug(false, false, false);
  828. $result = ob_get_clean();
  829. $expected = <<<EXPECTED
  830. ########## DEBUG ##########
  831. false
  832. ###########################
  833. EXPECTED;
  834. $expected = sprintf($expected, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 9);
  835. $this->assertEquals($expected, $result);
  836. }
  837. /**
  838. * test pr()
  839. *
  840. * @return void
  841. */
  842. public function testPr() {
  843. $this->skipIf(php_sapi_name() === 'cli', 'Skipping web test in cli mode');
  844. ob_start();
  845. pr('this is a test');
  846. $result = ob_get_clean();
  847. $expected = "<pre>this is a test</pre>";
  848. $this->assertEquals($expected, $result);
  849. ob_start();
  850. pr(array('this' => 'is', 'a' => 'test'));
  851. $result = ob_get_clean();
  852. $expected = "<pre>Array\n(\n [this] => is\n [a] => test\n)\n</pre>";
  853. $this->assertEquals($expected, $result);
  854. }
  855. /**
  856. * test pr()
  857. *
  858. * @return void
  859. */
  860. public function testPrCli() {
  861. $this->skipIf(php_sapi_name() != 'cli', 'Skipping cli test in web mode');
  862. ob_start();
  863. pr('this is a test');
  864. $result = ob_get_clean();
  865. $expected = "\nthis is a test\n";
  866. $this->assertEquals($expected, $result);
  867. ob_start();
  868. pr(array('this' => 'is', 'a' => 'test'));
  869. $result = ob_get_clean();
  870. $expected = "\nArray\n(\n [this] => is\n [a] => test\n)\n\n";
  871. $this->assertEquals($expected, $result);
  872. }
  873. /**
  874. * test stripslashes_deep()
  875. *
  876. * @return void
  877. */
  878. public function testStripslashesDeep() {
  879. $this->skipIf(ini_get('magic_quotes_sybase') === '1', 'magic_quotes_sybase is on.');
  880. $this->assertEquals(stripslashes_deep("tes\'t"), "tes't");
  881. $this->assertEquals(stripslashes_deep('tes\\' . chr(0) . 't'), 'tes' . chr(0) . 't');
  882. $this->assertEquals(stripslashes_deep('tes\"t'), 'tes"t');
  883. $this->assertEquals(stripslashes_deep("tes\'t"), "tes't");
  884. $this->assertEquals(stripslashes_deep('te\\st'), 'test');
  885. $nested = array(
  886. 'a' => "tes\'t",
  887. 'b' => 'tes\\' . chr(0) . 't',
  888. 'c' => array(
  889. 'd' => 'tes\"t',
  890. 'e' => "te\'s\'t",
  891. array('f' => "tes\'t")
  892. ),
  893. 'g' => 'te\\st'
  894. );
  895. $expected = array(
  896. 'a' => "tes't",
  897. 'b' => 'tes' . chr(0) . 't',
  898. 'c' => array(
  899. 'd' => 'tes"t',
  900. 'e' => "te's't",
  901. array('f' => "tes't")
  902. ),
  903. 'g' => 'test'
  904. );
  905. $this->assertEquals($expected, stripslashes_deep($nested));
  906. }
  907. /**
  908. * test stripslashes_deep() with magic_quotes_sybase on
  909. *
  910. * @return void
  911. */
  912. public function testStripslashesDeepSybase() {
  913. if (!(ini_get('magic_quotes_sybase') === '1')) {
  914. $this->markTestSkipped('magic_quotes_sybase is off');
  915. }
  916. $this->assertEquals(stripslashes_deep("tes\'t"), "tes\'t");
  917. $nested = array(
  918. 'a' => "tes't",
  919. 'b' => "tes''t",
  920. 'c' => array(
  921. 'd' => "tes'''t",
  922. 'e' => "tes''''t",
  923. array('f' => "tes''t")
  924. ),
  925. 'g' => "te'''''st"
  926. );
  927. $expected = array(
  928. 'a' => "tes't",
  929. 'b' => "tes't",
  930. 'c' => array(
  931. 'd' => "tes''t",
  932. 'e' => "tes''t",
  933. array('f' => "tes't")
  934. ),
  935. 'g' => "te'''st"
  936. );
  937. $this->assertEquals($expected, stripslashes_deep($nested));
  938. }
  939. /**
  940. * Tests that the stackTrace() method is a shortcut for Debugger::trace()
  941. *
  942. * @return void
  943. */
  944. public function testStackTrace() {
  945. ob_start();
  946. list(, $expected) = array(stackTrace(), Debugger::trace());
  947. $result = ob_get_clean();
  948. $this->assertEquals($expected, $result);
  949. $opts = array('args' => true);
  950. ob_start();
  951. list(, $expected) = array(stackTrace($opts), Debugger::trace($opts));
  952. $result = ob_get_clean();
  953. $this->assertEquals($expected, $result);
  954. }
  955. /**
  956. * test pluginSplit
  957. *
  958. * @return void
  959. */
  960. public function testPluginSplit() {
  961. $result = pluginSplit('Something.else');
  962. $this->assertEquals(array('Something', 'else'), $result);
  963. $result = pluginSplit('Something.else.more.dots');
  964. $this->assertEquals(array('Something', 'else.more.dots'), $result);
  965. $result = pluginSplit('Somethingelse');
  966. $this->assertEquals(array(null, 'Somethingelse'), $result);
  967. $result = pluginSplit('Something.else', true);
  968. $this->assertEquals(array('Something.', 'else'), $result);
  969. $result = pluginSplit('Something.else.more.dots', true);
  970. $this->assertEquals(array('Something.', 'else.more.dots'), $result);
  971. $result = pluginSplit('Post', false, 'Blog');
  972. $this->assertEquals(array('Blog', 'Post'), $result);
  973. $result = pluginSplit('Blog.Post', false, 'Ultimate');
  974. $this->assertEquals(array('Blog', 'Post'), $result);
  975. }
  976. }