2008-05-30 11:40:08 +00:00
< ? php
/* SVN FILE: $Id$ */
/**
2009-03-19 21:10:13 +00:00
* ComponentTest file
2008-05-30 11:40:08 +00:00
*
* Long description for file
*
* PHP versions 4 and 5
*
* CakePHP ( tm ) Tests < https :// trac . cakephp . org / wiki / Developement / TestSuite >
2008-10-30 17:30:26 +00:00
* Copyright 2005 - 2008 , Cake Software Foundation , Inc . ( http :// www . cakefoundation . org )
2008-05-30 11:40:08 +00:00
*
* Licensed under The Open Group Test Suite License
* Redistributions of files must retain the above copyright notice .
*
* @ filesource
2008-10-30 17:30:26 +00:00
* @ copyright Copyright 2005 - 2008 , Cake Software Foundation , Inc . ( http :// www . cakefoundation . org )
* @ link https :// trac . cakephp . org / wiki / Developement / TestSuite CakePHP ( tm ) Tests
2009-03-19 21:10:13 +00:00
* @ package cake
2008-10-30 17:30:26 +00:00
* @ subpackage cake . tests . cases . libs . controller
* @ since CakePHP ( tm ) v 1.2 . 0.5436
* @ version $Revision $
* @ modifiedby $LastChangedBy $
* @ lastmodified $Date $
* @ license http :// www . opensource . org / licenses / opengroup . php The Open Group Test Suite License
2008-05-30 11:40:08 +00:00
*/
2008-06-04 02:39:05 +00:00
App :: import ( 'Core' , array ( 'Component' , 'Controller' ));
if ( ! class_exists ( 'AppController' )) {
2008-06-05 15:20:45 +00:00
/**
* AppController class
2008-06-06 16:18:14 +00:00
*
2008-10-30 17:30:26 +00:00
* @ package cake
* @ subpackage cake . tests . cases . libs . controller
2008-06-05 15:20:45 +00:00
*/
2008-06-04 02:39:05 +00:00
class AppController extends Controller {
2008-06-05 15:20:45 +00:00
/**
* name property
2008-06-06 16:18:14 +00:00
*
2008-06-05 15:20:45 +00:00
* @ var string 'App'
* @ access public
*/
2008-06-04 02:39:05 +00:00
var $name = 'App' ;
2008-06-05 15:20:45 +00:00
/**
* uses property
2008-06-06 16:18:14 +00:00
*
2008-06-05 15:20:45 +00:00
* @ var array
* @ access public
*/
2008-06-04 02:39:05 +00:00
var $uses = array ();
2008-06-05 15:20:45 +00:00
/**
* helpers property
2008-06-06 16:18:14 +00:00
*
2008-06-05 15:20:45 +00:00
* @ var array
* @ access public
*/
2008-06-04 02:39:05 +00:00
var $helpers = array ();
2008-06-05 15:20:45 +00:00
/**
* components property
2008-06-06 16:18:14 +00:00
*
2008-06-05 15:20:45 +00:00
* @ var array
* @ access public
*/
2008-06-04 02:39:05 +00:00
var $components = array ( 'Orange' => array ( 'colour' => 'blood orange' ));
2008-06-06 16:18:14 +00:00
2008-06-04 02:39:05 +00:00
}
2008-08-19 01:34:44 +00:00
} else if ( ! defined ( 'APP_CONTROLLER_EXISTS' )){
2008-06-20 20:17:23 +00:00
define ( 'APP_CONTROLLER_EXISTS' , true );
2008-06-04 02:39:05 +00:00
}
/**
* ParamTestComponent
*
2009-03-19 21:10:13 +00:00
* @ package cake
* @ subpackage cake . tests . cases . libs . controller
2008-06-04 02:39:05 +00:00
*/
class ParamTestComponent extends Object {
2008-06-05 15:20:45 +00:00
/**
* name property
2008-06-06 16:18:14 +00:00
*
2008-06-05 15:20:45 +00:00
* @ var string 'ParamTest'
* @ access public
*/
2008-06-04 02:39:05 +00:00
var $name = 'ParamTest' ;
2008-06-10 22:38:05 +00:00
/**
2008-06-05 15:20:45 +00:00
* components property
2008-06-06 16:18:14 +00:00
*
2008-06-05 15:20:45 +00:00
* @ var array
* @ access public
*/
2008-06-04 02:39:05 +00:00
var $components = array ( 'Banana' => array ( 'config' => 'value' ));
2008-06-10 22:38:05 +00:00
/**
2008-06-05 15:20:45 +00:00
* initialize method
2008-06-06 16:18:14 +00:00
*
* @ param mixed $controller
* @ param mixed $settings
2008-06-05 15:20:45 +00:00
* @ access public
* @ return void
*/
2008-06-04 02:39:05 +00:00
function initialize ( & $controller , $settings ) {
foreach ( $settings as $key => $value ) {
if ( is_numeric ( $key )) {
$this -> { $value } = true ;
} else {
$this -> { $key } = $value ;
}
}
}
}
2008-05-30 11:40:08 +00:00
/**
2009-03-19 21:10:13 +00:00
* ComponentTestController class
2008-05-30 11:40:08 +00:00
*
2009-03-19 21:10:13 +00:00
* @ package cake
2008-10-30 17:30:26 +00:00
* @ subpackage cake . tests . cases . libs . controller
2008-05-30 11:40:08 +00:00
*/
class ComponentTestController extends AppController {
2008-06-02 19:22:55 +00:00
/**
* name property
2008-06-06 16:18:14 +00:00
*
2008-06-02 19:22:55 +00:00
* @ var string 'ComponentTest'
* @ access public
*/
2008-05-31 03:54:22 +00:00
var $name = 'ComponentTest' ;
2008-06-02 19:22:55 +00:00
/**
* uses property
2008-06-06 16:18:14 +00:00
*
2008-06-02 19:22:55 +00:00
* @ var array
* @ access public
*/
2008-05-30 11:40:08 +00:00
var $uses = array ();
}
2008-06-02 19:22:55 +00:00
/**
* AppleComponent class
2008-06-06 16:18:14 +00:00
*
2008-10-30 17:30:26 +00:00
* @ package cake
* @ subpackage cake . tests . cases . libs . controller
2008-06-02 19:22:55 +00:00
*/
2008-05-31 03:54:22 +00:00
class AppleComponent extends Object {
2008-06-02 19:22:55 +00:00
/**
* components property
2008-06-06 16:18:14 +00:00
*
2008-06-02 19:22:55 +00:00
* @ var array
* @ access public
*/
2008-05-31 03:54:22 +00:00
var $components = array ( 'Orange' );
2008-06-02 19:22:55 +00:00
/**
* testName property
2008-06-06 16:18:14 +00:00
*
2008-06-02 19:22:55 +00:00
* @ var mixed null
* @ access public
*/
2008-06-02 04:57:06 +00:00
var $testName = null ;
2008-06-02 19:22:55 +00:00
/**
* startup method
2008-06-06 16:18:14 +00:00
*
* @ param mixed $controller
2008-06-02 19:22:55 +00:00
* @ access public
* @ return void
*/
2008-05-31 03:54:22 +00:00
function startup ( & $controller ) {
2008-06-02 04:57:06 +00:00
$this -> testName = $controller -> name ;
2008-05-31 03:54:22 +00:00
}
}
2008-06-02 19:22:55 +00:00
/**
* OrangeComponent class
2008-06-06 16:18:14 +00:00
*
2008-10-30 17:30:26 +00:00
* @ package cake
* @ subpackage cake . tests . cases . libs . controller
2008-06-02 19:22:55 +00:00
*/
2008-05-31 03:54:22 +00:00
class OrangeComponent extends Object {
2008-06-02 19:22:55 +00:00
/**
* components property
2008-06-06 16:18:14 +00:00
*
2008-06-02 19:22:55 +00:00
* @ var array
* @ access public
*/
2008-05-31 03:54:22 +00:00
var $components = array ( 'Banana' );
2008-06-02 19:22:55 +00:00
/**
* initialize method
2008-06-06 16:18:14 +00:00
*
* @ param mixed $controller
2008-06-02 19:22:55 +00:00
* @ access public
* @ return void
*/
2008-06-04 02:39:05 +00:00
function initialize ( & $controller , $settings ) {
2009-01-02 22:47:38 +00:00
$this -> Controller = $controller ;
2008-06-02 04:57:06 +00:00
$this -> Banana -> testField = 'OrangeField' ;
2008-06-04 02:39:05 +00:00
$this -> settings = $settings ;
2008-06-02 04:57:06 +00:00
}
2009-03-19 21:10:13 +00:00
/**
* startup method
*
* @ param Controller $controller
* @ return string
* @ access public
*/
2008-10-15 13:07:46 +00:00
function startup ( & $controller ) {
$controller -> foo = 'pass' ;
}
2008-05-31 03:54:22 +00:00
}
2008-06-02 19:22:55 +00:00
/**
* BananaComponent class
2008-06-06 16:18:14 +00:00
*
2008-10-30 17:30:26 +00:00
* @ package cake
* @ subpackage cake . tests . cases . libs . controller
2008-06-02 19:22:55 +00:00
*/
2008-05-31 03:54:22 +00:00
class BananaComponent extends Object {
2008-06-02 19:22:55 +00:00
/**
* testField property
2008-06-06 16:18:14 +00:00
*
2008-06-02 19:22:55 +00:00
* @ var string 'BananaField'
* @ access public
*/
2008-06-02 04:57:06 +00:00
var $testField = 'BananaField' ;
2009-03-19 21:10:13 +00:00
/**
* startup method
*
* @ param Controller $controller
* @ return string
* @ access public
*/
2008-10-15 13:07:46 +00:00
function startup ( & $controller ) {
$controller -> bar = 'fail' ;
}
2008-05-31 03:54:22 +00:00
}
2008-06-06 16:18:14 +00:00
/**
* MutuallyReferencingOneComponent class
*
2008-10-30 17:30:26 +00:00
* @ package cake
* @ subpackage cake . tests . cases . libs . controller
2008-06-06 16:18:14 +00:00
*/
class MutuallyReferencingOneComponent extends Object {
2009-03-19 21:10:13 +00:00
/**
* components property
*
* @ var array
* @ access public
*/
2008-06-06 16:18:14 +00:00
var $components = array ( 'MutuallyReferencingTwo' );
}
/**
* MutuallyReferencingTwoComponent class
*
2008-10-30 17:30:26 +00:00
* @ package cake
* @ subpackage cake . tests . cases . libs . controller
2008-06-06 16:18:14 +00:00
*/
class MutuallyReferencingTwoComponent extends Object {
2009-03-19 21:10:13 +00:00
/**
* components property
*
* @ var array
* @ access public
*/
2008-06-06 16:18:14 +00:00
var $components = array ( 'MutuallyReferencingOne' );
}
2009-01-02 22:47:38 +00:00
/**
* SomethingWithEmailComponent class
*
* @ package cake
* @ subpackage cake . tests . cases . libs . controller
*/
class SomethingWithEmailComponent extends Object {
2009-03-19 21:10:13 +00:00
/**
* components property
*
* @ var array
* @ access public
*/
2009-01-02 22:47:38 +00:00
var $components = array ( 'Email' );
}
2008-06-02 19:22:55 +00:00
/**
* ComponentTest class
2008-06-06 16:18:14 +00:00
*
2008-10-30 17:30:26 +00:00
* @ package cake
* @ subpackage cake . tests . cases . libs . controller
2008-06-02 19:22:55 +00:00
*/
2008-05-30 11:40:08 +00:00
class ComponentTest extends CakeTestCase {
2008-06-02 19:22:55 +00:00
/**
* setUp method
2008-06-06 16:18:14 +00:00
*
2008-06-02 19:22:55 +00:00
* @ access public
* @ return void
*/
2008-05-30 11:40:08 +00:00
function setUp () {
2008-10-15 13:07:46 +00:00
Configure :: write ( 'pluginPaths' , array (
TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS
));
2008-05-30 11:40:08 +00:00
}
2008-06-02 19:22:55 +00:00
/**
* testLoadComponents method
2008-06-06 16:18:14 +00:00
*
2008-06-02 19:22:55 +00:00
* @ access public
* @ return void
*/
2008-05-30 11:40:08 +00:00
function testLoadComponents () {
2008-05-31 03:54:22 +00:00
$Controller =& new ComponentTestController ();
2008-05-30 11:40:08 +00:00
$Controller -> components = array ( 'RequestHandler' );
2008-05-31 03:54:22 +00:00
$Component =& new Component ();
2008-05-30 11:40:08 +00:00
$Component -> init ( $Controller );
$this -> assertTrue ( is_a ( $Controller -> RequestHandler , 'RequestHandlerComponent' ));
2008-05-31 03:54:22 +00:00
$Controller =& new ComponentTestController ();
2008-05-30 11:40:08 +00:00
$Controller -> plugin = 'test_plugin' ;
$Controller -> components = array ( 'RequestHandler' , 'TestPluginComponent' );
2008-05-31 03:54:22 +00:00
$Component =& new Component ();
2008-05-30 11:40:08 +00:00
$Component -> init ( $Controller );
$this -> assertTrue ( is_a ( $Controller -> RequestHandler , 'RequestHandlerComponent' ));
$this -> assertTrue ( is_a ( $Controller -> TestPluginComponent , 'TestPluginComponentComponent' ));
2008-10-15 13:07:46 +00:00
$this -> assertTrue ( is_a (
$Controller -> TestPluginComponent -> TestPluginOtherComponent ,
'TestPluginOtherComponentComponent'
));
2008-05-30 11:40:08 +00:00
$this -> assertFalse ( isset ( $Controller -> TestPluginOtherComponent ));
2008-05-31 03:54:22 +00:00
$Controller =& new ComponentTestController ();
2008-05-30 11:40:08 +00:00
$Controller -> components = array ( 'Security' );
2008-05-31 03:54:22 +00:00
$Component =& new Component ();
2008-05-30 11:40:08 +00:00
$Component -> init ( $Controller );
$this -> assertTrue ( is_a ( $Controller -> Security , 'SecurityComponent' ));
$this -> assertTrue ( is_a ( $Controller -> Security -> Session , 'SessionComponent' ));
2008-05-31 03:54:22 +00:00
$Controller =& new ComponentTestController ();
2008-05-30 11:40:08 +00:00
$Controller -> components = array ( 'Security' , 'Cookie' , 'RequestHandler' );
2008-05-31 03:54:22 +00:00
$Component =& new Component ();
2008-05-30 11:40:08 +00:00
$Component -> init ( $Controller );
$this -> assertTrue ( is_a ( $Controller -> Security , 'SecurityComponent' ));
$this -> assertTrue ( is_a ( $Controller -> Security -> RequestHandler , 'RequestHandlerComponent' ));
$this -> assertTrue ( is_a ( $Controller -> RequestHandler , 'RequestHandlerComponent' ));
$this -> assertTrue ( is_a ( $Controller -> Cookie , 'CookieComponent' ));
}
2008-06-04 02:39:05 +00:00
/**
* test component loading
*
* @ return void
*/
2008-05-31 03:54:22 +00:00
function testNestedComponentLoading () {
$Controller =& new ComponentTestController ();
$Controller -> components = array ( 'Apple' );
$Controller -> constructClasses ();
2008-06-12 18:36:08 +00:00
$Controller -> Component -> initialize ( $Controller );
2008-05-31 03:54:22 +00:00
$this -> assertTrue ( is_a ( $Controller -> Apple , 'AppleComponent' ));
$this -> assertTrue ( is_a ( $Controller -> Apple -> Orange , 'OrangeComponent' ));
$this -> assertTrue ( is_a ( $Controller -> Apple -> Orange -> Banana , 'BananaComponent' ));
2009-01-02 22:47:38 +00:00
$this -> assertTrue ( is_a ( $Controller -> Apple -> Orange -> Controller , 'ComponentTestController' ));
2009-02-13 01:11:00 +00:00
$this -> assertTrue ( empty ( $Controller -> Apple -> Session ));
2009-03-19 21:10:13 +00:00
$this -> assertTrue ( empty ( $Controller -> Apple -> Orange -> Session ));
2008-05-31 03:54:22 +00:00
}
2008-06-04 02:39:05 +00:00
/**
2008-10-15 13:07:46 +00:00
* Tests Component :: startup () and only running callbacks for components directly attached to
* the controller .
2008-06-04 02:39:05 +00:00
*
* @ return void
*/
2008-05-31 03:54:22 +00:00
function testComponentStartup () {
$Controller =& new ComponentTestController ();
$Controller -> components = array ( 'Apple' );
$Controller -> constructClasses ();
2008-06-12 18:36:08 +00:00
$Controller -> Component -> initialize ( $Controller );
$Controller -> beforeFilter ();
$Controller -> Component -> startup ( $Controller );
2008-05-31 03:54:22 +00:00
$this -> assertTrue ( is_a ( $Controller -> Apple , 'AppleComponent' ));
2008-06-02 04:57:06 +00:00
$this -> assertEqual ( $Controller -> Apple -> testName , 'ComponentTest' );
2008-10-15 13:07:46 +00:00
$expected = ! ( defined ( 'APP_CONTROLLER_EXISTS' ) && APP_CONTROLLER_EXISTS );
$this -> assertEqual ( isset ( $Controller -> foo ), $expected );
$this -> assertFalse ( isset ( $Controller -> bar ));
2008-06-02 04:57:06 +00:00
}
2008-06-04 02:39:05 +00:00
/**
* test a component being used more than once .
2008-06-06 16:18:14 +00:00
*
2008-06-04 02:39:05 +00:00
* @ return void
*/
2008-06-02 04:57:06 +00:00
function testMultipleComponentInitialize () {
$Controller =& new ComponentTestController ();
$Controller -> components = array ( 'Orange' , 'Banana' );
$Controller -> constructClasses ();
2008-06-12 18:36:08 +00:00
$Controller -> Component -> initialize ( $Controller );
2008-06-02 04:57:06 +00:00
$this -> assertEqual ( $Controller -> Banana -> testField , 'OrangeField' );
$this -> assertEqual ( $Controller -> Orange -> Banana -> testField , 'OrangeField' );
2008-05-31 03:54:22 +00:00
}
2008-06-04 02:39:05 +00:00
/**
* Test Component declarations with Parameters
* tests merging of component parameters and merging / construction of components .
*
* @ return void
2008-06-06 16:18:14 +00:00
*/
2008-06-04 02:39:05 +00:00
function testComponentsWithParams () {
2008-06-20 20:17:23 +00:00
$this -> skipIf ( defined ( 'APP_CONTROLLER_EXISTS' ), 'Components with Params test will be skipped as it needs a non-existent AppController. As the an AppController class exists, this cannot be run.' );
2008-06-06 16:18:14 +00:00
2008-06-04 02:39:05 +00:00
$Controller =& new ComponentTestController ();
$Controller -> components = array ( 'ParamTest' => array ( 'test' => 'value' , 'flag' ), 'Apple' );
2008-06-06 16:18:14 +00:00
2008-06-04 02:39:05 +00:00
$Controller -> constructClasses ();
2008-06-12 18:36:08 +00:00
$Controller -> Component -> initialize ( $Controller );
2008-06-06 16:18:14 +00:00
2008-06-04 02:39:05 +00:00
$this -> assertTrue ( is_a ( $Controller -> ParamTest , 'ParamTestComponent' ));
$this -> assertTrue ( is_a ( $Controller -> ParamTest -> Banana , 'BananaComponent' ));
$this -> assertTrue ( is_a ( $Controller -> Orange , 'OrangeComponent' ));
$this -> assertTrue ( is_a ( $Controller -> Session , 'SessionComponent' ));
$this -> assertEqual ( $Controller -> Orange -> settings , array ( 'colour' => 'blood orange' ));
$this -> assertEqual ( $Controller -> ParamTest -> test , 'value' );
$this -> assertEqual ( $Controller -> ParamTest -> flag , true );
//Settings are merged from app controller and current controller.
$Controller =& new ComponentTestController ();
2008-10-15 13:07:46 +00:00
$Controller -> components = array (
'ParamTest' => array ( 'test' => 'value' ),
'Orange' => array ( 'ripeness' => 'perfect' )
);
2008-06-04 02:39:05 +00:00
$Controller -> constructClasses ();
2008-06-12 18:36:08 +00:00
$Controller -> Component -> initialize ( $Controller );
2008-06-06 16:18:14 +00:00
2008-10-15 13:07:46 +00:00
$expected = array ( 'colour' => 'blood orange' , 'ripeness' => 'perfect' );
$this -> assertEqual ( $Controller -> Orange -> settings , $expected );
2008-06-04 02:39:05 +00:00
$this -> assertEqual ( $Controller -> ParamTest -> test , 'value' );
}
2008-06-06 16:18:14 +00:00
/**
* Test mutually referencing components .
*
2008-10-15 13:07:46 +00:00
* @ return void
2008-06-06 16:18:14 +00:00
*/
function testMutuallyReferencingComponents () {
$Controller =& new ComponentTestController ();
$Controller -> components = array ( 'MutuallyReferencingOne' );
$Controller -> constructClasses ();
$Controller -> Component -> initialize ( $Controller );
2008-10-15 13:07:46 +00:00
$this -> assertTrue ( is_a (
$Controller -> MutuallyReferencingOne ,
'MutuallyReferencingOneComponent'
));
$this -> assertTrue ( is_a (
$Controller -> MutuallyReferencingOne -> MutuallyReferencingTwo ,
'MutuallyReferencingTwoComponent'
));
$this -> assertTrue ( is_a (
$Controller -> MutuallyReferencingOne -> MutuallyReferencingTwo -> MutuallyReferencingOne ,
'MutuallyReferencingOneComponent'
));
2008-06-06 16:18:14 +00:00
}
2009-01-02 22:47:38 +00:00
/**
* Test mutually referencing components .
*
* @ return void
*/
function testSomethingReferencingEmailComponent () {
$Controller =& new ComponentTestController ();
$Controller -> components = array ( 'SomethingWithEmail' );
$Controller -> constructClasses ();
$Controller -> Component -> initialize ( $Controller );
$Controller -> beforeFilter ();
$Controller -> Component -> startup ( $Controller );
2009-03-19 21:10:13 +00:00
2009-01-02 22:47:38 +00:00
$this -> assertTrue ( is_a (
$Controller -> SomethingWithEmail ,
'SomethingWithEmailComponent'
));
$this -> assertTrue ( is_a (
$Controller -> SomethingWithEmail -> Email ,
'EmailComponent'
));
$this -> assertTrue ( is_a (
$Controller -> SomethingWithEmail -> Email -> Controller ,
'ComponentTestController'
));
}
2009-02-12 13:36:20 +00:00
/**
* test that SessionComponent doesn ' t get added if its already in the components array .
*
* @ return void
**/
function testDoubleLoadingOfSessionComponent () {
$Controller =& new ComponentTestController ();
$Controller -> uses = array ();
$Controller -> components = array ( 'Session' );
$Controller -> constructClasses ();
$this -> assertEqual ( $Controller -> components , array ( 'Session' => '' , 'Orange' => array ( 'colour' => 'blood orange' )));
}
2008-05-30 11:40:08 +00:00
}
2008-06-06 16:18:14 +00:00
?>