9
* CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
10
* Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
12
* Licensed under The Open Group Test Suite License
13
* Redistributions of files must retain the above copyright notice.
15
* @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
16
* @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
18
* @subpackage cake.tests.cases.libs
19
* @since CakePHP(tm) v 1.2.0.5432
20
* @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
22
App::import('Core', 'Configure');
28
* @subpackage cake.tests.cases.libs
30
class ConfigureTest extends CakeTestCase {
39
$this->_cacheDisable = Configure::read('Cache.disable');
40
$this->_debug = Configure::read('debug');
42
Configure::write('Cache.disable', true);
62
if (file_exists(TMP . 'cache' . DS . 'persistent' . DS . 'cake_core_core_paths')) {
63
unlink(TMP . 'cache' . DS . 'persistent' . DS . 'cake_core_core_paths');
65
if (file_exists(TMP . 'cache' . DS . 'persistent' . DS . 'cake_core_dir_map')) {
66
unlink(TMP . 'cache' . DS . 'persistent' . DS . 'cake_core_dir_map');
68
if (file_exists(TMP . 'cache' . DS . 'persistent' . DS . 'cake_core_file_map')) {
69
unlink(TMP . 'cache' . DS . 'persistent' . DS . 'cake_core_file_map');
71
if (file_exists(TMP . 'cache' . DS . 'persistent' . DS . 'cake_core_object_map')) {
72
unlink(TMP . 'cache' . DS . 'persistent' . DS . 'cake_core_object_map');
74
if (file_exists(TMP . 'cache' . DS . 'persistent' . DS . 'test.config.php')) {
75
unlink(TMP . 'cache' . DS . 'persistent' . DS . 'test.config.php');
77
if (file_exists(TMP . 'cache' . DS . 'persistent' . DS . 'test.php')) {
78
unlink(TMP . 'cache' . DS . 'persistent' . DS . 'test.php');
80
Configure::write('debug', $this->_debug);
81
Configure::write('Cache.disable', $this->_cacheDisable);
92
Configure::write('level1.level2.level3_1', $expected);
93
Configure::write('level1.level2.level3_2', 'something_else');
94
$result = Configure::read('level1.level2.level3_1');
95
$this->assertEqual($expected, $result);
97
$result = Configure::read('level1.level2.level3_2');
98
$this->assertEqual($result, 'something_else');
100
$result = Configure::read('debug');
101
$this->assertTrue($result >= 0);
110
function testWrite() {
111
$writeResult = Configure::write('SomeName.someKey', 'myvalue');
112
$this->assertTrue($writeResult);
113
$result = Configure::read('SomeName.someKey');
114
$this->assertEqual($result, 'myvalue');
116
$writeResult = Configure::write('SomeName.someKey', null);
117
$this->assertTrue($writeResult);
118
$result = Configure::read('SomeName.someKey');
119
$this->assertEqual($result, null);
121
$expected = array('One' => array('Two' => array('Three' => array('Four' => array('Five' => 'cool')))));
122
$writeResult = Configure::write('Key', $expected);
123
$this->assertTrue($writeResult);
125
$result = Configure::read('Key');
126
$this->assertEqual($expected, $result);
128
$result = Configure::read('Key.One');
129
$this->assertEqual($expected['One'], $result);
131
$result = Configure::read('Key.One.Two');
132
$this->assertEqual($expected['One']['Two'], $result);
134
$result = Configure::read('Key.One.Two.Three.Four.Five');
135
$this->assertEqual('cool', $result);
139
* testSetErrorReporting Level
143
function testSetErrorReportingLevel() {
144
Configure::write('log', false);
146
Configure::write('debug', 0);
147
$result = ini_get('error_reporting');
148
$this->assertEqual($result, 0);
150
Configure::write('debug', 2);
151
$result = ini_get('error_reporting');
152
$this->assertEqual($result, E_ALL & ~E_DEPRECATED);
154
$result = ini_get('display_errors');
155
$this->assertEqual($result, 1);
157
Configure::write('debug', 0);
158
$result = ini_get('error_reporting');
159
$this->assertEqual($result, 0);
163
* test that log and debug configure values interact well.
167
function testInteractionOfDebugAndLog() {
168
Configure::write('log', false);
170
Configure::write('debug', 0);
171
$this->assertEqual(ini_get('error_reporting'), 0);
172
$this->assertEqual(ini_get('display_errors'), 0);
174
Configure::write('log', E_WARNING);
175
Configure::write('debug', 0);
176
$this->assertEqual(ini_get('error_reporting'), E_WARNING);
177
$this->assertEqual(ini_get('display_errors'), 0);
179
Configure::write('debug', 2);
180
$this->assertEqual(ini_get('error_reporting'), E_ALL & ~E_DEPRECATED);
181
$this->assertEqual(ini_get('display_errors'), 1);
183
Configure::write('debug', 0);
184
Configure::write('log', false);
185
$this->assertEqual(ini_get('error_reporting'), 0);
186
$this->assertEqual(ini_get('display_errors'), 0);
195
function testDelete() {
196
Configure::write('SomeName.someKey', 'myvalue');
197
$result = Configure::read('SomeName.someKey');
198
$this->assertEqual($result, 'myvalue');
200
Configure::delete('SomeName.someKey');
201
$result = Configure::read('SomeName.someKey');
202
$this->assertTrue($result === null);
204
Configure::write('SomeName', array('someKey' => 'myvalue', 'otherKey' => 'otherValue'));
206
$result = Configure::read('SomeName.someKey');
207
$this->assertEqual($result, 'myvalue');
209
$result = Configure::read('SomeName.otherKey');
210
$this->assertEqual($result, 'otherValue');
212
Configure::delete('SomeName');
214
$result = Configure::read('SomeName.someKey');
215
$this->assertTrue($result === null);
217
$result = Configure::read('SomeName.otherKey');
218
$this->assertTrue($result === null);
227
function testLoad() {
228
$result = Configure::load('non_existing_configuration_file');
229
$this->assertFalse($result);
231
$result = Configure::load('config');
232
$this->assertTrue($result);
234
$result = Configure::load('../../index');
235
$this->assertFalse($result);
244
function testLoadPlugin() {
245
App::build(array('plugins' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS)), true);
246
$result = Configure::load('test_plugin.load');
247
$this->assertTrue($result);
248
$expected = '/test_app/plugins/test_plugin/config/load.php';
249
$config = Configure::read('plugin_load');
250
$this->assertEqual($config, $expected);
252
$result = Configure::load('test_plugin.more.load');
253
$this->assertTrue($result);
254
$expected = '/test_app/plugins/test_plugin/config/more.load.php';
255
$config = Configure::read('plugin_more_load');
256
$this->assertEqual($config, $expected);
265
function testStoreAndLoad() {
266
Configure::write('Cache.disable', false);
268
$expected = array('data' => 'value with backslash \, \'singlequote\' and "doublequotes"');
269
Configure::store('SomeExample', 'test', $expected);
271
Configure::load('test');
272
$config = Configure::read('SomeExample');
273
$this->assertEqual($config, $expected);
276
'data' => array('first' => 'value with backslash \, \'singlequote\' and "doublequotes"', 'second' => 'value2'),
279
Configure::store('AnotherExample', 'test_config', $expected);
281
Configure::load('test_config');
282
$config = Configure::read('AnotherExample');
283
$this->assertEqual($config, $expected);
292
function testVersion() {
293
$result = Configure::version();
294
$this->assertTrue(version_compare($result, '1.2', '>='));
299
* AppImportTest class
302
* @subpackage cake.tests.cases.libs
304
class AppImportTest extends CakeTestCase {
312
function testBuild() {
313
$old = App::path('models');
317
ROOT . DS . LIBS . 'model' . DS
319
$this->assertEqual($expected, $old);
321
App::build(array('models' => array('/path/to/models/')));
323
$new = App::path('models');
329
ROOT . DS . LIBS . 'model' . DS
331
$this->assertEqual($expected, $new);
333
App::build(); //reset defaults
334
$defaults = App::path('models');
335
$this->assertEqual($old, $defaults);
339
* testBuildWithReset method
344
function testBuildWithReset() {
345
$old = App::path('models');
349
ROOT . DS . LIBS . 'model' . DS
351
$this->assertEqual($expected, $old);
353
App::build(array('models' => array('/path/to/models/')), true);
355
$new = App::path('models');
360
$this->assertEqual($expected, $new);
362
App::build(); //reset defaults
363
$defaults = App::path('models');
364
$this->assertEqual($old, $defaults);
373
function testCore() {
374
$model = App::core('models');
375
$this->assertEqual(array(ROOT . DS . LIBS . 'model' . DS), $model);
377
$view = App::core('views');
378
$this->assertEqual(array(ROOT . DS . LIBS . 'view' . DS), $view);
380
$controller = App::core('controllers');
381
$this->assertEqual(array(ROOT . DS . LIBS . 'controller' . DS), $controller);
386
* testListObjects method
391
function testListObjects() {
392
$result = App::objects('class', TEST_CAKE_CORE_INCLUDE_PATH . 'libs');
393
$this->assertTrue(in_array('Xml', $result));
394
$this->assertTrue(in_array('Cache', $result));
395
$this->assertTrue(in_array('HttpSocket', $result));
397
$result = App::objects('behavior');
398
$this->assertTrue(in_array('Tree', $result));
400
$result = App::objects('controller');
401
$this->assertTrue(in_array('Pages', $result));
403
$result = App::objects('component');
404
$this->assertTrue(in_array('Auth', $result));
406
$result = App::objects('view');
407
$this->assertTrue(in_array('Media', $result));
409
$result = App::objects('helper');
410
$this->assertTrue(in_array('Html', $result));
412
$result = App::objects('model');
413
$notExpected = array('AppModel', 'ModelBehavior', 'ConnectionManager', 'DbAcl', 'Model', 'CakeSchema');
414
foreach ($notExpected as $class) {
415
$this->assertFalse(in_array($class, $result));
418
$result = App::objects('file');
419
$this->assertFalse($result);
421
$result = App::objects('file', 'non_existing_configure');
423
$this->assertEqual($result, $expected);
425
$result = App::objects('NonExistingType');
426
$this->assertFalse($result);
430
TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'libs' . DS
433
$result = App::objects('plugin', null, false);
434
$this->assertTrue(in_array('Cache', $result));
435
$this->assertTrue(in_array('Log', $result));
441
* test that pluginPath can find paths for plugins.
445
function testPluginPath() {
447
'plugins' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS)
449
$path = App::pluginPath('test_plugin');
450
$expected = TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS . 'test_plugin' . DS;
451
$this->assertEqual($path, $expected);
453
$path = App::pluginPath('TestPlugin');
454
$expected = TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS . 'test_plugin' . DS;
455
$this->assertEqual($path, $expected);
457
$path = App::pluginPath('TestPluginTwo');
458
$expected = TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS . 'test_plugin_two' . DS;
459
$this->assertEqual($path, $expected);
464
* test that pluginPath can find paths for plugins.
468
function testThemePath() {
470
'views' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views' . DS)
472
$path = App::themePath('test_theme');
473
$expected = TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views' . DS . 'themed' . DS . 'test_theme' . DS;
474
$this->assertEqual($path, $expected);
476
$path = App::themePath('TestTheme');
477
$expected = TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views' . DS . 'themed' . DS . 'test_theme' . DS;
478
$this->assertEqual($path, $expected);
484
* testClassLoading method
489
function testClassLoading() {
490
$file = App::import();
491
$this->assertTrue($file);
493
$file = App::import('Model', 'Model', false);
494
$this->assertTrue($file);
495
$this->assertTrue(class_exists('Model'));
497
$file = App::import('Controller', 'Controller', false);
498
$this->assertTrue($file);
499
$this->assertTrue(class_exists('Controller'));
501
$file = App::import('Component', 'Component', false);
502
$this->assertTrue($file);
503
$this->assertTrue(class_exists('Component'));
505
$file = App::import('Shell', 'Shell', false);
506
$this->assertTrue($file);
507
$this->assertTrue(class_exists('Shell'));
509
$file = App::import('Model', 'SomeRandomModelThatDoesNotExist', false);
510
$this->assertFalse($file);
512
$file = App::import('Model', 'AppModel', false);
513
$this->assertTrue($file);
514
$this->assertTrue(class_exists('AppModel'));
516
$file = App::import('WrongType', null, true, array(), '');
517
$this->assertTrue($file);
519
$file = App::import('Model', 'NonExistingPlugin.NonExistingModel', false);
520
$this->assertFalse($file);
522
$file = App::import('Core', 'NonExistingPlugin.NonExistingModel', false);
523
$this->assertFalse($file);
525
$file = App::import('Model', array('NonExistingPlugin.NonExistingModel'), false);
526
$this->assertFalse($file);
528
$file = App::import('Core', array('NonExistingPlugin.NonExistingModel'), false);
529
$this->assertFalse($file);
531
$file = App::import('Core', array('NonExistingPlugin.NonExistingModel.AnotherChild'), false);
532
$this->assertFalse($file);
534
if (!class_exists('AppController')) {
535
$classes = array_flip(get_declared_classes());
538
$this->assertFalse(isset($classes['PagesController']));
539
$this->assertFalse(isset($classes['AppController']));
541
$this->assertFalse(isset($classes['pagescontroller']));
542
$this->assertFalse(isset($classes['appcontroller']));
545
$file = App::import('Controller', 'Pages');
546
$this->assertTrue($file);
547
$this->assertTrue(class_exists('PagesController'));
549
$classes = array_flip(get_declared_classes());
552
$this->assertTrue(isset($classes['PagesController']));
553
$this->assertTrue(isset($classes['AppController']));
555
$this->assertTrue(isset($classes['pagescontroller']));
556
$this->assertTrue(isset($classes['appcontroller']));
559
$file = App::import('Behavior', 'Containable');
560
$this->assertTrue($file);
561
$this->assertTrue(class_exists('ContainableBehavior'));
563
$file = App::import('Component', 'RequestHandler');
564
$this->assertTrue($file);
565
$this->assertTrue(class_exists('RequestHandlerComponent'));
567
$file = App::import('Helper', 'Form');
568
$this->assertTrue($file);
569
$this->assertTrue(class_exists('FormHelper'));
571
$file = App::import('Model', 'NonExistingModel');
572
$this->assertFalse($file);
574
$file = App::import('Datasource', 'DboSource');
575
$this->assertTrue($file);
576
$this->assertTrue(class_exists('DboSource'));
582
* test import() with plugins
586
function testPluginImporting() {
588
'libs' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'libs' . DS),
589
'plugins' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS)
592
$result = App::import('Controller', 'TestPlugin.Tests');
593
$this->assertTrue($result);
594
$this->assertTrue(class_exists('TestPluginAppController'));
595
$this->assertTrue(class_exists('TestsController'));
597
$result = App::import('Lib', 'TestPlugin.TestPluginLibrary');
598
$this->assertTrue($result);
599
$this->assertTrue(class_exists('TestPluginLibrary'));
601
$result = App::import('Lib', 'Library');
602
$this->assertTrue($result);
603
$this->assertTrue(class_exists('Library'));
605
$result = App::import('Helper', 'TestPlugin.OtherHelper');
606
$this->assertTrue($result);
607
$this->assertTrue(class_exists('OtherHelperHelper'));
609
$result = App::import('Helper', 'TestPlugin.TestPluginApp');
610
$this->assertTrue($result);
611
$this->assertTrue(class_exists('TestPluginAppHelper'));
613
$result = App::import('Datasource', 'TestPlugin.TestSource');
614
$this->assertTrue($result);
615
$this->assertTrue(class_exists('TestSource'));
621
* test that building helper paths actually works.
624
* @link http://cakephp.lighthouseapp.com/projects/42648/tickets/410
626
function testImportingHelpersFromAlternatePaths() {
628
$this->assertFalse(class_exists('BananaHelper'), 'BananaHelper exists, cannot test importing it.');
629
App::import('Helper', 'Banana');
630
$this->assertFalse(class_exists('BananaHelper'), 'BananaHelper was not found because the path does not exist.');
633
'helpers' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views' . DS . 'helpers' . DS)
635
App::build(array('vendors' => array(TEST_CAKE_CORE_INCLUDE_PATH)));
636
$this->assertFalse(class_exists('BananaHelper'), 'BananaHelper exists, cannot test importing it.');
637
App::import('Helper', 'Banana');
638
$this->assertTrue(class_exists('BananaHelper'), 'BananaHelper was not loaded.');
644
* testFileLoading method
649
function testFileLoading () {
650
$file = App::import('File', 'RealFile', false, array(), TEST_CAKE_CORE_INCLUDE_PATH . 'config' . DS . 'config.php');
651
$this->assertTrue($file);
653
$file = App::import('File', 'NoFile', false, array(), TEST_CAKE_CORE_INCLUDE_PATH . 'config' . DS . 'cake' . DS . 'config.php');
654
$this->assertFalse($file);
656
// import($type = null, $name = null, $parent = true, $file = null, $search = array(), $return = false) {
659
* testFileLoadingWithArray method
664
function testFileLoadingWithArray() {
665
$type = array('type' => 'File', 'name' => 'SomeName', 'parent' => false,
666
'file' => TEST_CAKE_CORE_INCLUDE_PATH . DS . 'config' . DS . 'config.php');
667
$file = App::import($type);
668
$this->assertTrue($file);
670
$type = array('type' => 'File', 'name' => 'NoFile', 'parent' => false,
671
'file' => TEST_CAKE_CORE_INCLUDE_PATH . 'config' . DS . 'cake' . DS . 'config.php');
672
$file = App::import($type);
673
$this->assertFalse($file);
677
* testFileLoadingReturnValue method
682
function testFileLoadingReturnValue () {
683
$file = App::import('File', 'Name', false, array(), TEST_CAKE_CORE_INCLUDE_PATH . 'config' . DS . 'config.php', true);
684
$this->assertTrue($file);
686
$this->assertTrue(isset($file['Cake.version']));
688
$type = array('type' => 'File', 'name' => 'OtherName', 'parent' => false,
689
'file' => TEST_CAKE_CORE_INCLUDE_PATH . 'config' . DS . 'config.php', 'return' => true);
690
$file = App::import($type);
691
$this->assertTrue($file);
693
$this->assertTrue(isset($file['Cake.version']));
697
* testLoadingWithSearch method
702
function testLoadingWithSearch () {
703
$file = App::import('File', 'NewName', false, array(TEST_CAKE_CORE_INCLUDE_PATH ), 'config.php');
704
$this->assertTrue($file);
706
$file = App::import('File', 'AnotherNewName', false, array(LIBS), 'config.php');
707
$this->assertFalse($file);
711
* testLoadingWithSearchArray method
716
function testLoadingWithSearchArray () {
717
$type = array('type' => 'File', 'name' => 'RandomName', 'parent' => false, 'file' => 'config.php', 'search' => array(TEST_CAKE_CORE_INCLUDE_PATH ));
718
$file = App::import($type);
719
$this->assertTrue($file);
721
$type = array('type' => 'File', 'name' => 'AnotherRandomName', 'parent' => false, 'file' => 'config.php', 'search' => array(LIBS));
722
$file = App::import($type);
723
$this->assertFalse($file);
727
* testMultipleLoading method
732
function testMultipleLoading() {
733
$toLoad = array('I18n', 'CakeSocket');
735
$classes = array_flip(get_declared_classes());
736
$this->assertFalse(isset($classes['i18n']));
737
$this->assertFalse(isset($classes['CakeSocket']));
739
$load = App::import($toLoad);
740
$this->assertTrue($load);
742
$classes = array_flip(get_declared_classes());
745
$this->assertTrue(isset($classes['I18n']));
747
$this->assertTrue(isset($classes['i18n']));
750
$load = App::import(array('I18n', 'SomeNotFoundClass', 'CakeSocket'));
751
$this->assertFalse($load);
753
$load = App::import($toLoad);
754
$this->assertTrue($load);
758
* This test only works if you have plugins/my_plugin set up.
759
* plugins/my_plugin/models/my_plugin.php and other_model.php
763
function testMultipleLoadingByType() {
764
$classes = array_flip(get_declared_classes());
765
$this->assertFalse(isset($classes['OtherPlugin']));
766
$this->assertFalse(isset($classes['MyPlugin']));
769
$load = App::import('Model', array('MyPlugin.OtherPlugin', 'MyPlugin.MyPlugin'));
770
$this->assertTrue($load);
772
$classes = array_flip(get_declared_classes());
773
$this->assertTrue(isset($classes['OtherPlugin']));
774
$this->assertTrue(isset($classes['MyPlugin']));
777
function testLoadingVendor() {
779
'plugins' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS),
780
'vendors' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'vendors'. DS),
784
$result = App::import('Vendor', 'TestPlugin.TestPluginAsset', array('ext' => 'css'));
785
$text = ob_get_clean();
786
$this->assertTrue($result);
787
$this->assertEqual($text, 'this is the test plugin asset css file');
790
$result = App::import('Vendor', 'TestAsset', array('ext' => 'css'));
791
$text = ob_get_clean();
792
$this->assertTrue($result);
793
$this->assertEqual($text, 'this is the test asset css file');
795
$result = App::import('Vendor', 'TestPlugin.SamplePlugin');
796
$this->assertTrue($result);
797
$this->assertTrue(class_exists('SamplePluginClassTestName'));
799
$result = App::import('Vendor', 'ConfigureTestVendorSample');
800
$this->assertTrue($result);
801
$this->assertTrue(class_exists('ConfigureTestVendorSample'));
804
$result = App::import('Vendor', 'SomeName', array('file' => 'some.name.php'));
805
$text = ob_get_clean();
806
$this->assertTrue($result);
807
$this->assertEqual($text, 'This is a file with dot in file name');
810
$result = App::import('Vendor', 'TestHello', array('file' => 'Test'.DS.'hello.php'));
811
$text = ob_get_clean();
812
$this->assertTrue($result);
813
$this->assertEqual($text, 'This is the hello.php file in Test directory');
816
$result = App::import('Vendor', 'MyTest', array('file' => 'Test'.DS.'MyTest.php'));
817
$text = ob_get_clean();
818
$this->assertTrue($result);
819
$this->assertEqual($text, 'This is the MyTest.php file');
822
$result = App::import('Vendor', 'Welcome');
823
$text = ob_get_clean();
824
$this->assertTrue($result);
825
$this->assertEqual($text, 'This is the welcome.php file in vendors directory');
828
$result = App::import('Vendor', 'TestPlugin.Welcome');
829
$text = ob_get_clean();
830
$this->assertTrue($result);
831
$this->assertEqual($text, 'This is the welcome.php file in test_plugin/vendors directory');