Move ResultWrapper subclasses to Rdbms
[mediawiki.git] / tests / phpunit / includes / password / UserPasswordPolicyTest.php
blob5ea7b1d2a9550eb0c492382e2e58eda07ad3c775
1 <?php
2 /**
3 * Testing for password-policy enforcement, based on a user's groups.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
20 * @file
23 class UserPasswordPolicyTest extends MediaWikiTestCase {
25 protected $policies = [
26 'checkuser' => [
27 'MinimalPasswordLength' => 10,
28 'MinimumPasswordLengthToLogin' => 6,
29 'PasswordCannotMatchUsername' => true,
31 'sysop' => [
32 'MinimalPasswordLength' => 8,
33 'MinimumPasswordLengthToLogin' => 1,
34 'PasswordCannotMatchUsername' => true,
36 'default' => [
37 'MinimalPasswordLength' => 4,
38 'MinimumPasswordLengthToLogin' => 1,
39 'PasswordCannotMatchBlacklist' => true,
40 'MaximalPasswordLength' => 4096,
44 protected $checks = [
45 'MinimalPasswordLength' => 'PasswordPolicyChecks::checkMinimalPasswordLength',
46 'MinimumPasswordLengthToLogin' => 'PasswordPolicyChecks::checkMinimumPasswordLengthToLogin',
47 'PasswordCannotMatchUsername' => 'PasswordPolicyChecks::checkPasswordCannotMatchUsername',
48 'PasswordCannotMatchBlacklist' => 'PasswordPolicyChecks::checkPasswordCannotMatchBlacklist',
49 'MaximalPasswordLength' => 'PasswordPolicyChecks::checkMaximalPasswordLength',
52 private function getUserPasswordPolicy() {
53 return new UserPasswordPolicy( $this->policies, $this->checks );
56 /**
57 * @covers UserPasswordPolicy::getPoliciesForUser
59 public function testGetPoliciesForUser() {
61 $upp = $this->getUserPasswordPolicy();
63 $user = User::newFromName( 'TestUserPolicy' );
64 $user->addToDatabase();
65 $user->addGroup( 'sysop' );
67 $this->assertArrayEquals(
69 'MinimalPasswordLength' => 8,
70 'MinimumPasswordLengthToLogin' => 1,
71 'PasswordCannotMatchUsername' => 1,
72 'PasswordCannotMatchBlacklist' => true,
73 'MaximalPasswordLength' => 4096,
75 $upp->getPoliciesForUser( $user )
79 /**
80 * @covers UserPasswordPolicy::getPoliciesForGroups
82 public function testGetPoliciesForGroups() {
83 $effective = UserPasswordPolicy::getPoliciesForGroups(
84 $this->policies,
85 [ 'user', 'checkuser' ],
86 $this->policies['default']
89 $this->assertArrayEquals(
91 'MinimalPasswordLength' => 10,
92 'MinimumPasswordLengthToLogin' => 6,
93 'PasswordCannotMatchUsername' => true,
94 'PasswordCannotMatchBlacklist' => true,
95 'MaximalPasswordLength' => 4096,
97 $effective
102 * @dataProvider provideCheckUserPassword
103 * @covers UserPasswordPolicy::checkUserPassword
105 public function testCheckUserPassword( $username, $groups, $password, $valid, $ok, $msg ) {
107 $upp = $this->getUserPasswordPolicy();
109 $user = User::newFromName( $username );
110 $user->addToDatabase();
111 foreach ( $groups as $group ) {
112 $user->addGroup( $group );
115 $status = $upp->checkUserPassword( $user, $password );
116 $this->assertSame( $valid, $status->isGood(), $msg . ' - password valid' );
117 $this->assertSame( $ok, $status->isOK(), $msg . ' - can login' );
120 public function provideCheckUserPassword() {
121 return [
123 'PassPolicyUser',
126 false,
127 false,
128 'No groups, default policy, password too short to login'
131 'PassPolicyUser',
132 [ 'user' ],
133 'aaa',
134 false,
135 true,
136 'Default policy, short password'
139 'PassPolicyUser',
140 [ 'sysop' ],
141 'abcdabcdabcd',
142 true,
143 true,
144 'Sysop with good password'
147 'PassPolicyUser',
148 [ 'sysop' ],
149 'abcd',
150 false,
151 true,
152 'Sysop with short password'
155 'PassPolicyUser',
156 [ 'sysop', 'checkuser' ],
157 'abcdabcd',
158 false,
159 true,
160 'Checkuser with short password'
163 'PassPolicyUser',
164 [ 'sysop', 'checkuser' ],
165 'abcd',
166 false,
167 false,
168 'Checkuser with too short password to login'
171 'Useruser',
172 [ 'user' ],
173 'Passpass',
174 false,
175 true,
176 'Username & password on blacklist'
182 * @dataProvider provideMaxOfPolicies
183 * @covers UserPasswordPolicy::maxOfPolicies
185 public function testMaxOfPolicies( $p1, $p2, $max, $msg ) {
186 $this->assertArrayEquals(
187 $max,
188 UserPasswordPolicy::maxOfPolicies( $p1, $p2 ),
189 $msg
193 public function provideMaxOfPolicies() {
194 return [
196 [ 'MinimalPasswordLength' => 8 ], // p1
197 [ 'MinimalPasswordLength' => 2 ], // p2
198 [ 'MinimalPasswordLength' => 8 ], // max
199 'Basic max in p1'
202 [ 'MinimalPasswordLength' => 2 ], // p1
203 [ 'MinimalPasswordLength' => 8 ], // p2
204 [ 'MinimalPasswordLength' => 8 ], // max
205 'Basic max in p2'
208 [ 'MinimalPasswordLength' => 8 ], // p1
210 'MinimalPasswordLength' => 2,
211 'PasswordCannotMatchUsername' => 1,
212 ], // p2
214 'MinimalPasswordLength' => 8,
215 'PasswordCannotMatchUsername' => 1,
216 ], // max
217 'Missing items in p1'
221 'MinimalPasswordLength' => 8,
222 'PasswordCannotMatchUsername' => 1,
223 ], // p1
225 'MinimalPasswordLength' => 2,
226 ], // p2
228 'MinimalPasswordLength' => 8,
229 'PasswordCannotMatchUsername' => 1,
230 ], // max
231 'Missing items in p2'