ControllerMergeVarsTest.php 6.2 KB

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