BasicsTest.php 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084
  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 am()
  192. *
  193. * @return void
  194. */
  195. public function testAm() {
  196. $result = am(array('one', 'two'), 2, 3, 4);
  197. $expected = array('one', 'two', 2, 3, 4);
  198. $this->assertEquals($expected, $result);
  199. $result = am(array('one' => array(2, 3), 'two' => array('foo')), array('one' => array(4, 5)));
  200. $expected = array('one' => array(4, 5), 'two' => array('foo'));
  201. $this->assertEquals($expected, $result);
  202. }
  203. /**
  204. * test cache()
  205. *
  206. * @return void
  207. */
  208. public function testCache() {
  209. Cache::disable();
  210. $result = cache('basics_test', 'simple cache write');
  211. $this->assertNull($result);
  212. $result = cache('basics_test');
  213. $this->assertNull($result);
  214. Cache::enable();
  215. $result = cache('basics_test', 'simple cache write');
  216. $this->assertTrue((bool)$result);
  217. $this->assertTrue(file_exists(CACHE . 'basics_test'));
  218. $result = cache('basics_test');
  219. $this->assertEquals('simple cache write', $result);
  220. if (file_exists(CACHE . 'basics_test')) {
  221. unlink(CACHE . 'basics_test');
  222. }
  223. cache('basics_test', 'expired', '+1 second');
  224. sleep(2);
  225. $result = cache('basics_test', null, '+1 second');
  226. $this->assertNull($result);
  227. }
  228. /**
  229. * test clearCache()
  230. *
  231. * @return void
  232. */
  233. public function testClearCache() {
  234. $cacheOff = Configure::read('Cache.disable');
  235. $this->skipIf($cacheOff, 'Cache is disabled, skipping clearCache() tests.');
  236. cache('views/basics_test.cache', 'simple cache write');
  237. $this->assertTrue(file_exists(CACHE . 'views/basics_test.cache'));
  238. cache('views/basics_test_2.cache', 'simple cache write 2');
  239. $this->assertTrue(file_exists(CACHE . 'views/basics_test_2.cache'));
  240. cache('views/basics_test_3.cache', 'simple cache write 3');
  241. $this->assertTrue(file_exists(CACHE . 'views/basics_test_3.cache'));
  242. $result = clearCache(array('basics_test', 'basics_test_2'), 'views', '.cache');
  243. $this->assertTrue($result);
  244. $this->assertFalse(file_exists(CACHE . 'views/basics_test.cache'));
  245. $this->assertFalse(file_exists(CACHE . 'views/basics_test.cache'));
  246. $this->assertTrue(file_exists(CACHE . 'views/basics_test_3.cache'));
  247. $result = clearCache(null, 'views', '.cache');
  248. $this->assertTrue($result);
  249. $this->assertFalse(file_exists(CACHE . 'views/basics_test_3.cache'));
  250. // Different path from views and with prefix
  251. cache('models/basics_test.cache', 'simple cache write');
  252. $this->assertTrue(file_exists(CACHE . 'models/basics_test.cache'));
  253. cache('models/basics_test_2.cache', 'simple cache write 2');
  254. $this->assertTrue(file_exists(CACHE . 'models/basics_test_2.cache'));
  255. cache('models/basics_test_3.cache', 'simple cache write 3');
  256. $this->assertTrue(file_exists(CACHE . 'models/basics_test_3.cache'));
  257. $result = clearCache('basics', 'models', '.cache');
  258. $this->assertTrue($result);
  259. $this->assertFalse(file_exists(CACHE . 'models/basics_test.cache'));
  260. $this->assertFalse(file_exists(CACHE . 'models/basics_test_2.cache'));
  261. $this->assertFalse(file_exists(CACHE . 'models/basics_test_3.cache'));
  262. // checking if empty files were not removed
  263. $emptyExists = file_exists(CACHE . 'views/empty');
  264. if (!$emptyExists) {
  265. cache('views/empty', '');
  266. }
  267. cache('views/basics_test.php', 'simple cache write');
  268. $this->assertTrue(file_exists(CACHE . 'views/basics_test.php'));
  269. $this->assertTrue(file_exists(CACHE . 'views/empty'));
  270. $result = clearCache();
  271. $this->assertTrue($result);
  272. $this->assertTrue(file_exists(CACHE . 'views/empty'));
  273. $this->assertFalse(file_exists(CACHE . 'views/basics_test.php'));
  274. if (!$emptyExists) {
  275. unlink(CACHE . 'views/empty');
  276. }
  277. }
  278. /**
  279. * test __()
  280. *
  281. * @return void
  282. */
  283. public function testTranslate() {
  284. Configure::write('Config.language', 'rule_1_po');
  285. $result = __('Plural Rule 1');
  286. $expected = 'Plural Rule 1 (translated)';
  287. $this->assertEquals($expected, $result);
  288. $result = __('Plural Rule 1 (from core)');
  289. $expected = 'Plural Rule 1 (from core translated)';
  290. $this->assertEquals($expected, $result);
  291. $result = __('Some string with %s', 'arguments');
  292. $expected = 'Some string with arguments';
  293. $this->assertEquals($expected, $result);
  294. $result = __('Some string with %s %s', 'multiple', 'arguments');
  295. $expected = 'Some string with multiple arguments';
  296. $this->assertEquals($expected, $result);
  297. $result = __('Some string with %s %s', array('multiple', 'arguments'));
  298. $expected = 'Some string with multiple arguments';
  299. $this->assertEquals($expected, $result);
  300. $result = __('Testing %2$s %1$s', 'order', 'different');
  301. $expected = 'Testing different order';
  302. $this->assertEquals($expected, $result);
  303. $result = __('Testing %2$s %1$s', array('order', 'different'));
  304. $expected = 'Testing different order';
  305. $this->assertEquals($expected, $result);
  306. $result = __('Testing %.2f number', 1.2345);
  307. $expected = 'Testing 1.23 number';
  308. $this->assertEquals($expected, $result);
  309. }
  310. /**
  311. * testTranslatePercent
  312. *
  313. * @return void
  314. */
  315. public function testTranslatePercent() {
  316. $result = __('%s are 100% real fruit', 'Apples');
  317. $expected = 'Apples are 100% real fruit';
  318. $this->assertEquals($expected, $result, 'Percent sign at end of word should be considered literal');
  319. $result = __('%s are %d% real fruit', 'Apples', 100);
  320. $expected = 'Apples are 100% real fruit';
  321. $this->assertEquals($expected, $result, 'A digit marker should not be misinterpreted');
  322. $result = __('%s are %s% real fruit', 'Apples', 100);
  323. $expected = 'Apples are 100% real fruit';
  324. $this->assertEquals($expected, $result, 'A string marker should not be misinterpreted');
  325. $result = __('%nonsense %s', 'Apples');
  326. $expected = '%nonsense Apples';
  327. $this->assertEquals($expected, $result, 'A percent sign at the start of the string should be considered literal');
  328. $result = __('%s are awesome%', 'Apples');
  329. $expected = 'Apples are awesome%';
  330. $this->assertEquals($expected, $result, 'A percent sign at the end of the string should be considered literal');
  331. $result = __('%2$d %1$s entered the bowl', 'Apples', 2);
  332. $expected = '2 Apples entered the bowl';
  333. $this->assertEquals($expected, $result, 'Positional replacement markers should not be misinterpreted');
  334. $result = __('%.2f% of all %s agree', 99.44444, 'Cats');
  335. $expected = '99.44% of all Cats agree';
  336. $this->assertEquals($expected, $result, 'significant-digit placeholder should not be misinterpreted');
  337. }
  338. /**
  339. * testTranslateWithFormatSpecifiers
  340. *
  341. * @return void
  342. */
  343. public function testTranslateWithFormatSpecifiers() {
  344. $expected = 'Check, one, two, three';
  345. $result = __('Check, %+10s, three', 'one, two');
  346. $this->assertEquals($expected, $result);
  347. $expected = 'Check, +1, two, three';
  348. $result = __('Check, %+5d, two, three', 1);
  349. $this->assertEquals($expected, $result);
  350. $expected = 'Check, @@one, two, three';
  351. $result = __('Check, %\'@+10s, three', 'one, two');
  352. $this->assertEquals($expected, $result);
  353. $expected = 'Check, one, two , three';
  354. $result = __('Check, %-10s, three', 'one, two');
  355. $this->assertEquals($expected, $result);
  356. $expected = 'Check, one, two##, three';
  357. $result = __('Check, %\'#-10s, three', 'one, two');
  358. $this->assertEquals($expected, $result);
  359. $expected = 'Check, one, two, three';
  360. $result = __d('default', 'Check, %+10s, three', 'one, two');
  361. $this->assertEquals($expected, $result);
  362. $expected = 'Check, @@one, two, three';
  363. $result = __d('default', 'Check, %\'@+10s, three', 'one, two');
  364. $this->assertEquals($expected, $result);
  365. $expected = 'Check, one, two , three';
  366. $result = __d('default', 'Check, %-10s, three', 'one, two');
  367. $this->assertEquals($expected, $result);
  368. $expected = 'Check, one, two##, three';
  369. $result = __d('default', 'Check, %\'#-10s, three', 'one, two');
  370. $this->assertEquals($expected, $result);
  371. }
  372. /**
  373. * testTranslateDomainPluralWithFormatSpecifiers
  374. *
  375. * @return void
  376. */
  377. public function testTranslateDomainPluralWithFormatSpecifiers() {
  378. $result = __dn('core', '%+5d item.', '%+5d items.', 1, 1);
  379. $expected = ' +1 item.';
  380. $this->assertEquals($expected, $result);
  381. $result = __dn('core', '%-5d item.', '%-5d items.', 10, 10);
  382. $expected = '10 items.';
  383. $this->assertEquals($expected, $result);
  384. $result = __dn('core', '%\'#+5d item.', '%\'*+5d items.', 1, 1);
  385. $expected = '###+1 item.';
  386. $this->assertEquals($expected, $result);
  387. $result = __dn('core', '%\'#+5d item.', '%\'*+5d items.', 90, 90);
  388. $expected = '**+90 items.';
  389. $this->assertEquals($expected, $result);
  390. $result = __dn('core', '%\'#+5d item.', '%\'*+5d items.', 9000, 9000);
  391. $expected = '+9000 items.';
  392. $this->assertEquals($expected, $result);
  393. }
  394. /**
  395. * test testTranslatePluralWithFormatSpecifiers
  396. *
  397. * @return void
  398. */
  399. public function testTranslatePluralWithFormatSpecifiers() {
  400. Configure::write('Config.language', 'rule_1_po');
  401. $result = __n('%-5d = 1', '%-5d = 0 or > 1', 10);
  402. $expected = '%-5d = 0 or > 1 (translated)';
  403. $this->assertEquals($expected, $result);
  404. }
  405. /**
  406. * test testTranslateDomainCategoryWithFormatSpecifiers
  407. *
  408. * @return void
  409. */
  410. public function testTranslateDomainCategoryWithFormatSpecifiers() {
  411. Configure::write('Config.language', 'rule_1_po');
  412. $result = __dc('default', '%+10s world', 6, 'hello');
  413. $expected = ' hello world';
  414. $this->assertEquals($expected, $result);
  415. $result = __dc('default', '%-10s world', 6, 'hello');
  416. $expected = 'hello world';
  417. $this->assertEquals($expected, $result);
  418. $result = __dc('default', '%\'@-10s world', 6, 'hello');
  419. $expected = 'hello@@@@@ world';
  420. $this->assertEquals($expected, $result);
  421. }
  422. /**
  423. * test testTranslateDomainCategoryPluralWithFormatSpecifiers
  424. *
  425. * @return void
  426. */
  427. public function testTranslateDomainCategoryPluralWithFormatSpecifiers() {
  428. Configure::write('Config.language', 'rule_1_po');
  429. $result = __dcn('default', '%-5d = 1', '%-5d = 0 or > 1', 0, 6);
  430. $expected = '%-5d = 0 or > 1 (translated)';
  431. $this->assertEquals($expected, $result);
  432. $result = __dcn('default', '%-5d = 1', '%-5d = 0 or > 1', 1, 6);
  433. $expected = '%-5d = 1 (translated)';
  434. $this->assertEquals($expected, $result);
  435. }
  436. /**
  437. * test testTranslateCategoryWithFormatSpecifiers
  438. *
  439. * @return void
  440. */
  441. public function testTranslateCategoryWithFormatSpecifiers() {
  442. $result = __c('Some string with %+10s', 6, 'arguments');
  443. $expected = 'Some string with arguments';
  444. $this->assertEquals($expected, $result);
  445. $result = __c('Some string with %-10s: args', 6, 'arguments');
  446. $expected = 'Some string with arguments : args';
  447. $this->assertEquals($expected, $result);
  448. $result = __c('Some string with %\'*-10s: args', 6, 'arguments');
  449. $expected = 'Some string with arguments*: args';
  450. $this->assertEquals($expected, $result);
  451. }
  452. /**
  453. * test __n()
  454. *
  455. * @return void
  456. */
  457. public function testTranslatePlural() {
  458. Configure::write('Config.language', 'rule_1_po');
  459. $result = __n('%d = 1', '%d = 0 or > 1', 0);
  460. $expected = '%d = 0 or > 1 (translated)';
  461. $this->assertEquals($expected, $result);
  462. $result = __n('%d = 1', '%d = 0 or > 1', 1);
  463. $expected = '%d = 1 (translated)';
  464. $this->assertEquals($expected, $result);
  465. $result = __n('%d = 1 (from core)', '%d = 0 or > 1 (from core)', 2);
  466. $expected = '%d = 0 or > 1 (from core translated)';
  467. $this->assertEquals($expected, $result);
  468. $result = __n('%d item.', '%d items.', 1, 1);
  469. $expected = '1 item.';
  470. $this->assertEquals($expected, $result);
  471. $result = __n('%d item for id %s', '%d items for id %s', 2, 2, '1234');
  472. $expected = '2 items for id 1234';
  473. $this->assertEquals($expected, $result);
  474. $result = __n('%d item for id %s', '%d items for id %s', 2, array(2, '1234'));
  475. $expected = '2 items for id 1234';
  476. $this->assertEquals($expected, $result);
  477. }
  478. /**
  479. * test __d()
  480. *
  481. * @return void
  482. */
  483. public function testTranslateDomain() {
  484. Configure::write('Config.language', 'rule_1_po');
  485. $result = __d('default', 'Plural Rule 1');
  486. $expected = 'Plural Rule 1 (translated)';
  487. $this->assertEquals($expected, $result);
  488. $result = __d('core', 'Plural Rule 1');
  489. $expected = 'Plural Rule 1';
  490. $this->assertEquals($expected, $result);
  491. $result = __d('core', 'Plural Rule 1 (from core)');
  492. $expected = 'Plural Rule 1 (from core translated)';
  493. $this->assertEquals($expected, $result);
  494. $result = __d('core', 'Some string with %s', 'arguments');
  495. $expected = 'Some string with arguments';
  496. $this->assertEquals($expected, $result);
  497. $result = __d('core', 'Some string with %s %s', 'multiple', 'arguments');
  498. $expected = 'Some string with multiple arguments';
  499. $this->assertEquals($expected, $result);
  500. $result = __d('core', 'Some string with %s %s', array('multiple', 'arguments'));
  501. $expected = 'Some string with multiple arguments';
  502. $this->assertEquals($expected, $result);
  503. }
  504. /**
  505. * test __dn()
  506. *
  507. * @return void
  508. */
  509. public function testTranslateDomainPlural() {
  510. Configure::write('Config.language', 'rule_1_po');
  511. $result = __dn('default', '%d = 1', '%d = 0 or > 1', 0);
  512. $expected = '%d = 0 or > 1 (translated)';
  513. $this->assertEquals($expected, $result);
  514. $result = __dn('core', '%d = 1', '%d = 0 or > 1', 0);
  515. $expected = '%d = 0 or > 1';
  516. $this->assertEquals($expected, $result);
  517. $result = __dn('core', '%d = 1 (from core)', '%d = 0 or > 1 (from core)', 0);
  518. $expected = '%d = 0 or > 1 (from core translated)';
  519. $this->assertEquals($expected, $result);
  520. $result = __dn('default', '%d = 1', '%d = 0 or > 1', 1);
  521. $expected = '%d = 1 (translated)';
  522. $this->assertEquals($expected, $result);
  523. $result = __dn('core', '%d item.', '%d items.', 1, 1);
  524. $expected = '1 item.';
  525. $this->assertEquals($expected, $result);
  526. $result = __dn('core', '%d item for id %s', '%d items for id %s', 2, 2, '1234');
  527. $expected = '2 items for id 1234';
  528. $this->assertEquals($expected, $result);
  529. $result = __dn('core', '%d item for id %s', '%d items for id %s', 2, array(2, '1234'));
  530. $expected = '2 items for id 1234';
  531. $this->assertEquals($expected, $result);
  532. }
  533. /**
  534. * test __c()
  535. *
  536. * @return void
  537. */
  538. public function testTranslateCategory() {
  539. Configure::write('Config.language', 'rule_1_po');
  540. $result = __c('Plural Rule 1', 6);
  541. $expected = 'Plural Rule 1 (translated)';
  542. $this->assertEquals($expected, $result);
  543. $result = __c('Plural Rule 1 (from core)', 6);
  544. $expected = 'Plural Rule 1 (from core translated)';
  545. $this->assertEquals($expected, $result);
  546. $result = __c('Some string with %s', 6, 'arguments');
  547. $expected = 'Some string with arguments';
  548. $this->assertEquals($expected, $result);
  549. $result = __c('Some string with %s %s', 6, 'multiple', 'arguments');
  550. $expected = 'Some string with multiple arguments';
  551. $this->assertEquals($expected, $result);
  552. $result = __c('Some string with %s %s', 6, array('multiple', 'arguments'));
  553. $expected = 'Some string with multiple arguments';
  554. $this->assertEquals($expected, $result);
  555. }
  556. /**
  557. * test __dc()
  558. *
  559. * @return void
  560. */
  561. public function testTranslateDomainCategory() {
  562. Configure::write('Config.language', 'rule_1_po');
  563. $result = __dc('default', 'Plural Rule 1', 6);
  564. $expected = 'Plural Rule 1 (translated)';
  565. $this->assertEquals($expected, $result);
  566. $result = __dc('default', 'Plural Rule 1 (from core)', 6);
  567. $expected = 'Plural Rule 1 (from core translated)';
  568. $this->assertEquals($expected, $result);
  569. $result = __dc('core', 'Plural Rule 1', 6);
  570. $expected = 'Plural Rule 1';
  571. $this->assertEquals($expected, $result);
  572. $result = __dc('core', 'Plural Rule 1 (from core)', 6);
  573. $expected = 'Plural Rule 1 (from core translated)';
  574. $this->assertEquals($expected, $result);
  575. $result = __dc('core', 'Some string with %s', 6, 'arguments');
  576. $expected = 'Some string with arguments';
  577. $this->assertEquals($expected, $result);
  578. $result = __dc('core', 'Some string with %s %s', 6, 'multiple', 'arguments');
  579. $expected = 'Some string with multiple arguments';
  580. $this->assertEquals($expected, $result);
  581. $result = __dc('core', 'Some string with %s %s', 6, array('multiple', 'arguments'));
  582. $expected = 'Some string with multiple arguments';
  583. $this->assertEquals($expected, $result);
  584. }
  585. /**
  586. * test __dcn()
  587. *
  588. * @return void
  589. */
  590. public function testTranslateDomainCategoryPlural() {
  591. Configure::write('Config.language', 'rule_1_po');
  592. $result = __dcn('default', '%d = 1', '%d = 0 or > 1', 0, 6);
  593. $expected = '%d = 0 or > 1 (translated)';
  594. $this->assertEquals($expected, $result);
  595. $result = __dcn('default', '%d = 1 (from core)', '%d = 0 or > 1 (from core)', 1, 6);
  596. $expected = '%d = 1 (from core translated)';
  597. $this->assertEquals($expected, $result);
  598. $result = __dcn('core', '%d = 1', '%d = 0 or > 1', 0, 6);
  599. $expected = '%d = 0 or > 1';
  600. $this->assertEquals($expected, $result);
  601. $result = __dcn('core', '%d item.', '%d items.', 1, 6, 1);
  602. $expected = '1 item.';
  603. $this->assertEquals($expected, $result);
  604. $result = __dcn('core', '%d item for id %s', '%d items for id %s', 2, 6, 2, '1234');
  605. $expected = '2 items for id 1234';
  606. $this->assertEquals($expected, $result);
  607. $result = __dcn('core', '%d item for id %s', '%d items for id %s', 2, 6, array(2, '1234'));
  608. $expected = '2 items for id 1234';
  609. $this->assertEquals($expected, $result);
  610. }
  611. /**
  612. * test fileExistsInPath()
  613. *
  614. * @return void
  615. */
  616. public function testFileExistsInPath() {
  617. if (!function_exists('ini_set')) {
  618. $this->markTestSkipped('%s ini_set function not available');
  619. }
  620. $_includePath = ini_get('include_path');
  621. $path = TMP . 'basics_test';
  622. $folder1 = $path . DS . 'folder1';
  623. $folder2 = $path . DS . 'folder2';
  624. $file1 = $path . DS . 'file1.php';
  625. $file2 = $folder1 . DS . 'file2.php';
  626. $file3 = $folder1 . DS . 'file3.php';
  627. $file4 = $folder2 . DS . 'file4.php';
  628. new Folder($path, true);
  629. new Folder($folder1, true);
  630. new Folder($folder2, true);
  631. touch($file1);
  632. touch($file2);
  633. touch($file3);
  634. touch($file4);
  635. ini_set('include_path', $path . PATH_SEPARATOR . $folder1);
  636. $this->assertEquals(fileExistsInPath('file1.php'), $file1);
  637. $this->assertEquals(fileExistsInPath('file2.php'), $file2);
  638. $this->assertEquals(fileExistsInPath('folder1' . DS . 'file2.php'), $file2);
  639. $this->assertEquals(fileExistsInPath($file2), $file2);
  640. $this->assertEquals(fileExistsInPath('file3.php'), $file3);
  641. $this->assertEquals(fileExistsInPath($file4), $file4);
  642. $this->assertFalse(fileExistsInPath('file1'));
  643. $this->assertFalse(fileExistsInPath('file4.php'));
  644. $Folder = new Folder($path);
  645. $Folder->delete();
  646. ini_set('include_path', $_includePath);
  647. }
  648. /**
  649. * test convertSlash()
  650. *
  651. * @return void
  652. */
  653. public function testConvertSlash() {
  654. $result = convertSlash('\path\to\location\\');
  655. $expected = '\path\to\location\\';
  656. $this->assertEquals($expected, $result);
  657. $result = convertSlash('/path/to/location/');
  658. $expected = 'path_to_location';
  659. $this->assertEquals($expected, $result);
  660. }
  661. /**
  662. * test debug()
  663. *
  664. * @return void
  665. */
  666. public function testDebug() {
  667. ob_start();
  668. debug('this-is-a-test', false);
  669. $result = ob_get_clean();
  670. $expectedText = <<<EXPECTED
  671. %s (line %d)
  672. ########## DEBUG ##########
  673. 'this-is-a-test'
  674. ###########################
  675. EXPECTED;
  676. $expected = sprintf($expectedText, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 9);
  677. $this->assertEquals($expected, $result);
  678. ob_start();
  679. debug('<div>this-is-a-test</div>', true);
  680. $result = ob_get_clean();
  681. $expectedHtml = <<<EXPECTED
  682. <div class="cake-debug-output">
  683. <span><strong>%s</strong> (line <strong>%d</strong>)</span>
  684. <pre class="cake-debug">
  685. &#039;&lt;div&gt;this-is-a-test&lt;/div&gt;&#039;
  686. </pre>
  687. </div>
  688. EXPECTED;
  689. $expected = sprintf($expectedHtml, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 10);
  690. $this->assertEquals($expected, $result);
  691. ob_start();
  692. debug('<div>this-is-a-test</div>', true, true);
  693. $result = ob_get_clean();
  694. $expected = <<<EXPECTED
  695. <div class="cake-debug-output">
  696. <span><strong>%s</strong> (line <strong>%d</strong>)</span>
  697. <pre class="cake-debug">
  698. &#039;&lt;div&gt;this-is-a-test&lt;/div&gt;&#039;
  699. </pre>
  700. </div>
  701. EXPECTED;
  702. $expected = sprintf($expected, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 10);
  703. $this->assertEquals($expected, $result);
  704. ob_start();
  705. debug('<div>this-is-a-test</div>', true, false);
  706. $result = ob_get_clean();
  707. $expected = <<<EXPECTED
  708. <div class="cake-debug-output">
  709. <pre class="cake-debug">
  710. &#039;&lt;div&gt;this-is-a-test&lt;/div&gt;&#039;
  711. </pre>
  712. </div>
  713. EXPECTED;
  714. $expected = sprintf($expected, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 10);
  715. $this->assertEquals($expected, $result);
  716. ob_start();
  717. debug('<div>this-is-a-test</div>', null);
  718. $result = ob_get_clean();
  719. $expectedHtml = <<<EXPECTED
  720. <div class="cake-debug-output">
  721. <span><strong>%s</strong> (line <strong>%d</strong>)</span>
  722. <pre class="cake-debug">
  723. &#039;&lt;div&gt;this-is-a-test&lt;/div&gt;&#039;
  724. </pre>
  725. </div>
  726. EXPECTED;
  727. $expectedText = <<<EXPECTED
  728. %s (line %d)
  729. ########## DEBUG ##########
  730. '<div>this-is-a-test</div>'
  731. ###########################
  732. EXPECTED;
  733. if (php_sapi_name() === 'cli') {
  734. $expected = sprintf($expectedText, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 18);
  735. } else {
  736. $expected = sprintf($expectedHtml, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 19);
  737. }
  738. $this->assertEquals($expected, $result);
  739. ob_start();
  740. debug('<div>this-is-a-test</div>', null, false);
  741. $result = ob_get_clean();
  742. $expectedHtml = <<<EXPECTED
  743. <div class="cake-debug-output">
  744. <pre class="cake-debug">
  745. &#039;&lt;div&gt;this-is-a-test&lt;/div&gt;&#039;
  746. </pre>
  747. </div>
  748. EXPECTED;
  749. $expectedText = <<<EXPECTED
  750. ########## DEBUG ##########
  751. '<div>this-is-a-test</div>'
  752. ###########################
  753. EXPECTED;
  754. if (php_sapi_name() === 'cli') {
  755. $expected = sprintf($expectedText, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 18);
  756. } else {
  757. $expected = sprintf($expectedHtml, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 19);
  758. }
  759. $this->assertEquals($expected, $result);
  760. ob_start();
  761. debug('<div>this-is-a-test</div>', false);
  762. $result = ob_get_clean();
  763. $expected = <<<EXPECTED
  764. %s (line %d)
  765. ########## DEBUG ##########
  766. '<div>this-is-a-test</div>'
  767. ###########################
  768. EXPECTED;
  769. $expected = sprintf($expected, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 9);
  770. $this->assertEquals($expected, $result);
  771. ob_start();
  772. debug('<div>this-is-a-test</div>', false, true);
  773. $result = ob_get_clean();
  774. $expected = <<<EXPECTED
  775. %s (line %d)
  776. ########## DEBUG ##########
  777. '<div>this-is-a-test</div>'
  778. ###########################
  779. EXPECTED;
  780. $expected = sprintf($expected, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 9);
  781. $this->assertEquals($expected, $result);
  782. ob_start();
  783. debug('<div>this-is-a-test</div>', false, false);
  784. $result = ob_get_clean();
  785. $expected = <<<EXPECTED
  786. ########## DEBUG ##########
  787. '<div>this-is-a-test</div>'
  788. ###########################
  789. EXPECTED;
  790. $expected = sprintf($expected, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 9);
  791. $this->assertEquals($expected, $result);
  792. ob_start();
  793. debug(false, false, false);
  794. $result = ob_get_clean();
  795. $expected = <<<EXPECTED
  796. ########## DEBUG ##########
  797. false
  798. ###########################
  799. EXPECTED;
  800. $expected = sprintf($expected, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 9);
  801. $this->assertEquals($expected, $result);
  802. }
  803. /**
  804. * test pr()
  805. *
  806. * @return void
  807. */
  808. public function testPr() {
  809. $this->skipIf(php_sapi_name() === 'cli', 'Skipping web test in cli mode');
  810. ob_start();
  811. pr('this is a test');
  812. $result = ob_get_clean();
  813. $expected = "<pre>this is a test</pre>";
  814. $this->assertEquals($expected, $result);
  815. ob_start();
  816. pr(array('this' => 'is', 'a' => 'test'));
  817. $result = ob_get_clean();
  818. $expected = "<pre>Array\n(\n [this] => is\n [a] => test\n)\n</pre>";
  819. $this->assertEquals($expected, $result);
  820. }
  821. /**
  822. * test pr()
  823. *
  824. * @return void
  825. */
  826. public function testPrCli() {
  827. $this->skipIf(php_sapi_name() != 'cli', 'Skipping cli test in web mode');
  828. ob_start();
  829. pr('this is a test');
  830. $result = ob_get_clean();
  831. $expected = "\nthis is a test\n";
  832. $this->assertEquals($expected, $result);
  833. ob_start();
  834. pr(array('this' => 'is', 'a' => 'test'));
  835. $result = ob_get_clean();
  836. $expected = "\nArray\n(\n [this] => is\n [a] => test\n)\n\n";
  837. $this->assertEquals($expected, $result);
  838. }
  839. /**
  840. * test pluginSplit
  841. *
  842. * @return void
  843. */
  844. public function testPluginSplit() {
  845. $result = pluginSplit('Something.else');
  846. $this->assertEquals(array('Something', 'else'), $result);
  847. $result = pluginSplit('Something.else.more.dots');
  848. $this->assertEquals(array('Something', 'else.more.dots'), $result);
  849. $result = pluginSplit('Somethingelse');
  850. $this->assertEquals(array(null, 'Somethingelse'), $result);
  851. $result = pluginSplit('Something.else', true);
  852. $this->assertEquals(array('Something.', 'else'), $result);
  853. $result = pluginSplit('Something.else.more.dots', true);
  854. $this->assertEquals(array('Something.', 'else.more.dots'), $result);
  855. $result = pluginSplit('Post', false, 'Blog');
  856. $this->assertEquals(array('Blog', 'Post'), $result);
  857. $result = pluginSplit('Blog.Post', false, 'Ultimate');
  858. $this->assertEquals(array('Blog', 'Post'), $result);
  859. }
  860. /**
  861. * test namespaceSplit
  862. *
  863. * @return void
  864. */
  865. public function testNamespaceSplit() {
  866. $result = namespaceSplit('Something');
  867. $this->assertEquals(array('', 'Something'), $result);
  868. $result = namespaceSplit('\Something');
  869. $this->assertEquals(array('', 'Something'), $result);
  870. $result = namespaceSplit('Cake\Something');
  871. $this->assertEquals(array('Cake', 'Something'), $result);
  872. $result = namespaceSplit('Cake\Test\Something');
  873. $this->assertEquals(array('Cake\Test', 'Something'), $result);
  874. }
  875. }