ControllerMergeVarsTest.php 5.9 KB

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