Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

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