/
ScaffoldViewTest.php
315 lines (265 loc) · 10 KB
/
ScaffoldViewTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
<?php
/**
* CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
* Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice
*
* @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
* @since CakePHP(tm) v 2.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
namespace Cake\Test\TestCase\View;
use Cake\Controller\Controller;
use Cake\Controller\Scaffold;
use Cake\Core\App;
use Cake\Core\Configure;
use Cake\Core\Plugin;
use Cake\Network\Request;
use Cake\Routing\Router;
use Cake\TestSuite\TestCase;
use Cake\View\ScaffoldView;
use TestApp\Controller\ScaffoldArticlesController;
require_once dirname(__DIR__) . DS . 'Model' . DS . 'models.php';
if (!class_exists('Cake\Model\ScaffoldMock')) {
class_alias('Cake\Test\TestCase\Model\ScaffoldMock', 'Cake\Model\ScaffoldMock');
}
/**
* TestScaffoldView class
*
* @package Cake.Test.TestCase.View
*/
class TestScaffoldView extends ScaffoldView {
/**
* testGetFilename method
*
* @param string $action
* @return void
*/
public function testGetFilename($action) {
return $this->_getViewFileName($action);
}
}
/**
* ScaffoldViewTest class
*
* @package Cake.Test.TestCase.View
*/
class ScaffoldViewTest extends TestCase {
/**
* fixtures property
*
* @var array
*/
public $fixtures = [
'core.article',
'core.user',
'core.comment',
'core.articles_tag',
'core.tag'
];
/**
* setUp method
*
* @return void
*/
public function setUp() {
parent::setUp();
$this->markTestIncomplete('Need to revisit once models work again.');
Configure::write('App.namespace', 'TestApp');
Router::connect('/:controller/:action/*');
Router::connect('/:controller', ['action' => 'index']);
$this->request = new Request();
$this->Controller = new ScaffoldArticlesController($this->request);
$this->Controller->response = $this->getMock('Cake\Network\Response', array('_sendHeader'));
App::build(array(
'View' => array(CAKE . 'Test' . DS . 'TestApp' . DS . 'View' . DS),
'Plugin' => array(CAKE . 'Test' . DS . 'TestApp' . DS . 'Plugin' . DS)
));
Plugin::load('TestPlugin');
}
/**
* tearDown method
*
* @return void
*/
public function tearDown() {
unset($this->Controller, $this->request);
parent::tearDown();
}
/**
* testGetViewFilename method
*
* @return void
*/
public function testGetViewFilename() {
Configure::write('Routing.prefixes', array('admin'));
$this->Controller->request->params['action'] = 'index';
$ScaffoldView = new TestScaffoldView($this->Controller);
$result = $ScaffoldView->testGetFilename('index');
$expected = CAKE . 'View' . DS . 'Scaffolds' . DS . 'index.ctp';
$this->assertEquals($expected, $result);
$result = $ScaffoldView->testGetFilename('edit');
$expected = CAKE . 'View' . DS . 'Scaffolds' . DS . 'form.ctp';
$this->assertEquals($expected, $result);
$result = $ScaffoldView->testGetFilename('add');
$expected = CAKE . 'View' . DS . 'Scaffolds' . DS . 'form.ctp';
$this->assertEquals($expected, $result);
$result = $ScaffoldView->testGetFilename('view');
$expected = CAKE . 'View' . DS . 'Scaffolds' . DS . 'view.ctp';
$this->assertEquals($expected, $result);
$result = $ScaffoldView->testGetFilename('admin_index');
$expected = CAKE . 'View' . DS . 'Scaffolds' . DS . 'index.ctp';
$this->assertEquals($expected, $result);
$result = $ScaffoldView->testGetFilename('admin_view');
$expected = CAKE . 'View' . DS . 'Scaffolds' . DS . 'view.ctp';
$this->assertEquals($expected, $result);
$result = $ScaffoldView->testGetFilename('admin_edit');
$expected = CAKE . 'View' . DS . 'Scaffolds' . DS . 'form.ctp';
$this->assertEquals($expected, $result);
$result = $ScaffoldView->testGetFilename('admin_add');
$expected = CAKE . 'View' . DS . 'Scaffolds' . DS . 'form.ctp';
$this->assertEquals($expected, $result);
$result = $ScaffoldView->testGetFilename('error');
$expected = CAKE . 'View' . DS . 'Errors' . DS . 'scaffold_error.ctp';
$this->assertEquals($expected, $result);
$Controller = new ScaffoldArticlesController($this->request);
$Controller->scaffold = 'admin';
$Controller->viewPath = 'Posts';
$Controller->request['action'] = 'admin_edit';
$ScaffoldView = new TestScaffoldView($Controller);
$result = $ScaffoldView->testGetFilename('admin_edit');
$expected = CAKE . 'Test' . DS . 'TestApp' . DS . 'View' . DS . 'Posts' . DS . 'scaffold.form.ctp';
$this->assertEquals($expected, $result);
$result = $ScaffoldView->testGetFilename('edit');
$expected = CAKE . 'Test' . DS . 'TestApp' . DS . 'View' . DS . 'Posts' . DS . 'scaffold.form.ctp';
$this->assertEquals($expected, $result);
$Controller = new ScaffoldArticlesController($this->request);
$Controller->scaffold = 'admin';
$Controller->viewPath = 'Tests';
$Controller->request->addParams(array(
'plugin' => 'test_plugin',
'action' => 'admin_add',
'admin' => true
));
$Controller->plugin = 'TestPlugin';
$ScaffoldView = new TestScaffoldView($Controller);
$pluginPath = CAKE . 'Test' . DS . 'TestApp' . DS . 'Plugin' . DS . 'TestPlugin' . DS;
$result = $ScaffoldView->testGetFilename('admin_add');
$expected = $pluginPath . 'View' . DS . 'Tests' . DS . 'scaffold.form.ctp';
$this->assertEquals($expected, $result);
$result = $ScaffoldView->testGetFilename('add');
$expected = $pluginPath . 'View' . DS . 'Tests' . DS . 'scaffold.form.ctp';
$this->assertEquals($expected, $result);
}
/**
* test getting the view file name for themed scaffolds.
*
* @return void
*/
public function testGetViewFileNameWithTheme() {
$this->Controller->request['action'] = 'index';
$this->Controller->viewPath = 'Posts';
$this->Controller->theme = 'TestTheme';
$ScaffoldView = new TestScaffoldView($this->Controller);
$themePath = CAKE . 'Test' . DS . 'TestApp' . DS . 'View' . DS . 'Themed' . DS . 'TestTheme' . DS;
$result = $ScaffoldView->testGetFilename('index');
$expected = $themePath . 'Posts' . DS . 'scaffold.index.ctp';
$this->assertEquals($expected, $result);
}
/**
* test default index scaffold generation
*
* @return void
*/
public function testIndexScaffold() {
$params = [
'plugin' => null,
'pass' => [],
'controller' => 'articles',
'action' => 'index',
];
$this->Controller->request->addParams($params);
$this->Controller->request->webroot = '/';
$this->Controller->request->base = '';
$this->Controller->request->here = '/articles/index';
Router::pushRequest($this->Controller->request);
$this->Controller->constructClasses();
ob_start();
new Scaffold($this->Controller, $this->Controller->request);
$this->Controller->response->send();
$result = ob_get_clean();
$this->assertRegExp('#<h2>Articles</h2>#', $result);
$this->assertRegExp('#<table cellpadding="0" cellspacing="0">#', $result);
$this->assertRegExp('#<a href="/users/view/1">1</a>#', $result); //belongsTo links
$this->assertRegExp('#<li><a href="/articles/add">New Article</a></li>#', $result);
$this->assertRegExp('#<li><a href="/users">List Users</a></li>#', $result);
$this->assertRegExp('#<li><a href="/comments/add">New Comment</a></li>#', $result);
}
/**
* test default view scaffold generation
*
* @return void
*/
public function testViewScaffold() {
$this->Controller->request->base = '';
$this->Controller->request->webroot = '/';
$params = [
'plugin' => null,
'pass' => [1],
'controller' => 'articles',
'action' => 'view',
];
$this->Controller->request->addParams($params);
//set router.
Router::pushRequest($this->Controller->request);
$this->Controller->constructClasses();
ob_start();
new Scaffold($this->Controller, $this->Controller->request);
$this->Controller->response->send();
$result = ob_get_clean();
$this->assertRegExp('#<h2>View Article</h2>#', $result);
$this->assertRegExp('#<dl>#', $result);
$this->assertRegExp('#<a href="/users/view/1">1</a>#', $result); //belongsTo links
$this->assertRegExp('#<li><a href="/articles/edit/1">Edit Article</a>\s*</li>#', $result);
$this->assertRegExp('#<a href="\#" onclick="if[^>]*>Delete Article</a>\s*</li>#', $result);
//check related table
$this->assertRegExp('#<div class="related">\s*<h3>Related Comments</h3>\s*<table cellpadding="0" cellspacing="0">#', $result);
$this->assertRegExp('#<li><a href="/comments/add">New Comment</a></li>#', $result);
$this->assertNotRegExp('#<th>JoinThing</th>#', $result);
}
/**
* test default edit scaffold generation
*
* @return void
*/
public function testEditScaffold() {
$this->Controller->request->base = '';
$this->Controller->request->webroot = '/';
$this->Controller->request->here = '/articles/edit/1';
$params = [
'plugin' => null,
'pass' => [1],
'controller' => 'articles',
'action' => 'edit',
];
$this->Controller->request->addParams($params);
//set router.
Router::pushRequest($this->Controller->request);
$this->Controller->constructClasses();
ob_start();
new Scaffold($this->Controller, $this->Controller->request);
$this->Controller->response->send();
$result = ob_get_clean();
$this->assertContains('<form action="/articles/edit/1" id="ArticleEditForm" method="post"', $result);
$this->assertContains('<legend>Edit Article</legend>', $result);
$this->assertContains('input type="hidden" name="Article[id]" value="1" id="ArticleId"', $result);
$this->assertContains('select name="Article[user_id]" id="ArticleUserId"', $result);
$this->assertContains('input name="Article[title]" maxlength="255" type="text" value="First Article" id="ArticleTitle"', $result);
$this->assertContains('input name="Article[published]" maxlength="1" type="text" value="Y" id="ArticlePublished"', $result);
$this->assertContains('textarea name="Article[body]" cols="30" rows="6" id="ArticleBody"', $result);
$this->assertRegExp('/<a href="\#" onclick="if[^>]*>Delete<\/a><\/li>/', $result);
}
}