ControllerMergeVarsTest.php 6.0 KB

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