-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathViewTest.php
More file actions
378 lines (346 loc) · 10.2 KB
/
ViewTest.php
File metadata and controls
378 lines (346 loc) · 10.2 KB
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
<?php
/**
* UserManagerController::view()のテスト
*
* @author Noriko Arai <arai@nii.ac.jp>
* @author Shohei Nakajima <nakajimashouhei@gmail.com>
* @link http://www.netcommons.org NetCommons Project
* @license http://www.netcommons.org/license.txt NetCommons License
* @copyright Copyright 2014, NetCommons Project
*/
App::uses('UserManagerControllerTestCase', 'UserManager.TestSuite');
App::uses('UserAttribute', 'UserAttributes.Model');
/**
* UserManagerController::view()のテスト
*
* @author Shohei Nakajima <nakajimashouhei@gmail.com>
* @package NetCommons\UserManager\Test\Case\Controller\UserManagerController
*/
class UserManagerControllerViewTest extends UserManagerControllerTestCase {
/**
* Controller name
*
* @var string
*/
protected $_controller = 'user_manager';
/**
* setUp method
*
* @return void
*/
public function setUp() {
parent::setUp();
}
/**
* tearDown method
*
* @return void
*/
public function tearDown() {
UserAttribute::$userAttributes = null;
//ログアウト
TestAuthGeneral::logout($this);
parent::tearDown();
}
/**
* UserManagerController::view()テストのDataProvider
*
* ### 戻り値
* - userId ユーザID
*
* @return array データ
*/
public function dataProvider() {
$result = array();
$result[0]['userId'] = '1';
$result[1]['userId'] = '2';
$result[2]['userId'] = '4';
$result[3]['userId'] = '5';
$result[4]['userId'] = '7';
$result[5]['userId'] = '9999';
return $result;
}
/**
* システム管理者がログインした場合のテスト
*
* @param int $userId ユーザID
* @return void
* @dataProvider dataProvider
*/
public function testSystemAdmin($userId) {
//ログイン
TestAuthGeneral::login($this);
//テスト実行
if ($userId === '7' || $userId === '9999') {
$this->_testGetAction(
array('action' => 'view', 'user_id' => $userId), null, 'BadRequestException', 'view'
);
} else {
$this->_testGetAction(
array('action' => 'view', 'user_id' => $userId), null, null, 'view'
);
//チェック
$expected = $this->__getExpected($userId);
$this->__assertUser($expected);
$this->assertTextContains(
'/user_manager/user_manager/edit/' . Hash::get($expected, 'User.id'), $this->view
);
$this->__assertUserView($expected);
$this->assertTextContains(
'/user_manager/users_roles_rooms/edit/' . Hash::get($expected, 'User.id'), $this->view
);
$this->__assertRoomsView($expected);
if ($userId === '1') {
$this->assertTextContains(
__d('user_manager', 'All user attributes is displaying. ' .
'If you want to set of the group, please setting from the handle of the header menu.'),
$this->view
);
} else {
$this->assertTextContains(
__d('user_manager', 'All user attributes is displaying.'), $this->view
);
}
}
}
/**
* システム管理者がログインした場合のテスト
*
* @param int $userId ユーザID
* @return void
* @dataProvider dataProvider
*/
public function testSiteManager($userId) {
//ログイン
TestAuthGeneral::login($this, UserRole::USER_ROLE_KEY_ADMINISTRATOR);
//テスト実行
if ($userId === '7' || $userId === '9999') {
$this->_testGetAction(
array('action' => 'view', 'user_id' => $userId), null, 'BadRequestException', 'view'
);
} else {
$this->_testGetAction(
array('action' => 'view', 'user_id' => $userId), null, null, 'view'
);
//チェック
$expected = $this->__getExpected($userId);
$this->__assertUser($expected);
if ($userId === '1') {
$this->assertTextNotContains(
'/user_manager/user_manager/edit/' . Hash::get($expected, 'User.id'), $this->view
);
$this->assertTextNotContains(
'/user_manager/users_roles_rooms/edit/' . Hash::get($expected, 'User.id'), $this->view
);
} else {
$this->assertTextContains(
'/user_manager/user_manager/edit/' . Hash::get($expected, 'User.id'), $this->view
);
$this->assertTextContains(
'/user_manager/users_roles_rooms/edit/' . Hash::get($expected, 'User.id'), $this->view
);
}
$this->__assertUserView($expected);
$this->__assertRoomsView($expected);
$this->assertTextContains(
__d('user_manager', 'All user attributes is displaying.'), $this->view
);
}
}
/**
* $this->viewVars['user']のチェック
*
* @param array $expected 期待値
* @return void
*/
private function __assertUser($expected) {
//チェック
$this->assertEquals($this->vars['user']['User']['id'], Hash::get($expected, 'User.id'));
$this->assertEquals($this->vars['user']['UsersLanguage'][0]['id'], Hash::get($expected, 'UsersLanguage.0.id'));
//$this->assertEquals($this->vars['user']['UsersLanguage'][1]['id'], Hash::get($expected, 'UsersLanguage.1.id'));
$this->assertEquals($this->vars['user']['UsersLanguage'][0]['language_id'], '2');
//$this->assertEquals($this->vars['user']['UsersLanguage'][1]['language_id'], '1');
$this->assertEquals($this->vars['user']['UsersLanguage'][0]['user_id'], Hash::get($expected, 'User.id'));
//$this->assertEquals($this->vars['user']['UsersLanguage'][1]['user_id'], Hash::get($expected, 'User.id'));
$this->assertEquals($this->vars['user']['TrackableCreator']['id'], Hash::get($expected, 'TrackableCreator.id'));
$this->assertEquals($this->vars['user']['TrackableUpdater']['id'], Hash::get($expected, 'TrackableUpdater.id'));
$this->assertEquals($this->vars['user']['Role']['id'], Hash::get($expected, 'Role.id'));
$this->assertEquals($this->vars['user']['UserRoleSetting']['id'], Hash::get($expected, 'UserRoleSetting.id'));
if (isset($expected['UploadFile'])) {
$this->assertEquals(
$this->vars['user']['UploadFile']['avatar']['id'], Hash::get($expected, 'UploadFile.avatar.id')
);
$this->assertEquals(
$this->vars['user']['UploadFile']['avatar']['content_key'], Hash::get($expected, 'UploadFile.avatar.content_key')
);
}
}
/**
* viewのチェック
*
* @param array $expected 期待値
* @return void
*/
private function __assertUserView($expected) {
$this->assertTextContains(
'/users/users/download/' . Hash::get($expected, 'User.id') . '/avatar/medium', $this->view
);
$this->assertTextContains(
'>ログインID</div><div class="form-control nc-data-label">' . Hash::get($expected, 'User.username') . '<', $this->view
);
$this->assertTextContains(
'>ハンドル</div><div class="form-control nc-data-label">' . Hash::get($expected, 'User.handlename') . '<', $this->view
);
$this->assertTextContains(
'>' . Hash::get($expected, 'UsersLanguage.0.name') . '<', $this->view
);
$this->assertTextContains(
'>' . Hash::get($expected, 'UsersLanguage.0.search_keywords') . '<', $this->view
);
}
/**
* viewのチェック
*
* @param array $expected 期待値
* @return void
*/
private function __assertRoomsView($expected) {
$view = preg_replace('/[>][\s]+([^a-z])/u', '>$1', $this->view);
$view = preg_replace('/[\s]+</u', '<', $view);
$this->assertTextContains('<td>Public</td>', $view);
$this->assertTextContains('<td><span class="rooms-tree"></span>Public room</td>', $view);
if (Hash::get($expected, 'User.id') === '1' || Hash::get($expected, 'User.id') === '2') {
$this->assertTextContains('<td>Community room 1</td>', $view);
$this->assertTextContains('<td>Community room 2</td>', $view);
} elseif (Hash::get($expected, 'User.id') === '4') {
$this->assertTextNotContains('<td>Community room 1</td>', $view);
$this->assertTextContains('<td>Community room 2</td>', $view);
} elseif (Hash::get($expected, 'User.id') === '5') {
$this->assertTextNotContains('<td>Community room 2</td>', $view);
$this->assertTextContains('<td>' . __d('rooms', 'Not found.') . '</td>', $view);
}
}
/**
* 期待値の取得
*
* @param int $userId 期待値
* @return array
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
private function __getExpected($userId) {
if ($userId === '1') {
$expected = array(
'User' => array(
'id' => $userId,
'username' => 'system_administrator',
'handlename' => 'System Administrator',
),
'UsersLanguage' => array(
0 => array(
'id' => '1', 'name' => 'System Administrator Name', 'search_keywords' => 'default',
)
),
'TrackableCreator' => array(
'id' => '1',
),
'TrackableUpdater' => array(
'id' => '1',
),
'Role' => array(
'id' => '1',
),
'UserRoleSetting' => array(
'id' => '1',
),
);
} elseif ($userId === '2') {
$expected = array(
'User' => array(
'id' => $userId,
'username' => 'site_manager',
'handlename' => 'Site Manager',
),
'UsersLanguage' => array(
0 => array(
'id' => '2', 'name' => 'Site Manager Name', 'search_keywords' => 'default',
)
),
'TrackableCreator' => array(
'id' => '1',
),
'TrackableUpdater' => array(
'id' => '1',
),
'Role' => array(
'id' => '2',
),
'UserRoleSetting' => array(
'id' => '2',
),
'UploadFile' => array(
'avatar' => array(
'id' => '1',
'content_key' => '2',
),
),
'UploadFile' => array(
'avatar' => array(
'id' => '1',
'content_key' => '2',
),
),
);
} elseif ($userId === '4') {
$expected = array(
'User' => array(
'id' => $userId,
'username' => 'editor',
'handlename' => 'Editor',
),
'UsersLanguage' => array(
0 => array(
'id' => '4', 'name' => 'Editor Name', 'search_keywords' => 'default',
)
),
'TrackableCreator' => array(
'id' => '1',
),
'TrackableUpdater' => array(
'id' => '1',
),
'Role' => array(
'id' => '3',
),
'UserRoleSetting' => array(
'id' => '3',
),
);
} elseif ($userId === '5') {
$expected = array(
'User' => array(
'id' => $userId,
'username' => 'general_user',
'handlename' => 'General User',
),
'UsersLanguage' => array(
0 => array(
'id' => '5', 'name' => 'General User Name', 'search_keywords' => 'default',
)
),
'TrackableCreator' => array(
'id' => '1',
),
'TrackableUpdater' => array(
'id' => '1',
),
'Role' => array(
'id' => '3',
),
'UserRoleSetting' => array(
'id' => '3',
),
);
}
return $expected;
}
}