BasicsTest.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909
  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 CakePHP(tm) v 1.2.0.4206
  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. * test __n()
  312. *
  313. * @return void
  314. */
  315. public function testTranslatePlural() {
  316. Configure::write('Config.language', 'rule_1_po');
  317. $result = __n('%d = 1', '%d = 0 or > 1', 0);
  318. $expected = '%d = 0 or > 1 (translated)';
  319. $this->assertEquals($expected, $result);
  320. $result = __n('%d = 1', '%d = 0 or > 1', 1);
  321. $expected = '%d = 1 (translated)';
  322. $this->assertEquals($expected, $result);
  323. $result = __n('%d = 1 (from core)', '%d = 0 or > 1 (from core)', 2);
  324. $expected = '%d = 0 or > 1 (from core translated)';
  325. $this->assertEquals($expected, $result);
  326. $result = __n('%d item.', '%d items.', 1, 1);
  327. $expected = '1 item.';
  328. $this->assertEquals($expected, $result);
  329. $result = __n('%d item for id %s', '%d items for id %s', 2, 2, '1234');
  330. $expected = '2 items for id 1234';
  331. $this->assertEquals($expected, $result);
  332. $result = __n('%d item for id %s', '%d items for id %s', 2, array(2, '1234'));
  333. $expected = '2 items for id 1234';
  334. $this->assertEquals($expected, $result);
  335. }
  336. /**
  337. * test __d()
  338. *
  339. * @return void
  340. */
  341. public function testTranslateDomain() {
  342. Configure::write('Config.language', 'rule_1_po');
  343. $result = __d('default', 'Plural Rule 1');
  344. $expected = 'Plural Rule 1 (translated)';
  345. $this->assertEquals($expected, $result);
  346. $result = __d('core', 'Plural Rule 1');
  347. $expected = 'Plural Rule 1';
  348. $this->assertEquals($expected, $result);
  349. $result = __d('core', 'Plural Rule 1 (from core)');
  350. $expected = 'Plural Rule 1 (from core translated)';
  351. $this->assertEquals($expected, $result);
  352. $result = __d('core', 'Some string with %s', 'arguments');
  353. $expected = 'Some string with arguments';
  354. $this->assertEquals($expected, $result);
  355. $result = __d('core', 'Some string with %s %s', 'multiple', 'arguments');
  356. $expected = 'Some string with multiple arguments';
  357. $this->assertEquals($expected, $result);
  358. $result = __d('core', 'Some string with %s %s', array('multiple', 'arguments'));
  359. $expected = 'Some string with multiple arguments';
  360. $this->assertEquals($expected, $result);
  361. }
  362. /**
  363. * test __dn()
  364. *
  365. * @return void
  366. */
  367. public function testTranslateDomainPlural() {
  368. Configure::write('Config.language', 'rule_1_po');
  369. $result = __dn('default', '%d = 1', '%d = 0 or > 1', 0);
  370. $expected = '%d = 0 or > 1 (translated)';
  371. $this->assertEquals($expected, $result);
  372. $result = __dn('core', '%d = 1', '%d = 0 or > 1', 0);
  373. $expected = '%d = 0 or > 1';
  374. $this->assertEquals($expected, $result);
  375. $result = __dn('core', '%d = 1 (from core)', '%d = 0 or > 1 (from core)', 0);
  376. $expected = '%d = 0 or > 1 (from core translated)';
  377. $this->assertEquals($expected, $result);
  378. $result = __dn('default', '%d = 1', '%d = 0 or > 1', 1);
  379. $expected = '%d = 1 (translated)';
  380. $this->assertEquals($expected, $result);
  381. $result = __dn('core', '%d item.', '%d items.', 1, 1);
  382. $expected = '1 item.';
  383. $this->assertEquals($expected, $result);
  384. $result = __dn('core', '%d item for id %s', '%d items for id %s', 2, 2, '1234');
  385. $expected = '2 items for id 1234';
  386. $this->assertEquals($expected, $result);
  387. $result = __dn('core', '%d item for id %s', '%d items for id %s', 2, array(2, '1234'));
  388. $expected = '2 items for id 1234';
  389. $this->assertEquals($expected, $result);
  390. }
  391. /**
  392. * test __c()
  393. *
  394. * @return void
  395. */
  396. public function testTranslateCategory() {
  397. Configure::write('Config.language', 'rule_1_po');
  398. $result = __c('Plural Rule 1', 6);
  399. $expected = 'Plural Rule 1 (translated)';
  400. $this->assertEquals($expected, $result);
  401. $result = __c('Plural Rule 1 (from core)', 6);
  402. $expected = 'Plural Rule 1 (from core translated)';
  403. $this->assertEquals($expected, $result);
  404. $result = __c('Some string with %s', 6, 'arguments');
  405. $expected = 'Some string with arguments';
  406. $this->assertEquals($expected, $result);
  407. $result = __c('Some string with %s %s', 6, 'multiple', 'arguments');
  408. $expected = 'Some string with multiple arguments';
  409. $this->assertEquals($expected, $result);
  410. $result = __c('Some string with %s %s', 6, array('multiple', 'arguments'));
  411. $expected = 'Some string with multiple arguments';
  412. $this->assertEquals($expected, $result);
  413. }
  414. /**
  415. * test __dc()
  416. *
  417. * @return void
  418. */
  419. public function testTranslateDomainCategory() {
  420. Configure::write('Config.language', 'rule_1_po');
  421. $result = __dc('default', 'Plural Rule 1', 6);
  422. $expected = 'Plural Rule 1 (translated)';
  423. $this->assertEquals($expected, $result);
  424. $result = __dc('default', 'Plural Rule 1 (from core)', 6);
  425. $expected = 'Plural Rule 1 (from core translated)';
  426. $this->assertEquals($expected, $result);
  427. $result = __dc('core', 'Plural Rule 1', 6);
  428. $expected = 'Plural Rule 1';
  429. $this->assertEquals($expected, $result);
  430. $result = __dc('core', 'Plural Rule 1 (from core)', 6);
  431. $expected = 'Plural Rule 1 (from core translated)';
  432. $this->assertEquals($expected, $result);
  433. $result = __dc('core', 'Some string with %s', 6, 'arguments');
  434. $expected = 'Some string with arguments';
  435. $this->assertEquals($expected, $result);
  436. $result = __dc('core', 'Some string with %s %s', 6, 'multiple', 'arguments');
  437. $expected = 'Some string with multiple arguments';
  438. $this->assertEquals($expected, $result);
  439. $result = __dc('core', 'Some string with %s %s', 6, array('multiple', 'arguments'));
  440. $expected = 'Some string with multiple arguments';
  441. $this->assertEquals($expected, $result);
  442. }
  443. /**
  444. * test __dcn()
  445. *
  446. * @return void
  447. */
  448. public function testTranslateDomainCategoryPlural() {
  449. Configure::write('Config.language', 'rule_1_po');
  450. $result = __dcn('default', '%d = 1', '%d = 0 or > 1', 0, 6);
  451. $expected = '%d = 0 or > 1 (translated)';
  452. $this->assertEquals($expected, $result);
  453. $result = __dcn('default', '%d = 1 (from core)', '%d = 0 or > 1 (from core)', 1, 6);
  454. $expected = '%d = 1 (from core translated)';
  455. $this->assertEquals($expected, $result);
  456. $result = __dcn('core', '%d = 1', '%d = 0 or > 1', 0, 6);
  457. $expected = '%d = 0 or > 1';
  458. $this->assertEquals($expected, $result);
  459. $result = __dcn('core', '%d item.', '%d items.', 1, 6, 1);
  460. $expected = '1 item.';
  461. $this->assertEquals($expected, $result);
  462. $result = __dcn('core', '%d item for id %s', '%d items for id %s', 2, 6, 2, '1234');
  463. $expected = '2 items for id 1234';
  464. $this->assertEquals($expected, $result);
  465. $result = __dcn('core', '%d item for id %s', '%d items for id %s', 2, 6, array(2, '1234'));
  466. $expected = '2 items for id 1234';
  467. $this->assertEquals($expected, $result);
  468. }
  469. /**
  470. * test fileExistsInPath()
  471. *
  472. * @return void
  473. */
  474. public function testFileExistsInPath() {
  475. if (!function_exists('ini_set')) {
  476. $this->markTestSkipped('%s ini_set function not available');
  477. }
  478. $_includePath = ini_get('include_path');
  479. $path = TMP . 'basics_test';
  480. $folder1 = $path . DS . 'folder1';
  481. $folder2 = $path . DS . 'folder2';
  482. $file1 = $path . DS . 'file1.php';
  483. $file2 = $folder1 . DS . 'file2.php';
  484. $file3 = $folder1 . DS . 'file3.php';
  485. $file4 = $folder2 . DS . 'file4.php';
  486. new Folder($path, true);
  487. new Folder($folder1, true);
  488. new Folder($folder2, true);
  489. touch($file1);
  490. touch($file2);
  491. touch($file3);
  492. touch($file4);
  493. ini_set('include_path', $path . PATH_SEPARATOR . $folder1);
  494. $this->assertEquals(fileExistsInPath('file1.php'), $file1);
  495. $this->assertEquals(fileExistsInPath('file2.php'), $file2);
  496. $this->assertEquals(fileExistsInPath('folder1' . DS . 'file2.php'), $file2);
  497. $this->assertEquals(fileExistsInPath($file2), $file2);
  498. $this->assertEquals(fileExistsInPath('file3.php'), $file3);
  499. $this->assertEquals(fileExistsInPath($file4), $file4);
  500. $this->assertFalse(fileExistsInPath('file1'));
  501. $this->assertFalse(fileExistsInPath('file4.php'));
  502. $Folder = new Folder($path);
  503. $Folder->delete();
  504. ini_set('include_path', $_includePath);
  505. }
  506. /**
  507. * test convertSlash()
  508. *
  509. * @return void
  510. */
  511. public function testConvertSlash() {
  512. $result = convertSlash('\path\to\location\\');
  513. $expected = '\path\to\location\\';
  514. $this->assertEquals($expected, $result);
  515. $result = convertSlash('/path/to/location/');
  516. $expected = 'path_to_location';
  517. $this->assertEquals($expected, $result);
  518. }
  519. /**
  520. * test debug()
  521. *
  522. * @return void
  523. */
  524. public function testDebug() {
  525. ob_start();
  526. debug('this-is-a-test', false);
  527. $result = ob_get_clean();
  528. $expectedText = <<<EXPECTED
  529. %s (line %d)
  530. ########## DEBUG ##########
  531. 'this-is-a-test'
  532. ###########################
  533. EXPECTED;
  534. $expected = sprintf($expectedText, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 9);
  535. $this->assertEquals($expected, $result);
  536. ob_start();
  537. debug('<div>this-is-a-test</div>', true);
  538. $result = ob_get_clean();
  539. $expectedHtml = <<<EXPECTED
  540. <div class="cake-debug-output">
  541. <span><strong>%s</strong> (line <strong>%d</strong>)</span>
  542. <pre class="cake-debug">
  543. &#039;&lt;div&gt;this-is-a-test&lt;/div&gt;&#039;
  544. </pre>
  545. </div>
  546. EXPECTED;
  547. $expected = sprintf($expectedHtml, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 10);
  548. $this->assertEquals($expected, $result);
  549. ob_start();
  550. debug('<div>this-is-a-test</div>', true, true);
  551. $result = ob_get_clean();
  552. $expected = <<<EXPECTED
  553. <div class="cake-debug-output">
  554. <span><strong>%s</strong> (line <strong>%d</strong>)</span>
  555. <pre class="cake-debug">
  556. &#039;&lt;div&gt;this-is-a-test&lt;/div&gt;&#039;
  557. </pre>
  558. </div>
  559. EXPECTED;
  560. $expected = sprintf($expected, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 10);
  561. $this->assertEquals($expected, $result);
  562. ob_start();
  563. debug('<div>this-is-a-test</div>', true, false);
  564. $result = ob_get_clean();
  565. $expected = <<<EXPECTED
  566. <div class="cake-debug-output">
  567. <pre class="cake-debug">
  568. &#039;&lt;div&gt;this-is-a-test&lt;/div&gt;&#039;
  569. </pre>
  570. </div>
  571. EXPECTED;
  572. $expected = sprintf($expected, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 10);
  573. $this->assertEquals($expected, $result);
  574. ob_start();
  575. debug('<div>this-is-a-test</div>', null);
  576. $result = ob_get_clean();
  577. $expectedHtml = <<<EXPECTED
  578. <div class="cake-debug-output">
  579. <span><strong>%s</strong> (line <strong>%d</strong>)</span>
  580. <pre class="cake-debug">
  581. &#039;&lt;div&gt;this-is-a-test&lt;/div&gt;&#039;
  582. </pre>
  583. </div>
  584. EXPECTED;
  585. $expectedText = <<<EXPECTED
  586. %s (line %d)
  587. ########## DEBUG ##########
  588. '<div>this-is-a-test</div>'
  589. ###########################
  590. EXPECTED;
  591. if (php_sapi_name() === 'cli') {
  592. $expected = sprintf($expectedText, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 18);
  593. } else {
  594. $expected = sprintf($expectedHtml, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 19);
  595. }
  596. $this->assertEquals($expected, $result);
  597. ob_start();
  598. debug('<div>this-is-a-test</div>', null, false);
  599. $result = ob_get_clean();
  600. $expectedHtml = <<<EXPECTED
  601. <div class="cake-debug-output">
  602. <pre class="cake-debug">
  603. &#039;&lt;div&gt;this-is-a-test&lt;/div&gt;&#039;
  604. </pre>
  605. </div>
  606. EXPECTED;
  607. $expectedText = <<<EXPECTED
  608. ########## DEBUG ##########
  609. '<div>this-is-a-test</div>'
  610. ###########################
  611. EXPECTED;
  612. if (php_sapi_name() === 'cli') {
  613. $expected = sprintf($expectedText, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 18);
  614. } else {
  615. $expected = sprintf($expectedHtml, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 19);
  616. }
  617. $this->assertEquals($expected, $result);
  618. ob_start();
  619. debug('<div>this-is-a-test</div>', false);
  620. $result = ob_get_clean();
  621. $expected = <<<EXPECTED
  622. %s (line %d)
  623. ########## DEBUG ##########
  624. '<div>this-is-a-test</div>'
  625. ###########################
  626. EXPECTED;
  627. $expected = sprintf($expected, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 9);
  628. $this->assertEquals($expected, $result);
  629. ob_start();
  630. debug('<div>this-is-a-test</div>', false, true);
  631. $result = ob_get_clean();
  632. $expected = <<<EXPECTED
  633. %s (line %d)
  634. ########## DEBUG ##########
  635. '<div>this-is-a-test</div>'
  636. ###########################
  637. EXPECTED;
  638. $expected = sprintf($expected, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 9);
  639. $this->assertEquals($expected, $result);
  640. ob_start();
  641. debug('<div>this-is-a-test</div>', false, false);
  642. $result = ob_get_clean();
  643. $expected = <<<EXPECTED
  644. ########## DEBUG ##########
  645. '<div>this-is-a-test</div>'
  646. ###########################
  647. EXPECTED;
  648. $expected = sprintf($expected, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 9);
  649. $this->assertEquals($expected, $result);
  650. ob_start();
  651. debug(false, false, false);
  652. $result = ob_get_clean();
  653. $expected = <<<EXPECTED
  654. ########## DEBUG ##########
  655. false
  656. ###########################
  657. EXPECTED;
  658. $expected = sprintf($expected, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 9);
  659. $this->assertEquals($expected, $result);
  660. }
  661. /**
  662. * test pr()
  663. *
  664. * @return void
  665. */
  666. public function testPr() {
  667. $this->skipIf(php_sapi_name() == 'cli', 'Skipping web test in cli mode');
  668. ob_start();
  669. pr('this is a test');
  670. $result = ob_get_clean();
  671. $expected = "<pre>this is a test</pre>";
  672. $this->assertEquals($expected, $result);
  673. ob_start();
  674. pr(array('this' => 'is', 'a' => 'test'));
  675. $result = ob_get_clean();
  676. $expected = "<pre>Array\n(\n [this] => is\n [a] => test\n)\n</pre>";
  677. $this->assertEquals($expected, $result);
  678. }
  679. /**
  680. * test pr()
  681. *
  682. * @return void
  683. */
  684. public function testPrCli() {
  685. $this->skipIf(php_sapi_name() != 'cli', 'Skipping cli test in web mode');
  686. ob_start();
  687. pr('this is a test');
  688. $result = ob_get_clean();
  689. $expected = "\nthis is a test\n";
  690. $this->assertEquals($expected, $result);
  691. ob_start();
  692. pr(array('this' => 'is', 'a' => 'test'));
  693. $result = ob_get_clean();
  694. $expected = "\nArray\n(\n [this] => is\n [a] => test\n)\n\n";
  695. $this->assertEquals($expected, $result);
  696. }
  697. /**
  698. * test pluginSplit
  699. *
  700. * @return void
  701. */
  702. public function testPluginSplit() {
  703. $result = pluginSplit('Something.else');
  704. $this->assertEquals(array('Something', 'else'), $result);
  705. $result = pluginSplit('Something.else.more.dots');
  706. $this->assertEquals(array('Something', 'else.more.dots'), $result);
  707. $result = pluginSplit('Somethingelse');
  708. $this->assertEquals(array(null, 'Somethingelse'), $result);
  709. $result = pluginSplit('Something.else', true);
  710. $this->assertEquals(array('Something.', 'else'), $result);
  711. $result = pluginSplit('Something.else.more.dots', true);
  712. $this->assertEquals(array('Something.', 'else.more.dots'), $result);
  713. $result = pluginSplit('Post', false, 'Blog');
  714. $this->assertEquals(array('Blog', 'Post'), $result);
  715. $result = pluginSplit('Blog.Post', false, 'Ultimate');
  716. $this->assertEquals(array('Blog', 'Post'), $result);
  717. }
  718. /**
  719. * test namespaceSplit
  720. *
  721. * @return void
  722. */
  723. public function testNamespaceSplit() {
  724. $result = namespaceSplit('Something');
  725. $this->assertEquals(array('', 'Something'), $result);
  726. $result = namespaceSplit('\Something');
  727. $this->assertEquals(array('', 'Something'), $result);
  728. $result = namespaceSplit('Cake\Something');
  729. $this->assertEquals(array('Cake', 'Something'), $result);
  730. $result = namespaceSplit('Cake\Test\Something');
  731. $this->assertEquals(array('Cake\Test', 'Something'), $result);
  732. }
  733. }