PageRenderTime 59ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/cake/tests/cases/libs/controller/component.test.php

https://github.com/hardsshah/bookmarks
PHP | 491 lines | 205 code | 25 blank | 261 comment | 6 complexity | c1f151038804e1e60eb3f7f941861019 MD5 | raw file
  1. <?php
  2. /* SVN FILE: $Id$ */
  3. /**
  4. * ComponentTest file
  5. *
  6. * Long description for file
  7. *
  8. * PHP versions 4 and 5
  9. *
  10. * CakePHP(tm) Tests <https://trac.cakephp.org/wiki/Developement/TestSuite>
  11. * Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
  12. *
  13. * Licensed under The Open Group Test Suite License
  14. * Redistributions of files must retain the above copyright notice.
  15. *
  16. * @filesource
  17. * @copyright Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
  18. * @link https://trac.cakephp.org/wiki/Developement/TestSuite CakePHP(tm) Tests
  19. * @package cake
  20. * @subpackage cake.tests.cases.libs.controller
  21. * @since CakePHP(tm) v 1.2.0.5436
  22. * @version $Revision$
  23. * @modifiedby $LastChangedBy$
  24. * @lastmodified $Date$
  25. * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
  26. */
  27. App::import('Core', array('Component', 'Controller'));
  28. if (!class_exists('AppController')) {
  29. /**
  30. * AppController class
  31. *
  32. * @package cake
  33. * @subpackage cake.tests.cases.libs.controller
  34. */
  35. class AppController extends Controller {
  36. /**
  37. * name property
  38. *
  39. * @var string 'App'
  40. * @access public
  41. */
  42. var $name = 'App';
  43. /**
  44. * uses property
  45. *
  46. * @var array
  47. * @access public
  48. */
  49. var $uses = array();
  50. /**
  51. * helpers property
  52. *
  53. * @var array
  54. * @access public
  55. */
  56. var $helpers = array();
  57. /**
  58. * components property
  59. *
  60. * @var array
  61. * @access public
  62. */
  63. var $components = array('Orange' => array('colour' => 'blood orange'));
  64. }
  65. } else if (!defined('APP_CONTROLLER_EXISTS')){
  66. define('APP_CONTROLLER_EXISTS', true);
  67. }
  68. /**
  69. * ParamTestComponent
  70. *
  71. * @package cake
  72. * @subpackage cake.tests.cases.libs.controller
  73. */
  74. class ParamTestComponent extends Object {
  75. /**
  76. * name property
  77. *
  78. * @var string 'ParamTest'
  79. * @access public
  80. */
  81. var $name = 'ParamTest';
  82. /**
  83. * components property
  84. *
  85. * @var array
  86. * @access public
  87. */
  88. var $components = array('Banana' => array('config' => 'value'));
  89. /**
  90. * initialize method
  91. *
  92. * @param mixed $controller
  93. * @param mixed $settings
  94. * @access public
  95. * @return void
  96. */
  97. function initialize(&$controller, $settings) {
  98. foreach ($settings as $key => $value) {
  99. if (is_numeric($key)) {
  100. $this->{$value} = true;
  101. } else {
  102. $this->{$key} = $value;
  103. }
  104. }
  105. }
  106. }
  107. /**
  108. * ComponentTestController class
  109. *
  110. * @package cake
  111. * @subpackage cake.tests.cases.libs.controller
  112. */
  113. class ComponentTestController extends AppController {
  114. /**
  115. * name property
  116. *
  117. * @var string 'ComponentTest'
  118. * @access public
  119. */
  120. var $name = 'ComponentTest';
  121. /**
  122. * uses property
  123. *
  124. * @var array
  125. * @access public
  126. */
  127. var $uses = array();
  128. }
  129. /**
  130. * AppleComponent class
  131. *
  132. * @package cake
  133. * @subpackage cake.tests.cases.libs.controller
  134. */
  135. class AppleComponent extends Object {
  136. /**
  137. * components property
  138. *
  139. * @var array
  140. * @access public
  141. */
  142. var $components = array('Orange');
  143. /**
  144. * testName property
  145. *
  146. * @var mixed null
  147. * @access public
  148. */
  149. var $testName = null;
  150. /**
  151. * startup method
  152. *
  153. * @param mixed $controller
  154. * @access public
  155. * @return void
  156. */
  157. function startup(&$controller) {
  158. $this->testName = $controller->name;
  159. }
  160. }
  161. /**
  162. * OrangeComponent class
  163. *
  164. * @package cake
  165. * @subpackage cake.tests.cases.libs.controller
  166. */
  167. class OrangeComponent extends Object {
  168. /**
  169. * components property
  170. *
  171. * @var array
  172. * @access public
  173. */
  174. var $components = array('Banana');
  175. /**
  176. * initialize method
  177. *
  178. * @param mixed $controller
  179. * @access public
  180. * @return void
  181. */
  182. function initialize(&$controller, $settings) {
  183. $this->Controller = $controller;
  184. $this->Banana->testField = 'OrangeField';
  185. $this->settings = $settings;
  186. }
  187. /**
  188. * startup method
  189. *
  190. * @param Controller $controller
  191. * @return string
  192. * @access public
  193. */
  194. function startup(&$controller) {
  195. $controller->foo = 'pass';
  196. }
  197. }
  198. /**
  199. * BananaComponent class
  200. *
  201. * @package cake
  202. * @subpackage cake.tests.cases.libs.controller
  203. */
  204. class BananaComponent extends Object {
  205. /**
  206. * testField property
  207. *
  208. * @var string 'BananaField'
  209. * @access public
  210. */
  211. var $testField = 'BananaField';
  212. /**
  213. * startup method
  214. *
  215. * @param Controller $controller
  216. * @return string
  217. * @access public
  218. */
  219. function startup(&$controller) {
  220. $controller->bar = 'fail';
  221. }
  222. }
  223. /**
  224. * MutuallyReferencingOneComponent class
  225. *
  226. * @package cake
  227. * @subpackage cake.tests.cases.libs.controller
  228. */
  229. class MutuallyReferencingOneComponent extends Object {
  230. /**
  231. * components property
  232. *
  233. * @var array
  234. * @access public
  235. */
  236. var $components = array('MutuallyReferencingTwo');
  237. }
  238. /**
  239. * MutuallyReferencingTwoComponent class
  240. *
  241. * @package cake
  242. * @subpackage cake.tests.cases.libs.controller
  243. */
  244. class MutuallyReferencingTwoComponent extends Object {
  245. /**
  246. * components property
  247. *
  248. * @var array
  249. * @access public
  250. */
  251. var $components = array('MutuallyReferencingOne');
  252. }
  253. /**
  254. * SomethingWithEmailComponent class
  255. *
  256. * @package cake
  257. * @subpackage cake.tests.cases.libs.controller
  258. */
  259. class SomethingWithEmailComponent extends Object {
  260. /**
  261. * components property
  262. *
  263. * @var array
  264. * @access public
  265. */
  266. var $components = array('Email');
  267. }
  268. /**
  269. * ComponentTest class
  270. *
  271. * @package cake
  272. * @subpackage cake.tests.cases.libs.controller
  273. */
  274. class ComponentTest extends CakeTestCase {
  275. /**
  276. * setUp method
  277. *
  278. * @access public
  279. * @return void
  280. */
  281. function setUp() {
  282. Configure::write('pluginPaths', array(
  283. TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS
  284. ));
  285. }
  286. /**
  287. * testLoadComponents method
  288. *
  289. * @access public
  290. * @return void
  291. */
  292. function testLoadComponents() {
  293. $Controller =& new ComponentTestController();
  294. $Controller->components = array('RequestHandler');
  295. $Component =& new Component();
  296. $Component->init($Controller);
  297. $this->assertTrue(is_a($Controller->RequestHandler, 'RequestHandlerComponent'));
  298. $Controller =& new ComponentTestController();
  299. $Controller->plugin = 'test_plugin';
  300. $Controller->components = array('RequestHandler', 'TestPluginComponent');
  301. $Component =& new Component();
  302. $Component->init($Controller);
  303. $this->assertTrue(is_a($Controller->RequestHandler, 'RequestHandlerComponent'));
  304. $this->assertTrue(is_a($Controller->TestPluginComponent, 'TestPluginComponentComponent'));
  305. $this->assertTrue(is_a(
  306. $Controller->TestPluginComponent->TestPluginOtherComponent,
  307. 'TestPluginOtherComponentComponent'
  308. ));
  309. $this->assertFalse(isset($Controller->TestPluginOtherComponent));
  310. $Controller =& new ComponentTestController();
  311. $Controller->components = array('Security');
  312. $Component =& new Component();
  313. $Component->init($Controller);
  314. $this->assertTrue(is_a($Controller->Security, 'SecurityComponent'));
  315. $this->assertTrue(is_a($Controller->Security->Session, 'SessionComponent'));
  316. $Controller =& new ComponentTestController();
  317. $Controller->components = array('Security', 'Cookie', 'RequestHandler');
  318. $Component =& new Component();
  319. $Component->init($Controller);
  320. $this->assertTrue(is_a($Controller->Security, 'SecurityComponent'));
  321. $this->assertTrue(is_a($Controller->Security->RequestHandler, 'RequestHandlerComponent'));
  322. $this->assertTrue(is_a($Controller->RequestHandler, 'RequestHandlerComponent'));
  323. $this->assertTrue(is_a($Controller->Cookie, 'CookieComponent'));
  324. }
  325. /**
  326. * test component loading
  327. *
  328. * @return void
  329. */
  330. function testNestedComponentLoading() {
  331. $Controller =& new ComponentTestController();
  332. $Controller->components = array('Apple');
  333. $Controller->constructClasses();
  334. $Controller->Component->initialize($Controller);
  335. $this->assertTrue(is_a($Controller->Apple, 'AppleComponent'));
  336. $this->assertTrue(is_a($Controller->Apple->Orange, 'OrangeComponent'));
  337. $this->assertTrue(is_a($Controller->Apple->Orange->Banana, 'BananaComponent'));
  338. $this->assertTrue(is_a($Controller->Apple->Orange->Controller, 'ComponentTestController'));
  339. $this->assertTrue(empty($Controller->Apple->Session));
  340. $this->assertTrue(empty($Controller->Apple->Orange->Session));
  341. }
  342. /**
  343. * Tests Component::startup() and only running callbacks for components directly attached to
  344. * the controller.
  345. *
  346. * @return void
  347. */
  348. function testComponentStartup() {
  349. $Controller =& new ComponentTestController();
  350. $Controller->components = array('Apple');
  351. $Controller->constructClasses();
  352. $Controller->Component->initialize($Controller);
  353. $Controller->beforeFilter();
  354. $Controller->Component->startup($Controller);
  355. $this->assertTrue(is_a($Controller->Apple, 'AppleComponent'));
  356. $this->assertEqual($Controller->Apple->testName, 'ComponentTest');
  357. $expected = !(defined('APP_CONTROLLER_EXISTS') && APP_CONTROLLER_EXISTS);
  358. $this->assertEqual(isset($Controller->foo), $expected);
  359. $this->assertFalse(isset($Controller->bar));
  360. }
  361. /**
  362. * test a component being used more than once.
  363. *
  364. * @return void
  365. */
  366. function testMultipleComponentInitialize() {
  367. $Controller =& new ComponentTestController();
  368. $Controller->components = array('Orange', 'Banana');
  369. $Controller->constructClasses();
  370. $Controller->Component->initialize($Controller);
  371. $this->assertEqual($Controller->Banana->testField, 'OrangeField');
  372. $this->assertEqual($Controller->Orange->Banana->testField, 'OrangeField');
  373. }
  374. /**
  375. * Test Component declarations with Parameters
  376. * tests merging of component parameters and merging / construction of components.
  377. *
  378. * @return void
  379. */
  380. function testComponentsWithParams() {
  381. $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.');
  382. $Controller =& new ComponentTestController();
  383. $Controller->components = array('ParamTest' => array('test' => 'value', 'flag'), 'Apple');
  384. $Controller->constructClasses();
  385. $Controller->Component->initialize($Controller);
  386. $this->assertTrue(is_a($Controller->ParamTest, 'ParamTestComponent'));
  387. $this->assertTrue(is_a($Controller->ParamTest->Banana, 'BananaComponent'));
  388. $this->assertTrue(is_a($Controller->Orange, 'OrangeComponent'));
  389. $this->assertTrue(is_a($Controller->Session, 'SessionComponent'));
  390. $this->assertEqual($Controller->Orange->settings, array('colour' => 'blood orange'));
  391. $this->assertEqual($Controller->ParamTest->test, 'value');
  392. $this->assertEqual($Controller->ParamTest->flag, true);
  393. //Settings are merged from app controller and current controller.
  394. $Controller =& new ComponentTestController();
  395. $Controller->components = array(
  396. 'ParamTest' => array('test' => 'value'),
  397. 'Orange' => array('ripeness' => 'perfect')
  398. );
  399. $Controller->constructClasses();
  400. $Controller->Component->initialize($Controller);
  401. $expected = array('colour' => 'blood orange', 'ripeness' => 'perfect');
  402. $this->assertEqual($Controller->Orange->settings, $expected);
  403. $this->assertEqual($Controller->ParamTest->test, 'value');
  404. }
  405. /**
  406. * Test mutually referencing components.
  407. *
  408. * @return void
  409. */
  410. function testMutuallyReferencingComponents() {
  411. $Controller =& new ComponentTestController();
  412. $Controller->components = array('MutuallyReferencingOne');
  413. $Controller->constructClasses();
  414. $Controller->Component->initialize($Controller);
  415. $this->assertTrue(is_a(
  416. $Controller->MutuallyReferencingOne,
  417. 'MutuallyReferencingOneComponent'
  418. ));
  419. $this->assertTrue(is_a(
  420. $Controller->MutuallyReferencingOne->MutuallyReferencingTwo,
  421. 'MutuallyReferencingTwoComponent'
  422. ));
  423. $this->assertTrue(is_a(
  424. $Controller->MutuallyReferencingOne->MutuallyReferencingTwo->MutuallyReferencingOne,
  425. 'MutuallyReferencingOneComponent'
  426. ));
  427. }
  428. /**
  429. * Test mutually referencing components.
  430. *
  431. * @return void
  432. */
  433. function testSomethingReferencingEmailComponent() {
  434. $Controller =& new ComponentTestController();
  435. $Controller->components = array('SomethingWithEmail');
  436. $Controller->constructClasses();
  437. $Controller->Component->initialize($Controller);
  438. $Controller->beforeFilter();
  439. $Controller->Component->startup($Controller);
  440. $this->assertTrue(is_a(
  441. $Controller->SomethingWithEmail,
  442. 'SomethingWithEmailComponent'
  443. ));
  444. $this->assertTrue(is_a(
  445. $Controller->SomethingWithEmail->Email,
  446. 'EmailComponent'
  447. ));
  448. $this->assertTrue(is_a(
  449. $Controller->SomethingWithEmail->Email->Controller,
  450. 'ComponentTestController'
  451. ));
  452. }
  453. /**
  454. * test that SessionComponent doesn't get added if its already in the components array.
  455. *
  456. * @return void
  457. **/
  458. function testDoubleLoadingOfSessionComponent() {
  459. $Controller =& new ComponentTestController();
  460. $Controller->uses = array();
  461. $Controller->components = array('Session');
  462. $Controller->constructClasses();
  463. $this->assertEqual($Controller->components, array('Session' => '', 'Orange' => array('colour' => 'blood orange')));
  464. }
  465. }
  466. ?>