BasicsTest.php 28 KB

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