MediaWiki  1.29.0
UserPasswordPolicyTest.php
Go to the documentation of this file.
1 <?php
27 
28  protected $policies = [
29  'checkuser' => [
30  'MinimalPasswordLength' => 10,
31  'MinimumPasswordLengthToLogin' => 6,
32  'PasswordCannotMatchUsername' => true,
33  ],
34  'sysop' => [
35  'MinimalPasswordLength' => 8,
36  'MinimumPasswordLengthToLogin' => 1,
37  'PasswordCannotMatchUsername' => true,
38  ],
39  'default' => [
40  'MinimalPasswordLength' => 4,
41  'MinimumPasswordLengthToLogin' => 1,
42  'PasswordCannotMatchBlacklist' => true,
43  'MaximalPasswordLength' => 4096,
44  ],
45  ];
46 
47  protected $checks = [
48  'MinimalPasswordLength' => 'PasswordPolicyChecks::checkMinimalPasswordLength',
49  'MinimumPasswordLengthToLogin' => 'PasswordPolicyChecks::checkMinimumPasswordLengthToLogin',
50  'PasswordCannotMatchUsername' => 'PasswordPolicyChecks::checkPasswordCannotMatchUsername',
51  'PasswordCannotMatchBlacklist' => 'PasswordPolicyChecks::checkPasswordCannotMatchBlacklist',
52  'MaximalPasswordLength' => 'PasswordPolicyChecks::checkMaximalPasswordLength',
53  ];
54 
55  private function getUserPasswordPolicy() {
56  return new UserPasswordPolicy( $this->policies, $this->checks );
57  }
58 
62  public function testGetPoliciesForUser() {
63 
64  $upp = $this->getUserPasswordPolicy();
65 
66  $user = User::newFromName( 'TestUserPolicy' );
67  $user->addToDatabase();
68  $user->addGroup( 'sysop' );
69 
70  $this->assertArrayEquals(
71  [
72  'MinimalPasswordLength' => 8,
73  'MinimumPasswordLengthToLogin' => 1,
74  'PasswordCannotMatchUsername' => 1,
75  'PasswordCannotMatchBlacklist' => true,
76  'MaximalPasswordLength' => 4096,
77  ],
78  $upp->getPoliciesForUser( $user )
79  );
80  }
81 
85  public function testGetPoliciesForGroups() {
87  $this->policies,
88  [ 'user', 'checkuser' ],
89  $this->policies['default']
90  );
91 
92  $this->assertArrayEquals(
93  [
94  'MinimalPasswordLength' => 10,
95  'MinimumPasswordLengthToLogin' => 6,
96  'PasswordCannotMatchUsername' => true,
97  'PasswordCannotMatchBlacklist' => true,
98  'MaximalPasswordLength' => 4096,
99  ],
100  $effective
101  );
102  }
103 
108  public function testCheckUserPassword( $username, $groups, $password, $valid, $ok, $msg ) {
109 
110  $upp = $this->getUserPasswordPolicy();
111 
113  $user->addToDatabase();
114  foreach ( $groups as $group ) {
115  $user->addGroup( $group );
116  }
117 
118  $status = $upp->checkUserPassword( $user, $password );
119  $this->assertSame( $valid, $status->isGood(), $msg . ' - password valid' );
120  $this->assertSame( $ok, $status->isOK(), $msg . ' - can login' );
121  }
122 
123  public function provideCheckUserPassword() {
124  return [
125  [
126  'PassPolicyUser',
127  [],
128  '',
129  false,
130  false,
131  'No groups, default policy, password too short to login'
132  ],
133  [
134  'PassPolicyUser',
135  [ 'user' ],
136  'aaa',
137  false,
138  true,
139  'Default policy, short password'
140  ],
141  [
142  'PassPolicyUser',
143  [ 'sysop' ],
144  'abcdabcdabcd',
145  true,
146  true,
147  'Sysop with good password'
148  ],
149  [
150  'PassPolicyUser',
151  [ 'sysop' ],
152  'abcd',
153  false,
154  true,
155  'Sysop with short password'
156  ],
157  [
158  'PassPolicyUser',
159  [ 'sysop', 'checkuser' ],
160  'abcdabcd',
161  false,
162  true,
163  'Checkuser with short password'
164  ],
165  [
166  'PassPolicyUser',
167  [ 'sysop', 'checkuser' ],
168  'abcd',
169  false,
170  false,
171  'Checkuser with too short password to login'
172  ],
173  [
174  'Useruser',
175  [ 'user' ],
176  'Passpass',
177  false,
178  true,
179  'Username & password on blacklist'
180  ],
181  ];
182  }
183 
188  public function testMaxOfPolicies( $p1, $p2, $max, $msg ) {
189  $this->assertArrayEquals(
190  $max,
192  $msg
193  );
194  }
195 
196  public function provideMaxOfPolicies() {
197  return [
198  [
199  [ 'MinimalPasswordLength' => 8 ], // p1
200  [ 'MinimalPasswordLength' => 2 ], // p2
201  [ 'MinimalPasswordLength' => 8 ], // max
202  'Basic max in p1'
203  ],
204  [
205  [ 'MinimalPasswordLength' => 2 ], // p1
206  [ 'MinimalPasswordLength' => 8 ], // p2
207  [ 'MinimalPasswordLength' => 8 ], // max
208  'Basic max in p2'
209  ],
210  [
211  [ 'MinimalPasswordLength' => 8 ], // p1
212  [
213  'MinimalPasswordLength' => 2,
214  'PasswordCannotMatchUsername' => 1,
215  ], // p2
216  [
217  'MinimalPasswordLength' => 8,
218  'PasswordCannotMatchUsername' => 1,
219  ], // max
220  'Missing items in p1'
221  ],
222  [
223  [
224  'MinimalPasswordLength' => 8,
225  'PasswordCannotMatchUsername' => 1,
226  ], // p1
227  [
228  'MinimalPasswordLength' => 2,
229  ], // p2
230  [
231  'MinimalPasswordLength' => 8,
232  'PasswordCannotMatchUsername' => 1,
233  ], // max
234  'Missing items in p2'
235  ],
236  ];
237  }
238 
239 }
MediaWikiTestCase\assertArrayEquals
assertArrayEquals(array $expected, array $actual, $ordered=false, $named=false)
Assert that two arrays are equal.
Definition: MediaWikiTestCase.php:1498
UserPasswordPolicy\maxOfPolicies
static maxOfPolicies(array $p1, array $p2)
Utility function to get a policy that is the most restrictive of $p1 and $p2.
Definition: UserPasswordPolicy.php:181
UserPasswordPolicyTest\testGetPoliciesForGroups
testGetPoliciesForGroups()
UserPasswordPolicy::getPoliciesForGroups.
Definition: UserPasswordPolicyTest.php:85
UserPasswordPolicyTest
Database.
Definition: UserPasswordPolicyTest.php:26
UserPasswordPolicyTest\getUserPasswordPolicy
getUserPasswordPolicy()
Definition: UserPasswordPolicyTest.php:55
UserPasswordPolicyTest\$checks
$checks
Definition: UserPasswordPolicyTest.php:47
$status
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist Do not use this to implement individual filters if they are compatible with the ChangesListFilter and ChangesListFilterGroup structure use sub classes of those in conjunction with the ChangesListSpecialPageStructuredFilters hook This hook can be used to implement filters that do not implement that or custom behavior that is not an individual filter e g Watchlist and Watchlist you will want to construct new ChangesListBooleanFilter or ChangesListStringOptionsFilter objects When constructing you specify which group they belong to You can reuse existing or create your you must register them with $special registerFilterGroup 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:1049
$user
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 account $user
Definition: hooks.txt:246
User\newFromName
static newFromName( $name, $validate='valid')
Static factory method for creation from username.
Definition: User.php:556
UserPasswordPolicy\getPoliciesForGroups
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.
Definition: UserPasswordPolicy.php:158
UserPasswordPolicyTest\provideCheckUserPassword
provideCheckUserPassword()
Definition: UserPasswordPolicyTest.php:123
php
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
UserPasswordPolicyTest\testMaxOfPolicies
testMaxOfPolicies( $p1, $p2, $max, $msg)
provideMaxOfPolicies UserPasswordPolicy::maxOfPolicies
Definition: UserPasswordPolicyTest.php:188
MediaWikiTestCase
Definition: MediaWikiTestCase.php:13
UserPasswordPolicyTest\$policies
$policies
Definition: UserPasswordPolicyTest.php:28
UserPasswordPolicy
Check if a user's password complies with any password policies that apply to that user,...
Definition: UserPasswordPolicy.php:28
UserPasswordPolicyTest\provideMaxOfPolicies
provideMaxOfPolicies()
Definition: UserPasswordPolicyTest.php:196
UserPasswordPolicyTest\testGetPoliciesForUser
testGetPoliciesForUser()
UserPasswordPolicy::getPoliciesForUser.
Definition: UserPasswordPolicyTest.php:62
as
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
$username
this hook is for auditing only or null if authentication failed before getting that far $username
Definition: hooks.txt:783
UserPasswordPolicyTest\testCheckUserPassword
testCheckUserPassword( $username, $groups, $password, $valid, $ok, $msg)
provideCheckUserPassword UserPasswordPolicy::checkUserPassword
Definition: UserPasswordPolicyTest.php:108