Initial commit

This commit is contained in:
mareksebera 2014-09-10 20:20:58 +02:00
commit 3b93da31de
1004 changed files with 265840 additions and 0 deletions

View file

@ -0,0 +1,832 @@
<?php
/**
* AppTest file.
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @package Cake.Test.Case.Core
* @since CakePHP(tm) v 2.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
/**
* AppTest class
*
* @package Cake.Test.Case.Core
*/
class AppTest extends CakeTestCase {
/**
* tearDown method
*
* @return void
*/
public function tearDown() {
parent::tearDown();
CakePlugin::unload();
}
/**
* testBuild method
*
* @return void
*/
public function testBuild() {
$old = App::path('Model');
$expected = array(
APP . 'Model' . DS
);
$this->assertEquals($expected, $old);
App::build(array('Model' => array('/path/to/models/')));
$new = App::path('Model');
$expected = array(
'/path/to/models/',
APP . 'Model' . DS
);
$this->assertEquals($expected, $new);
App::build();
App::build(array('Model' => array('/path/to/models/')), App::PREPEND);
$new = App::path('Model');
$expected = array(
'/path/to/models/',
APP . 'Model' . DS
);
$this->assertEquals($expected, $new);
App::build();
App::build(array('Model' => array('/path/to/models/')), App::APPEND);
$new = App::path('Model');
$expected = array(
APP . 'Model' . DS,
'/path/to/models/'
);
$this->assertEquals($expected, $new);
App::build();
App::build(array(
'Model' => array('/path/to/models/'),
'Controller' => array('/path/to/controllers/'),
), App::APPEND);
$new = App::path('Model');
$expected = array(
APP . 'Model' . DS,
'/path/to/models/'
);
$this->assertEquals($expected, $new);
$new = App::path('Controller');
$expected = array(
APP . 'Controller' . DS,
'/path/to/controllers/'
);
$this->assertEquals($expected, $new);
App::build(); //reset defaults
$defaults = App::path('Model');
$this->assertEquals($old, $defaults);
}
/**
* tests that it is possible to set up paths using the CakePHP 1.3 notation for them (models, behaviors, controllers...)
*
* @return void
*/
public function testCompatibleBuild() {
$old = App::path('models');
$expected = array(
APP . 'Model' . DS
);
$this->assertEquals($expected, $old);
App::build(array('models' => array('/path/to/models/')));
$new = App::path('models');
$expected = array(
'/path/to/models/',
APP . 'Model' . DS
);
$this->assertEquals($expected, $new);
$this->assertEquals($expected, App::path('Model'));
App::build(array('datasources' => array('/path/to/datasources/')));
$expected = array(
'/path/to/datasources/',
APP . 'Model' . DS . 'Datasource' . DS
);
$result = App::path('datasources');
$this->assertEquals($expected, $result);
$this->assertEquals($expected, App::path('Model/Datasource'));
App::build(array('behaviors' => array('/path/to/behaviors/')));
$expected = array(
'/path/to/behaviors/',
APP . 'Model' . DS . 'Behavior' . DS
);
$result = App::path('behaviors');
$this->assertEquals($expected, $result);
$this->assertEquals($expected, App::path('Model/Behavior'));
App::build(array('controllers' => array('/path/to/controllers/')));
$expected = array(
'/path/to/controllers/',
APP . 'Controller' . DS
);
$result = App::path('controllers');
$this->assertEquals($expected, $result);
$this->assertEquals($expected, App::path('Controller'));
App::build(array('components' => array('/path/to/components/')));
$expected = array(
'/path/to/components/',
APP . 'Controller' . DS . 'Component' . DS
);
$result = App::path('components');
$this->assertEquals($expected, $result);
$this->assertEquals($expected, App::path('Controller/Component'));
App::build(array('views' => array('/path/to/views/')));
$expected = array(
'/path/to/views/',
APP . 'View' . DS
);
$result = App::path('views');
$this->assertEquals($expected, $result);
$this->assertEquals($expected, App::path('View'));
App::build(array('helpers' => array('/path/to/helpers/')));
$expected = array(
'/path/to/helpers/',
APP . 'View' . DS . 'Helper' . DS
);
$result = App::path('helpers');
$this->assertEquals($expected, $result);
$this->assertEquals($expected, App::path('View/Helper'));
App::build(array('shells' => array('/path/to/shells/')));
$expected = array(
'/path/to/shells/',
APP . 'Console' . DS . 'Command' . DS
);
$result = App::path('shells');
$this->assertEquals($expected, $result);
$this->assertEquals($expected, App::path('Console/Command'));
App::build(); //reset defaults
$defaults = App::path('Model');
$this->assertEquals($old, $defaults);
}
/**
* test package build() with App::REGISTER.
*
* @return void
*/
public function testBuildPackage() {
$pluginPaths = array(
'/foo/bar',
APP . 'Plugin' . DS,
dirname(dirname(CAKE)) . DS . 'plugins' . DS
);
App::build(array(
'Plugin' => array(
'/foo/bar'
)
));
$result = App::path('Plugin');
$this->assertEquals($pluginPaths, $result);
$paths = App::path('Service');
$this->assertEquals(array(), $paths);
App::build(array(
'Service' => array(
'%s' . 'Service' . DS,
),
), App::REGISTER);
$expected = array(
APP . 'Service' . DS,
);
$result = App::path('Service');
$this->assertEquals($expected, $result);
//Ensure new paths registered for other packages are not affected
$result = App::path('Plugin');
$this->assertEquals($pluginPaths, $result);
App::build();
$paths = App::path('Service');
$this->assertEquals(array(), $paths);
}
/**
* test path() with a plugin.
*
* @return void
*/
public function testPathWithPlugins() {
$basepath = CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS;
App::build(array(
'Plugin' => array($basepath),
));
CakePlugin::load('TestPlugin');
$result = App::path('Vendor', 'TestPlugin');
$this->assertEquals($basepath . 'TestPlugin' . DS . 'Vendor' . DS, $result[0]);
}
/**
* testBuildWithReset method
*
* @return void
*/
public function testBuildWithReset() {
$old = App::path('Model');
$expected = array(
APP . 'Model' . DS
);
$this->assertEquals($expected, $old);
App::build(array('Model' => array('/path/to/models/')), App::RESET);
$new = App::path('Model');
$expected = array(
'/path/to/models/'
);
$this->assertEquals($expected, $new);
App::build(); //reset defaults
$defaults = App::path('Model');
$this->assertEquals($old, $defaults);
}
/**
* testCore method
*
* @return void
*/
public function testCore() {
$model = App::core('Model');
$this->assertEquals(array(CAKE . 'Model' . DS), $model);
$view = App::core('View');
$this->assertEquals(array(CAKE . 'View' . DS), $view);
$controller = App::core('Controller');
$this->assertEquals(array(CAKE . 'Controller' . DS), $controller);
$component = App::core('Controller/Component');
$this->assertEquals(array(CAKE . 'Controller' . DS . 'Component' . DS), str_replace('/', DS, $component));
$auth = App::core('Controller/Component/Auth');
$this->assertEquals(array(CAKE . 'Controller' . DS . 'Component' . DS . 'Auth' . DS), str_replace('/', DS, $auth));
$datasource = App::core('Model/Datasource');
$this->assertEquals(array(CAKE . 'Model' . DS . 'Datasource' . DS), str_replace('/', DS, $datasource));
}
/**
* testListObjects method
*
* @return void
*/
public function testListObjects() {
$result = App::objects('class', CAKE . 'Routing', false);
$this->assertTrue(in_array('Dispatcher', $result));
$this->assertTrue(in_array('Router', $result));
App::build(array(
'Model/Behavior' => App::core('Model/Behavior'),
'Controller' => App::core('Controller'),
'Controller/Component' => App::core('Controller/Component'),
'View' => App::core('View'),
'Model' => App::core('Model'),
'View/Helper' => App::core('View/Helper'),
), App::RESET);
$result = App::objects('behavior', null, false);
$this->assertTrue(in_array('TreeBehavior', $result));
$result = App::objects('Model/Behavior', null, false);
$this->assertTrue(in_array('TreeBehavior', $result));
$result = App::objects('component', null, false);
$this->assertTrue(in_array('AuthComponent', $result));
$result = App::objects('Controller/Component', null, false);
$this->assertTrue(in_array('AuthComponent', $result));
$result = App::objects('view', null, false);
$this->assertTrue(in_array('MediaView', $result));
$result = App::objects('View', null, false);
$this->assertTrue(in_array('MediaView', $result));
$result = App::objects('helper', null, false);
$this->assertTrue(in_array('HtmlHelper', $result));
$result = App::objects('View/Helper', null, false);
$this->assertTrue(in_array('HtmlHelper', $result));
$result = App::objects('model', null, false);
$this->assertTrue(in_array('AcoAction', $result));
$result = App::objects('Model', null, false);
$this->assertTrue(in_array('AcoAction', $result));
$result = App::objects('file');
$this->assertFalse($result);
$result = App::objects('file', 'non_existing_configure');
$expected = array();
$this->assertEquals($expected, $result);
$result = App::objects('NonExistingType');
$this->assertSame(array(), $result);
App::build(array(
'plugins' => array(
CAKE . 'Test' . DS . 'test_app' . DS . 'Lib' . DS
)
));
$result = App::objects('plugin', null, false);
$this->assertTrue(in_array('Cache', $result));
$this->assertTrue(in_array('Log', $result));
App::build();
}
/**
* Make sure that .svn and friends are excluded from App::objects('plugin')
*
* @return void
*/
public function testListObjectsIgnoreDotDirectories() {
$path = CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS;
$this->skipIf(!is_writable($path), $path . ' is not writable.');
App::build(array(
'plugins' => array($path)
), App::RESET);
mkdir($path . '.svn');
$result = App::objects('plugin', null, false);
rmdir($path . '.svn');
$this->assertNotContains('.svn', $result);
}
/**
* Tests listing objects within a plugin
*
* @return void
*/
public function testListObjectsInPlugin() {
App::build(array(
'Model' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Model' . DS),
'plugins' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
), App::RESET);
CakePlugin::load(array('TestPlugin', 'TestPluginTwo'));
$result = App::objects('TestPlugin.model');
$this->assertTrue(in_array('TestPluginPost', $result));
$result = App::objects('TestPlugin.Model');
$this->assertTrue(in_array('TestPluginPost', $result));
$result = App::objects('TestPlugin.behavior');
$this->assertTrue(in_array('TestPluginPersisterOneBehavior', $result));
$result = App::objects('TestPlugin.Model/Behavior');
$this->assertTrue(in_array('TestPluginPersisterOneBehavior', $result));
$result = App::objects('TestPlugin.helper');
$expected = array('OtherHelperHelper', 'PluggedHelperHelper', 'TestPluginAppHelper');
$this->assertEquals($expected, $result);
$result = App::objects('TestPlugin.View/Helper');
$expected = array('OtherHelperHelper', 'PluggedHelperHelper', 'TestPluginAppHelper');
$this->assertEquals($expected, $result);
$result = App::objects('TestPlugin.component');
$this->assertTrue(in_array('OtherComponent', $result));
$result = App::objects('TestPlugin.Controller/Component');
$this->assertTrue(in_array('OtherComponent', $result));
$result = App::objects('TestPluginTwo.behavior');
$this->assertSame(array(), $result);
$result = App::objects('TestPluginTwo.Model/Behavior');
$this->assertSame(array(), $result);
$result = App::objects('model', null, false);
$this->assertTrue(in_array('Comment', $result));
$this->assertTrue(in_array('Post', $result));
$result = App::objects('Model', null, false);
$this->assertTrue(in_array('Comment', $result));
$this->assertTrue(in_array('Post', $result));
App::build();
}
/**
* test that themePath can find paths for themes.
*
* @return void
*/
public function testThemePath() {
App::build(array(
'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS)
));
$path = App::themePath('test_theme');
$expected = CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Themed' . DS . 'TestTheme' . DS;
$this->assertEquals($expected, $path);
$path = App::themePath('TestTheme');
$expected = CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Themed' . DS . 'TestTheme' . DS;
$this->assertEquals($expected, $path);
App::build();
}
/**
* testClassLoading method
*
* @return void
*/
public function testClassLoading() {
$file = App::import('Model', 'Model', false);
$this->assertTrue($file);
$this->assertTrue(class_exists('Model'));
$file = App::import('Controller', 'Controller', false);
$this->assertTrue($file);
$this->assertTrue(class_exists('Controller'));
$file = App::import('Component', 'Auth', false);
$this->assertTrue($file);
$this->assertTrue(class_exists('AuthComponent'));
$file = App::import('Shell', 'Shell', false);
$this->assertTrue($file);
$this->assertTrue(class_exists('Shell'));
$file = App::import('Configure', 'PhpReader');
$this->assertTrue($file);
$this->assertTrue(class_exists('PhpReader'));
$file = App::import('Model', 'SomeRandomModelThatDoesNotExist', false);
$this->assertFalse($file);
$file = App::import('Model', 'AppModel', false);
$this->assertTrue($file);
$this->assertTrue(class_exists('AppModel'));
$file = App::import('WrongType', null, true, array(), '');
$this->assertFalse($file);
$file = App::import('Model', 'NonExistingPlugin.NonExistingModel', false);
$this->assertFalse($file);
$file = App::import('Model', array('NonExistingPlugin.NonExistingModel'), false);
$this->assertFalse($file);
if (!class_exists('AppController', false)) {
$classes = array_flip(get_declared_classes());
$this->assertFalse(isset($classes['PagesController']));
$this->assertFalse(isset($classes['AppController']));
$file = App::import('Controller', 'Pages');
$this->assertTrue($file);
$this->assertTrue(class_exists('PagesController'));
$classes = array_flip(get_declared_classes());
$this->assertTrue(isset($classes['PagesController']));
$this->assertTrue(isset($classes['AppController']));
$file = App::import('Behavior', 'Containable');
$this->assertTrue($file);
$this->assertTrue(class_exists('ContainableBehavior'));
$file = App::import('Component', 'RequestHandler');
$this->assertTrue($file);
$this->assertTrue(class_exists('RequestHandlerComponent'));
$file = App::import('Helper', 'Form');
$this->assertTrue($file);
$this->assertTrue(class_exists('FormHelper'));
$file = App::import('Model', 'NonExistingModel');
$this->assertFalse($file);
$file = App::import('Datasource', 'DboSource');
$this->assertTrue($file);
$this->assertTrue(class_exists('DboSource'));
}
App::build();
}
/**
* test import() with plugins
*
* @return void
*/
public function testPluginImporting() {
App::build(array(
'Lib' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Lib' . DS),
'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
));
CakePlugin::load(array('TestPlugin', 'TestPluginTwo'));
$result = App::import('Controller', 'TestPlugin.Tests');
$this->assertTrue($result);
$this->assertTrue(class_exists('TestPluginAppController'));
$this->assertTrue(class_exists('TestsController'));
$result = App::import('Lib', 'TestPlugin.TestPluginLibrary');
$this->assertTrue($result);
$this->assertTrue(class_exists('TestPluginLibrary'));
$result = App::import('Lib', 'Library');
$this->assertTrue($result);
$this->assertTrue(class_exists('Library'));
$result = App::import('Helper', 'TestPlugin.OtherHelper');
$this->assertTrue($result);
$this->assertTrue(class_exists('OtherHelperHelper'));
$result = App::import('Helper', 'TestPlugin.TestPluginApp');
$this->assertTrue($result);
$this->assertTrue(class_exists('TestPluginAppHelper'));
$result = App::import('Datasource', 'TestPlugin.TestSource');
$this->assertTrue($result);
$this->assertTrue(class_exists('TestSource'));
App::uses('ExampleExample', 'TestPlugin.Vendor/Example');
$this->assertTrue(class_exists('ExampleExample'));
App::build();
}
/**
* test that building helper paths actually works.
*
* @return void
* @link https://cakephp.lighthouseapp.com/projects/42648/tickets/410
*/
public function testImportingHelpersFromAlternatePaths() {
$this->assertFalse(class_exists('BananaHelper', false), 'BananaHelper exists, cannot test importing it.');
App::build(array(
'View/Helper' => array(
CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Helper' . DS
)
));
$this->assertFalse(class_exists('BananaHelper', false), 'BananaHelper exists, cannot test importing it.');
App::import('Helper', 'Banana');
$this->assertTrue(class_exists('BananaHelper', false), 'BananaHelper was not loaded.');
App::build();
}
/**
* testFileLoading method
*
* @return void
*/
public function testFileLoading() {
$file = App::import('File', 'RealFile', false, array(), CAKE . 'Config' . DS . 'config.php');
$this->assertTrue($file);
$file = App::import('File', 'NoFile', false, array(), CAKE . 'Config' . DS . 'cake' . DS . 'config.php');
$this->assertFalse($file);
}
/**
* testFileLoadingWithArray method
*
* @return void
*/
public function testFileLoadingWithArray() {
$type = array(
'type' => 'File',
'name' => 'SomeName',
'parent' => false,
'file' => CAKE . DS . 'Config' . DS . 'config.php'
);
$file = App::import($type);
$this->assertTrue($file);
$type = array(
'type' => 'File',
'name' => 'NoFile',
'parent' => false,
'file' => CAKE . 'Config' . DS . 'cake' . DS . 'config.php'
);
$file = App::import($type);
$this->assertFalse($file);
}
/**
* testFileLoadingReturnValue method
*
* @return void
*/
public function testFileLoadingReturnValue() {
$file = App::import('File', 'Name', false, array(), CAKE . 'Config' . DS . 'config.php', true);
$this->assertTrue(!empty($file));
$this->assertTrue(isset($file['Cake.version']));
$type = array(
'type' => 'File',
'name' => 'OtherName',
'parent' => false,
'file' => CAKE . 'Config' . DS . 'config.php', 'return' => true
);
$file = App::import($type);
$this->assertTrue(!empty($file));
$this->assertTrue(isset($file['Cake.version']));
}
/**
* testLoadingWithSearch method
*
* @return void
*/
public function testLoadingWithSearch() {
$file = App::import('File', 'NewName', false, array(CAKE . 'Config' . DS), 'config.php');
$this->assertTrue($file);
$file = App::import('File', 'AnotherNewName', false, array(CAKE), 'config.php');
$this->assertFalse($file);
}
/**
* testLoadingWithSearchArray method
*
* @return void
*/
public function testLoadingWithSearchArray() {
$type = array(
'type' => 'File',
'name' => 'RandomName',
'parent' => false,
'file' => 'config.php',
'search' => array(CAKE . 'Config' . DS)
);
$file = App::import($type);
$this->assertTrue($file);
$type = array(
'type' => 'File',
'name' => 'AnotherRandomName',
'parent' => false,
'file' => 'config.php',
'search' => array(CAKE)
);
$file = App::import($type);
$this->assertFalse($file);
}
/**
* testMultipleLoading method
*
* @return void
*/
public function testMultipleLoading() {
if (class_exists('PersisterOne', false) || class_exists('PersisterTwo', false)) {
$this->markTestSkipped('Cannot test loading of classes that exist.');
}
App::build(array(
'Model' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Model' . DS)
));
$toLoad = array('PersisterOne', 'PersisterTwo');
$load = App::import('Model', $toLoad);
$this->assertTrue($load);
$classes = array_flip(get_declared_classes());
$this->assertTrue(isset($classes['PersisterOne']));
$this->assertTrue(isset($classes['PersisterTwo']));
$load = App::import('Model', array('PersisterOne', 'SomeNotFoundClass', 'PersisterTwo'));
$this->assertFalse($load);
}
public function testLoadingVendor() {
App::build(array(
'plugins' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS),
'vendors' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Vendor' . DS),
), App::RESET);
CakePlugin::load(array('TestPlugin', 'TestPluginTwo'));
ob_start();
$result = App::import('Vendor', 'css/TestAsset', array('ext' => 'css'));
$text = ob_get_clean();
$this->assertTrue($result);
$this->assertEquals('/* this is the test asset css file */', trim($text));
$result = App::import('Vendor', 'TestPlugin.sample/SamplePlugin');
$this->assertTrue($result);
$this->assertTrue(class_exists('SamplePluginClassTestName'));
$result = App::import('Vendor', 'sample/ConfigureTestVendorSample');
$this->assertTrue($result);
$this->assertTrue(class_exists('ConfigureTestVendorSample'));
ob_start();
$result = App::import('Vendor', 'SomeNameInSubfolder', array('file' => 'somename/some.name.php'));
$text = ob_get_clean();
$this->assertTrue($result);
$this->assertEquals('This is a file with dot in file name', $text);
ob_start();
$result = App::import('Vendor', 'TestHello', array('file' => 'Test' . DS . 'hello.php'));
$text = ob_get_clean();
$this->assertTrue($result);
$this->assertEquals('This is the hello.php file in Test directory', $text);
ob_start();
$result = App::import('Vendor', 'MyTest', array('file' => 'Test' . DS . 'MyTest.php'));
$text = ob_get_clean();
$this->assertTrue($result);
$this->assertEquals('This is the MyTest.php file', $text);
ob_start();
$result = App::import('Vendor', 'Welcome');
$text = ob_get_clean();
$this->assertTrue($result);
$this->assertEquals('This is the welcome.php file in vendors directory', $text);
ob_start();
$result = App::import('Vendor', 'TestPlugin.Welcome');
$text = ob_get_clean();
$this->assertTrue($result);
$this->assertEquals('This is the welcome.php file in test_plugin/vendors directory', $text);
}
/**
* Tests that the automatic class loader will also find in "libs" folder for both
* app and plugins if it does not find the class in other configured paths
*
* @return void
*/
public function testLoadClassInLibs() {
App::build(array(
'libs' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Lib' . DS),
'plugins' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
), App::RESET);
CakePlugin::load(array('TestPlugin', 'TestPluginTwo'));
$this->assertFalse(class_exists('CustomLibClass', false));
App::uses('CustomLibClass', 'TestPlugin.Custom/Package');
$this->assertTrue(class_exists('CustomLibClass'));
$this->assertFalse(class_exists('TestUtilityClass', false));
App::uses('TestUtilityClass', 'Utility');
$this->assertTrue(class_exists('TestUtilityClass'));
}
/**
* Tests that App::location() returns the defined path for a class
*
* @return void
*/
public function testClassLocation() {
App::uses('MyCustomClass', 'MyPackage/Name');
$this->assertEquals('MyPackage/Name', App::location('MyCustomClass'));
}
/**
* Test that paths() works.
*
* @return void
*/
public function testPaths() {
$result = App::paths();
$this->assertArrayHasKey('Plugin', $result);
$this->assertArrayHasKey('Controller', $result);
$this->assertArrayHasKey('Controller/Component', $result);
}
/**
* Proves that it is possible to load plugin libraries in top
* level Lib dir for plugins
*
* @return void
*/
public function testPluginLibClasses() {
App::build(array(
'plugins' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
), App::RESET);
CakePlugin::load(array('TestPlugin', 'TestPluginTwo'));
$this->assertFalse(class_exists('TestPluginOtherLibrary', false));
App::uses('TestPluginOtherLibrary', 'TestPlugin.Lib');
$this->assertTrue(class_exists('TestPluginOtherLibrary'));
}
}

View file

@ -0,0 +1,299 @@
<?php
/**
* CakePluginTest file.
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @package Cake.Test.Case.Core
* @since CakePHP(tm) v 2.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
App::uses('CakePlugin', 'Core');
/**
* CakePluginTest class
*
*/
class CakePluginTest extends CakeTestCase {
/**
* Sets the plugins folder for this test
*
* @return void
*/
public function setUp() {
parent::setUp();
App::build(array(
'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
), App::RESET);
App::objects('plugins', null, false);
}
/**
* Reverts the changes done to the environment while testing
*
* @return void
*/
public function tearDown() {
parent::tearDown();
CakePlugin::unload();
}
/**
* Tests loading a single plugin
*
* @return void
*/
public function testLoadSingle() {
CakePlugin::unload();
CakePlugin::load('TestPlugin');
$expected = array('TestPlugin');
$this->assertEquals($expected, CakePlugin::loaded());
}
/**
* Tests unloading plugins
*
* @return void
*/
public function testUnload() {
CakePlugin::load('TestPlugin');
$expected = array('TestPlugin');
$this->assertEquals($expected, CakePlugin::loaded());
CakePlugin::unload('TestPlugin');
$this->assertEquals(array(), CakePlugin::loaded());
CakePlugin::load('TestPlugin');
$expected = array('TestPlugin');
$this->assertEquals($expected, CakePlugin::loaded());
CakePlugin::unload('TestFakePlugin');
$this->assertEquals($expected, CakePlugin::loaded());
}
/**
* Tests loading a plugin and its bootstrap file
*
* @return void
*/
public function testLoadSingleWithBootstrap() {
CakePlugin::load('TestPlugin', array('bootstrap' => true));
$this->assertTrue(CakePlugin::loaded('TestPlugin'));
$this->assertEquals('loaded plugin bootstrap', Configure::read('CakePluginTest.test_plugin.bootstrap'));
}
/**
* Tests loading a plugin with bootstrap file and routes file
*
* @return void
*/
public function testLoadSingleWithBootstrapAndRoutes() {
CakePlugin::load('TestPlugin', array('bootstrap' => true, 'routes' => true));
$this->assertTrue(CakePlugin::loaded('TestPlugin'));
$this->assertEquals('loaded plugin bootstrap', Configure::read('CakePluginTest.test_plugin.bootstrap'));
CakePlugin::routes();
$this->assertEquals('loaded plugin routes', Configure::read('CakePluginTest.test_plugin.routes'));
}
/**
* Tests loading multiple plugins at once
*
* @return void
*/
public function testLoadMultiple() {
CakePlugin::load(array('TestPlugin', 'TestPluginTwo'));
$expected = array('TestPlugin', 'TestPluginTwo');
$this->assertEquals($expected, CakePlugin::loaded());
}
/**
* Tests loading multiple plugins and their bootstrap files
*
* @return void
*/
public function testLoadMultipleWithDefaults() {
CakePlugin::load(array('TestPlugin', 'TestPluginTwo'), array('bootstrap' => true, 'routes' => false));
$expected = array('TestPlugin', 'TestPluginTwo');
$this->assertEquals($expected, CakePlugin::loaded());
$this->assertEquals('loaded plugin bootstrap', Configure::read('CakePluginTest.test_plugin.bootstrap'));
$this->assertEquals('loaded plugin two bootstrap', Configure::read('CakePluginTest.test_plugin_two.bootstrap'));
}
/**
* Tests loading multiple plugins with default loading params and some overrides
*
* @return void
*/
public function testLoadMultipleWithDefaultsAndOverride() {
CakePlugin::load(
array('TestPlugin', 'TestPluginTwo' => array('routes' => false)),
array('bootstrap' => true, 'routes' => true)
);
$expected = array('TestPlugin', 'TestPluginTwo');
$this->assertEquals($expected, CakePlugin::loaded());
$this->assertEquals('loaded plugin bootstrap', Configure::read('CakePluginTest.test_plugin.bootstrap'));
$this->assertEquals(null, Configure::read('CakePluginTest.test_plugin_two.bootstrap'));
}
/**
* Tests that it is possible to load multiple bootstrap files at once
*
* @return void
*/
public function testMultipleBootstrapFiles() {
CakePlugin::load('TestPlugin', array('bootstrap' => array('bootstrap', 'custom_config')));
$this->assertTrue(CakePlugin::loaded('TestPlugin'));
$this->assertEquals('loaded plugin bootstrap', Configure::read('CakePluginTest.test_plugin.bootstrap'));
}
/**
* Tests that it is possible to load plugin bootstrap by calling a callback function
*
* @return void
*/
public function testCallbackBootstrap() {
CakePlugin::load('TestPlugin', array('bootstrap' => array($this, 'pluginBootstrap')));
$this->assertTrue(CakePlugin::loaded('TestPlugin'));
$this->assertEquals('called plugin bootstrap callback', Configure::read('CakePluginTest.test_plugin.bootstrap'));
}
/**
* Tests that loading a missing routes file throws a warning
*
* @return void
* @expectedException PHPUNIT_FRAMEWORK_ERROR_WARNING
*/
public function testLoadMultipleWithDefaultsMissingFile() {
CakePlugin::load(array('TestPlugin', 'TestPluginTwo'), array('bootstrap' => true, 'routes' => true));
CakePlugin::routes();
}
/**
* Test ignoring missing bootstrap/routes file
*
* @return void
*/
public function testIgnoreMissingFiles() {
CakePlugin::loadAll(array(array(
'bootstrap' => true,
'routes' => true,
'ignoreMissing' => true
)));
CakePlugin::routes();
}
/**
* Tests that CakePlugin::load() throws an exception on unknown plugin
*
* @return void
* @expectedException MissingPluginException
*/
public function testLoadNotFound() {
CakePlugin::load('MissingPlugin');
}
/**
* Tests that CakePlugin::path() returns the correct path for the loaded plugins
*
* @return void
*/
public function testPath() {
CakePlugin::load(array('TestPlugin', 'TestPluginTwo'));
$expected = CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS . 'TestPlugin' . DS;
$this->assertEquals($expected, CakePlugin::path('TestPlugin'));
$expected = CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS . 'TestPluginTwo' . DS;
$this->assertEquals($expected, CakePlugin::path('TestPluginTwo'));
}
/**
* Tests that CakePlugin::path() throws an exception on unknown plugin
*
* @return void
* @expectedException MissingPluginException
*/
public function testPathNotFound() {
CakePlugin::path('TestPlugin');
}
/**
* Tests that CakePlugin::loadAll() will load all plugins in the configured folder
*
* @return void
*/
public function testLoadAll() {
CakePlugin::loadAll();
$expected = array('PluginJs', 'TestPlugin', 'TestPluginTwo');
$this->assertEquals($expected, CakePlugin::loaded());
}
/**
* Tests that CakePlugin::loadAll() will load all plugins in the configured folder with bootstrap loading
*
* @return void
*/
public function testLoadAllWithDefaults() {
$defaults = array('bootstrap' => true);
CakePlugin::loadAll(array($defaults));
$expected = array('PluginJs', 'TestPlugin', 'TestPluginTwo');
$this->assertEquals($expected, CakePlugin::loaded());
$this->assertEquals('loaded js plugin bootstrap', Configure::read('CakePluginTest.js_plugin.bootstrap'));
$this->assertEquals('loaded plugin bootstrap', Configure::read('CakePluginTest.test_plugin.bootstrap'));
$this->assertEquals('loaded plugin two bootstrap', Configure::read('CakePluginTest.test_plugin_two.bootstrap'));
}
/**
* Tests that CakePlugin::loadAll() will load all plugins in the configured folder with defaults
* and merges in global defaults.
*
* @return void
*/
public function testLoadAllWithDefaultsAndOverride() {
CakePlugin::loadAll(array(array('bootstrap' => true), 'TestPlugin' => array('routes' => true)));
CakePlugin::routes();
$expected = array('PluginJs', 'TestPlugin', 'TestPluginTwo');
$this->assertEquals($expected, CakePlugin::loaded());
$this->assertEquals('loaded js plugin bootstrap', Configure::read('CakePluginTest.js_plugin.bootstrap'));
$this->assertEquals('loaded plugin routes', Configure::read('CakePluginTest.test_plugin.routes'));
$this->assertEquals('loaded plugin bootstrap', Configure::read('CakePluginTest.test_plugin.bootstrap'));
$this->assertEquals('loaded plugin two bootstrap', Configure::read('CakePluginTest.test_plugin_two.bootstrap'));
}
/**
* Tests that CakePlugin::loadAll() will load all plugins in the configured folder with defaults
* and overrides for a plugin
*
* @return void
*/
public function testLoadAllWithDefaultsAndOverrideComplex() {
CakePlugin::loadAll(array(array('bootstrap' => true), 'TestPlugin' => array('routes' => true, 'bootstrap' => false)));
CakePlugin::routes();
$expected = array('PluginJs', 'TestPlugin', 'TestPluginTwo');
$this->assertEquals($expected, CakePlugin::loaded());
$this->assertEquals('loaded js plugin bootstrap', Configure::read('CakePluginTest.js_plugin.bootstrap'));
$this->assertEquals('loaded plugin routes', Configure::read('CakePluginTest.test_plugin.routes'));
$this->assertEquals(null, Configure::read('CakePluginTest.test_plugin.bootstrap'));
$this->assertEquals('loaded plugin two bootstrap', Configure::read('CakePluginTest.test_plugin_two.bootstrap'));
}
/**
* Auxiliary function to test plugin bootstrap callbacks
*
* @return void
*/
public function pluginBootstrap() {
Configure::write('CakePluginTest.test_plugin.bootstrap', 'called plugin bootstrap callback');
}
}

View file

@ -0,0 +1,484 @@
<?php
/**
* ConfigureTest file
*
* Holds several tests
*
* CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
* @package Cake.Test.Case.Core
* @since CakePHP(tm) v 1.2.0.5432
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
App::uses('PhpReader', 'Configure');
/**
* ConfigureTest
*
* @package Cake.Test.Case.Core
*/
class ConfigureTest extends CakeTestCase {
/**
* setUp method
*
* @return void
*/
public function setUp() {
parent::setUp();
Configure::write('Cache.disable', true);
App::build();
App::objects('plugin', null, true);
}
/**
* tearDown method
*
* @return void
*/
public function tearDown() {
parent::tearDown();
if (file_exists(TMP . 'cache' . DS . 'persistent' . DS . 'cake_core_core_paths')) {
unlink(TMP . 'cache' . DS . 'persistent' . DS . 'cake_core_core_paths');
}
if (file_exists(TMP . 'cache' . DS . 'persistent' . DS . 'cake_core_dir_map')) {
unlink(TMP . 'cache' . DS . 'persistent' . DS . 'cake_core_dir_map');
}
if (file_exists(TMP . 'cache' . DS . 'persistent' . DS . 'cake_core_file_map')) {
unlink(TMP . 'cache' . DS . 'persistent' . DS . 'cake_core_file_map');
}
if (file_exists(TMP . 'cache' . DS . 'persistent' . DS . 'cake_core_object_map')) {
unlink(TMP . 'cache' . DS . 'persistent' . DS . 'cake_core_object_map');
}
if (file_exists(TMP . 'cache' . DS . 'persistent' . DS . 'test.config.php')) {
unlink(TMP . 'cache' . DS . 'persistent' . DS . 'test.config.php');
}
if (file_exists(TMP . 'cache' . DS . 'persistent' . DS . 'test.php')) {
unlink(TMP . 'cache' . DS . 'persistent' . DS . 'test.php');
}
Configure::drop('test');
}
/**
* Test to ensure bootrapping doesn't overwrite prior configs set under 'App' key
* @return void
*/
public function testBootstrap() {
$expected = array(
'foo' => 'bar'
);
Configure::write('App', $expected);
Configure::bootstrap(true);
$result = Configure::read('App');
$this->assertEquals($expected['foo'], $result['foo']);
$this->assertFalse($result['base']);
}
/**
* testRead method
*
* @return void
*/
public function testRead() {
$expected = 'ok';
Configure::write('level1.level2.level3_1', $expected);
Configure::write('level1.level2.level3_2', 'something_else');
$result = Configure::read('level1.level2.level3_1');
$this->assertEquals($expected, $result);
$result = Configure::read('level1.level2.level3_2');
$this->assertEquals('something_else', $result);
$result = Configure::read('debug');
$this->assertTrue($result >= 0);
$result = Configure::read();
$this->assertTrue(is_array($result));
$this->assertTrue(isset($result['debug']));
$this->assertTrue(isset($result['level1']));
$result = Configure::read('something_I_just_made_up_now');
$this->assertEquals(null, $result, 'Missing key should return null.');
}
/**
* testWrite method
*
* @return void
*/
public function testWrite() {
$writeResult = Configure::write('SomeName.someKey', 'myvalue');
$this->assertTrue($writeResult);
$result = Configure::read('SomeName.someKey');
$this->assertEquals('myvalue', $result);
$writeResult = Configure::write('SomeName.someKey', null);
$this->assertTrue($writeResult);
$result = Configure::read('SomeName.someKey');
$this->assertEquals(null, $result);
$expected = array('One' => array('Two' => array('Three' => array('Four' => array('Five' => 'cool')))));
$writeResult = Configure::write('Key', $expected);
$this->assertTrue($writeResult);
$result = Configure::read('Key');
$this->assertEquals($expected, $result);
$result = Configure::read('Key.One');
$this->assertEquals($expected['One'], $result);
$result = Configure::read('Key.One.Two');
$this->assertEquals($expected['One']['Two'], $result);
$result = Configure::read('Key.One.Two.Three.Four.Five');
$this->assertEquals('cool', $result);
Configure::write('one.two.three.four', '4');
$result = Configure::read('one.two.three.four');
$this->assertEquals('4', $result);
}
/**
* test setting display_errors with debug.
*
* @return void
*/
public function testDebugSettingDisplayErrors() {
Configure::write('debug', 0);
$result = ini_get('display_errors');
$this->assertEquals(0, $result);
Configure::write('debug', 2);
$result = ini_get('display_errors');
$this->assertEquals(1, $result);
}
/**
* testDelete method
*
* @return void
*/
public function testDelete() {
Configure::write('SomeName.someKey', 'myvalue');
$result = Configure::read('SomeName.someKey');
$this->assertEquals('myvalue', $result);
Configure::delete('SomeName.someKey');
$result = Configure::read('SomeName.someKey');
$this->assertTrue($result === null);
Configure::write('SomeName', array('someKey' => 'myvalue', 'otherKey' => 'otherValue'));
$result = Configure::read('SomeName.someKey');
$this->assertEquals('myvalue', $result);
$result = Configure::read('SomeName.otherKey');
$this->assertEquals('otherValue', $result);
Configure::delete('SomeName');
$result = Configure::read('SomeName.someKey');
$this->assertTrue($result === null);
$result = Configure::read('SomeName.otherKey');
$this->assertTrue($result === null);
}
/**
* testCheck method
*
* @return void
*/
public function testCheck() {
Configure::write('ConfigureTestCase', 'value');
$this->assertTrue(Configure::check('ConfigureTestCase'));
$this->assertFalse(Configure::check('NotExistingConfigureTestCase'));
}
/**
* testCheckingSavedEmpty method
*
* @return void
*/
public function testCheckingSavedEmpty() {
$this->assertTrue(Configure::write('ConfigureTestCase', 0));
$this->assertTrue(Configure::check('ConfigureTestCase'));
$this->assertTrue(Configure::write('ConfigureTestCase', '0'));
$this->assertTrue(Configure::check('ConfigureTestCase'));
$this->assertTrue(Configure::write('ConfigureTestCase', false));
$this->assertTrue(Configure::check('ConfigureTestCase'));
$this->assertTrue(Configure::write('ConfigureTestCase', null));
$this->assertFalse(Configure::check('ConfigureTestCase'));
}
/**
* testCheckKeyWithSpaces method
*
* @return void
*/
public function testCheckKeyWithSpaces() {
$this->assertTrue(Configure::write('Configure Test', "test"));
$this->assertTrue(Configure::check('Configure Test'));
Configure::delete('Configure Test');
$this->assertTrue(Configure::write('Configure Test.Test Case', "test"));
$this->assertTrue(Configure::check('Configure Test.Test Case'));
}
/**
* testCheckEmpty
*
* @return void
*/
public function testCheckEmpty() {
$this->assertFalse(Configure::check());
}
/**
* testLoad method
*
* @expectedException RuntimeException
* @return void
*/
public function testLoadExceptionOnNonExistantFile() {
Configure::config('test', new PhpReader());
Configure::load('non_existing_configuration_file', 'test');
}
/**
* test load method for default config creation
*
* @return void
*/
public function testLoadDefaultConfig() {
try {
Configure::load('non_existing_configuration_file');
} catch (Exception $e) {
$result = Configure::configured('default');
$this->assertTrue($result);
}
}
/**
* test load with merging
*
* @return void
*/
public function testLoadWithMerge() {
Configure::config('test', new PhpReader(CAKE . 'Test' . DS . 'test_app' . DS . 'Config' . DS));
$result = Configure::load('var_test', 'test');
$this->assertTrue($result);
$this->assertEquals('value', Configure::read('Read'));
$result = Configure::load('var_test2', 'test', true);
$this->assertTrue($result);
$this->assertEquals('value2', Configure::read('Read'));
$this->assertEquals('buried2', Configure::read('Deep.Second.SecondDeepest'));
$this->assertEquals('buried', Configure::read('Deep.Deeper.Deepest'));
$this->assertEquals('Overwrite', Configure::read('TestAcl.classname'));
$this->assertEquals('one', Configure::read('TestAcl.custom'));
}
/**
* test loading with overwrite
*
* @return void
*/
public function testLoadNoMerge() {
Configure::config('test', new PhpReader(CAKE . 'Test' . DS . 'test_app' . DS . 'Config' . DS));
$result = Configure::load('var_test', 'test');
$this->assertTrue($result);
$this->assertEquals('value', Configure::read('Read'));
$result = Configure::load('var_test2', 'test', false);
$this->assertTrue($result);
$this->assertEquals('value2', Configure::read('Read'));
$this->assertEquals('buried2', Configure::read('Deep.Second.SecondDeepest'));
$this->assertNull(Configure::read('Deep.Deeper.Deepest'));
}
/**
* testLoad method
*
* @return void
*/
public function testLoadPlugin() {
App::build(array(
'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
), App::RESET);
Configure::config('test', new PhpReader());
CakePlugin::load('TestPlugin');
$result = Configure::load('TestPlugin.load', 'test');
$this->assertTrue($result);
$expected = '/test_app/plugins/test_plugin/config/load.php';
$config = Configure::read('plugin_load');
$this->assertEquals($expected, $config);
$result = Configure::load('TestPlugin.more.load', 'test');
$this->assertTrue($result);
$expected = '/test_app/plugins/test_plugin/config/more.load.php';
$config = Configure::read('plugin_more_load');
$this->assertEquals($expected, $config);
CakePlugin::unload();
}
/**
* testStore method
*
* @return void
*/
public function testStoreAndRestore() {
Configure::write('Cache.disable', false);
Configure::write('Testing', 'yummy');
$this->assertTrue(Configure::store('store_test', 'default'));
Configure::delete('Testing');
$this->assertNull(Configure::read('Testing'));
Configure::restore('store_test', 'default');
$this->assertEquals('yummy', Configure::read('Testing'));
Cache::delete('store_test', 'default');
}
/**
* test that store and restore only store/restore the provided data.
*
* @return void
*/
public function testStoreAndRestoreWithData() {
Configure::write('Cache.disable', false);
Configure::write('testing', 'value');
Configure::store('store_test', 'default', array('store_test' => 'one'));
Configure::delete('testing');
$this->assertNull(Configure::read('store_test'), 'Calling store with data shouldn\'t modify runtime.');
Configure::restore('store_test', 'default');
$this->assertEquals('one', Configure::read('store_test'));
$this->assertNull(Configure::read('testing'), 'Values that were not stored are not restored.');
Cache::delete('store_test', 'default');
}
/**
* testVersion method
*
* @return void
*/
public function testVersion() {
$result = Configure::version();
$this->assertTrue(version_compare($result, '1.2', '>='));
}
/**
* test adding new readers.
*
* @return void
*/
public function testReaderSetup() {
$reader = new PhpReader();
Configure::config('test', $reader);
$configured = Configure::configured();
$this->assertTrue(in_array('test', $configured));
$this->assertTrue(Configure::configured('test'));
$this->assertFalse(Configure::configured('fake_garbage'));
$this->assertTrue(Configure::drop('test'));
$this->assertFalse(Configure::drop('test'), 'dropping things that do not exist should return false.');
}
/**
* test reader() throwing exceptions on missing interface.
*
* @expectedException PHPUnit_Framework_Error
* @return void
*/
public function testReaderExceptionOnIncorrectClass() {
$reader = new StdClass();
Configure::config('test', $reader);
}
/**
* Test that clear wipes all values.
*
* @return void
*/
public function testClear() {
Configure::write('test', 'value');
$this->assertTrue(Configure::clear());
$this->assertNull(Configure::read('debug'));
$this->assertNull(Configure::read('test'));
}
/**
* @expectedException ConfigureException
* @return void
*/
public function testDumpNoAdapter() {
Configure::dump(TMP . 'test.php', 'does_not_exist');
}
/**
* test dump integrated with the PhpReader.
*
* @return void
*/
public function testDump() {
Configure::config('test_reader', new PhpReader(TMP));
$result = Configure::dump('config_test.php', 'test_reader');
$this->assertTrue($result > 0);
$result = file_get_contents(TMP . 'config_test.php');
$this->assertContains('<?php', $result);
$this->assertContains('$config = ', $result);
if (file_exists(TMP . 'config_test.php')) {
unlink(TMP . 'config_test.php');
}
}
/**
* Test dumping only some of the data.
*
* @return void
*/
public function testDumpPartial() {
Configure::config('test_reader', new PhpReader(TMP));
$result = Configure::dump('config_test.php', 'test_reader', array('Error'));
$this->assertTrue($result > 0);
$result = file_get_contents(TMP . 'config_test.php');
$this->assertContains('<?php', $result);
$this->assertContains('$config = ', $result);
$this->assertContains('Error', $result);
$this->assertNotContains('debug', $result);
if (file_exists(TMP . 'config_test.php')) {
unlink(TMP . 'config_test.php');
}
}
}

View file

@ -0,0 +1,679 @@
<?php
/**
* ObjectTest file
*
* CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
* @package Cake.Test.Case.Core
* @since CakePHP(tm) v 1.2.0.5432
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
App::uses('Object', 'Core');
App::uses('Router', 'Routing');
App::uses('Controller', 'Controller');
App::uses('Model', 'Model');
/**
* RequestActionPost class
*
* @package Cake.Test.Case.Core
*/
class RequestActionPost extends CakeTestModel {
/**
* useTable property
*
* @var string
*/
public $useTable = 'posts';
}
/**
* RequestActionController class
*
* @package Cake.Test.Case.Core
*/
class RequestActionController extends Controller {
/**
* uses property
*
* @var array
*/
public $uses = array('RequestActionPost');
/**
* test_request_action method
*
* @return void
*/
public function test_request_action() {
return 'This is a test';
}
/**
* another_ra_test method
*
* @param mixed $id
* @param mixed $other
* @return void
*/
public function another_ra_test($id, $other) {
return $id + $other;
}
/**
* normal_request_action method
*
* @return void
*/
public function normal_request_action() {
return 'Hello World';
}
/**
* returns $this->here
*
* @return void
*/
public function return_here() {
return $this->here;
}
/**
* paginate_request_action method
*
* @return void
*/
public function paginate_request_action() {
$this->paginate();
return true;
}
/**
* post pass, testing post passing
*
* @return array
*/
public function post_pass() {
return $this->request->data;
}
/**
* test param passing and parsing.
*
* @return array
*/
public function params_pass() {
return $this->request;
}
public function param_check() {
$this->autoRender = false;
$content = '';
if (isset($this->request->params[0])) {
$content = 'return found';
}
$this->response->body($content);
}
}
/**
* TestObject class
*
* @package Cake.Test.Case.Core
*/
class TestObject extends Object {
/**
* firstName property
*
* @var string
*/
public $firstName = 'Joel';
/**
* lastName property
*
* @var string
*/
public $lastName = 'Moss';
/**
* methodCalls property
*
* @var array
*/
public $methodCalls = array();
/**
* emptyMethod method
*
* @return void
*/
public function emptyMethod() {
$this->methodCalls[] = 'emptyMethod';
}
/**
* oneParamMethod method
*
* @param mixed $param
* @return void
*/
public function oneParamMethod($param) {
$this->methodCalls[] = array('oneParamMethod' => array($param));
}
/**
* twoParamMethod method
*
* @param mixed $param
* @param mixed $paramTwo
* @return void
*/
public function twoParamMethod($param, $paramTwo) {
$this->methodCalls[] = array('twoParamMethod' => array($param, $paramTwo));
}
/**
* threeParamMethod method
*
* @param mixed $param
* @param mixed $paramTwo
* @param mixed $paramThree
* @return void
*/
public function threeParamMethod($param, $paramTwo, $paramThree) {
$this->methodCalls[] = array('threeParamMethod' => array($param, $paramTwo, $paramThree));
}
/**
* fourParamMethod method
*
* @param mixed $param
* @param mixed $paramTwo
* @param mixed $paramThree
* @param mixed $paramFour
* @return void
*/
public function fourParamMethod($param, $paramTwo, $paramThree, $paramFour) {
$this->methodCalls[] = array('fourParamMethod' => array($param, $paramTwo, $paramThree, $paramFour));
}
/**
* fiveParamMethod method
*
* @param mixed $param
* @param mixed $paramTwo
* @param mixed $paramThree
* @param mixed $paramFour
* @param mixed $paramFive
* @return void
*/
public function fiveParamMethod($param, $paramTwo, $paramThree, $paramFour, $paramFive) {
$this->methodCalls[] = array('fiveParamMethod' => array($param, $paramTwo, $paramThree, $paramFour, $paramFive));
}
/**
* crazyMethod method
*
* @param mixed $param
* @param mixed $paramTwo
* @param mixed $paramThree
* @param mixed $paramFour
* @param mixed $paramFive
* @param mixed $paramSix
* @param mixed $paramSeven
* @return void
*/
public function crazyMethod($param, $paramTwo, $paramThree, $paramFour, $paramFive, $paramSix, $paramSeven = null) {
$this->methodCalls[] = array('crazyMethod' => array($param, $paramTwo, $paramThree, $paramFour, $paramFive, $paramSix, $paramSeven));
}
/**
* methodWithOptionalParam method
*
* @param mixed $param
* @return void
*/
public function methodWithOptionalParam($param = null) {
$this->methodCalls[] = array('methodWithOptionalParam' => array($param));
}
/**
* undocumented function
*
* @return void
*/
public function set($properties = array()) {
return parent::_set($properties);
}
}
/**
* ObjectTestModel class
*
* @package Cake.Test.Case.Core
*/
class ObjectTestModel extends CakeTestModel {
public $useTable = false;
}
/**
* Object Test class
*
* @package Cake.Test.Case.Core
*/
class ObjectTest extends CakeTestCase {
/**
* fixtures
*
* @var string
*/
public $fixtures = array('core.post', 'core.test_plugin_comment', 'core.comment');
/**
* setUp method
*
* @return void
*/
public function setUp() {
parent::setUp();
$this->object = new TestObject();
}
/**
* tearDown method
*
* @return void
*/
public function tearDown() {
parent::tearDown();
CakePlugin::unload();
unset($this->object);
}
/**
* testLog method
*
* @return void
*/
public function testLog() {
if (file_exists(LOGS . 'error.log')) {
unlink(LOGS . 'error.log');
}
$this->assertTrue($this->object->log('Test warning 1'));
$this->assertTrue($this->object->log(array('Test' => 'warning 2')));
$result = file(LOGS . 'error.log');
$this->assertRegExp('/^2[0-9]{3}-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+ Error: Test warning 1$/', $result[0]);
$this->assertRegExp('/^2[0-9]{3}-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+ Error: Array$/', $result[1]);
$this->assertRegExp('/^\($/', $result[2]);
$this->assertRegExp('/\[Test\] => warning 2$/', $result[3]);
$this->assertRegExp('/^\)$/', $result[4]);
unlink(LOGS . 'error.log');
$this->assertTrue($this->object->log('Test warning 1', LOG_WARNING));
$this->assertTrue($this->object->log(array('Test' => 'warning 2'), LOG_WARNING));
$result = file(LOGS . 'error.log');
$this->assertRegExp('/^2[0-9]{3}-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+ Warning: Test warning 1$/', $result[0]);
$this->assertRegExp('/^2[0-9]{3}-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+ Warning: Array$/', $result[1]);
$this->assertRegExp('/^\($/', $result[2]);
$this->assertRegExp('/\[Test\] => warning 2$/', $result[3]);
$this->assertRegExp('/^\)$/', $result[4]);
unlink(LOGS . 'error.log');
}
/**
* testSet method
*
* @return void
*/
public function testSet() {
$this->object->set('a string');
$this->assertEquals('Joel', $this->object->firstName);
$this->object->set(array('firstName'));
$this->assertEquals('Joel', $this->object->firstName);
$this->object->set(array('firstName' => 'Ashley'));
$this->assertEquals('Ashley', $this->object->firstName);
$this->object->set(array('firstName' => 'Joel', 'lastName' => 'Moose'));
$this->assertEquals('Joel', $this->object->firstName);
$this->assertEquals('Moose', $this->object->lastName);
}
/**
* testToString method
*
* @return void
*/
public function testToString() {
$result = strtolower($this->object->toString());
$this->assertEquals('testobject', $result);
}
/**
* testMethodDispatching method
*
* @return void
*/
public function testMethodDispatching() {
$this->object->emptyMethod();
$expected = array('emptyMethod');
$this->assertSame($expected, $this->object->methodCalls);
$this->object->oneParamMethod('Hello');
$expected[] = array('oneParamMethod' => array('Hello'));
$this->assertSame($expected, $this->object->methodCalls);
$this->object->twoParamMethod(true, false);
$expected[] = array('twoParamMethod' => array(true, false));
$this->assertSame($expected, $this->object->methodCalls);
$this->object->threeParamMethod(true, false, null);
$expected[] = array('threeParamMethod' => array(true, false, null));
$this->assertSame($expected, $this->object->methodCalls);
$this->object->crazyMethod(1, 2, 3, 4, 5, 6, 7);
$expected[] = array('crazyMethod' => array(1, 2, 3, 4, 5, 6, 7));
$this->assertSame($expected, $this->object->methodCalls);
$this->object = new TestObject();
$this->assertSame($this->object->methodCalls, array());
$this->object->dispatchMethod('emptyMethod');
$expected = array('emptyMethod');
$this->assertSame($expected, $this->object->methodCalls);
$this->object->dispatchMethod('oneParamMethod', array('Hello'));
$expected[] = array('oneParamMethod' => array('Hello'));
$this->assertSame($expected, $this->object->methodCalls);
$this->object->dispatchMethod('twoParamMethod', array(true, false));
$expected[] = array('twoParamMethod' => array(true, false));
$this->assertSame($expected, $this->object->methodCalls);
$this->object->dispatchMethod('threeParamMethod', array(true, false, null));
$expected[] = array('threeParamMethod' => array(true, false, null));
$this->assertSame($expected, $this->object->methodCalls);
$this->object->dispatchMethod('fourParamMethod', array(1, 2, 3, 4));
$expected[] = array('fourParamMethod' => array(1, 2, 3, 4));
$this->assertSame($expected, $this->object->methodCalls);
$this->object->dispatchMethod('fiveParamMethod', array(1, 2, 3, 4, 5));
$expected[] = array('fiveParamMethod' => array(1, 2, 3, 4, 5));
$this->assertSame($expected, $this->object->methodCalls);
$this->object->dispatchMethod('crazyMethod', array(1, 2, 3, 4, 5, 6, 7));
$expected[] = array('crazyMethod' => array(1, 2, 3, 4, 5, 6, 7));
$this->assertSame($expected, $this->object->methodCalls);
$this->object->dispatchMethod('methodWithOptionalParam', array('Hello'));
$expected[] = array('methodWithOptionalParam' => array("Hello"));
$this->assertSame($expected, $this->object->methodCalls);
$this->object->dispatchMethod('methodWithOptionalParam');
$expected[] = array('methodWithOptionalParam' => array(null));
$this->assertSame($expected, $this->object->methodCalls);
}
/**
* testRequestAction method
*
* @return void
*/
public function testRequestAction() {
App::build(array(
'Model' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Model' . DS),
'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS),
'Controller' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Controller' . DS)
), App::RESET);
$this->assertNull(Router::getRequest(), 'request stack should be empty.');
$result = $this->object->requestAction('');
$this->assertFalse($result);
$result = $this->object->requestAction('/request_action/test_request_action');
$expected = 'This is a test';
$this->assertEquals($expected, $result);
$result = $this->object->requestAction(
Configure::read('App.fullBaseUrl') . '/request_action/test_request_action'
);
$expected = 'This is a test';
$this->assertEquals($expected, $result);
$result = $this->object->requestAction('/request_action/another_ra_test/2/5');
$expected = 7;
$this->assertEquals($expected, $result);
$result = $this->object->requestAction('/tests_apps/index', array('return'));
$expected = 'This is the TestsAppsController index view ';
$this->assertEquals($expected, $result);
$result = $this->object->requestAction('/tests_apps/some_method');
$expected = 5;
$this->assertEquals($expected, $result);
$result = $this->object->requestAction('/request_action/paginate_request_action');
$this->assertTrue($result);
$result = $this->object->requestAction('/request_action/normal_request_action');
$expected = 'Hello World';
$this->assertEquals($expected, $result);
$this->assertNull(Router::getRequest(), 'requests were not popped off the stack, this will break url generation');
}
/**
* test requestAction() and plugins.
*
* @return void
*/
public function testRequestActionPlugins() {
App::build(array(
'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS),
), App::RESET);
CakePlugin::load('TestPlugin');
Router::reload();
$result = $this->object->requestAction('/test_plugin/tests/index', array('return'));
$expected = 'test plugin index';
$this->assertEquals($expected, $result);
$result = $this->object->requestAction('/test_plugin/tests/index/some_param', array('return'));
$expected = 'test plugin index';
$this->assertEquals($expected, $result);
$result = $this->object->requestAction(
array('controller' => 'tests', 'action' => 'index', 'plugin' => 'test_plugin'), array('return')
);
$expected = 'test plugin index';
$this->assertEquals($expected, $result);
$result = $this->object->requestAction('/test_plugin/tests/some_method');
$expected = 25;
$this->assertEquals($expected, $result);
$result = $this->object->requestAction(
array('controller' => 'tests', 'action' => 'some_method', 'plugin' => 'test_plugin')
);
$expected = 25;
$this->assertEquals($expected, $result);
}
/**
* test requestAction() with arrays.
*
* @return void
*/
public function testRequestActionArray() {
App::build(array(
'Model' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Model' . DS),
'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS),
'Controller' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Controller' . DS),
'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
), App::RESET);
CakePlugin::load(array('TestPlugin'));
$result = $this->object->requestAction(
array('controller' => 'request_action', 'action' => 'test_request_action')
);
$expected = 'This is a test';
$this->assertEquals($expected, $result);
$result = $this->object->requestAction(
array('controller' => 'request_action', 'action' => 'another_ra_test'),
array('pass' => array('5', '7'))
);
$expected = 12;
$this->assertEquals($expected, $result);
$result = $this->object->requestAction(
array('controller' => 'tests_apps', 'action' => 'index'), array('return')
);
$expected = 'This is the TestsAppsController index view ';
$this->assertEquals($expected, $result);
$result = $this->object->requestAction(array('controller' => 'tests_apps', 'action' => 'some_method'));
$expected = 5;
$this->assertEquals($expected, $result);
$result = $this->object->requestAction(
array('controller' => 'request_action', 'action' => 'normal_request_action')
);
$expected = 'Hello World';
$this->assertEquals($expected, $result);
$result = $this->object->requestAction(
array('controller' => 'request_action', 'action' => 'paginate_request_action')
);
$this->assertTrue($result);
$result = $this->object->requestAction(
array('controller' => 'request_action', 'action' => 'paginate_request_action'),
array('pass' => array(5), 'named' => array('param' => 'value'))
);
$this->assertTrue($result);
}
/**
* Test that requestAction() does not forward the 0 => return value.
*
* @return void
*/
public function testRequestActionRemoveReturnParam() {
$result = $this->object->requestAction(
'/request_action/param_check', array('return')
);
$this->assertEquals('', $result, 'Return key was found');
}
/**
* Test that requestAction() is populating $this->params properly
*
* @return void
*/
public function testRequestActionParamParseAndPass() {
$result = $this->object->requestAction('/request_action/params_pass');
$this->assertEquals('request_action/params_pass', $result->url);
$this->assertEquals('request_action', $result['controller']);
$this->assertEquals('params_pass', $result['action']);
$this->assertEquals(null, $result['plugin']);
$result = $this->object->requestAction('/request_action/params_pass/sort:desc/limit:5');
$expected = array('sort' => 'desc', 'limit' => 5);
$this->assertEquals($expected, $result['named']);
$result = $this->object->requestAction(
array('controller' => 'request_action', 'action' => 'params_pass'),
array('named' => array('sort' => 'desc', 'limit' => 5))
);
$this->assertEquals($expected, $result['named']);
}
/**
* Test that requestAction handles get parameters correctly.
*
* @return void
*/
public function testRequestActionGetParameters() {
$result = $this->object->requestAction(
'/request_action/params_pass?get=value&limit=5'
);
$this->assertEquals('value', $result->query['get']);
$result = $this->object->requestAction(
array('controller' => 'request_action', 'action' => 'params_pass'),
array('url' => array('get' => 'value', 'limit' => 5))
);
$this->assertEquals('value', $result->query['get']);
}
/**
* test that requestAction does not fish data out of the POST
* superglobal.
*
* @return void
*/
public function testRequestActionNoPostPassing() {
$_tmp = $_POST;
$_POST = array('data' => array(
'item' => 'value'
));
$result = $this->object->requestAction(array('controller' => 'request_action', 'action' => 'post_pass'));
$this->assertEmpty($result);
$result = $this->object->requestAction(
array('controller' => 'request_action', 'action' => 'post_pass'),
array('data' => $_POST['data'])
);
$expected = $_POST['data'];
$this->assertEquals($expected, $result);
$result = $this->object->requestAction('/request_action/post_pass');
$expected = $_POST['data'];
$this->assertEquals($expected, $result);
$_POST = $_tmp;
}
/**
* Test requestAction with post data.
*
* @return void
*/
public function testRequestActionPostWithData() {
$data = array(
'Post' => array('id' => 2)
);
$result = $this->object->requestAction(
array('controller' => 'request_action', 'action' => 'post_pass'),
array('data' => $data)
);
$this->assertEquals($data, $result);
$result = $this->object->requestAction(
'/request_action/post_pass',
array('data' => $data)
);
$this->assertEquals($data, $result);
}
}