MediaWiki  1.28.0
UserPasswordPolicyTest.php
Go to the documentation of this file.
1 <?php
24 
25  protected $policies = [
26  'checkuser' => [
27  'MinimalPasswordLength' => 10,
28  'MinimumPasswordLengthToLogin' => 6,
29  'PasswordCannotMatchUsername' => true,
30  ],
31  'sysop' => [
32  'MinimalPasswordLength' => 8,
33  'MinimumPasswordLengthToLogin' => 1,
34  'PasswordCannotMatchUsername' => true,
35  ],
36  'default' => [
37  'MinimalPasswordLength' => 4,
38  'MinimumPasswordLengthToLogin' => 1,
39  'PasswordCannotMatchBlacklist' => true,
40  'MaximalPasswordLength' => 4096,
41  ],
42  ];
43 
44  protected $checks = [
45  'MinimalPasswordLength' => 'PasswordPolicyChecks::checkMinimalPasswordLength',
46  'MinimumPasswordLengthToLogin' => 'PasswordPolicyChecks::checkMinimumPasswordLengthToLogin',
47  'PasswordCannotMatchUsername' => 'PasswordPolicyChecks::checkPasswordCannotMatchUsername',
48  'PasswordCannotMatchBlacklist' => 'PasswordPolicyChecks::checkPasswordCannotMatchBlacklist',
49  'MaximalPasswordLength' => 'PasswordPolicyChecks::checkMaximalPasswordLength',
50  ];
51 
52  private function getUserPasswordPolicy() {
53  return new UserPasswordPolicy( $this->policies, $this->checks );
54  }
55 
59  public function testGetPoliciesForUser() {
60 
61  $upp = $this->getUserPasswordPolicy();
62 
63  $user = User::newFromName( 'TestUserPolicy' );
64  $user->addGroup( 'sysop' );
65 
66  $this->assertArrayEquals(
67  [
68  'MinimalPasswordLength' => 8,
69  'MinimumPasswordLengthToLogin' => 1,
70  'PasswordCannotMatchUsername' => 1,
71  'PasswordCannotMatchBlacklist' => true,
72  'MaximalPasswordLength' => 4096,
73  ],
74  $upp->getPoliciesForUser( $user )
75  );
76  }
77 
81  public function testGetPoliciesForGroups() {
83  $this->policies,
84  [ 'user', 'checkuser' ],
85  $this->policies['default']
86  );
87 
88  $this->assertArrayEquals(
89  [
90  'MinimalPasswordLength' => 10,
91  'MinimumPasswordLengthToLogin' => 6,
92  'PasswordCannotMatchUsername' => true,
93  'PasswordCannotMatchBlacklist' => true,
94  'MaximalPasswordLength' => 4096,
95  ],
96  $effective
97  );
98  }
99 
104  public function testCheckUserPassword( $username, $groups, $password, $valid, $ok, $msg ) {
105 
106  $upp = $this->getUserPasswordPolicy();
107 
109  foreach ( $groups as $group ) {
110  $user->addGroup( $group );
111  }
112 
113  $status = $upp->checkUserPassword( $user, $password );
114  $this->assertSame( $valid, $status->isGood(), $msg . ' - password valid' );
115  $this->assertSame( $ok, $status->isOK(), $msg . ' - can login' );
116  }
117 
118  public function provideCheckUserPassword() {
119  return [
120  [
121  'PassPolicyUser',
122  [],
123  '',
124  false,
125  false,
126  'No groups, default policy, password too short to login'
127  ],
128  [
129  'PassPolicyUser',
130  [ 'user' ],
131  'aaa',
132  false,
133  true,
134  'Default policy, short password'
135  ],
136  [
137  'PassPolicyUser',
138  [ 'sysop' ],
139  'abcdabcdabcd',
140  true,
141  true,
142  'Sysop with good password'
143  ],
144  [
145  'PassPolicyUser',
146  [ 'sysop' ],
147  'abcd',
148  false,
149  true,
150  'Sysop with short password'
151  ],
152  [
153  'PassPolicyUser',
154  [ 'sysop', 'checkuser' ],
155  'abcdabcd',
156  false,
157  true,
158  'Checkuser with short password'
159  ],
160  [
161  'PassPolicyUser',
162  [ 'sysop', 'checkuser' ],
163  'abcd',
164  false,
165  false,
166  'Checkuser with too short password to login'
167  ],
168  [
169  'Useruser',
170  [ 'user' ],
171  'Passpass',
172  false,
173  true,
174  'Username & password on blacklist'
175  ],
176  ];
177  }
178 
183  public function testMaxOfPolicies( $p1, $p2, $max, $msg ) {
184  $this->assertArrayEquals(
185  $max,
187  $msg
188  );
189  }
190 
191  public function provideMaxOfPolicies() {
192  return [
193  [
194  [ 'MinimalPasswordLength' => 8 ], // p1
195  [ 'MinimalPasswordLength' => 2 ], // p2
196  [ 'MinimalPasswordLength' => 8 ], // max
197  'Basic max in p1'
198  ],
199  [
200  [ 'MinimalPasswordLength' => 2 ], // p1
201  [ 'MinimalPasswordLength' => 8 ], // p2
202  [ 'MinimalPasswordLength' => 8 ], // max
203  'Basic max in p2'
204  ],
205  [
206  [ 'MinimalPasswordLength' => 8 ], // p1
207  [
208  'MinimalPasswordLength' => 2,
209  'PasswordCannotMatchUsername' => 1,
210  ], // p2
211  [
212  'MinimalPasswordLength' => 8,
213  'PasswordCannotMatchUsername' => 1,
214  ], // max
215  'Missing items in p1'
216  ],
217  [
218  [
219  'MinimalPasswordLength' => 8,
220  'PasswordCannotMatchUsername' => 1,
221  ], // p1
222  [
223  'MinimalPasswordLength' => 2,
224  ], // p2
225  [
226  'MinimalPasswordLength' => 8,
227  'PasswordCannotMatchUsername' => 1,
228  ], // max
229  'Missing items in p2'
230  ],
231  ];
232  }
233 
234 }
static newFromName($name, $validate= 'valid')
Static factory method for creation from username.
Definition: User.php:525
static maxOfPolicies(array $p1, array $p2)
Utility function to get a policy that is the most restrictive of $p1 and $p2.
testCheckUserPassword($username, $groups, $password, $valid, $ok, $msg)
provideCheckUserPassword UserPasswordPolicy::checkUserPassword
assertArrayEquals(array $expected, array $actual, $ordered=false, $named=false)
Assert that two arrays are equal.
testMaxOfPolicies($p1, $p2, $max, $msg)
provideMaxOfPolicies UserPasswordPolicy::maxOfPolicies
Check if a user's password complies with any password policies that apply to that user...
testGetPoliciesForUser()
UserPasswordPolicy::getPoliciesForUser.
static getPoliciesForGroups(array $policies, array $userGroups, array $defaultPolicy)
Utility function to get the effective policy from a list of policies, based on a list of groups...
testGetPoliciesForGroups()
UserPasswordPolicy::getPoliciesForGroups.
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
please add to it if you re going to add events to the MediaWiki code where normally authentication against an external auth plugin would be creating a local account $user
Definition: hooks.txt:242
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
this hook is for auditing only or null if authentication failed before getting that far $username
Definition: hooks.txt:802
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist e g Watchlist removed from all revisions and log entries to which it was applied This gives extensions a chance to take it off their books as the deletion has already been partly carried out by this point or something similar the user will be unable to create the tag set $status
Definition: hooks.txt:1046