BasicsTest.php 34 KB

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