7 * CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
8 * Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
10 * Licensed under The Open Group Test Suite License
11 * Redistributions of files must retain the above copyright notice.
13 * @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
14 * @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
16 * @subpackage cake.tests.cases
17 * @since CakePHP(tm) v 1.2.0.4206
18 * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
20 require_once CAKE
. 'basics.php';
21 App
::import('Core', 'Folder');
27 * @subpackage cake.tests.cases
29 class BasicsTest
extends CakeTestCase
{
39 'locales' => array(TEST_CAKE_CORE_INCLUDE_PATH
. 'tests' . DS
. 'test_app' . DS
. 'locale' . DS
)
41 $this->_language
= Configure
::read('Config.language');
52 Configure
::write('Config.language', $this->_language
);
56 * test the array_diff_key compatibility function.
61 function testArrayDiffKey() {
62 $one = array('one' => 1, 'two' => 2, 'three' => 3);
63 $two = array('one' => 'one', 'two' => 'two');
64 $result = array_diff_key($one, $two);
65 $expected = array('three' => 3);
66 $this->assertEqual($result, $expected);
68 $one = array('one' => array('value', 'value-two'), 'two' => 2, 'three' => 3);
69 $two = array('two' => 'two');
70 $result = array_diff_key($one, $two);
71 $expected = array('one' => array('value', 'value-two'), 'three' => 3);
72 $this->assertEqual($result, $expected);
74 $one = array('one' => null, 'two' => 2, 'three' => '', 'four' => 0);
75 $two = array('two' => 'two');
76 $result = array_diff_key($one, $two);
77 $expected = array('one' => null, 'three' => '', 'four' => 0);
78 $this->assertEqual($result, $expected);
80 $one = array('minYear' => null, 'maxYear' => null, 'separator' => '-', 'interval' => 1, 'monthNames' => true);
81 $two = array('minYear' => null, 'maxYear' => null, 'separator' => '-', 'interval' => 1, 'monthNames' => true);
82 $result = array_diff_key($one, $two);
83 $this->assertEqual($result, array());
93 $this->skipIf(!function_exists('ini_get') ||
ini_get('safe_mode') === '1', '%s safe mode is on');
98 $_SERVER['HTTP_HOST'] = 'localhost';
99 $this->assertEqual(env('HTTP_BASE'), '.localhost');
101 $_SERVER['HTTP_HOST'] = 'com.ar';
102 $this->assertEqual(env('HTTP_BASE'), '.com.ar');
104 $_SERVER['HTTP_HOST'] = 'example.ar';
105 $this->assertEqual(env('HTTP_BASE'), '.example.ar');
107 $_SERVER['HTTP_HOST'] = 'example.com';
108 $this->assertEqual(env('HTTP_BASE'), '.example.com');
110 $_SERVER['HTTP_HOST'] = 'www.example.com';
111 $this->assertEqual(env('HTTP_BASE'), '.example.com');
113 $_SERVER['HTTP_HOST'] = 'subdomain.example.com';
114 $this->assertEqual(env('HTTP_BASE'), '.example.com');
116 $_SERVER['HTTP_HOST'] = 'example.com.ar';
117 $this->assertEqual(env('HTTP_BASE'), '.example.com.ar');
119 $_SERVER['HTTP_HOST'] = 'www.example.com.ar';
120 $this->assertEqual(env('HTTP_BASE'), '.example.com.ar');
122 $_SERVER['HTTP_HOST'] = 'subdomain.example.com.ar';
123 $this->assertEqual(env('HTTP_BASE'), '.example.com.ar');
125 $_SERVER['HTTP_HOST'] = 'double.subdomain.example.com';
126 $this->assertEqual(env('HTTP_BASE'), '.subdomain.example.com');
128 $_SERVER['HTTP_HOST'] = 'double.subdomain.example.com.ar';
129 $this->assertEqual(env('HTTP_BASE'), '.subdomain.example.com.ar');
131 $_SERVER = $_ENV = array();
133 $_SERVER['SCRIPT_NAME'] = '/a/test/test.php';
134 $this->assertEqual(env('SCRIPT_NAME'), '/a/test/test.php');
136 $_SERVER = $_ENV = array();
138 $_ENV['CGI_MODE'] = 'BINARY';
139 $_ENV['SCRIPT_URL'] = '/a/test/test.php';
140 $this->assertEqual(env('SCRIPT_NAME'), '/a/test/test.php');
142 $_SERVER = $_ENV = array();
144 $this->assertFalse(env('HTTPS'));
146 $_SERVER['HTTPS'] = 'on';
147 $this->assertTrue(env('HTTPS'));
149 $_SERVER['HTTPS'] = '1';
150 $this->assertTrue(env('HTTPS'));
152 $_SERVER['HTTPS'] = 'I am not empty';
153 $this->assertTrue(env('HTTPS'));
155 $_SERVER['HTTPS'] = 1;
156 $this->assertTrue(env('HTTPS'));
158 $_SERVER['HTTPS'] = 'off';
159 $this->assertFalse(env('HTTPS'));
161 $_SERVER['HTTPS'] = false;
162 $this->assertFalse(env('HTTPS'));
164 $_SERVER['HTTPS'] = '';
165 $this->assertFalse(env('HTTPS'));
169 $_ENV['SCRIPT_URI'] = 'https://domain.test/a/test.php';
170 $this->assertTrue(env('HTTPS'));
172 $_ENV['SCRIPT_URI'] = 'http://domain.test/a/test.php';
173 $this->assertFalse(env('HTTPS'));
175 $_SERVER = $_ENV = array();
177 $this->assertFalse(env('TEST_ME'));
179 $_ENV['TEST_ME'] = 'a';
180 $this->assertEqual(env('TEST_ME'), 'a');
182 $_SERVER['TEST_ME'] = 'b';
183 $this->assertEqual(env('TEST_ME'), 'b');
185 unset($_ENV['TEST_ME']);
186 $this->assertEqual(env('TEST_ME'), 'b');
188 $_SERVER = $__SERVER;
199 function testUses() {
200 $this->skipIf(class_exists('Security') ||
class_exists('Sanitize'), '%s Security and/or Sanitize class already loaded');
202 $this->assertFalse(class_exists('Security'));
203 $this->assertFalse(class_exists('Sanitize'));
205 uses('Security', 'Sanitize');
207 $this->assertTrue(class_exists('Security'));
208 $this->assertTrue(class_exists('Sanitize'));
219 $result = h($string);
220 $this->assertEqual('<foo>', $result);
222 $in = array('this & that', '<p>Which one</p>');
224 $expected = array('this & that', '<p>Which one</p>');
225 $this->assertEqual($expected, $result);
235 $result = a('this', 'that', 'bar');
236 $this->assertEqual(array('this', 'that', 'bar'), $result);
246 $result = aa('a', 'b', 'c', 'd');
247 $expected = array('a' => 'b', 'c' => 'd');
248 $this->assertEqual($expected, $result);
250 $result = aa('a', 'b', 'c', 'd', 'e');
251 $expected = array('a' => 'b', 'c' => 'd', 'e' => null);
252 $this->assertEqual($result, $expected);
262 $result = am(array('one', 'two'), 2, 3, 4);
263 $expected = array('one', 'two', 2, 3, 4);
264 $this->assertEqual($result, $expected);
266 $result = am(array('one' => array(2, 3), 'two' => array('foo')), array('one' => array(4, 5)));
267 $expected = array('one' => array(4, 5),'two' => array('foo'));
268 $this->assertEqual($result, $expected);
277 function testCache() {
278 $_cacheDisable = Configure
::read('Cache.disable');
279 if ($this->skipIf($_cacheDisable, 'Cache is disabled, skipping cache() tests. %s')) {
283 Configure
::write('Cache.disable', true);
284 $result = cache('basics_test', 'simple cache write');
285 $this->assertNull($result);
287 $result = cache('basics_test');
288 $this->assertNull($result);
290 Configure
::write('Cache.disable', false);
291 $result = cache('basics_test', 'simple cache write');
292 $this->assertTrue($result);
293 $this->assertTrue(file_exists(CACHE
. 'basics_test'));
295 $result = cache('basics_test');
296 $this->assertEqual($result, 'simple cache write');
297 @unlink
(CACHE
. 'basics_test');
299 cache('basics_test', 'expired', '+1 second');
301 $result = cache('basics_test', null, '+1 second');
302 $this->assertNull($result);
304 Configure
::write('Cache.disable', $_cacheDisable);
313 function testClearCache() {
314 $cacheOff = Configure
::read('Cache.disable');
315 if ($this->skipIf($cacheOff, 'Cache is disabled, skipping clearCache() tests. %s')) {
319 cache('views' . DS
. 'basics_test.cache', 'simple cache write');
320 $this->assertTrue(file_exists(CACHE
. 'views' . DS
. 'basics_test.cache'));
322 cache('views' . DS
. 'basics_test_2.cache', 'simple cache write 2');
323 $this->assertTrue(file_exists(CACHE
. 'views' . DS
. 'basics_test_2.cache'));
325 cache('views' . DS
. 'basics_test_3.cache', 'simple cache write 3');
326 $this->assertTrue(file_exists(CACHE
. 'views' . DS
. 'basics_test_3.cache'));
328 $result = clearCache(array('basics_test', 'basics_test_2'), 'views', '.cache');
329 $this->assertTrue($result);
330 $this->assertFalse(file_exists(CACHE
. 'views' . DS
. 'basics_test.cache'));
331 $this->assertFalse(file_exists(CACHE
. 'views' . DS
. 'basics_test.cache'));
332 $this->assertTrue(file_exists(CACHE
. 'views' . DS
. 'basics_test_3.cache'));
334 $result = clearCache(null, 'views', '.cache');
335 $this->assertTrue($result);
336 $this->assertFalse(file_exists(CACHE
. 'views' . DS
. 'basics_test_3.cache'));
338 // Different path from views and with prefix
339 cache('models' . DS
. 'basics_test.cache', 'simple cache write');
340 $this->assertTrue(file_exists(CACHE
. 'models' . DS
. 'basics_test.cache'));
342 cache('models' . DS
. 'basics_test_2.cache', 'simple cache write 2');
343 $this->assertTrue(file_exists(CACHE
. 'models' . DS
. 'basics_test_2.cache'));
345 cache('models' . DS
. 'basics_test_3.cache', 'simple cache write 3');
346 $this->assertTrue(file_exists(CACHE
. 'models' . DS
. 'basics_test_3.cache'));
348 $result = clearCache('basics', 'models', '.cache');
349 $this->assertTrue($result);
350 $this->assertFalse(file_exists(CACHE
. 'models' . DS
. 'basics_test.cache'));
351 $this->assertFalse(file_exists(CACHE
. 'models' . DS
. 'basics_test_2.cache'));
352 $this->assertFalse(file_exists(CACHE
. 'models' . DS
. 'basics_test_3.cache'));
354 // checking if empty files were not removed
355 $emptyExists = file_exists(CACHE
. 'views' . DS
. 'empty');
357 cache('views' . DS
. 'empty', '');
359 cache('views' . DS
. 'basics_test.php', 'simple cache write');
360 $this->assertTrue(file_exists(CACHE
. 'views' . DS
. 'basics_test.php'));
361 $this->assertTrue(file_exists(CACHE
. 'views' . DS
. 'empty'));
363 $result = clearCache();
364 $this->assertTrue($result);
365 $this->assertTrue(file_exists(CACHE
. 'views' . DS
. 'empty'));
366 $this->assertFalse(file_exists(CACHE
. 'views' . DS
. 'basics_test.php'));
368 unlink(CACHE
. 'views' . DS
. 'empty');
379 Configure
::write('Config.language', 'rule_1_po');
381 $result = __('Plural Rule 1', true);
382 $expected = 'Plural Rule 1 (translated)';
383 $this->assertEqual($result, $expected);
385 $result = __('Plural Rule 1 (from core)', true);
386 $expected = 'Plural Rule 1 (from core translated)';
387 $this->assertEqual($result, $expected);
390 __('Plural Rule 1 (from core)');
391 $result = ob_get_clean();
392 $expected = 'Plural Rule 1 (from core translated)';
393 $this->assertEqual($result, $expected);
403 Configure
::write('Config.language', 'rule_1_po');
405 $result = __n('%d = 1', '%d = 0 or > 1', 0, true);
406 $expected = '%d = 0 or > 1 (translated)';
407 $this->assertEqual($result, $expected);
409 $result = __n('%d = 1', '%d = 0 or > 1', 1, true);
410 $expected = '%d = 1 (translated)';
411 $this->assertEqual($result, $expected);
413 $result = __n('%d = 1 (from core)', '%d = 0 or > 1 (from core)', 2, true);
414 $expected = '%d = 0 or > 1 (from core translated)';
415 $this->assertEqual($result, $expected);
418 __n('%d = 1 (from core)', '%d = 0 or > 1 (from core)', 2);
419 $result = ob_get_clean();
420 $expected = '%d = 0 or > 1 (from core translated)';
421 $this->assertEqual($result, $expected);
431 Configure
::write('Config.language', 'rule_1_po');
433 $result = __d('default', 'Plural Rule 1', true);
434 $expected = 'Plural Rule 1 (translated)';
435 $this->assertEqual($result, $expected);
437 $result = __d('core', 'Plural Rule 1', true);
438 $expected = 'Plural Rule 1';
439 $this->assertEqual($result, $expected);
441 $result = __d('core', 'Plural Rule 1 (from core)', true);
442 $expected = 'Plural Rule 1 (from core translated)';
443 $this->assertEqual($result, $expected);
446 __d('core', 'Plural Rule 1 (from core)');
447 $result = ob_get_clean();
448 $expected = 'Plural Rule 1 (from core translated)';
449 $this->assertEqual($result, $expected);
458 function test__dn() {
459 Configure
::write('Config.language', 'rule_1_po');
461 $result = __dn('default', '%d = 1', '%d = 0 or > 1', 0, true);
462 $expected = '%d = 0 or > 1 (translated)';
463 $this->assertEqual($result, $expected);
465 $result = __dn('core', '%d = 1', '%d = 0 or > 1', 0, true);
466 $expected = '%d = 0 or > 1';
467 $this->assertEqual($result, $expected);
469 $result = __dn('core', '%d = 1 (from core)', '%d = 0 or > 1 (from core)', 0, true);
470 $expected = '%d = 0 or > 1 (from core translated)';
471 $this->assertEqual($result, $expected);
473 $result = __dn('default', '%d = 1', '%d = 0 or > 1', 1, true);
474 $expected = '%d = 1 (translated)';
475 $this->assertEqual($result, $expected);
478 __dn('core', '%d = 1 (from core)', '%d = 0 or > 1 (from core)', 2);
479 $result = ob_get_clean();
480 $expected = '%d = 0 or > 1 (from core translated)';
481 $this->assertEqual($result, $expected);
491 Configure
::write('Config.language', 'rule_1_po');
493 $result = __c('Plural Rule 1', 6, true);
494 $expected = 'Plural Rule 1 (translated)';
495 $this->assertEqual($result, $expected);
497 $result = __c('Plural Rule 1 (from core)', 6, true);
498 $expected = 'Plural Rule 1 (from core translated)';
499 $this->assertEqual($result, $expected);
502 __c('Plural Rule 1 (from core)', 6);
503 $result = ob_get_clean();
504 $expected = 'Plural Rule 1 (from core translated)';
505 $this->assertEqual($result, $expected);
514 function test__dc() {
515 Configure
::write('Config.language', 'rule_1_po');
517 $result = __dc('default', 'Plural Rule 1', 6, true);
518 $expected = 'Plural Rule 1 (translated)';
519 $this->assertEqual($result, $expected);
521 $result = __dc('default', 'Plural Rule 1 (from core)', 6, true);
522 $expected = 'Plural Rule 1 (from core translated)';
523 $this->assertEqual($result, $expected);
525 $result = __dc('core', 'Plural Rule 1', 6, true);
526 $expected = 'Plural Rule 1';
527 $this->assertEqual($result, $expected);
529 $result = __dc('core', 'Plural Rule 1 (from core)', 6, true);
530 $expected = 'Plural Rule 1 (from core translated)';
531 $this->assertEqual($result, $expected);
534 __dc('default', 'Plural Rule 1 (from core)', 6);
535 $result = ob_get_clean();
536 $expected = 'Plural Rule 1 (from core translated)';
537 $this->assertEqual($result, $expected);
546 function test__dcn() {
547 Configure
::write('Config.language', 'rule_1_po');
549 $result = __dcn('default', '%d = 1', '%d = 0 or > 1', 0, 6, true);
550 $expected = '%d = 0 or > 1 (translated)';
551 $this->assertEqual($result, $expected);
553 $result = __dcn('default', '%d = 1 (from core)', '%d = 0 or > 1 (from core)', 1, 6, true);
554 $expected = '%d = 1 (from core translated)';
555 $this->assertEqual($result, $expected);
557 $result = __dcn('core', '%d = 1', '%d = 0 or > 1', 0, 6, true);
558 $expected = '%d = 0 or > 1';
559 $this->assertEqual($result, $expected);
562 __dcn('default', '%d = 1 (from core)', '%d = 0 or > 1 (from core)', 1, 6);
563 $result = ob_get_clean();
564 $expected = '%d = 1 (from core translated)';
565 $this->assertEqual($result, $expected);
574 function testLogError() {
575 @unlink
(LOGS
. 'error.log');
577 LogError('Testing LogError() basic function');
578 LogError("Testing with\nmulti-line\nstring");
580 $result = file_get_contents(LOGS
. 'error.log');
581 $this->assertPattern('/Error: Testing LogError\(\) basic function/', $result);
582 $this->assertNoPattern("/Error: Testing with\nmulti-line\nstring/", $result);
583 $this->assertPattern('/Error: Testing with multi-line string/', $result);
587 * test fileExistsInPath()
592 function testFileExistsInPath() {
593 $this->skipUnless(function_exists('ini_set'), '%s ini_set function not available');
595 $_includePath = ini_get('include_path');
597 $path = TMP
. 'basics_test';
598 $folder1 = $path . DS
. 'folder1';
599 $folder2 = $path . DS
. 'folder2';
600 $file1 = $path . DS
. 'file1.php';
601 $file2 = $folder1 . DS
. 'file2.php';
602 $file3 = $folder1 . DS
. 'file3.php';
603 $file4 = $folder2 . DS
. 'file4.php';
605 new Folder($path, true);
606 new Folder($folder1, true);
607 new Folder($folder2, true);
613 ini_set('include_path', $path . PATH_SEPARATOR
. $folder1);
615 $this->assertEqual(fileExistsInPath('file1.php'), $file1);
616 $this->assertEqual(fileExistsInPath('file2.php'), $file2);
617 $this->assertEqual(fileExistsInPath('folder1' . DS
. 'file2.php'), $file2);
618 $this->assertEqual(fileExistsInPath($file2), $file2);
619 $this->assertEqual(fileExistsInPath('file3.php'), $file3);
620 $this->assertEqual(fileExistsInPath($file4), $file4);
622 $this->assertFalse(fileExistsInPath('file1'));
623 $this->assertFalse(fileExistsInPath('file4.php'));
625 $Folder = new Folder($path);
628 ini_set('include_path', $_includePath);
632 * test convertSlash()
637 function testConvertSlash() {
638 $result = convertSlash('\path\to\location\\');
639 $expected = '\path\to\location\\';
640 $this->assertEqual($result, $expected);
642 $result = convertSlash('/path/to/location/');
643 $expected = 'path_to_location';
644 $this->assertEqual($result, $expected);
653 function testDebug() {
655 debug('this-is-a-test');
656 $result = ob_get_clean();
657 $pattern = '/.*\>(.+?tests(\/|\\\)cases(\/|\\\)basics\.test\.php|';
658 $pattern .= preg_quote(substr(__FILE__
, 1), '/') . ')';
659 $pattern .= '.*line.*' . (__LINE__
- 4) . '.*this-is-a-test.*/s';
660 $this->assertPattern($pattern, $result);
663 debug('<div>this-is-a-test</div>', true);
664 $result = ob_get_clean();
665 $pattern = '/.*\>(.+?tests(\/|\\\)cases(\/|\\\)basics\.test\.php|';
666 $pattern .= preg_quote(substr(__FILE__
, 1), '/') . ')';
667 $pattern .= '.*line.*' . (__LINE__
- 4) . '.*<div>this-is-a-test<\/div>.*/s';
668 $this->assertPattern($pattern, $result);
679 pr('this is a test');
680 $result = ob_get_clean();
681 $expected = "<pre>this is a test</pre>";
682 $this->assertEqual($result, $expected);
685 pr(array('this' => 'is', 'a' => 'test'));
686 $result = ob_get_clean();
687 $expected = "<pre>Array\n(\n [this] => is\n [a] => test\n)\n</pre>";
688 $this->assertEqual($result, $expected);
697 function testParams() {
698 $this->assertNull(params('weekend'));
699 $this->assertNull(params(array()));
700 $this->assertEqual(params(array('weekend')), array('weekend'));
702 $nested = array(array('weekend'));
703 $this->assertEqual(params($nested), array('weekend'));
705 $multiple = array(array('weekend'), 'jean-luc', 'godard');
706 $this->assertEqual(params($multiple), $multiple);
710 * test stripslashes_deep()
715 function testStripslashesDeep() {
716 $this->skipIf(ini_get('magic_quotes_sybase') === '1', '%s magic_quotes_sybase is on');
718 $this->assertEqual(stripslashes_deep("tes\'t"), "tes't");
719 $this->assertEqual(stripslashes_deep('tes\\' . chr(0) .'t'), 'tes' . chr(0) .'t');
720 $this->assertEqual(stripslashes_deep('tes\"t'), 'tes"t');
721 $this->assertEqual(stripslashes_deep("tes\'t"), "tes't");
722 $this->assertEqual(stripslashes_deep('te\\st'), 'test');
726 'b' => 'tes\\' . chr(0) .'t',
730 array('f' => "tes\'t")
736 'b' => 'tes' . chr(0) .'t',
740 array('f' => "tes't")
744 $this->assertEqual(stripslashes_deep($nested), $expected);
748 * test stripslashes_deep() with magic_quotes_sybase on
753 function testStripslashesDeepSybase() {
754 $this->skipUnless(ini_get('magic_quotes_sybase') === '1', '%s magic_quotes_sybase is off');
756 $this->assertEqual(stripslashes_deep("tes\'t"), "tes\'t");
764 array('f' => "tes''t")
774 array('f' => "tes't")
778 $this->assertEqual(stripslashes_deep($nested), $expected);
788 $this->assertEqual(ife(true, 'a', 'b'), 'a');
789 $this->assertEqual(ife(' ', 'a', 'b'), 'a');
790 $this->assertEqual(ife('test', 'a', 'b'), 'a');
791 $this->assertEqual(ife(23, 'a', 'b'), 'a');
792 $this->assertEqual(ife(array('t' => 'est'), 'a', 'b'), 'a');
794 $this->assertEqual(ife(false, 'a', 'b'), 'b');
795 $this->assertEqual(ife(null, 'a', 'b'), 'b');
796 $this->assertEqual(ife('', 'a', 'b'), 'b');
797 $this->assertEqual(ife(0, 'a', 'b'), 'b');
798 $this->assertEqual(ife(array(), 'a', 'b'), 'b');
806 function testPluginSplit() {
807 $result = pluginSplit('Something.else');
808 $this->assertEqual($result, array('Something', 'else'));
810 $result = pluginSplit('Something.else.more.dots');
811 $this->assertEqual($result, array('Something', 'else.more.dots'));
813 $result = pluginSplit('Somethingelse');
814 $this->assertEqual($result, array(null, 'Somethingelse'));
816 $result = pluginSplit('Something.else', true);
817 $this->assertEqual($result, array('Something.', 'else'));
819 $result = pluginSplit('Something.else.more.dots', true);
820 $this->assertEqual($result, array('Something.', 'else.more.dots'));
822 $result = pluginSplit('Post', false, 'Blog');
823 $this->assertEqual($result, array('Blog', 'Post'));
825 $result = pluginSplit('Blog.Post', false, 'Ultimate');
826 $this->assertEqual($result, array('Blog', 'Post'));