-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathProtectedSetPasswordValidateTest.php
More file actions
314 lines (292 loc) · 8.86 KB
/
ProtectedSetPasswordValidateTest.php
File metadata and controls
314 lines (292 loc) · 8.86 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
<?php
/**
* User::_setPasswordValidate()のテスト
*
* @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('UsersModelTestCase', 'Users.TestSuite');
/**
* User::_setPasswordValidate()のテスト
*
* @author Shohei Nakajima <nakajimashouhei@gmail.com>
* @package NetCommons\Users\Test\Case\Model\User
*/
class UserProtectedSetPasswordValidateTest extends UsersModelTestCase {
/**
* Fixtures
*
* @var array
*/
public $fixtures = array(
'plugin.users.user',
'plugin.users.user_select_count',
'plugin.users.users_language',
);
/**
* Plugin name
*
* @var string
*/
public $plugin = 'users';
/**
* Model name
*
* @var string
*/
protected $_modelName = 'User';
/**
* Method name
*
* @var string
*/
protected $_methodName = '_setPasswordValidate';
/**
* setUp method
*
* @return void
*/
public function setUp() {
parent::setUp();
$this->_loginByRoleKey('system_administrator');
}
/**
* _setPasswordValidate()テストのDataProvider
*
* ### 戻り値
* - data データ
* - options Model::save()のオプション
* - unset unsetしたかどうか
*
* @return array データ
*/
public function dataProvider() {
return array(
array(
'data' => array('User' => array('id' => '1', 'password' => 'aaaaa')),
'options' => array(),
'unset' => false,
),
array(
'data' => array('User' => array('password' => '')),
'options' => array(),
'unset' => false,
),
array(
'data' => array('User' => array('id' => '1', 'password' => '')),
'options' => array('validatePassword' => true),
'unset' => false,
),
array(
'data' => array('User' => array('id' => '1', 'password' => 'aaaa')),
'options' => array('self' => true),
'unset' => false,
),
array(
'data' => array('User' => array('id' => '1', 'password' => '')),
'options' => array(),
'unset' => true,
),
);
}
/**
* _setPasswordValidate()のテスト
*
* @param array $data データ
* @param array $options Model::save()のオプション
* @param bool $unset unsetしたかどうか
* @dataProvider dataProvider
* @return void
*/
public function testSetPasswordValidate($data, $options, $unset) {
$model = $this->_modelName;
$methodName = $this->_methodName;
//テストデータ
$this->$model->data = $data;
//テスト実施
$this->_testReflectionMethod(
$this->$model, $methodName, array($options)
);
//チェック
if ($unset) {
$this->assertArrayNotHasKey('password', $this->$model->validate);
$this->assertArrayNotHasKey('password_again', $this->$model->validate);
$this->assertArrayNotHasKey('password_current', $this->$model->validate);
$this->assertArrayNotHasKey('password', $this->$model->data);
} else {
$this->assertArrayHasKey('password', $this->$model->validate);
$expected = array('notBlank', 'alphaNumericSymbols', 'minLength');
$this->assertEquals($expected, array_keys($this->$model->validate['password']));
$this->assertArrayHasKey('password_again', $this->$model->validate);
$expected = array('notBlank', 'equalToField');
$this->assertEquals($expected, array_keys($this->$model->validate['password_again']));
if (isset($options['self'])) {
$this->assertArrayHasKey('password_current', $this->$model->validate);
$expected = array('notBlank', 'currentPassword');
$this->assertEquals($expected, array_keys($this->$model->validate['password_current']));
}
}
}
/**
* _setPasswordValidate()テストのDataProvider
*
* ### 戻り値
* - field テストフィールド
* - data テストデータ
* - expected 期待値
* - options Model::save()のオプション
*
* @return array データ
*/
public function dataProviderValidationError() {
return array(
//notBlankテスト
array(
'field' => 'password',
'data' => array(
'username' => 'aaaaa', 'password' => '', 'password_again' => 'aaaa',
),
'expected' => sprintf(__d('net_commons', 'Please input %s.'), __d('users', 'password')),
'options' => array(),
),
array(
'field' => 'password_again',
'data' => array(
'username' => 'aaaaa', 'password' => 'aaaa', 'password_again' => '',
),
'expected' => sprintf(__d('net_commons', 'Please input %s.'), __d('net_commons', 'Re-enter')),
'options' => array(),
),
array(
'field' => 'password_current',
'data' => array(
'id' => '1',
'username' => 'aaaaa', 'password' => 'aaaa', 'password_again' => 'aaaa',
'password_current' => '',
),
'expected' => sprintf(__d('net_commons', 'Please input %s.'), __d('net_commons', 'Current password')),
'options' => array('self' => true),
),
//alphaNumericSymbolsテスト
array(
'field' => 'password',
'data' => array(
'username' => 'aaaaa', 'password' => 'aaaaあaaaa', 'password_again' => 'aaaaあaaaa',
),
'expected' => sprintf(
__d('net_commons', 'Only alphabets, numbers and symbols are allowed to use for %s.'),
__d('users', 'password')
),
'options' => array(),
),
//minLengthテスト
array(
'field' => 'password',
'data' => array(
'username' => 'aaaaa', 'password' => 'aaa', 'password_again' => 'aaa',
),
'expected' => __d('net_commons', 'Please choose at least %s characters string.', 4),
'options' => array(),
),
//equalToFieldテスト
array(
'field' => 'password_again',
'data' => array(
'username' => 'aaaaa', 'password' => 'aaaa', 'password_again' => 'aaaaa',
),
'expected' => __d('net_commons', 'The input data does not match. Please try again.'),
'options' => array(),
),
//currentPasswordテスト
array(
'field' => 'password_current',
'data' => array(
'id' => '1',
'username' => 'system_administrator',
'password' => 'aaaa', 'password_again' => 'aaaa', 'password_current' => 'aaaa',
),
'expected' => __d('net_commons', 'Current password is wrong.'),
'options' => array('self' => true),
),
//正常
array(
'field' => '',
'data' => array(
'username' => 'aaaaa', 'password' => 'aaaa', 'password_again' => 'aaaa',
),
'expected' => true,
'options' => array(),
),
array(
'field' => '',
'data' => array(
'id' => '1',
'username' => 'system_administrator',
'password' => 'aaaa', 'password_again' => 'aaaa', 'password_current' => 'system_administrator',
),
'expected' => true,
'options' => array('self' => true),
),
);
}
/**
* _setPasswordValidate()のValidationErrorテスト
*
* @param string $field テストフィールド
* @param array $data テストデータ
* @param string|bool $expected 期待値
* @param array $options Model::save()のオプション
* @dataProvider dataProviderValidationError
* @return void
*/
public function testValidationError($field, $data, $expected, $options) {
$model = $this->_modelName;
//テストデータ
Current::write('User.id', '1');
Current::write('PluginsRole.0.plugin_key', 'user_manager');
$this->_mockForReturn($model, 'UserAttributes.UserAttribute', 'getUserAttributesForLayout', array(
'1' => array(
'2' => array(
'1' => array(
'UserAttributeSetting' => array(
'id' => '2', 'user_attribute_key' => 'username', 'data_type_key' => 'text',
'row' => '1', 'col' => '2', 'weight' => '1', 'required' => '1', 'display' => '1',
'only_administrator_readable' => '1', 'only_administrator_editable' => '1',
'is_system' => '1', 'display_label' => '1', 'display_search_result' => '0',
'self_public_setting' => '0', 'self_email_setting' => '0', 'is_multilingualization' => '0',
'auto_regist_display' => null, 'auto_regist_weight' => '1',
),
'UserAttribute' => array(
'id' => '2', 'language_id' => '2', 'key' => 'username', 'name' => 'ログインID',
),
),
),
)
));
$this->_mockForReturn($model, 'UserRoles.UserAttributesRole', 'getUserAttributesRole', array(
array(
'UserAttributesRole' => array(
'id' => '1', 'role_key' => 'system_administrator', 'user_attribute_key' => 'username',
'self_readable' => '1', 'self_editable' => '1', 'other_readable' => '1', 'other_editable' => '1',
)
)
));
$this->$model->prepare();
//テスト実施
$this->$model->set($data);
$result = $this->$model->validates($options);
//チェック
if ($expected === true) {
$this->assertTrue($result);
$this->assertArrayNotHasKey('password', $this->$model->validationErrors);
$this->assertArrayNotHasKey('password_again', $this->$model->validationErrors);
$this->assertArrayNotHasKey('password_current', $this->$model->validationErrors);
} else {
$this->assertFalse($result);
$this->assertEquals($this->$model->validationErrors[$field][0], $expected);
}
}
}