ControllerMergeVarsTest.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. <?php
  2. /**
  3. * Controller Merge vars Test file
  4. *
  5. * Isolated from the Controller and Component test as to not pollute their AppController class
  6. *
  7. * PHP 5
  8. *
  9. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  10. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. *
  12. * Licensed under The MIT License
  13. * For full copyright and license information, please see the LICENSE.txt
  14. * Redistributions of files must retain the above copyright notice
  15. *
  16. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  17. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  18. * @package Cake.Test.Case.Controller
  19. * @since CakePHP(tm) v 1.2.3
  20. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  21. */
  22. App::uses('Controller', 'Controller');
  23. /**
  24. * Test case AppController
  25. *
  26. * @package Cake.Test.Case.Controller
  27. */
  28. class MergeVarsAppController extends Controller {
  29. /**
  30. * components
  31. *
  32. * @var array
  33. */
  34. public $components = array('MergeVar' => array('flag', 'otherFlag', 'redirect' => false));
  35. /**
  36. * helpers
  37. *
  38. * @var array
  39. */
  40. public $helpers = array('MergeVar' => array('format' => 'html', 'terse'));
  41. }
  42. /**
  43. * MergeVar Component
  44. *
  45. * @package Cake.Test.Case.Controller
  46. */
  47. class MergeVarComponent extends Object {
  48. }
  49. /**
  50. * Additional controller for testing
  51. *
  52. * @package Cake.Test.Case.Controller
  53. */
  54. class MergeVariablesController extends MergeVarsAppController {
  55. /**
  56. * uses
  57. *
  58. * @var arrays
  59. */
  60. public $uses = array();
  61. /**
  62. * parent for mergeVars
  63. *
  64. * @var string
  65. */
  66. protected $_mergeParent = 'MergeVarsAppController';
  67. }
  68. /**
  69. * MergeVarPlugin App Controller
  70. *
  71. * @package Cake.Test.Case.Controller
  72. */
  73. class MergeVarPluginAppController extends MergeVarsAppController {
  74. /**
  75. * components
  76. *
  77. * @var array
  78. */
  79. public $components = array('Auth' => array('setting' => 'val', 'otherVal'));
  80. /**
  81. * helpers
  82. *
  83. * @var array
  84. */
  85. public $helpers = array('Javascript');
  86. /**
  87. * parent for mergeVars
  88. *
  89. * @var string
  90. */
  91. protected $_mergeParent = 'MergeVarsAppController';
  92. }
  93. /**
  94. * MergePostsController
  95. *
  96. * @package Cake.Test.Case.Controller
  97. */
  98. class MergePostsController extends MergeVarPluginAppController {
  99. /**
  100. * uses
  101. *
  102. * @var array
  103. */
  104. public $uses = array();
  105. }
  106. /**
  107. * Test Case for Controller Merging of Vars.
  108. *
  109. * @package Cake.Test.Case.Controller
  110. */
  111. class ControllerMergeVarsTest extends CakeTestCase {
  112. /**
  113. * test that component settings are not duplicated when merging component settings
  114. *
  115. * @return void
  116. */
  117. public function testComponentParamMergingNoDuplication() {
  118. $Controller = new MergeVariablesController();
  119. $Controller->constructClasses();
  120. $expected = array('MergeVar' => array('flag', 'otherFlag', 'redirect' => false));
  121. $this->assertEquals($expected, $Controller->components, 'Duplication of settings occurred. %s');
  122. }
  123. /**
  124. * test component merges with redeclared components
  125. *
  126. * @return void
  127. */
  128. public function testComponentMergingWithRedeclarations() {
  129. $Controller = new MergeVariablesController();
  130. $Controller->components['MergeVar'] = array('remote', 'redirect' => true);
  131. $Controller->constructClasses();
  132. $expected = array('MergeVar' => array('flag', 'otherFlag', 'redirect' => true, 'remote'));
  133. $this->assertEquals($expected, $Controller->components, 'Merging of settings is wrong. %s');
  134. }
  135. /**
  136. * test merging of helpers array, ensure no duplication occurs
  137. *
  138. * @return void
  139. */
  140. public function testHelperSettingMergingNoDuplication() {
  141. $Controller = new MergeVariablesController();
  142. $Controller->constructClasses();
  143. $expected = array('MergeVar' => array('format' => 'html', 'terse'));
  144. $this->assertEquals($expected, $Controller->helpers, 'Duplication of settings occurred. %s');
  145. }
  146. /**
  147. * Test that helpers declared in appcontroller come before those in the subclass
  148. * orderwise
  149. *
  150. * @return void
  151. */
  152. public function testHelperOrderPrecedence() {
  153. $Controller = new MergeVariablesController();
  154. $Controller->helpers = array('Custom', 'Foo' => array('something'));
  155. $Controller->constructClasses();
  156. $expected = array(
  157. 'MergeVar' => array('format' => 'html', 'terse'),
  158. 'Custom' => null,
  159. 'Foo' => array('something')
  160. );
  161. $this->assertSame($expected, $Controller->helpers, 'Order is incorrect.');
  162. }
  163. /**
  164. * test merging of vars with plugin
  165. *
  166. * @return void
  167. */
  168. public function testMergeVarsWithPlugin() {
  169. $Controller = new MergePostsController();
  170. $Controller->components = array('Email' => array('ports' => 'open'));
  171. $Controller->plugin = 'MergeVarPlugin';
  172. $Controller->constructClasses();
  173. $expected = array(
  174. 'MergeVar' => array('flag', 'otherFlag', 'redirect' => false),
  175. 'Auth' => array('setting' => 'val', 'otherVal'),
  176. 'Email' => array('ports' => 'open')
  177. );
  178. $this->assertEquals($expected, $Controller->components, 'Components are unexpected.');
  179. $expected = array(
  180. 'MergeVar' => array('format' => 'html', 'terse'),
  181. 'Javascript' => null
  182. );
  183. $this->assertEquals($expected, $Controller->helpers, 'Helpers are unexpected.');
  184. $Controller = new MergePostsController();
  185. $Controller->components = array();
  186. $Controller->plugin = 'MergeVarPlugin';
  187. $Controller->constructClasses();
  188. $expected = array(
  189. 'MergeVar' => array('flag', 'otherFlag', 'redirect' => false),
  190. 'Auth' => array('setting' => 'val', 'otherVal'),
  191. );
  192. $this->assertEquals($expected, $Controller->components, 'Components are unexpected.');
  193. }
  194. /**
  195. * Ensure that _mergeControllerVars is not being greedy and merging with
  196. * AppController when you make an instance of Controller
  197. *
  198. * @return void
  199. */
  200. public function testMergeVarsNotGreedy() {
  201. $Controller = new Controller();
  202. $Controller->components = array();
  203. $Controller->uses = array();
  204. $Controller->constructClasses();
  205. $this->assertFalse(isset($Controller->Session));
  206. }
  207. /**
  208. * Ensure that $modelClass is correct even when Controller::$uses
  209. * has been iterated, eg: by a Component, or event handlers.
  210. */
  211. public function testMergeVarsModelClass() {
  212. $Controller = new MergeVariablescontroller();
  213. $Controller->uses = array('Test', 'TestAlias');
  214. $lastModel = end($Controller->uses);
  215. $Controller->constructClasses();
  216. $this->assertEquals($Controller->uses[0], $Controller->modelClass);
  217. }
  218. }