Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
12345 anikendra 1
<?php
2
/**
3
 * ScaffoldTest file
4
 *
5
 * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
6
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
7
 *
8
 * Licensed under The MIT License
9
 * For full copyright and license information, please see the LICENSE.txt
10
 * Redistributions of files must retain the above copyright notice
11
 *
12
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
13
 * @link          http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
14
 * @package       Cake.Test.Case.Controller
15
 * @since         CakePHP(tm) v 1.2.0.5436
16
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
17
 */
18
 
19
App::uses('Router', 'Routing');
20
App::uses('Controller', 'Controller');
21
App::uses('Scaffold', 'Controller');
22
App::uses('ScaffoldView', 'View');
23
App::uses('AppModel', 'Model');
24
 
25
require_once dirname(dirname(__FILE__)) . DS . 'Model' . DS . 'models.php';
26
 
27
/**
28
 * ScaffoldMockController class
29
 *
30
 * @package       Cake.Test.Case.Controller
31
 */
32
class ScaffoldMockController extends Controller {
33
 
34
/**
35
 * scaffold property
36
 *
37
 * @var mixed
38
 */
39
	public $scaffold;
40
}
41
 
42
/**
43
 * ScaffoldMockControllerWithFields class
44
 *
45
 * @package       Cake.Test.Case.Controller
46
 */
47
class ScaffoldMockControllerWithFields extends Controller {
48
 
49
/**
50
 * name property
51
 *
52
 * @var string
53
 */
54
	public $name = 'ScaffoldMock';
55
 
56
/**
57
 * scaffold property
58
 *
59
 * @var mixed
60
 */
61
	public $scaffold;
62
 
63
/**
64
 * function beforeScaffold
65
 *
66
 * @param string method
67
 * @return bool true
68
 */
69
	public function beforeScaffold($method) {
70
		$this->set('scaffoldFields', array('title'));
71
		return true;
72
	}
73
 
74
}
75
 
76
/**
77
 * TestScaffoldMock class
78
 *
79
 * @package       Cake.Test.Case.Controller
80
 */
81
class TestScaffoldMock extends Scaffold {
82
 
83
/**
84
 * Overload _scaffold
85
 *
86
 * @param CakeRequest $request
87
 * @return void
88
 */
89
	protected function _scaffold(CakeRequest $request) {
90
		$this->_params = $request;
91
	}
92
 
93
/**
94
 * Get Params from the Controller.
95
 *
96
 * @return unknown
97
 */
98
	public function getParams() {
99
		return $this->_params;
100
	}
101
 
102
}
103
 
104
/**
105
 * Scaffold Test class
106
 *
107
 * @package       Cake.Test.Case.Controller
108
 */
109
class ScaffoldTest extends CakeTestCase {
110
 
111
/**
112
 * Controller property
113
 *
114
 * @var SecurityTestController
115
 */
116
	public $Controller;
117
 
118
/**
119
 * fixtures property
120
 *
121
 * @var array
122
 */
123
	public $fixtures = array('core.article', 'core.user', 'core.comment', 'core.join_thing', 'core.tag');
124
 
125
/**
126
 * setUp method
127
 *
128
 * @return void
129
 */
130
	public function setUp() {
131
		parent::setUp();
132
		Configure::write('Config.language', 'eng');
133
		$request = new CakeRequest(null, false);
134
		$this->Controller = new ScaffoldMockController($request);
135
		$this->Controller->response = $this->getMock('CakeResponse', array('_sendHeader'));
136
	}
137
 
138
/**
139
 * tearDown method
140
 *
141
 * @return void
142
 */
143
	public function tearDown() {
144
		parent::tearDown();
145
		unset($this->Controller);
146
	}
147
 
148
/**
149
 * Test the correct Generation of Scaffold Params.
150
 * This ensures that the correct action and view will be generated
151
 *
152
 * @return void
153
 */
154
	public function testScaffoldParams() {
155
		$params = array(
156
			'plugin' => null,
157
			'pass' => array(),
158
			'form' => array(),
159
			'named' => array(),
160
			'url' => array('url' => 'admin/scaffold_mock/edit'),
161
			'controller' => 'scaffold_mock',
162
			'action' => 'admin_edit',
163
			'admin' => true,
164
		);
165
		$this->Controller->request->base = '';
166
		$this->Controller->request->webroot = '/';
167
		$this->Controller->request->here = '/admin/scaffold_mock/edit';
168
		$this->Controller->request->addParams($params);
169
 
170
		//set router.
171
		Router::setRequestInfo($this->Controller->request);
172
 
173
		$this->Controller->constructClasses();
174
		$Scaffold = new TestScaffoldMock($this->Controller, $this->Controller->request);
175
		$result = $Scaffold->getParams();
176
		$this->assertEquals('admin_edit', $result['action']);
177
	}
178
 
179
/**
180
 * test that the proper names and variable values are set by Scaffold
181
 *
182
 * @return void
183
 */
184
	public function testScaffoldVariableSetting() {
185
		$params = array(
186
			'plugin' => null,
187
			'pass' => array(),
188
			'form' => array(),
189
			'named' => array(),
190
			'url' => array('url' => 'admin/scaffold_mock/edit'),
191
			'controller' => 'scaffold_mock',
192
			'action' => 'admin_edit',
193
			'admin' => true,
194
		);
195
		$this->Controller->request->base = '';
196
		$this->Controller->request->webroot = '/';
197
		$this->Controller->request->here = '/admin/scaffold_mock/edit';
198
		$this->Controller->request->addParams($params);
199
 
200
		//set router.
201
		Router::setRequestInfo($this->Controller->request);
202
 
203
		$this->Controller->constructClasses();
204
		$Scaffold = new TestScaffoldMock($this->Controller, $this->Controller->request);
205
		$result = $Scaffold->controller->viewVars;
206
 
207
		$this->assertEquals('Scaffold :: Admin Edit :: Scaffold Mock', $result['title_for_layout']);
208
		$this->assertEquals('Scaffold Mock', $result['singularHumanName']);
209
		$this->assertEquals('Scaffold Mock', $result['pluralHumanName']);
210
		$this->assertEquals('ScaffoldMock', $result['modelClass']);
211
		$this->assertEquals('id', $result['primaryKey']);
212
		$this->assertEquals('title', $result['displayField']);
213
		$this->assertEquals('scaffoldMock', $result['singularVar']);
214
		$this->assertEquals('scaffoldMock', $result['pluralVar']);
215
		$this->assertEquals(array('id', 'user_id', 'title', 'body', 'published', 'created', 'updated'), $result['scaffoldFields']);
216
		$this->assertArrayHasKey('plugin', $result['associations']['belongsTo']['User']);
217
	}
218
 
219
/**
220
 * test that Scaffold overrides the view property even if its set to 'Theme'
221
 *
222
 * @return void
223
 */
224
	public function testScaffoldChangingViewProperty() {
225
		$this->Controller->action = 'edit';
226
		$this->Controller->theme = 'TestTheme';
227
		$this->Controller->viewClass = 'Theme';
228
		$this->Controller->constructClasses();
229
		new TestScaffoldMock($this->Controller, $this->Controller->request);
230
 
231
		$this->assertEquals('Scaffold', $this->Controller->viewClass);
232
	}
233
 
234
/**
235
 * test that scaffold outputs flash messages when sessions are unset.
236
 *
237
 * @return void
238
 */
239
	public function testScaffoldFlashMessages() {
240
		$params = array(
241
			'plugin' => null,
242
			'pass' => array(1),
243
			'form' => array(),
244
			'named' => array(),
245
			'url' => array('url' => 'scaffold_mock'),
246
			'controller' => 'scaffold_mock',
247
			'action' => 'edit',
248
		);
249
		$this->Controller->request->base = '';
250
		$this->Controller->request->webroot = '/';
251
		$this->Controller->request->here = '/scaffold_mock/edit';
252
		$this->Controller->request->addParams($params);
253
 
254
		//set router.
255
		Router::reload();
256
		Router::setRequestInfo($this->Controller->request);
257
		$this->Controller->request->data = array(
258
			'ScaffoldMock' => array(
259
				'id' => 1,
260
				'title' => 'New title',
261
				'body' => 'new body'
262
			)
263
		);
264
		$this->Controller->constructClasses();
265
		unset($this->Controller->Session);
266
 
267
		ob_start();
268
		new Scaffold($this->Controller, $this->Controller->request);
269
		$this->Controller->response->send();
270
		$result = ob_get_clean();
271
		$this->assertRegExp('/Scaffold Mock has been updated/', $result);
272
	}
273
 
274
/**
275
 * test that habtm relationship keys get added to scaffoldFields.
276
 *
277
 * @return void
278
 */
279
	public function testHabtmFieldAdditionWithScaffoldForm() {
280
		CakePlugin::unload();
281
		$params = array(
282
			'plugin' => null,
283
			'pass' => array(1),
284
			'form' => array(),
285
			'named' => array(),
286
			'url' => array('url' => 'scaffold_mock'),
287
			'controller' => 'scaffold_mock',
288
			'action' => 'edit',
289
		);
290
		$this->Controller->request->base = '';
291
		$this->Controller->request->webroot = '/';
292
		$this->Controller->request->here = '/scaffold_mock/edit';
293
		$this->Controller->request->addParams($params);
294
 
295
		//set router.
296
		Router::reload();
297
		Router::setRequestInfo($this->Controller->request);
298
 
299
		$this->Controller->constructClasses();
300
		ob_start();
301
		$Scaffold = new Scaffold($this->Controller, $this->Controller->request);
302
		$this->Controller->response->send();
303
		$result = ob_get_clean();
304
		$this->assertRegExp('/name="data\[ScaffoldTag\]\[ScaffoldTag\]"/', $result);
305
 
306
		$result = $Scaffold->controller->viewVars;
307
		$this->assertEquals(array('id', 'user_id', 'title', 'body', 'published', 'created', 'updated', 'ScaffoldTag'), $result['scaffoldFields']);
308
	}
309
 
310
/**
311
 * test that the proper names and variable values are set by Scaffold
312
 *
313
 * @return void
314
 */
315
	public function testEditScaffoldWithScaffoldFields() {
316
		$request = new CakeRequest(null, false);
317
		$this->Controller = new ScaffoldMockControllerWithFields($request);
318
		$this->Controller->response = $this->getMock('CakeResponse', array('_sendHeader'));
319
 
320
		$params = array(
321
			'plugin' => null,
322
			'pass' => array(1),
323
			'form' => array(),
324
			'named' => array(),
325
			'url' => array('url' => 'scaffold_mock/edit'),
326
			'controller' => 'scaffold_mock',
327
			'action' => 'edit',
328
		);
329
		$this->Controller->request->base = '';
330
		$this->Controller->request->webroot = '/';
331
		$this->Controller->request->here = '/scaffold_mock/edit';
332
		$this->Controller->request->addParams($params);
333
 
334
		//set router.
335
		Router::reload();
336
		Router::setRequestInfo($this->Controller->request);
337
 
338
		$this->Controller->constructClasses();
339
		ob_start();
340
		new Scaffold($this->Controller, $this->Controller->request);
341
		$this->Controller->response->send();
342
		$result = ob_get_clean();
343
 
344
		$this->assertNotRegExp('/textarea name="data\[ScaffoldMock\]\[body\]" cols="30" rows="6" id="ScaffoldMockBody"/', $result);
345
	}
346
 
347
}