PageRenderTime 41ms CodeModel.GetById 5ms RepoModel.GetById 0ms app.codeStats 0ms

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

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