Skip to content
This repository
Browse code

Adding ControllerMergeVarsTestCase to more extensively test merging o…

…f vars in Controller::__mergeVars.

Fixing issue where components declared in AppController would get doubled settings if no components were declared in subclasses.
Fixes #6459

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@8201 3807eeeb-6ff5-0310-8944-8be069107fe0
  • Loading branch information...
commit 16eb51e91c53c780c6089df397051516052467b1 1 parent 3e8e7a2
Mark Story authored June 24, 2009
8  cake/libs/controller/controller.php
@@ -393,7 +393,9 @@ function __mergeVars() {
393 393
 					if ($var === 'components') {
394 394
 						$normal = Set::normalize($this->{$var});
395 395
 						$app = Set::normalize($appVars[$var]);
396  
-						$this->{$var} = Set::merge($app, $normal);
  396
+						if ($app !== $normal) {
  397
+							$this->{$var} = Set::merge($app, $normal);
  398
+						}
397 399
 					} else {
398 400
 						$this->{$var} = Set::merge($this->{$var}, array_diff($appVars[$var], $this->{$var}));
399 401
 					}
@@ -415,7 +417,9 @@ function __mergeVars() {
415 417
 					if ($var === 'components') {
416 418
 						$normal = Set::normalize($this->{$var});
417 419
 						$app = Set::normalize($appVars[$var]);
418  
-						$this->{$var} = Set::merge($normal, array_diff_assoc($app, $normal));
  420
+						if ($app !== $normal) {
  421
+							$this->{$var} = Set::merge($app, $normal);
  422
+						}
419 423
 					} else {
420 424
 						$this->{$var} = Set::merge($this->{$var}, array_diff($appVars[$var], $this->{$var}));
421 425
 					}
222  cake/tests/cases/libs/controller/controller_merge_vars.test.php
... ...
@@ -0,0 +1,222 @@
  1
+<?php
  2
+/* SVN FILE: $Id$ */
  3
+/**
  4
+ * Controller Merge vars Test file
  5
+ *
  6
+ * Isolated from the Controller and Component test as to not pollute their AppController class
  7
+ *
  8
+ * PHP versions 4 and 5
  9
+ *
  10
+ * CakePHP(tm) Tests <https://trac.cakephp.org/wiki/Developement/TestSuite>
  11
+ * Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
  12
+ *
  13
+ *  Licensed under The Open Group Test Suite License
  14
+ *  Redistributions of files must retain the above copyright notice.
  15
+ *
  16
+ * @filesource
  17
+ * @copyright     Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
  18
+ * @link          https://trac.cakephp.org/wiki/Developement/TestSuite CakePHP(tm) Tests
  19
+ * @package       cake
  20
+ * @subpackage    cake.tests.cases.libs.controller
  21
+ * @since         CakePHP(tm) v 1.2.3
  22
+ * @version       $Revision$
  23
+ * @modifiedby    $LastChangedBy$
  24
+ * @lastmodified  $Date$
  25
+ * @license       http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
  26
+ */
  27
+if (!class_exists('AppController')) {
  28
+	/**
  29
+	 * Test case AppController requred
  30
+	 *
  31
+	 * @package cake.tests.cases.libs.controller
  32
+	 **/
  33
+	class AppController extends Controller {
  34
+		/**
  35
+		 * components
  36
+		 *
  37
+		 * @var array
  38
+		 **/
  39
+		var $components = array('MergeVar' => array('flag', 'otherFlag', 'redirect' => false));
  40
+		/**
  41
+		 * helpers
  42
+		 *
  43
+		 * @var array
  44
+		 **/
  45
+		var $helpers = array('MergeVar' => array('format' => 'html', 'terse'));
  46
+	}
  47
+} elseif (!defined('APP_CONTROLLER_EXISTS')) {
  48
+	define('APP_CONTROLLER_EXISTS', true);
  49
+}
  50
+
  51
+/**
  52
+ * MergeVar Component
  53
+ *
  54
+ * @package cake.tests.cases.libs.controller
  55
+ **/
  56
+class MergeVarComponent extends Object {
  57
+
  58
+}
  59
+
  60
+/**
  61
+ * Additional controller for testing
  62
+ *
  63
+ * @package cake.tests.cases.libs.controller
  64
+ **/
  65
+class MergeVariablesController extends AppController {
  66
+/**
  67
+ * name
  68
+ *
  69
+ * @var string
  70
+ **/
  71
+	var $name = 'MergeVariables';
  72
+/**
  73
+ * uses
  74
+ *
  75
+ * @var arrays
  76
+ **/
  77
+	var $uses = array();
  78
+}
  79
+
  80
+/**
  81
+ * MergeVarPlugin App Controller
  82
+ *
  83
+ * @package cake.tests.cases.libs.controller
  84
+ **/
  85
+class MergeVarPluginAppController extends AppController {
  86
+/**
  87
+ * components
  88
+ *
  89
+ * @var array
  90
+ **/
  91
+	var $components = array('Auth' => array('setting' => 'val', 'otherVal'));
  92
+/**
  93
+ * helpers
  94
+ *
  95
+ * @var array
  96
+ **/
  97
+	var $helpers = array('Javascript');
  98
+}
  99
+
  100
+/**
  101
+ * MergePostsController
  102
+ *
  103
+ * @package cake.tests.cases.libs.controller
  104
+ **/
  105
+class MergePostsController extends MergeVarPluginAppController {
  106
+/**
  107
+ * name
  108
+ *
  109
+ * @var string
  110
+ **/
  111
+	var $name = 'MergePosts';
  112
+/**
  113
+ * uses
  114
+ *
  115
+ * @var array
  116
+ **/
  117
+	var $uses = array();
  118
+}
  119
+
  120
+
  121
+/**
  122
+ * Test Case for Controller Merging of Vars.
  123
+ *
  124
+ * @package cake.tests.cases.libs.controller
  125
+ **/
  126
+class ControllerMergeVarsTestCase extends CakeTestCase {
  127
+
  128
+/**
  129
+ * end test
  130
+ *
  131
+ * @return void
  132
+ **/
  133
+	function endTest() {
  134
+		ClassRegistry::flush();
  135
+	}
  136
+/**
  137
+ * test that component settings are not duplicated when merging component settings
  138
+ *
  139
+ * @return void
  140
+ **/
  141
+	function testComponentParamMergingNoDuplication() {
  142
+		$Controller =& new MergeVariablesController();
  143
+		$Controller->constructClasses();
  144
+
  145
+		$expected = array('MergeVar' => array('flag', 'otherFlag', 'redirect' => false));
  146
+		$this->assertEqual($Controller->components, $expected, 'Duplication of settings occured. %s');
  147
+	}
  148
+/**
  149
+ * test component merges with redeclared components
  150
+ *
  151
+ * @return void
  152
+ **/
  153
+	function testComponentMergingWithRedeclarations() {
  154
+		$Controller =& new MergeVariablesController();
  155
+		$Controller->components['MergeVar'] = array('remote', 'redirect' => true);
  156
+		$Controller->constructClasses();
  157
+
  158
+		$expected = array('MergeVar' => array('flag', 'otherFlag', 'redirect' => true, 'remote'));
  159
+		$this->assertEqual($Controller->components, $expected, 'Merging of settings is wrong. %s');
  160
+	}
  161
+/**
  162
+ * test merging of helpers array, ensure no duplication occurs
  163
+ *
  164
+ * @return void
  165
+ **/
  166
+	function testHelperSettingMergingNoDuplication() {
  167
+		$Controller =& new MergeVariablesController();
  168
+		$Controller->constructClasses();
  169
+
  170
+		$expected = array('MergeVar' => array('format' => 'html', 'terse'));
  171
+		$this->assertEqual($Controller->helpers, $expected, 'Duplication of settings occured. %s');
  172
+	}
  173
+/**
  174
+ * test merging of vars with plugin
  175
+ *
  176
+ * @return void
  177
+ **/
  178
+	function testMergeVarsWithPlugin() {
  179
+		$Controller =& new MergePostsController();
  180
+		$Controller->components = array('Email' => array('ports' => 'open'));
  181
+		$Controller->plugin = 'MergeVarPlugin';
  182
+		$Controller->constructClasses();
  183
+
  184
+		$expected = array(
  185
+			'MergeVar' => array('flag', 'otherFlag', 'redirect' => false),
  186
+			'Auth' => array('setting' => 'val', 'otherVal'),
  187
+			'Email' => array('ports' => 'open')
  188
+		);
  189
+		$this->assertEqual($Controller->components, $expected, 'Components are unexpected %s');
  190
+		
  191
+		$expected = array(
  192
+			'Javascript',
  193
+			'MergeVar' => array('format' => 'html', 'terse')
  194
+		);
  195
+		$this->assertEqual($Controller->helpers, $expected, 'Helpers are unexpected %s');
  196
+
  197
+		$Controller =& new MergePostsController();
  198
+		$Controller->components = array();
  199
+		$Controller->plugin = 'MergeVarPlugin';
  200
+		$Controller->constructClasses();
  201
+
  202
+		$expected = array(
  203
+			'MergeVar' => array('flag', 'otherFlag', 'redirect' => false),
  204
+			'Auth' => array('setting' => 'val', 'otherVal'),
  205
+		);
  206
+		$this->assertEqual($Controller->components, $expected, 'Components are unexpected %s');
  207
+	}
  208
+/**
  209
+ * Ensure that __mergeVars is not being greedy and merging with
  210
+ * AppController when you make an instance of Controller
  211
+ *
  212
+ * @return void
  213
+ **/
  214
+	function testMergeVarsNotGreedy() {
  215
+		$Controller =& new Controller();
  216
+		$Controller->components = array();
  217
+		$Controller->uses = array();
  218
+		$Controller->constructClasses();
  219
+
  220
+		$this->assertTrue(isset($Controller->Session));
  221
+	}
  222
+}

0 notes on commit 16eb51e

Please sign in to comment.
Something went wrong with that request. Please try again.