BasicsTest.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934
  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 __()
  192. *
  193. * @return void
  194. */
  195. public function testTranslate() {
  196. Configure::write('Config.language', 'rule_1_po');
  197. $result = __('Plural Rule 1');
  198. $expected = 'Plural Rule 1 (translated)';
  199. $this->assertEquals($expected, $result);
  200. $result = __('Plural Rule 1 (from core)');
  201. $expected = 'Plural Rule 1 (from core translated)';
  202. $this->assertEquals($expected, $result);
  203. $result = __('Some string with %s', 'arguments');
  204. $expected = 'Some string with arguments';
  205. $this->assertEquals($expected, $result);
  206. $result = __('Some string with %s %s', 'multiple', 'arguments');
  207. $expected = 'Some string with multiple arguments';
  208. $this->assertEquals($expected, $result);
  209. $result = __('Some string with %s %s', array('multiple', 'arguments'));
  210. $expected = 'Some string with multiple arguments';
  211. $this->assertEquals($expected, $result);
  212. $result = __('Testing %2$s %1$s', 'order', 'different');
  213. $expected = 'Testing different order';
  214. $this->assertEquals($expected, $result);
  215. $result = __('Testing %2$s %1$s', array('order', 'different'));
  216. $expected = 'Testing different order';
  217. $this->assertEquals($expected, $result);
  218. $result = __('Testing %.2f number', 1.2345);
  219. $expected = 'Testing 1.23 number';
  220. $this->assertEquals($expected, $result);
  221. }
  222. /**
  223. * testTranslatePercent
  224. *
  225. * @return void
  226. */
  227. public function testTranslatePercent() {
  228. $result = __('%s are 100% real fruit', 'Apples');
  229. $expected = 'Apples are 100% real fruit';
  230. $this->assertEquals($expected, $result, 'Percent sign at end of word should be considered literal');
  231. $result = __('%s are %d% real fruit', 'Apples', 100);
  232. $expected = 'Apples are 100% real fruit';
  233. $this->assertEquals($expected, $result, 'A digit marker should not be misinterpreted');
  234. $result = __('%s are %s% real fruit', 'Apples', 100);
  235. $expected = 'Apples are 100% real fruit';
  236. $this->assertEquals($expected, $result, 'A string marker should not be misinterpreted');
  237. $result = __('%nonsense %s', 'Apples');
  238. $expected = '%nonsense Apples';
  239. $this->assertEquals($expected, $result, 'A percent sign at the start of the string should be considered literal');
  240. $result = __('%s are awesome%', 'Apples');
  241. $expected = 'Apples are awesome%';
  242. $this->assertEquals($expected, $result, 'A percent sign at the end of the string should be considered literal');
  243. $result = __('%2$d %1$s entered the bowl', 'Apples', 2);
  244. $expected = '2 Apples entered the bowl';
  245. $this->assertEquals($expected, $result, 'Positional replacement markers should not be misinterpreted');
  246. $result = __('%.2f% of all %s agree', 99.44444, 'Cats');
  247. $expected = '99.44% of all Cats agree';
  248. $this->assertEquals($expected, $result, 'significant-digit placeholder should not be misinterpreted');
  249. }
  250. /**
  251. * testTranslateWithFormatSpecifiers
  252. *
  253. * @return void
  254. */
  255. public function testTranslateWithFormatSpecifiers() {
  256. $expected = 'Check, one, two, three';
  257. $result = __('Check, %+10s, three', 'one, two');
  258. $this->assertEquals($expected, $result);
  259. $expected = 'Check, +1, two, three';
  260. $result = __('Check, %+5d, two, three', 1);
  261. $this->assertEquals($expected, $result);
  262. $expected = 'Check, @@one, two, three';
  263. $result = __('Check, %\'@+10s, three', 'one, two');
  264. $this->assertEquals($expected, $result);
  265. $expected = 'Check, one, two , three';
  266. $result = __('Check, %-10s, three', 'one, two');
  267. $this->assertEquals($expected, $result);
  268. $expected = 'Check, one, two##, three';
  269. $result = __('Check, %\'#-10s, three', 'one, two');
  270. $this->assertEquals($expected, $result);
  271. $expected = 'Check, one, two, three';
  272. $result = __d('default', 'Check, %+10s, three', 'one, two');
  273. $this->assertEquals($expected, $result);
  274. $expected = 'Check, @@one, two, three';
  275. $result = __d('default', 'Check, %\'@+10s, three', 'one, two');
  276. $this->assertEquals($expected, $result);
  277. $expected = 'Check, one, two , three';
  278. $result = __d('default', 'Check, %-10s, three', 'one, two');
  279. $this->assertEquals($expected, $result);
  280. $expected = 'Check, one, two##, three';
  281. $result = __d('default', 'Check, %\'#-10s, three', 'one, two');
  282. $this->assertEquals($expected, $result);
  283. }
  284. /**
  285. * testTranslateDomainPluralWithFormatSpecifiers
  286. *
  287. * @return void
  288. */
  289. public function testTranslateDomainPluralWithFormatSpecifiers() {
  290. $result = __dn('core', '%+5d item.', '%+5d items.', 1, 1);
  291. $expected = ' +1 item.';
  292. $this->assertEquals($expected, $result);
  293. $result = __dn('core', '%-5d item.', '%-5d items.', 10, 10);
  294. $expected = '10 items.';
  295. $this->assertEquals($expected, $result);
  296. $result = __dn('core', '%\'#+5d item.', '%\'*+5d items.', 1, 1);
  297. $expected = '###+1 item.';
  298. $this->assertEquals($expected, $result);
  299. $result = __dn('core', '%\'#+5d item.', '%\'*+5d items.', 90, 90);
  300. $expected = '**+90 items.';
  301. $this->assertEquals($expected, $result);
  302. $result = __dn('core', '%\'#+5d item.', '%\'*+5d items.', 9000, 9000);
  303. $expected = '+9000 items.';
  304. $this->assertEquals($expected, $result);
  305. }
  306. /**
  307. * test testTranslatePluralWithFormatSpecifiers
  308. *
  309. * @return void
  310. */
  311. public function testTranslatePluralWithFormatSpecifiers() {
  312. Configure::write('Config.language', 'rule_1_po');
  313. $result = __n('%-5d = 1', '%-5d = 0 or > 1', 10);
  314. $expected = '%-5d = 0 or > 1 (translated)';
  315. $this->assertEquals($expected, $result);
  316. }
  317. /**
  318. * test testTranslateDomainCategoryWithFormatSpecifiers
  319. *
  320. * @return void
  321. */
  322. public function testTranslateDomainCategoryWithFormatSpecifiers() {
  323. Configure::write('Config.language', 'rule_1_po');
  324. $result = __dc('default', '%+10s world', 6, 'hello');
  325. $expected = ' hello world';
  326. $this->assertEquals($expected, $result);
  327. $result = __dc('default', '%-10s world', 6, 'hello');
  328. $expected = 'hello world';
  329. $this->assertEquals($expected, $result);
  330. $result = __dc('default', '%\'@-10s world', 6, 'hello');
  331. $expected = 'hello@@@@@ world';
  332. $this->assertEquals($expected, $result);
  333. }
  334. /**
  335. * test testTranslateDomainCategoryPluralWithFormatSpecifiers
  336. *
  337. * @return void
  338. */
  339. public function testTranslateDomainCategoryPluralWithFormatSpecifiers() {
  340. Configure::write('Config.language', 'rule_1_po');
  341. $result = __dcn('default', '%-5d = 1', '%-5d = 0 or > 1', 0, 6);
  342. $expected = '%-5d = 0 or > 1 (translated)';
  343. $this->assertEquals($expected, $result);
  344. $result = __dcn('default', '%-5d = 1', '%-5d = 0 or > 1', 1, 6);
  345. $expected = '%-5d = 1 (translated)';
  346. $this->assertEquals($expected, $result);
  347. }
  348. /**
  349. * test testTranslateCategoryWithFormatSpecifiers
  350. *
  351. * @return void
  352. */
  353. public function testTranslateCategoryWithFormatSpecifiers() {
  354. $result = __c('Some string with %+10s', 6, 'arguments');
  355. $expected = 'Some string with arguments';
  356. $this->assertEquals($expected, $result);
  357. $result = __c('Some string with %-10s: args', 6, 'arguments');
  358. $expected = 'Some string with arguments : args';
  359. $this->assertEquals($expected, $result);
  360. $result = __c('Some string with %\'*-10s: args', 6, 'arguments');
  361. $expected = 'Some string with arguments*: args';
  362. $this->assertEquals($expected, $result);
  363. }
  364. /**
  365. * test __n()
  366. *
  367. * @return void
  368. */
  369. public function testTranslatePlural() {
  370. Configure::write('Config.language', 'rule_1_po');
  371. $result = __n('%d = 1', '%d = 0 or > 1', 0);
  372. $expected = '%d = 0 or > 1 (translated)';
  373. $this->assertEquals($expected, $result);
  374. $result = __n('%d = 1', '%d = 0 or > 1', 1);
  375. $expected = '%d = 1 (translated)';
  376. $this->assertEquals($expected, $result);
  377. $result = __n('%d = 1 (from core)', '%d = 0 or > 1 (from core)', 2);
  378. $expected = '%d = 0 or > 1 (from core translated)';
  379. $this->assertEquals($expected, $result);
  380. $result = __n('%d item.', '%d items.', 1, 1);
  381. $expected = '1 item.';
  382. $this->assertEquals($expected, $result);
  383. $result = __n('%d item for id %s', '%d items for id %s', 2, 2, '1234');
  384. $expected = '2 items for id 1234';
  385. $this->assertEquals($expected, $result);
  386. $result = __n('%d item for id %s', '%d items for id %s', 2, array(2, '1234'));
  387. $expected = '2 items for id 1234';
  388. $this->assertEquals($expected, $result);
  389. }
  390. /**
  391. * test __d()
  392. *
  393. * @return void
  394. */
  395. public function testTranslateDomain() {
  396. Configure::write('Config.language', 'rule_1_po');
  397. $result = __d('default', 'Plural Rule 1');
  398. $expected = 'Plural Rule 1 (translated)';
  399. $this->assertEquals($expected, $result);
  400. $result = __d('core', 'Plural Rule 1');
  401. $expected = 'Plural Rule 1';
  402. $this->assertEquals($expected, $result);
  403. $result = __d('core', 'Plural Rule 1 (from core)');
  404. $expected = 'Plural Rule 1 (from core translated)';
  405. $this->assertEquals($expected, $result);
  406. $result = __d('core', 'Some string with %s', 'arguments');
  407. $expected = 'Some string with arguments';
  408. $this->assertEquals($expected, $result);
  409. $result = __d('core', 'Some string with %s %s', 'multiple', 'arguments');
  410. $expected = 'Some string with multiple arguments';
  411. $this->assertEquals($expected, $result);
  412. $result = __d('core', 'Some string with %s %s', array('multiple', 'arguments'));
  413. $expected = 'Some string with multiple arguments';
  414. $this->assertEquals($expected, $result);
  415. }
  416. /**
  417. * test __dn()
  418. *
  419. * @return void
  420. */
  421. public function testTranslateDomainPlural() {
  422. Configure::write('Config.language', 'rule_1_po');
  423. $result = __dn('default', '%d = 1', '%d = 0 or > 1', 0);
  424. $expected = '%d = 0 or > 1 (translated)';
  425. $this->assertEquals($expected, $result);
  426. $result = __dn('core', '%d = 1', '%d = 0 or > 1', 0);
  427. $expected = '%d = 0 or > 1';
  428. $this->assertEquals($expected, $result);
  429. $result = __dn('core', '%d = 1 (from core)', '%d = 0 or > 1 (from core)', 0);
  430. $expected = '%d = 0 or > 1 (from core translated)';
  431. $this->assertEquals($expected, $result);
  432. $result = __dn('default', '%d = 1', '%d = 0 or > 1', 1);
  433. $expected = '%d = 1 (translated)';
  434. $this->assertEquals($expected, $result);
  435. $result = __dn('core', '%d item.', '%d items.', 1, 1);
  436. $expected = '1 item.';
  437. $this->assertEquals($expected, $result);
  438. $result = __dn('core', '%d item for id %s', '%d items for id %s', 2, 2, '1234');
  439. $expected = '2 items for id 1234';
  440. $this->assertEquals($expected, $result);
  441. $result = __dn('core', '%d item for id %s', '%d items for id %s', 2, array(2, '1234'));
  442. $expected = '2 items for id 1234';
  443. $this->assertEquals($expected, $result);
  444. }
  445. /**
  446. * test __c()
  447. *
  448. * @return void
  449. */
  450. public function testTranslateCategory() {
  451. Configure::write('Config.language', 'rule_1_po');
  452. $result = __c('Plural Rule 1', 6);
  453. $expected = 'Plural Rule 1 (translated)';
  454. $this->assertEquals($expected, $result);
  455. $result = __c('Plural Rule 1 (from core)', 6);
  456. $expected = 'Plural Rule 1 (from core translated)';
  457. $this->assertEquals($expected, $result);
  458. $result = __c('Some string with %s', 6, 'arguments');
  459. $expected = 'Some string with arguments';
  460. $this->assertEquals($expected, $result);
  461. $result = __c('Some string with %s %s', 6, 'multiple', 'arguments');
  462. $expected = 'Some string with multiple arguments';
  463. $this->assertEquals($expected, $result);
  464. $result = __c('Some string with %s %s', 6, array('multiple', 'arguments'));
  465. $expected = 'Some string with multiple arguments';
  466. $this->assertEquals($expected, $result);
  467. }
  468. /**
  469. * test __dc()
  470. *
  471. * @return void
  472. */
  473. public function testTranslateDomainCategory() {
  474. Configure::write('Config.language', 'rule_1_po');
  475. $result = __dc('default', 'Plural Rule 1', 6);
  476. $expected = 'Plural Rule 1 (translated)';
  477. $this->assertEquals($expected, $result);
  478. $result = __dc('default', 'Plural Rule 1 (from core)', 6);
  479. $expected = 'Plural Rule 1 (from core translated)';
  480. $this->assertEquals($expected, $result);
  481. $result = __dc('core', 'Plural Rule 1', 6);
  482. $expected = 'Plural Rule 1';
  483. $this->assertEquals($expected, $result);
  484. $result = __dc('core', 'Plural Rule 1 (from core)', 6);
  485. $expected = 'Plural Rule 1 (from core translated)';
  486. $this->assertEquals($expected, $result);
  487. $result = __dc('core', 'Some string with %s', 6, 'arguments');
  488. $expected = 'Some string with arguments';
  489. $this->assertEquals($expected, $result);
  490. $result = __dc('core', 'Some string with %s %s', 6, 'multiple', 'arguments');
  491. $expected = 'Some string with multiple arguments';
  492. $this->assertEquals($expected, $result);
  493. $result = __dc('core', 'Some string with %s %s', 6, array('multiple', 'arguments'));
  494. $expected = 'Some string with multiple arguments';
  495. $this->assertEquals($expected, $result);
  496. }
  497. /**
  498. * test __dcn()
  499. *
  500. * @return void
  501. */
  502. public function testTranslateDomainCategoryPlural() {
  503. Configure::write('Config.language', 'rule_1_po');
  504. $result = __dcn('default', '%d = 1', '%d = 0 or > 1', 0, 6);
  505. $expected = '%d = 0 or > 1 (translated)';
  506. $this->assertEquals($expected, $result);
  507. $result = __dcn('default', '%d = 1 (from core)', '%d = 0 or > 1 (from core)', 1, 6);
  508. $expected = '%d = 1 (from core translated)';
  509. $this->assertEquals($expected, $result);
  510. $result = __dcn('core', '%d = 1', '%d = 0 or > 1', 0, 6);
  511. $expected = '%d = 0 or > 1';
  512. $this->assertEquals($expected, $result);
  513. $result = __dcn('core', '%d item.', '%d items.', 1, 6, 1);
  514. $expected = '1 item.';
  515. $this->assertEquals($expected, $result);
  516. $result = __dcn('core', '%d item for id %s', '%d items for id %s', 2, 6, 2, '1234');
  517. $expected = '2 items for id 1234';
  518. $this->assertEquals($expected, $result);
  519. $result = __dcn('core', '%d item for id %s', '%d items for id %s', 2, 6, array(2, '1234'));
  520. $expected = '2 items for id 1234';
  521. $this->assertEquals($expected, $result);
  522. }
  523. /**
  524. * test debug()
  525. *
  526. * @return void
  527. */
  528. public function testDebug() {
  529. ob_start();
  530. debug('this-is-a-test', false);
  531. $result = ob_get_clean();
  532. $expectedText = <<<EXPECTED
  533. %s (line %d)
  534. ########## DEBUG ##########
  535. 'this-is-a-test'
  536. ###########################
  537. EXPECTED;
  538. $expected = sprintf($expectedText, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 9);
  539. $this->assertEquals($expected, $result);
  540. ob_start();
  541. debug('<div>this-is-a-test</div>', true);
  542. $result = ob_get_clean();
  543. $expectedHtml = <<<EXPECTED
  544. <div class="cake-debug-output">
  545. <span><strong>%s</strong> (line <strong>%d</strong>)</span>
  546. <pre class="cake-debug">
  547. &#039;&lt;div&gt;this-is-a-test&lt;/div&gt;&#039;
  548. </pre>
  549. </div>
  550. EXPECTED;
  551. $expected = sprintf($expectedHtml, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 10);
  552. $this->assertEquals($expected, $result);
  553. ob_start();
  554. debug('<div>this-is-a-test</div>', true, true);
  555. $result = ob_get_clean();
  556. $expected = <<<EXPECTED
  557. <div class="cake-debug-output">
  558. <span><strong>%s</strong> (line <strong>%d</strong>)</span>
  559. <pre class="cake-debug">
  560. &#039;&lt;div&gt;this-is-a-test&lt;/div&gt;&#039;
  561. </pre>
  562. </div>
  563. EXPECTED;
  564. $expected = sprintf($expected, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 10);
  565. $this->assertEquals($expected, $result);
  566. ob_start();
  567. debug('<div>this-is-a-test</div>', true, false);
  568. $result = ob_get_clean();
  569. $expected = <<<EXPECTED
  570. <div class="cake-debug-output">
  571. <pre class="cake-debug">
  572. &#039;&lt;div&gt;this-is-a-test&lt;/div&gt;&#039;
  573. </pre>
  574. </div>
  575. EXPECTED;
  576. $expected = sprintf($expected, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 10);
  577. $this->assertEquals($expected, $result);
  578. ob_start();
  579. debug('<div>this-is-a-test</div>', null);
  580. $result = ob_get_clean();
  581. $expectedHtml = <<<EXPECTED
  582. <div class="cake-debug-output">
  583. <span><strong>%s</strong> (line <strong>%d</strong>)</span>
  584. <pre class="cake-debug">
  585. &#039;&lt;div&gt;this-is-a-test&lt;/div&gt;&#039;
  586. </pre>
  587. </div>
  588. EXPECTED;
  589. $expectedText = <<<EXPECTED
  590. %s (line %d)
  591. ########## DEBUG ##########
  592. '<div>this-is-a-test</div>'
  593. ###########################
  594. EXPECTED;
  595. if (php_sapi_name() === 'cli') {
  596. $expected = sprintf($expectedText, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 18);
  597. } else {
  598. $expected = sprintf($expectedHtml, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 19);
  599. }
  600. $this->assertEquals($expected, $result);
  601. ob_start();
  602. debug('<div>this-is-a-test</div>', null, false);
  603. $result = ob_get_clean();
  604. $expectedHtml = <<<EXPECTED
  605. <div class="cake-debug-output">
  606. <pre class="cake-debug">
  607. &#039;&lt;div&gt;this-is-a-test&lt;/div&gt;&#039;
  608. </pre>
  609. </div>
  610. EXPECTED;
  611. $expectedText = <<<EXPECTED
  612. ########## DEBUG ##########
  613. '<div>this-is-a-test</div>'
  614. ###########################
  615. EXPECTED;
  616. if (php_sapi_name() === 'cli') {
  617. $expected = sprintf($expectedText, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 18);
  618. } else {
  619. $expected = sprintf($expectedHtml, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 19);
  620. }
  621. $this->assertEquals($expected, $result);
  622. ob_start();
  623. debug('<div>this-is-a-test</div>', false);
  624. $result = ob_get_clean();
  625. $expected = <<<EXPECTED
  626. %s (line %d)
  627. ########## DEBUG ##########
  628. '<div>this-is-a-test</div>'
  629. ###########################
  630. EXPECTED;
  631. $expected = sprintf($expected, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 9);
  632. $this->assertEquals($expected, $result);
  633. ob_start();
  634. debug('<div>this-is-a-test</div>', false, true);
  635. $result = ob_get_clean();
  636. $expected = <<<EXPECTED
  637. %s (line %d)
  638. ########## DEBUG ##########
  639. '<div>this-is-a-test</div>'
  640. ###########################
  641. EXPECTED;
  642. $expected = sprintf($expected, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 9);
  643. $this->assertEquals($expected, $result);
  644. ob_start();
  645. debug('<div>this-is-a-test</div>', false, false);
  646. $result = ob_get_clean();
  647. $expected = <<<EXPECTED
  648. ########## DEBUG ##########
  649. '<div>this-is-a-test</div>'
  650. ###########################
  651. EXPECTED;
  652. $expected = sprintf($expected, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 9);
  653. $this->assertEquals($expected, $result);
  654. ob_start();
  655. debug(false, false, false);
  656. $result = ob_get_clean();
  657. $expected = <<<EXPECTED
  658. ########## DEBUG ##########
  659. false
  660. ###########################
  661. EXPECTED;
  662. $expected = sprintf($expected, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 9);
  663. $this->assertEquals($expected, $result);
  664. }
  665. /**
  666. * test pr()
  667. *
  668. * @return void
  669. */
  670. public function testPr() {
  671. $this->skipIf(php_sapi_name() === 'cli', 'Skipping web test in cli mode');
  672. ob_start();
  673. pr('this is a test');
  674. $result = ob_get_clean();
  675. $expected = "<pre>this is a test</pre>";
  676. $this->assertEquals($expected, $result);
  677. ob_start();
  678. pr(array('this' => 'is', 'a' => 'test'));
  679. $result = ob_get_clean();
  680. $expected = "<pre>Array\n(\n [this] => is\n [a] => test\n)\n</pre>";
  681. $this->assertEquals($expected, $result);
  682. }
  683. /**
  684. * test pr()
  685. *
  686. * @return void
  687. */
  688. public function testPrCli() {
  689. $this->skipIf(php_sapi_name() != 'cli', 'Skipping cli test in web mode');
  690. ob_start();
  691. pr('this is a test');
  692. $result = ob_get_clean();
  693. $expected = "\nthis is a test\n";
  694. $this->assertEquals($expected, $result);
  695. ob_start();
  696. pr(array('this' => 'is', 'a' => 'test'));
  697. $result = ob_get_clean();
  698. $expected = "\nArray\n(\n [this] => is\n [a] => test\n)\n\n";
  699. $this->assertEquals($expected, $result);
  700. }
  701. /**
  702. * Test splitting plugin names.
  703. *
  704. * @return void
  705. */
  706. public function testPluginSplit() {
  707. $result = pluginSplit('Something.else');
  708. $this->assertEquals(array('Something', 'else'), $result);
  709. $result = pluginSplit('Something.else.more.dots');
  710. $this->assertEquals(array('Something', 'else.more.dots'), $result);
  711. $result = pluginSplit('Somethingelse');
  712. $this->assertEquals(array(null, 'Somethingelse'), $result);
  713. $result = pluginSplit('Something.else', true);
  714. $this->assertEquals(array('Something.', 'else'), $result);
  715. $result = pluginSplit('Something.else.more.dots', true);
  716. $this->assertEquals(array('Something.', 'else.more.dots'), $result);
  717. $result = pluginSplit('Post', false, 'Blog');
  718. $this->assertEquals(array('Blog', 'Post'), $result);
  719. $result = pluginSplit('Blog.Post', false, 'Ultimate');
  720. $this->assertEquals(array('Blog', 'Post'), $result);
  721. }
  722. /**
  723. * test namespaceSplit
  724. *
  725. * @return void
  726. */
  727. public function testNamespaceSplit() {
  728. $result = namespaceSplit('Something');
  729. $this->assertEquals(array('', 'Something'), $result);
  730. $result = namespaceSplit('\Something');
  731. $this->assertEquals(array('', 'Something'), $result);
  732. $result = namespaceSplit('Cake\Something');
  733. $this->assertEquals(array('Cake', 'Something'), $result);
  734. $result = namespaceSplit('Cake\Test\Something');
  735. $this->assertEquals(array('Cake\Test', 'Something'), $result);
  736. }
  737. /**
  738. * Tests that the stackTrace() method is a shortcut for Debugger::trace()
  739. *
  740. * @return void
  741. */
  742. public function testStackTrace() {
  743. ob_start();
  744. list($r, $expected) = [stackTrace(), \Cake\Utility\Debugger::trace()];
  745. $result = ob_get_clean();
  746. $this->assertEquals($expected, $result);
  747. $opts = ['args' => true];
  748. ob_start();
  749. list($r, $expected) = [stackTrace($opts), \Cake\Utility\Debugger::trace($opts)];
  750. $result = ob_get_clean();
  751. $this->assertEquals($expected, $result);
  752. }
  753. }