pictcode / lib / Cake / Test / Case / View / ScaffoldViewTest.php @ 635eef61
履歴 | 表示 | アノテート | ダウンロード (14.34 KB)
1 | 635eef61 | spyder1211 | <?php
|
---|---|---|---|
2 | /**
|
||
3 | * ScaffoldViewTest 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.View
|
||
15 | * @since CakePHP(tm) v 2.0
|
||
16 | * @license http://www.opensource.org/licenses/mit-license.php MIT License
|
||
17 | */
|
||
18 | |||
19 | App::uses('Controller', 'Controller'); |
||
20 | App::uses('Scaffold', 'Controller'); |
||
21 | App::uses('ScaffoldView', 'View'); |
||
22 | App::uses('AppModel', 'Model'); |
||
23 | |||
24 | require_once dirname(dirname(__FILE__)) . DS . 'Model' . DS . 'models.php'; |
||
25 | |||
26 | /**
|
||
27 | * TestScaffoldView class
|
||
28 | *
|
||
29 | * @package Cake.Test.Case.Controller
|
||
30 | */
|
||
31 | class TestScaffoldView extends ScaffoldView { |
||
32 | |||
33 | /**
|
||
34 | * testGetFilename method
|
||
35 | *
|
||
36 | * @param string $action
|
||
37 | * @return void
|
||
38 | */
|
||
39 | public function testGetFilename($action) { |
||
40 | return $this->_getViewFileName($action); |
||
41 | } |
||
42 | |||
43 | } |
||
44 | |||
45 | /**
|
||
46 | * ScaffoldViewMockController class
|
||
47 | *
|
||
48 | * @package Cake.Test.Case.Controller
|
||
49 | */
|
||
50 | class ScaffoldViewMockController extends Controller { |
||
51 | |||
52 | /**
|
||
53 | * name property
|
||
54 | *
|
||
55 | * @var string
|
||
56 | */
|
||
57 | public $name = 'ScaffoldMock'; |
||
58 | |||
59 | /**
|
||
60 | * scaffold property
|
||
61 | *
|
||
62 | * @var mixed
|
||
63 | */
|
||
64 | public $scaffold; |
||
65 | } |
||
66 | |||
67 | /**
|
||
68 | * ScaffoldViewTest class
|
||
69 | *
|
||
70 | * @package Cake.Test.Case.Controller
|
||
71 | */
|
||
72 | class ScaffoldViewTest extends CakeTestCase { |
||
73 | |||
74 | /**
|
||
75 | * fixtures property
|
||
76 | *
|
||
77 | * @var array
|
||
78 | */
|
||
79 | public $fixtures = array('core.article', 'core.user', 'core.comment', 'core.join_thing', 'core.tag'); |
||
80 | |||
81 | /**
|
||
82 | * setUp method
|
||
83 | *
|
||
84 | * @return void
|
||
85 | */
|
||
86 | public function setUp() { |
||
87 | parent::setUp();
|
||
88 | $this->request = new CakeRequest(null, false); |
||
89 | $this->Controller = new ScaffoldViewMockController($this->request); |
||
90 | $this->Controller->response = $this->getMock('CakeResponse', array('_sendHeader')); |
||
91 | |||
92 | App::build(array( |
||
93 | 'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS), |
||
94 | 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS) |
||
95 | )); |
||
96 | CakePlugin::load('TestPlugin'); |
||
97 | } |
||
98 | |||
99 | /**
|
||
100 | * tearDown method
|
||
101 | *
|
||
102 | * @return void
|
||
103 | */
|
||
104 | public function tearDown() { |
||
105 | unset($this->Controller, $this->request); |
||
106 | parent::tearDown();
|
||
107 | } |
||
108 | |||
109 | /**
|
||
110 | * testGetViewFilename method
|
||
111 | *
|
||
112 | * @return void
|
||
113 | */
|
||
114 | public function testGetViewFilename() { |
||
115 | $_admin = Configure::read('Routing.prefixes'); |
||
116 | Configure::write('Routing.prefixes', array('admin')); |
||
117 | |||
118 | $this->Controller->request->params['action'] = 'index'; |
||
119 | $ScaffoldView = new TestScaffoldView($this->Controller); |
||
120 | $result = $ScaffoldView->testGetFilename('index'); |
||
121 | $expected = CAKE . 'View' . DS . 'Scaffolds' . DS . 'index.ctp'; |
||
122 | $this->assertEquals($expected, $result); |
||
123 | |||
124 | $result = $ScaffoldView->testGetFilename('edit'); |
||
125 | $expected = CAKE . 'View' . DS . 'Scaffolds' . DS . 'form.ctp'; |
||
126 | $this->assertEquals($expected, $result); |
||
127 | |||
128 | $result = $ScaffoldView->testGetFilename('add'); |
||
129 | $expected = CAKE . 'View' . DS . 'Scaffolds' . DS . 'form.ctp'; |
||
130 | $this->assertEquals($expected, $result); |
||
131 | |||
132 | $result = $ScaffoldView->testGetFilename('view'); |
||
133 | $expected = CAKE . 'View' . DS . 'Scaffolds' . DS . 'view.ctp'; |
||
134 | $this->assertEquals($expected, $result); |
||
135 | |||
136 | $result = $ScaffoldView->testGetFilename('admin_index'); |
||
137 | $expected = CAKE . 'View' . DS . 'Scaffolds' . DS . 'index.ctp'; |
||
138 | $this->assertEquals($expected, $result); |
||
139 | |||
140 | $result = $ScaffoldView->testGetFilename('admin_view'); |
||
141 | $expected = CAKE . 'View' . DS . 'Scaffolds' . DS . 'view.ctp'; |
||
142 | $this->assertEquals($expected, $result); |
||
143 | |||
144 | $result = $ScaffoldView->testGetFilename('admin_edit'); |
||
145 | $expected = CAKE . 'View' . DS . 'Scaffolds' . DS . 'form.ctp'; |
||
146 | $this->assertEquals($expected, $result); |
||
147 | |||
148 | $result = $ScaffoldView->testGetFilename('admin_add'); |
||
149 | $expected = CAKE . 'View' . DS . 'Scaffolds' . DS . 'form.ctp'; |
||
150 | $this->assertEquals($expected, $result); |
||
151 | |||
152 | $result = $ScaffoldView->testGetFilename('error'); |
||
153 | $expected = CAKE . 'View' . DS . 'Errors' . DS . 'scaffold_error.ctp'; |
||
154 | $this->assertEquals($expected, $result); |
||
155 | |||
156 | $Controller = new ScaffoldViewMockController($this->request); |
||
157 | $Controller->scaffold = 'admin'; |
||
158 | $Controller->viewPath = 'Posts'; |
||
159 | $Controller->request['action'] = 'admin_edit'; |
||
160 | |||
161 | $ScaffoldView = new TestScaffoldView($Controller); |
||
162 | $result = $ScaffoldView->testGetFilename('admin_edit'); |
||
163 | $expected = CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Posts' . DS . 'scaffold.form.ctp'; |
||
164 | $this->assertEquals($expected, $result); |
||
165 | |||
166 | $result = $ScaffoldView->testGetFilename('edit'); |
||
167 | $expected = CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Posts' . DS . 'scaffold.form.ctp'; |
||
168 | $this->assertEquals($expected, $result); |
||
169 | |||
170 | $Controller = new ScaffoldViewMockController($this->request); |
||
171 | $Controller->scaffold = 'admin'; |
||
172 | $Controller->viewPath = 'Tests'; |
||
173 | $Controller->request->addParams(array( |
||
174 | 'plugin' => 'test_plugin', |
||
175 | 'action' => 'admin_add', |
||
176 | 'admin' => true |
||
177 | )); |
||
178 | $Controller->plugin = 'TestPlugin'; |
||
179 | |||
180 | $ScaffoldView = new TestScaffoldView($Controller); |
||
181 | $result = $ScaffoldView->testGetFilename('admin_add'); |
||
182 | $expected = CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . |
||
183 | DS . 'TestPlugin' . DS . 'View' . DS . 'Tests' . DS . 'scaffold.form.ctp'; |
||
184 | $this->assertEquals($expected, $result); |
||
185 | |||
186 | $result = $ScaffoldView->testGetFilename('add'); |
||
187 | $expected = CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . |
||
188 | DS . 'TestPlugin' . DS . 'View' . DS . 'Tests' . DS . 'scaffold.form.ctp'; |
||
189 | $this->assertEquals($expected, $result); |
||
190 | |||
191 | Configure::write('Routing.prefixes', $_admin); |
||
192 | } |
||
193 | |||
194 | /**
|
||
195 | * test getting the view file name for themed scaffolds.
|
||
196 | *
|
||
197 | * @return void
|
||
198 | */
|
||
199 | public function testGetViewFileNameWithTheme() { |
||
200 | $this->Controller->request['action'] = 'index'; |
||
201 | $this->Controller->viewPath = 'Posts'; |
||
202 | $this->Controller->theme = 'TestTheme'; |
||
203 | $ScaffoldView = new TestScaffoldView($this->Controller); |
||
204 | |||
205 | $result = $ScaffoldView->testGetFilename('index'); |
||
206 | $expected = CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . |
||
207 | 'Themed' . DS . 'TestTheme' . DS . 'Posts' . DS . 'scaffold.index.ctp'; |
||
208 | $this->assertEquals($expected, $result); |
||
209 | } |
||
210 | |||
211 | /**
|
||
212 | * test default index scaffold generation
|
||
213 | *
|
||
214 | * @return void
|
||
215 | */
|
||
216 | public function testIndexScaffold() { |
||
217 | $params = array( |
||
218 | 'plugin' => null, |
||
219 | 'pass' => array(), |
||
220 | 'form' => array(), |
||
221 | 'named' => array(), |
||
222 | 'url' => array('url' => 'scaffold_mock'), |
||
223 | 'controller' => 'scaffold_mock', |
||
224 | 'action' => 'index', |
||
225 | ); |
||
226 | $this->Controller->request->addParams($params); |
||
227 | $this->Controller->request->webroot = '/'; |
||
228 | $this->Controller->request->base = ''; |
||
229 | $this->Controller->request->here = '/scaffold_mock/index'; |
||
230 | |||
231 | //set router.
|
||
232 | Router::reload();
|
||
233 | Router::setRequestInfo($this->Controller->request); |
||
234 | |||
235 | $this->Controller->constructClasses(); |
||
236 | ob_start(); |
||
237 | new Scaffold($this->Controller, $this->Controller->request); |
||
238 | $this->Controller->response->send(); |
||
239 | $result = ob_get_clean();
|
||
240 | |||
241 | $this->assertRegExp('#<h2>Scaffold Mock</h2>#', $result); |
||
242 | $this->assertRegExp('#<table cellpadding="0" cellspacing="0">#', $result); |
||
243 | |||
244 | $this->assertRegExp('#<a href="/scaffold_users/view/1">1</a>#', $result); //belongsTo links |
||
245 | $this->assertRegExp('#<li><a href="/scaffold_mock/add">New Scaffold Mock</a></li>#', $result); |
||
246 | $this->assertRegExp('#<li><a href="/scaffold_users">List Scaffold Users</a></li>#', $result); |
||
247 | $this->assertRegExp('#<li><a href="/scaffold_comments/add">New Comment</a></li>#', $result); |
||
248 | } |
||
249 | |||
250 | /**
|
||
251 | * test default view scaffold generation
|
||
252 | *
|
||
253 | * @return void
|
||
254 | */
|
||
255 | public function testViewScaffold() { |
||
256 | $this->Controller->request->base = ''; |
||
257 | $this->Controller->request->here = '/scaffold_mock'; |
||
258 | $this->Controller->request->webroot = '/'; |
||
259 | $params = array( |
||
260 | 'plugin' => null, |
||
261 | 'pass' => array(1), |
||
262 | 'form' => array(), |
||
263 | 'named' => array(), |
||
264 | 'url' => array('url' => 'scaffold_mock/view/1'), |
||
265 | 'controller' => 'scaffold_mock', |
||
266 | 'action' => 'view', |
||
267 | ); |
||
268 | $this->Controller->request->addParams($params); |
||
269 | |||
270 | //set router.
|
||
271 | Router::reload();
|
||
272 | Router::setRequestInfo($this->Controller->request); |
||
273 | $this->Controller->constructClasses(); |
||
274 | |||
275 | ob_start(); |
||
276 | new Scaffold($this->Controller, $this->Controller->request); |
||
277 | $this->Controller->response->send(); |
||
278 | $result = ob_get_clean();
|
||
279 | |||
280 | $this->assertRegExp('/<h2>View Scaffold Mock<\/h2>/', $result); |
||
281 | $this->assertRegExp('/<dl>/', $result); |
||
282 | |||
283 | $this->assertRegExp('/<a href="\/scaffold_users\/view\/1">1<\/a>/', $result); //belongsTo links |
||
284 | $this->assertRegExp('/<li><a href="\/scaffold_mock\/edit\/1">Edit Scaffold Mock<\/a>\s<\/li>/', $result); |
||
285 | $this->assertRegExp('/<a href="\#" onclick="if[^>]*>Delete Scaffold Mock<\/a>\s<\/li>/', $result); |
||
286 | //check related table
|
||
287 | $this->assertRegExp('/<div class="related">\s*<h3>Related Scaffold Comments<\/h3>\s*<table cellpadding="0" cellspacing="0">/', $result); |
||
288 | $this->assertRegExp('/<li><a href="\/scaffold_comments\/add">New Comment<\/a><\/li>/', $result); |
||
289 | $this->assertNotRegExp('/<th>JoinThing<\/th>/', $result); |
||
290 | } |
||
291 | |||
292 | /**
|
||
293 | * test default view scaffold generation
|
||
294 | *
|
||
295 | * @return void
|
||
296 | */
|
||
297 | public function testEditScaffold() { |
||
298 | $this->Controller->request->base = ''; |
||
299 | $this->Controller->request->webroot = '/'; |
||
300 | $this->Controller->request->here = '/scaffold_mock/edit/1'; |
||
301 | |||
302 | $params = array( |
||
303 | 'plugin' => null, |
||
304 | 'pass' => array(1), |
||
305 | 'form' => array(), |
||
306 | 'named' => array(), |
||
307 | 'url' => array('url' => 'scaffold_mock'), |
||
308 | 'controller' => 'scaffold_mock', |
||
309 | 'action' => 'edit', |
||
310 | ); |
||
311 | $this->Controller->request->addParams($params); |
||
312 | |||
313 | //set router.
|
||
314 | Router::reload();
|
||
315 | Router::setRequestInfo($this->Controller->request); |
||
316 | $this->Controller->constructClasses(); |
||
317 | |||
318 | ob_start(); |
||
319 | new Scaffold($this->Controller, $this->Controller->request); |
||
320 | $this->Controller->response->send(); |
||
321 | $result = ob_get_clean();
|
||
322 | |||
323 | $this->assertContains('<form action="/scaffold_mock/edit/1" id="ScaffoldMockEditForm" method="post"', $result); |
||
324 | $this->assertContains('<legend>Edit Scaffold Mock</legend>', $result); |
||
325 | |||
326 | $this->assertContains('input type="hidden" name="data[ScaffoldMock][id]" value="1" id="ScaffoldMockId"', $result); |
||
327 | $this->assertContains('select name="data[ScaffoldMock][user_id]" id="ScaffoldMockUserId"', $result); |
||
328 | $this->assertContains('input name="data[ScaffoldMock][title]" maxlength="255" type="text" value="First Article" id="ScaffoldMockTitle"', $result); |
||
329 | $this->assertContains('input name="data[ScaffoldMock][published]" maxlength="1" type="text" value="Y" id="ScaffoldMockPublished"', $result); |
||
330 | $this->assertContains('textarea name="data[ScaffoldMock][body]" cols="30" rows="6" id="ScaffoldMockBody"', $result); |
||
331 | $this->assertRegExp('/<a href="\#" onclick="if[^>]*>Delete<\/a><\/li>/', $result); |
||
332 | } |
||
333 | |||
334 | /**
|
||
335 | * Test Admin Index Scaffolding.
|
||
336 | *
|
||
337 | * @return void
|
||
338 | */
|
||
339 | public function testAdminIndexScaffold() { |
||
340 | $_backAdmin = Configure::read('Routing.prefixes'); |
||
341 | |||
342 | Configure::write('Routing.prefixes', array('admin')); |
||
343 | $params = array( |
||
344 | 'plugin' => null, |
||
345 | 'pass' => array(), |
||
346 | 'form' => array(), |
||
347 | 'named' => array(), |
||
348 | 'prefix' => 'admin', |
||
349 | 'url' => array('url' => 'admin/scaffold_mock'), |
||
350 | 'controller' => 'scaffold_mock', |
||
351 | 'action' => 'admin_index', |
||
352 | 'admin' => 1, |
||
353 | ); |
||
354 | $this->Controller->request->base = ''; |
||
355 | $this->Controller->request->webroot = '/'; |
||
356 | $this->Controller->request->here = '/admin/scaffold_mock'; |
||
357 | $this->Controller->request->addParams($params); |
||
358 | |||
359 | //reset, and set router.
|
||
360 | Router::reload();
|
||
361 | Router::setRequestInfo($this->Controller->request); |
||
362 | |||
363 | $this->Controller->scaffold = 'admin'; |
||
364 | $this->Controller->constructClasses(); |
||
365 | |||
366 | ob_start(); |
||
367 | new Scaffold($this->Controller, $this->Controller->request); |
||
368 | $this->Controller->response->send(); |
||
369 | $result = ob_get_clean();
|
||
370 | |||
371 | $this->assertRegExp('/<h2>Scaffold Mock<\/h2>/', $result); |
||
372 | $this->assertRegExp('/<table cellpadding="0" cellspacing="0">/', $result); |
||
373 | |||
374 | $this->assertRegExp('/<li><a href="\/admin\/scaffold_mock\/add">New Scaffold Mock<\/a><\/li>/', $result); |
||
375 | |||
376 | Configure::write('Routing.prefixes', $_backAdmin); |
||
377 | } |
||
378 | |||
379 | /**
|
||
380 | * Test Admin Index Scaffolding.
|
||
381 | *
|
||
382 | * @return void
|
||
383 | */
|
||
384 | public function testAdminEditScaffold() { |
||
385 | Configure::write('Routing.prefixes', array('admin')); |
||
386 | $params = array( |
||
387 | 'plugin' => null, |
||
388 | 'pass' => array(1), |
||
389 | 'form' => array(), |
||
390 | 'named' => array(), |
||
391 | 'prefix' => 'admin', |
||
392 | 'url' => array('url' => 'admin/scaffold_mock/edit/1'), |
||
393 | 'controller' => 'scaffold_mock', |
||
394 | 'action' => 'admin_edit', |
||
395 | 'admin' => 1, |
||
396 | ); |
||
397 | $this->Controller->request->base = ''; |
||
398 | $this->Controller->request->webroot = '/'; |
||
399 | $this->Controller->request->here = '/admin/scaffold_mock/edit/1'; |
||
400 | $this->Controller->request->addParams($params); |
||
401 | |||
402 | //reset, and set router.
|
||
403 | Router::reload();
|
||
404 | Router::setRequestInfo($this->Controller->request); |
||
405 | |||
406 | $this->Controller->scaffold = 'admin'; |
||
407 | $this->Controller->constructClasses(); |
||
408 | |||
409 | ob_start(); |
||
410 | new Scaffold($this->Controller, $this->Controller->request); |
||
411 | $this->Controller->response->send(); |
||
412 | $result = ob_get_clean();
|
||
413 | |||
414 | $this->assertRegExp('#admin/scaffold_mock/edit/1#', $result); |
||
415 | $this->assertRegExp('#Scaffold Mock#', $result); |
||
416 | } |
||
417 | |||
418 | /**
|
||
419 | * Test Admin Index Scaffolding.
|
||
420 | *
|
||
421 | * @return void
|
||
422 | */
|
||
423 | public function testMultiplePrefixScaffold() { |
||
424 | $_backAdmin = Configure::read('Routing.prefixes'); |
||
425 | |||
426 | Configure::write('Routing.prefixes', array('admin', 'member')); |
||
427 | $params = array( |
||
428 | 'plugin' => null, |
||
429 | 'pass' => array(), |
||
430 | 'form' => array(), |
||
431 | 'named' => array(), |
||
432 | 'prefix' => 'member', |
||
433 | 'url' => array('url' => 'member/scaffold_mock'), |
||
434 | 'controller' => 'scaffold_mock', |
||
435 | 'action' => 'member_index', |
||
436 | 'member' => 1, |
||
437 | ); |
||
438 | $this->Controller->request->base = ''; |
||
439 | $this->Controller->request->webroot = '/'; |
||
440 | $this->Controller->request->here = '/member/scaffold_mock'; |
||
441 | $this->Controller->request->addParams($params); |
||
442 | |||
443 | //reset, and set router.
|
||
444 | Router::reload();
|
||
445 | Router::setRequestInfo($this->Controller->request); |
||
446 | |||
447 | $this->Controller->scaffold = 'member'; |
||
448 | $this->Controller->constructClasses(); |
||
449 | |||
450 | ob_start(); |
||
451 | new Scaffold($this->Controller, $this->Controller->request); |
||
452 | $this->Controller->response->send(); |
||
453 | $result = ob_get_clean();
|
||
454 | |||
455 | $this->assertRegExp('/<h2>Scaffold Mock<\/h2>/', $result); |
||
456 | $this->assertRegExp('/<table cellpadding="0" cellspacing="0">/', $result); |
||
457 | |||
458 | $this->assertRegExp('/<li><a href="\/member\/scaffold_mock\/add">New Scaffold Mock<\/a><\/li>/', $result); |
||
459 | |||
460 | Configure::write('Routing.prefixes', $_backAdmin); |
||
461 | } |
||
462 | |||
463 | } |