2010-07-04 18:00:23 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* ComponentCollectionTest file
|
|
|
|
*
|
2017-06-10 21:33:55 +00:00
|
|
|
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
|
2017-06-10 22:10:52 +00:00
|
|
|
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
|
2010-07-04 18:00:23 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
2013-02-08 12:22:51 +00:00
|
|
|
* For full copyright and license information, please see the LICENSE.txt
|
2010-07-04 18:00:23 +00:00
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2017-06-10 22:10:52 +00:00
|
|
|
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
|
2017-06-10 22:15:34 +00:00
|
|
|
* @link https://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Test.Case.Controller
|
2010-07-04 18:00:23 +00:00
|
|
|
* @since CakePHP(tm) v 2.0
|
2017-06-10 22:23:14 +00:00
|
|
|
* @license https://opensource.org/licenses/mit-license.php MIT License
|
2010-07-04 18:00:23 +00:00
|
|
|
*/
|
2013-05-30 22:11:14 +00:00
|
|
|
|
2012-02-18 14:32:48 +00:00
|
|
|
App::uses('CakeResponse', 'Network');
|
2010-12-09 05:13:11 +00:00
|
|
|
App::uses('CookieComponent', 'Controller/Component');
|
|
|
|
App::uses('SecurityComponent', 'Controller/Component');
|
|
|
|
App::uses('ComponentCollection', 'Controller');
|
2010-07-04 18:00:23 +00:00
|
|
|
|
2011-01-10 02:27:44 +00:00
|
|
|
/**
|
|
|
|
* Extended CookieComponent
|
|
|
|
*/
|
|
|
|
class CookieAliasComponent extends CookieComponent {
|
|
|
|
}
|
|
|
|
|
2010-07-04 18:00:23 +00:00
|
|
|
class ComponentCollectionTest extends CakeTestCase {
|
2012-03-12 02:20:25 +00:00
|
|
|
|
2010-07-04 18:00:23 +00:00
|
|
|
/**
|
2011-12-04 21:27:51 +00:00
|
|
|
* setUp
|
2010-07-04 18:00:23 +00:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2022-12-28 07:03:39 +00:00
|
|
|
public function setUp() : void {
|
2011-12-04 21:27:51 +00:00
|
|
|
parent::setUp();
|
2010-07-04 18:00:23 +00:00
|
|
|
$this->Components = new ComponentCollection();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-12-04 21:27:51 +00:00
|
|
|
* tearDown
|
2010-07-04 18:00:23 +00:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2022-12-28 07:03:39 +00:00
|
|
|
public function tearDown() : void {
|
2011-12-04 21:27:51 +00:00
|
|
|
parent::tearDown();
|
2012-04-16 02:20:34 +00:00
|
|
|
unset($this->Components);
|
2010-07-04 18:00:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test triggering callbacks on loaded helpers
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testLoad() {
|
2010-07-04 18:00:23 +00:00
|
|
|
$result = $this->Components->load('Cookie');
|
2010-12-24 17:54:04 +00:00
|
|
|
$this->assertInstanceOf('CookieComponent', $result);
|
|
|
|
$this->assertInstanceOf('CookieComponent', $this->Components->Cookie);
|
2010-07-04 18:00:23 +00:00
|
|
|
|
2012-11-16 11:14:28 +00:00
|
|
|
$result = $this->Components->loaded();
|
|
|
|
$this->assertEquals(array('Cookie'), $result, 'loaded() results are wrong.');
|
2010-07-04 18:00:23 +00:00
|
|
|
|
|
|
|
$this->assertTrue($this->Components->enabled('Cookie'));
|
|
|
|
|
|
|
|
$result = $this->Components->load('Cookie');
|
|
|
|
$this->assertSame($result, $this->Components->Cookie);
|
|
|
|
}
|
|
|
|
|
2011-01-10 02:27:44 +00:00
|
|
|
/**
|
|
|
|
* Tests loading as an alias
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testLoadWithAlias() {
|
2011-01-14 02:04:06 +00:00
|
|
|
$result = $this->Components->load('Cookie', array('className' => 'CookieAlias', 'somesetting' => true));
|
2011-01-10 02:27:44 +00:00
|
|
|
$this->assertInstanceOf('CookieAliasComponent', $result);
|
|
|
|
$this->assertInstanceOf('CookieAliasComponent', $this->Components->Cookie);
|
|
|
|
$this->assertTrue($this->Components->Cookie->settings['somesetting']);
|
|
|
|
|
2012-11-16 11:14:28 +00:00
|
|
|
$result = $this->Components->loaded();
|
|
|
|
$this->assertEquals(array('Cookie'), $result, 'loaded() results are wrong.');
|
2011-01-10 02:27:44 +00:00
|
|
|
|
|
|
|
$this->assertTrue($this->Components->enabled('Cookie'));
|
|
|
|
|
|
|
|
$result = $this->Components->load('Cookie');
|
|
|
|
$this->assertInstanceOf('CookieAliasComponent', $result);
|
2011-01-15 01:44:33 +00:00
|
|
|
|
2012-02-18 12:31:29 +00:00
|
|
|
App::build(array('Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)));
|
2011-05-13 06:03:11 +00:00
|
|
|
CakePlugin::load('TestPlugin');
|
2012-01-06 00:26:01 +00:00
|
|
|
$result = $this->Components->load('SomeOther', array('className' => 'TestPlugin.Other'));
|
|
|
|
$this->assertInstanceOf('OtherComponent', $result);
|
|
|
|
$this->assertInstanceOf('OtherComponent', $this->Components->SomeOther);
|
2011-01-15 01:44:33 +00:00
|
|
|
|
2012-11-16 11:14:28 +00:00
|
|
|
$result = $this->Components->loaded();
|
|
|
|
$this->assertEquals(array('Cookie', 'SomeOther'), $result, 'loaded() results are wrong.');
|
2011-01-15 01:44:33 +00:00
|
|
|
App::build();
|
2011-05-13 06:03:11 +00:00
|
|
|
CakePlugin::unload();
|
2011-01-10 02:27:44 +00:00
|
|
|
}
|
|
|
|
|
2010-07-04 18:00:23 +00:00
|
|
|
/**
|
|
|
|
* test load and enable = false
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testLoadWithEnableFalse() {
|
2010-11-16 03:53:49 +00:00
|
|
|
$result = $this->Components->load('Cookie', array('enabled' => false));
|
2010-12-24 17:54:04 +00:00
|
|
|
$this->assertInstanceOf('CookieComponent', $result);
|
|
|
|
$this->assertInstanceOf('CookieComponent', $this->Components->Cookie);
|
2010-07-04 18:00:23 +00:00
|
|
|
|
|
|
|
$this->assertFalse($this->Components->enabled('Cookie'), 'Cookie should be disabled');
|
|
|
|
}
|
2011-12-06 20:52:48 +00:00
|
|
|
|
2010-07-04 18:00:23 +00:00
|
|
|
/**
|
|
|
|
* test missingcomponent exception
|
|
|
|
*
|
2011-10-15 18:06:31 +00:00
|
|
|
* @expectedException MissingComponentException
|
2010-07-04 18:00:23 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-10-15 18:06:31 +00:00
|
|
|
public function testLoadMissingComponent() {
|
2010-07-04 18:00:23 +00:00
|
|
|
$this->Components->load('ThisComponentShouldAlwaysBeMissing');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test loading a plugin component.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testLoadPluginComponent() {
|
2010-07-04 18:00:23 +00:00
|
|
|
App::build(array(
|
2012-02-18 12:31:29 +00:00
|
|
|
'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS),
|
2010-07-04 18:00:23 +00:00
|
|
|
));
|
2011-05-13 06:03:11 +00:00
|
|
|
CakePlugin::load('TestPlugin');
|
2012-01-06 00:26:01 +00:00
|
|
|
$result = $this->Components->load('TestPlugin.Other');
|
|
|
|
$this->assertInstanceOf('OtherComponent', $result, 'Component class is wrong.');
|
|
|
|
$this->assertInstanceOf('OtherComponent', $this->Components->Other, 'Class is wrong');
|
2010-07-04 18:00:23 +00:00
|
|
|
App::build();
|
2011-05-13 06:03:11 +00:00
|
|
|
CakePlugin::unload();
|
2010-07-04 18:00:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test unload()
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testUnload() {
|
2010-07-04 18:00:23 +00:00
|
|
|
$this->Components->load('Cookie');
|
|
|
|
$this->Components->load('Security');
|
|
|
|
|
2012-11-16 11:14:28 +00:00
|
|
|
$result = $this->Components->loaded();
|
2010-07-04 18:00:23 +00:00
|
|
|
$this->assertEquals(array('Cookie', 'Security'), $result, 'loaded components is wrong');
|
|
|
|
|
|
|
|
$this->Components->unload('Cookie');
|
|
|
|
$this->assertFalse(isset($this->Components->Cookie));
|
|
|
|
$this->assertTrue(isset($this->Components->Security));
|
|
|
|
|
2012-11-16 11:14:28 +00:00
|
|
|
$result = $this->Components->loaded();
|
2010-07-04 18:00:23 +00:00
|
|
|
$this->assertEquals(array('Security'), $result, 'loaded components is wrong');
|
|
|
|
|
|
|
|
$result = $this->Components->enabled();
|
|
|
|
$this->assertEquals(array('Security'), $result, 'enabled components is wrong');
|
|
|
|
}
|
|
|
|
|
2010-09-15 02:52:51 +00:00
|
|
|
/**
|
|
|
|
* test getting the controller out of the collection
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testGetController() {
|
2010-09-15 02:52:51 +00:00
|
|
|
$controller = $this->getMock('Controller');
|
|
|
|
$controller->components = array('Security');
|
|
|
|
$this->Components->init($controller);
|
|
|
|
$result = $this->Components->getController();
|
|
|
|
|
|
|
|
$this->assertSame($controller, $result);
|
|
|
|
}
|
2011-04-17 10:35:21 +00:00
|
|
|
}
|