BasicsTest.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815
  1. <?php
  2. /**
  3. * BasicsTest file
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
  8. * Copyright 2005-2011, 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-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://book.cakephp.org/view/1196/Testing 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. /**
  22. * BasicsTest class
  23. *
  24. * @package Cake.Test.Case
  25. */
  26. class BasicsTest extends CakeTestCase {
  27. /**
  28. * setUp method
  29. *
  30. * @return void
  31. */
  32. public function setUp() {
  33. App::build(array(
  34. 'locales' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Locale' . DS)
  35. ));
  36. $this->_language = Configure::read('Config.language');
  37. }
  38. /**
  39. * tearDown method
  40. *
  41. * @return void
  42. */
  43. public function tearDown() {
  44. App::build();
  45. Configure::write('Config.language', $this->_language);
  46. }
  47. /**
  48. * test the array_diff_key compatibility function.
  49. *
  50. * @return void
  51. */
  52. public function testArrayDiffKey() {
  53. $one = array('one' => 1, 'two' => 2, 'three' => 3);
  54. $two = array('one' => 'one', 'two' => 'two');
  55. $result = array_diff_key($one, $two);
  56. $expected = array('three' => 3);
  57. $this->assertEqual($expected, $result);
  58. $one = array('one' => array('value', 'value-two'), 'two' => 2, 'three' => 3);
  59. $two = array('two' => 'two');
  60. $result = array_diff_key($one, $two);
  61. $expected = array('one' => array('value', 'value-two'), 'three' => 3);
  62. $this->assertEqual($expected, $result);
  63. $one = array('one' => null, 'two' => 2, 'three' => '', 'four' => 0);
  64. $two = array('two' => 'two');
  65. $result = array_diff_key($one, $two);
  66. $expected = array('one' => null, 'three' => '', 'four' => 0);
  67. $this->assertEqual($expected, $result);
  68. $one = array('minYear' => null, 'maxYear' => null, 'separator' => '-', 'interval' => 1, 'monthNames' => true);
  69. $two = array('minYear' => null, 'maxYear' => null, 'separator' => '-', 'interval' => 1, 'monthNames' => true);
  70. $result = array_diff_key($one, $two);
  71. $this->assertEqual($result, array());
  72. }
  73. /**
  74. * testHttpBase method
  75. *
  76. * @return void
  77. */
  78. public function testEnv() {
  79. $this->skipIf(!function_exists('ini_get') || ini_get('safe_mode') === '1', 'Safe mode is on.');
  80. $__SERVER = $_SERVER;
  81. $__ENV = $_ENV;
  82. $_SERVER['HTTP_HOST'] = 'localhost';
  83. $this->assertEqual(env('HTTP_BASE'), '.localhost');
  84. $_SERVER['HTTP_HOST'] = 'com.ar';
  85. $this->assertEqual(env('HTTP_BASE'), '.com.ar');
  86. $_SERVER['HTTP_HOST'] = 'example.ar';
  87. $this->assertEqual(env('HTTP_BASE'), '.example.ar');
  88. $_SERVER['HTTP_HOST'] = 'example.com';
  89. $this->assertEqual(env('HTTP_BASE'), '.example.com');
  90. $_SERVER['HTTP_HOST'] = 'www.example.com';
  91. $this->assertEqual(env('HTTP_BASE'), '.example.com');
  92. $_SERVER['HTTP_HOST'] = 'subdomain.example.com';
  93. $this->assertEqual(env('HTTP_BASE'), '.example.com');
  94. $_SERVER['HTTP_HOST'] = 'example.com.ar';
  95. $this->assertEqual(env('HTTP_BASE'), '.example.com.ar');
  96. $_SERVER['HTTP_HOST'] = 'www.example.com.ar';
  97. $this->assertEqual(env('HTTP_BASE'), '.example.com.ar');
  98. $_SERVER['HTTP_HOST'] = 'subdomain.example.com.ar';
  99. $this->assertEqual(env('HTTP_BASE'), '.example.com.ar');
  100. $_SERVER['HTTP_HOST'] = 'double.subdomain.example.com';
  101. $this->assertEqual(env('HTTP_BASE'), '.subdomain.example.com');
  102. $_SERVER['HTTP_HOST'] = 'double.subdomain.example.com.ar';
  103. $this->assertEqual(env('HTTP_BASE'), '.subdomain.example.com.ar');
  104. $_SERVER = $_ENV = array();
  105. $_SERVER['SCRIPT_NAME'] = '/a/test/test.php';
  106. $this->assertEqual(env('SCRIPT_NAME'), '/a/test/test.php');
  107. $_SERVER = $_ENV = array();
  108. $_ENV['CGI_MODE'] = 'BINARY';
  109. $_ENV['SCRIPT_URL'] = '/a/test/test.php';
  110. $this->assertEqual(env('SCRIPT_NAME'), '/a/test/test.php');
  111. $_SERVER = $_ENV = array();
  112. $this->assertFalse(env('HTTPS'));
  113. $_SERVER['HTTPS'] = 'on';
  114. $this->assertTrue(env('HTTPS'));
  115. $_SERVER['HTTPS'] = '1';
  116. $this->assertTrue(env('HTTPS'));
  117. $_SERVER['HTTPS'] = 'I am not empty';
  118. $this->assertTrue(env('HTTPS'));
  119. $_SERVER['HTTPS'] = 1;
  120. $this->assertTrue(env('HTTPS'));
  121. $_SERVER['HTTPS'] = 'off';
  122. $this->assertFalse(env('HTTPS'));
  123. $_SERVER['HTTPS'] = false;
  124. $this->assertFalse(env('HTTPS'));
  125. $_SERVER['HTTPS'] = '';
  126. $this->assertFalse(env('HTTPS'));
  127. $_SERVER = array();
  128. $_ENV['SCRIPT_URI'] = 'https://domain.test/a/test.php';
  129. $this->assertTrue(env('HTTPS'));
  130. $_ENV['SCRIPT_URI'] = 'http://domain.test/a/test.php';
  131. $this->assertFalse(env('HTTPS'));
  132. $_SERVER = $_ENV = array();
  133. $this->assertNull(env('TEST_ME'));
  134. $_ENV['TEST_ME'] = 'a';
  135. $this->assertEqual(env('TEST_ME'), 'a');
  136. $_SERVER['TEST_ME'] = 'b';
  137. $this->assertEqual(env('TEST_ME'), 'b');
  138. unset($_ENV['TEST_ME']);
  139. $this->assertEqual(env('TEST_ME'), 'b');
  140. $_SERVER = $__SERVER;
  141. $_ENV = $__ENV;
  142. }
  143. /**
  144. * Test h()
  145. *
  146. * @return void
  147. */
  148. public function testH() {
  149. $string = '<foo>';
  150. $result = h($string);
  151. $this->assertEqual('&lt;foo&gt;', $result);
  152. $in = array('this & that', '<p>Which one</p>');
  153. $result = h($in);
  154. $expected = array('this &amp; that', '&lt;p&gt;Which one&lt;/p&gt;');
  155. $this->assertEqual($expected, $result);
  156. $string = '<foo> & &nbsp;';
  157. $result = h($string);
  158. $this->assertEqual('&lt;foo&gt; &amp; &amp;nbsp;', $result);
  159. $string = '<foo> & &nbsp;';
  160. $result = h($string, false);
  161. $this->assertEqual('&lt;foo&gt; &amp; &nbsp;', $result);
  162. $string = '<foo> & &nbsp;';
  163. $result = h($string, 'UTF-8');
  164. $this->assertEqual('&lt;foo&gt; &amp; &amp;nbsp;', $result);
  165. $arr = array('<foo>', '&nbsp;');
  166. $result = h($arr);
  167. $expected = array(
  168. '&lt;foo&gt;',
  169. '&amp;nbsp;'
  170. );
  171. $this->assertEqual($expected, $result);
  172. $arr = array('<foo>', '&nbsp;');
  173. $result = h($arr, false);
  174. $expected = array(
  175. '&lt;foo&gt;',
  176. '&nbsp;'
  177. );
  178. $this->assertEqual($expected, $result);
  179. $arr = array('f' => '<foo>', 'n' => '&nbsp;');
  180. $result = h($arr, false);
  181. $expected = array(
  182. 'f' => '&lt;foo&gt;',
  183. 'n' => '&nbsp;'
  184. );
  185. $this->assertEqual($expected, $result);
  186. }
  187. /**
  188. * Test am()
  189. *
  190. * @return void
  191. */
  192. public function testAm() {
  193. $result = am(array('one', 'two'), 2, 3, 4);
  194. $expected = array('one', 'two', 2, 3, 4);
  195. $this->assertEqual($expected, $result);
  196. $result = am(array('one' => array(2, 3), 'two' => array('foo')), array('one' => array(4, 5)));
  197. $expected = array('one' => array(4, 5),'two' => array('foo'));
  198. $this->assertEqual($expected, $result);
  199. }
  200. /**
  201. * test cache()
  202. *
  203. * @return void
  204. */
  205. public function testCache() {
  206. $_cacheDisable = Configure::read('Cache.disable');
  207. $this->skipIf($_cacheDisable, 'Cache is disabled, skipping cache() tests.');
  208. Configure::write('Cache.disable', true);
  209. $result = cache('basics_test', 'simple cache write');
  210. $this->assertNull($result);
  211. $result = cache('basics_test');
  212. $this->assertNull($result);
  213. Configure::write('Cache.disable', false);
  214. $result = cache('basics_test', 'simple cache write');
  215. $this->assertTrue((boolean)$result);
  216. $this->assertTrue(file_exists(CACHE . 'basics_test'));
  217. $result = cache('basics_test');
  218. $this->assertEqual($result, 'simple cache write');
  219. @unlink(CACHE . 'basics_test');
  220. cache('basics_test', 'expired', '+1 second');
  221. sleep(2);
  222. $result = cache('basics_test', null, '+1 second');
  223. $this->assertNull($result);
  224. Configure::write('Cache.disable', $_cacheDisable);
  225. }
  226. /**
  227. * test clearCache()
  228. *
  229. * @return void
  230. */
  231. public function testClearCache() {
  232. $cacheOff = Configure::read('Cache.disable');
  233. $this->skipIf($cacheOff, 'Cache is disabled, skipping clearCache() tests.');
  234. cache('views' . DS . 'basics_test.cache', 'simple cache write');
  235. $this->assertTrue(file_exists(CACHE . 'views' . DS . 'basics_test.cache'));
  236. cache('views' . DS . 'basics_test_2.cache', 'simple cache write 2');
  237. $this->assertTrue(file_exists(CACHE . 'views' . DS . 'basics_test_2.cache'));
  238. cache('views' . DS . 'basics_test_3.cache', 'simple cache write 3');
  239. $this->assertTrue(file_exists(CACHE . 'views' . DS . 'basics_test_3.cache'));
  240. $result = clearCache(array('basics_test', 'basics_test_2'), 'views', '.cache');
  241. $this->assertTrue($result);
  242. $this->assertFalse(file_exists(CACHE . 'views' . DS . 'basics_test.cache'));
  243. $this->assertFalse(file_exists(CACHE . 'views' . DS . 'basics_test.cache'));
  244. $this->assertTrue(file_exists(CACHE . 'views' . DS . 'basics_test_3.cache'));
  245. $result = clearCache(null, 'views', '.cache');
  246. $this->assertTrue($result);
  247. $this->assertFalse(file_exists(CACHE . 'views' . DS . 'basics_test_3.cache'));
  248. // Different path from views and with prefix
  249. cache('models' . DS . 'basics_test.cache', 'simple cache write');
  250. $this->assertTrue(file_exists(CACHE . 'models' . DS . 'basics_test.cache'));
  251. cache('models' . DS . 'basics_test_2.cache', 'simple cache write 2');
  252. $this->assertTrue(file_exists(CACHE . 'models' . DS . 'basics_test_2.cache'));
  253. cache('models' . DS . 'basics_test_3.cache', 'simple cache write 3');
  254. $this->assertTrue(file_exists(CACHE . 'models' . DS . 'basics_test_3.cache'));
  255. $result = clearCache('basics', 'models', '.cache');
  256. $this->assertTrue($result);
  257. $this->assertFalse(file_exists(CACHE . 'models' . DS . 'basics_test.cache'));
  258. $this->assertFalse(file_exists(CACHE . 'models' . DS . 'basics_test_2.cache'));
  259. $this->assertFalse(file_exists(CACHE . 'models' . DS . 'basics_test_3.cache'));
  260. // checking if empty files were not removed
  261. $emptyExists = file_exists(CACHE . 'views' . DS . 'empty');
  262. if (!$emptyExists) {
  263. cache('views' . DS . 'empty', '');
  264. }
  265. cache('views' . DS . 'basics_test.php', 'simple cache write');
  266. $this->assertTrue(file_exists(CACHE . 'views' . DS . 'basics_test.php'));
  267. $this->assertTrue(file_exists(CACHE . 'views' . DS . 'empty'));
  268. $result = clearCache();
  269. $this->assertTrue($result);
  270. $this->assertTrue(file_exists(CACHE . 'views' . DS . 'empty'));
  271. $this->assertFalse(file_exists(CACHE . 'views' . DS . 'basics_test.php'));
  272. if (!$emptyExists) {
  273. unlink(CACHE . 'views' . DS . 'empty');
  274. }
  275. }
  276. /**
  277. * test __()
  278. *
  279. * @return void
  280. */
  281. public function test__() {
  282. Configure::write('Config.language', 'rule_1_po');
  283. $result = __('Plural Rule 1');
  284. $expected = 'Plural Rule 1 (translated)';
  285. $this->assertEqual($expected, $result);
  286. $result = __('Plural Rule 1 (from core)');
  287. $expected = 'Plural Rule 1 (from core translated)';
  288. $this->assertEqual($expected, $result);
  289. $result = __('Some string with %s', 'arguments');
  290. $expected = 'Some string with arguments';
  291. $this->assertEqual($expected, $result);
  292. $result = __('Some string with %s %s', 'multiple', 'arguments');
  293. $expected = 'Some string with multiple arguments';
  294. $this->assertEqual($expected, $result);
  295. $result = __('Some string with %s %s', array('multiple', 'arguments'));
  296. $expected = 'Some string with multiple arguments';
  297. $this->assertEqual($expected, $result);
  298. $result = __('Testing %2$s %1$s', 'order', 'different');
  299. $expected = 'Testing different order';
  300. $this->assertEqual($expected, $result);
  301. $result = __('Testing %2$s %1$s', array('order', 'different'));
  302. $expected = 'Testing different order';
  303. $this->assertEqual($expected, $result);
  304. $result = __('Testing %.2f number', 1.2345);
  305. $expected = 'Testing 1.23 number';
  306. $this->assertEqual($expected, $result);
  307. }
  308. /**
  309. * test __n()
  310. *
  311. * @return void
  312. */
  313. public function test__n() {
  314. Configure::write('Config.language', 'rule_1_po');
  315. $result = __n('%d = 1', '%d = 0 or > 1', 0);
  316. $expected = '%d = 0 or > 1 (translated)';
  317. $this->assertEqual($expected, $result);
  318. $result = __n('%d = 1', '%d = 0 or > 1', 1);
  319. $expected = '%d = 1 (translated)';
  320. $this->assertEqual($expected, $result);
  321. $result = __n('%d = 1 (from core)', '%d = 0 or > 1 (from core)', 2);
  322. $expected = '%d = 0 or > 1 (from core translated)';
  323. $this->assertEqual($expected, $result);
  324. $result = __n('%d item.', '%d items.', 1, 1);
  325. $expected = '1 item.';
  326. $this->assertEqual($expected, $result);
  327. $result = __n('%d item for id %s', '%d items for id %s', 2, 2, '1234');
  328. $expected = '2 items for id 1234';
  329. $this->assertEqual($expected, $result);
  330. $result = __n('%d item for id %s', '%d items for id %s', 2, array(2, '1234'));
  331. $expected = '2 items for id 1234';
  332. $this->assertEqual($expected, $result);
  333. }
  334. /**
  335. * test __d()
  336. *
  337. * @return void
  338. */
  339. public function test__d() {
  340. Configure::write('Config.language', 'rule_1_po');
  341. $result = __d('default', 'Plural Rule 1');
  342. $expected = 'Plural Rule 1 (translated)';
  343. $this->assertEqual($expected, $result);
  344. $result = __d('core', 'Plural Rule 1');
  345. $expected = 'Plural Rule 1';
  346. $this->assertEqual($expected, $result);
  347. $result = __d('core', 'Plural Rule 1 (from core)');
  348. $expected = 'Plural Rule 1 (from core translated)';
  349. $this->assertEqual($expected, $result);
  350. $result = __d('core', 'Some string with %s', 'arguments');
  351. $expected = 'Some string with arguments';
  352. $this->assertEqual($expected, $result);
  353. $result = __d('core', 'Some string with %s %s', 'multiple', 'arguments');
  354. $expected = 'Some string with multiple arguments';
  355. $this->assertEqual($expected, $result);
  356. $result = __d('core', 'Some string with %s %s', array('multiple', 'arguments'));
  357. $expected = 'Some string with multiple arguments';
  358. $this->assertEqual($expected, $result);
  359. }
  360. /**
  361. * test __dn()
  362. *
  363. * @return void
  364. */
  365. public function test__dn() {
  366. Configure::write('Config.language', 'rule_1_po');
  367. $result = __dn('default', '%d = 1', '%d = 0 or > 1', 0);
  368. $expected = '%d = 0 or > 1 (translated)';
  369. $this->assertEqual($expected, $result);
  370. $result = __dn('core', '%d = 1', '%d = 0 or > 1', 0);
  371. $expected = '%d = 0 or > 1';
  372. $this->assertEqual($expected, $result);
  373. $result = __dn('core', '%d = 1 (from core)', '%d = 0 or > 1 (from core)', 0);
  374. $expected = '%d = 0 or > 1 (from core translated)';
  375. $this->assertEqual($expected, $result);
  376. $result = __dn('default', '%d = 1', '%d = 0 or > 1', 1);
  377. $expected = '%d = 1 (translated)';
  378. $this->assertEqual($expected, $result);
  379. $result = __dn('core', '%d item.', '%d items.', 1, 1);
  380. $expected = '1 item.';
  381. $this->assertEqual($expected, $result);
  382. $result = __dn('core', '%d item for id %s', '%d items for id %s', 2, 2, '1234');
  383. $expected = '2 items for id 1234';
  384. $this->assertEqual($expected, $result);
  385. $result = __dn('core', '%d item for id %s', '%d items for id %s', 2, array(2, '1234'));
  386. $expected = '2 items for id 1234';
  387. $this->assertEqual($expected, $result);
  388. }
  389. /**
  390. * test __c()
  391. *
  392. * @return void
  393. */
  394. public function test__c() {
  395. Configure::write('Config.language', 'rule_1_po');
  396. $result = __c('Plural Rule 1', 6);
  397. $expected = 'Plural Rule 1 (translated)';
  398. $this->assertEqual($expected, $result);
  399. $result = __c('Plural Rule 1 (from core)', 6);
  400. $expected = 'Plural Rule 1 (from core translated)';
  401. $this->assertEqual($expected, $result);
  402. $result = __c('Some string with %s', 6, 'arguments');
  403. $expected = 'Some string with arguments';
  404. $this->assertEqual($expected, $result);
  405. $result = __c('Some string with %s %s', 6, 'multiple', 'arguments');
  406. $expected = 'Some string with multiple arguments';
  407. $this->assertEqual($expected, $result);
  408. $result = __c('Some string with %s %s', 6, array('multiple', 'arguments'));
  409. $expected = 'Some string with multiple arguments';
  410. $this->assertEqual($expected, $result);
  411. }
  412. /**
  413. * test __dc()
  414. *
  415. * @return void
  416. */
  417. public function test__dc() {
  418. Configure::write('Config.language', 'rule_1_po');
  419. $result = __dc('default', 'Plural Rule 1', 6);
  420. $expected = 'Plural Rule 1 (translated)';
  421. $this->assertEqual($expected, $result);
  422. $result = __dc('default', 'Plural Rule 1 (from core)', 6);
  423. $expected = 'Plural Rule 1 (from core translated)';
  424. $this->assertEqual($expected, $result);
  425. $result = __dc('core', 'Plural Rule 1', 6);
  426. $expected = 'Plural Rule 1';
  427. $this->assertEqual($expected, $result);
  428. $result = __dc('core', 'Plural Rule 1 (from core)', 6);
  429. $expected = 'Plural Rule 1 (from core translated)';
  430. $this->assertEqual($expected, $result);
  431. $result = __dc('core', 'Some string with %s', 6, 'arguments');
  432. $expected = 'Some string with arguments';
  433. $this->assertEqual($expected, $result);
  434. $result = __dc('core', 'Some string with %s %s', 6, 'multiple', 'arguments');
  435. $expected = 'Some string with multiple arguments';
  436. $this->assertEqual($expected, $result);
  437. $result = __dc('core', 'Some string with %s %s', 6, array('multiple', 'arguments'));
  438. $expected = 'Some string with multiple arguments';
  439. $this->assertEqual($expected, $result);
  440. }
  441. /**
  442. * test __dcn()
  443. *
  444. * @return void
  445. */
  446. public function test__dcn() {
  447. Configure::write('Config.language', 'rule_1_po');
  448. $result = __dcn('default', '%d = 1', '%d = 0 or > 1', 0, 6);
  449. $expected = '%d = 0 or > 1 (translated)';
  450. $this->assertEqual($expected, $result);
  451. $result = __dcn('default', '%d = 1 (from core)', '%d = 0 or > 1 (from core)', 1, 6);
  452. $expected = '%d = 1 (from core translated)';
  453. $this->assertEqual($expected, $result);
  454. $result = __dcn('core', '%d = 1', '%d = 0 or > 1', 0, 6);
  455. $expected = '%d = 0 or > 1';
  456. $this->assertEqual($expected, $result);
  457. $result = __dcn('core', '%d item.', '%d items.', 1, 6, 1);
  458. $expected = '1 item.';
  459. $this->assertEqual($expected, $result);
  460. $result = __dcn('core', '%d item for id %s', '%d items for id %s', 2, 6, 2, '1234');
  461. $expected = '2 items for id 1234';
  462. $this->assertEqual($expected, $result);
  463. $result = __dcn('core', '%d item for id %s', '%d items for id %s', 2, 6, array(2, '1234'));
  464. $expected = '2 items for id 1234';
  465. $this->assertEqual($expected, $result);
  466. }
  467. /**
  468. * test LogError()
  469. *
  470. * @return void
  471. */
  472. public function testLogError() {
  473. @unlink(LOGS . 'error.log');
  474. LogError('Testing LogError() basic function');
  475. LogError("Testing with\nmulti-line\nstring");
  476. $result = file_get_contents(LOGS . 'error.log');
  477. $this->assertPattern('/Error: Testing LogError\(\) basic function/', $result);
  478. $this->assertNoPattern("/Error: Testing with\nmulti-line\nstring/", $result);
  479. $this->assertPattern('/Error: Testing with multi-line string/', $result);
  480. }
  481. /**
  482. * test fileExistsInPath()
  483. *
  484. * @return void
  485. */
  486. public function testFileExistsInPath() {
  487. $this->skipUnless(function_exists('ini_set'), '%s ini_set function not available');
  488. $_includePath = ini_get('include_path');
  489. $path = TMP . 'basics_test';
  490. $folder1 = $path . DS . 'folder1';
  491. $folder2 = $path . DS . 'folder2';
  492. $file1 = $path . DS . 'file1.php';
  493. $file2 = $folder1 . DS . 'file2.php';
  494. $file3 = $folder1 . DS . 'file3.php';
  495. $file4 = $folder2 . DS . 'file4.php';
  496. new Folder($path, true);
  497. new Folder($folder1, true);
  498. new Folder($folder2, true);
  499. touch($file1);
  500. touch($file2);
  501. touch($file3);
  502. touch($file4);
  503. ini_set('include_path', $path . PATH_SEPARATOR . $folder1);
  504. $this->assertEqual(fileExistsInPath('file1.php'), $file1);
  505. $this->assertEqual(fileExistsInPath('file2.php'), $file2);
  506. $this->assertEqual(fileExistsInPath('folder1' . DS . 'file2.php'), $file2);
  507. $this->assertEqual(fileExistsInPath($file2), $file2);
  508. $this->assertEqual(fileExistsInPath('file3.php'), $file3);
  509. $this->assertEqual(fileExistsInPath($file4), $file4);
  510. $this->assertFalse(fileExistsInPath('file1'));
  511. $this->assertFalse(fileExistsInPath('file4.php'));
  512. $Folder = new Folder($path);
  513. $Folder->delete();
  514. ini_set('include_path', $_includePath);
  515. }
  516. /**
  517. * test convertSlash()
  518. *
  519. * @return void
  520. */
  521. public function testConvertSlash() {
  522. $result = convertSlash('\path\to\location\\');
  523. $expected = '\path\to\location\\';
  524. $this->assertEqual($expected, $result);
  525. $result = convertSlash('/path/to/location/');
  526. $expected = 'path_to_location';
  527. $this->assertEqual($expected, $result);
  528. }
  529. /**
  530. * test debug()
  531. *
  532. * @return void
  533. */
  534. public function testDebug() {
  535. ob_start();
  536. debug('this-is-a-test');
  537. $result = ob_get_clean();
  538. $pattern = '/(.+?Test(\/|\\\)Case(\/|\\\)BasicsTest\.php|';
  539. $pattern .= preg_quote(substr(__FILE__, 1), '/') . ')';
  540. $pattern .= '.*line.*' . (__LINE__ - 4) . '.*this-is-a-test.*/s';
  541. $this->assertRegExp($pattern, $result);
  542. ob_start();
  543. debug('<div>this-is-a-test</div>', true);
  544. $result = ob_get_clean();
  545. $pattern = '/(.+?Test(\/|\\\)Case(\/|\\\)BasicsTest\.php|';
  546. $pattern .= preg_quote(substr(__FILE__, 1), '/') . ')';
  547. $pattern .= '.*line.*' . (__LINE__ -4) . '.*&lt;div&gt;this-is-a-test&lt;\/div&gt;.*/s';
  548. $this->assertRegExp($pattern, $result);
  549. ob_start();
  550. debug('<div>this-is-a-test</div>', false);
  551. $result = ob_get_clean();
  552. $pattern = '/(.+?Test(\/|\\\)Case(\/|\\\)BasicsTest\.php|';
  553. $pattern .= preg_quote(substr(__FILE__, 1), '/') . ')';
  554. $pattern .= '.*line.*' . (__LINE__ - 4) . '.*\<div\>this-is-a-test\<\/div\>.*/s';
  555. $this->assertRegExp($pattern, $result);
  556. }
  557. /**
  558. * test pr()
  559. *
  560. * @return void
  561. */
  562. public function testPr() {
  563. ob_start();
  564. pr('this is a test');
  565. $result = ob_get_clean();
  566. $expected = "<pre>this is a test</pre>";
  567. $this->assertEqual($expected, $result);
  568. ob_start();
  569. pr(array('this' => 'is', 'a' => 'test'));
  570. $result = ob_get_clean();
  571. $expected = "<pre>Array\n(\n [this] => is\n [a] => test\n)\n</pre>";
  572. $this->assertEqual($expected, $result);
  573. }
  574. /**
  575. * test stripslashes_deep()
  576. *
  577. * @return void
  578. */
  579. public function testStripslashesDeep() {
  580. $this->skipIf(ini_get('magic_quotes_sybase') === '1', 'magic_quotes_sybase is on.');
  581. $this->assertEqual(stripslashes_deep("tes\'t"), "tes't");
  582. $this->assertEqual(stripslashes_deep('tes\\' . chr(0) .'t'), 'tes' . chr(0) .'t');
  583. $this->assertEqual(stripslashes_deep('tes\"t'), 'tes"t');
  584. $this->assertEqual(stripslashes_deep("tes\'t"), "tes't");
  585. $this->assertEqual(stripslashes_deep('te\\st'), 'test');
  586. $nested = array(
  587. 'a' => "tes\'t",
  588. 'b' => 'tes\\' . chr(0) .'t',
  589. 'c' => array(
  590. 'd' => 'tes\"t',
  591. 'e' => "te\'s\'t",
  592. array('f' => "tes\'t")
  593. ),
  594. 'g' => 'te\\st'
  595. );
  596. $expected = array(
  597. 'a' => "tes't",
  598. 'b' => 'tes' . chr(0) .'t',
  599. 'c' => array(
  600. 'd' => 'tes"t',
  601. 'e' => "te's't",
  602. array('f' => "tes't")
  603. ),
  604. 'g' => 'test'
  605. );
  606. $this->assertEqual(stripslashes_deep($nested), $expected);
  607. }
  608. /**
  609. * test stripslashes_deep() with magic_quotes_sybase on
  610. *
  611. * @return void
  612. */
  613. public function testStripslashesDeepSybase() {
  614. $this->skipUnless(ini_get('magic_quotes_sybase') === '1', 'magic_quotes_sybase is off');
  615. $this->assertEqual(stripslashes_deep("tes\'t"), "tes\'t");
  616. $nested = array(
  617. 'a' => "tes't",
  618. 'b' => "tes''t",
  619. 'c' => array(
  620. 'd' => "tes'''t",
  621. 'e' => "tes''''t",
  622. array('f' => "tes''t")
  623. ),
  624. 'g' => "te'''''st"
  625. );
  626. $expected = array(
  627. 'a' => "tes't",
  628. 'b' => "tes't",
  629. 'c' => array(
  630. 'd' => "tes''t",
  631. 'e' => "tes''t",
  632. array('f' => "tes't")
  633. ),
  634. 'g' => "te'''st"
  635. );
  636. $this->assertEqual(stripslashes_deep($nested), $expected);
  637. }
  638. /**
  639. * test pluginSplit
  640. *
  641. * @return void
  642. */
  643. public function testPluginSplit() {
  644. $result = pluginSplit('Something.else');
  645. $this->assertEqual($result, array('Something', 'else'));
  646. $result = pluginSplit('Something.else.more.dots');
  647. $this->assertEqual($result, array('Something', 'else.more.dots'));
  648. $result = pluginSplit('Somethingelse');
  649. $this->assertEqual($result, array(null, 'Somethingelse'));
  650. $result = pluginSplit('Something.else', true);
  651. $this->assertEqual($result, array('Something.', 'else'));
  652. $result = pluginSplit('Something.else.more.dots', true);
  653. $this->assertEqual($result, array('Something.', 'else.more.dots'));
  654. $result = pluginSplit('Post', false, 'Blog');
  655. $this->assertEqual($result, array('Blog', 'Post'));
  656. $result = pluginSplit('Blog.Post', false, 'Ultimate');
  657. $this->assertEqual($result, array('Blog', 'Post'));
  658. }
  659. }