Fix password policy handling in temporary password provider
authorGergő Tisza <tgr.huwiki@gmail.com>
Tue, 19 Feb 2019 04:19:13 +0000 (20:19 -0800)
committerGergő Tisza <tgr.huwiki@gmail.com>
Wed, 20 Feb 2019 20:25:49 +0000 (12:25 -0800)
Fix breakage caused by f15ecc60cd94. Also use correct check name.

Bug: T216196
Change-Id: Id2567adf8334742ef18a59a6c7e74b2b780ab43a

includes/auth/TemporaryPasswordAuthenticationRequest.php
tests/phpunit/includes/auth/TemporaryPasswordAuthenticationRequestTest.php

index bc7c779..1c87ea9 100644 (file)
@@ -69,11 +69,8 @@ class TemporaryPasswordAuthenticationRequest extends AuthenticationRequest {
                $minLength = $config->get( 'MinimalPasswordLength' );
                $policy = $config->get( 'PasswordPolicy' );
                foreach ( $policy['policies'] as $p ) {
-                       if ( isset( $p['MinimalPasswordLength'] ) ) {
-                               $minLength = max( $minLength, $p['MinimalPasswordLength'] );
-                       }
-                       if ( isset( $p['MinimalPasswordLengthToLogin'] ) ) {
-                               $minLength = max( $minLength, $p['MinimalPasswordLengthToLogin'] );
+                       foreach ( [ 'MinimalPasswordLength', 'MinimumPasswordLengthToLogin' ] as $check ) {
+                               $minLength = max( $minLength, $p[$check]['value'] ?? $p[$check] ?? 0 );
                        }
                }
 
index dc4ab6f..1ee4a03 100644 (file)
@@ -26,18 +26,32 @@ class TemporaryPasswordAuthenticationRequestTest extends AuthenticationRequestTe
                global $wgPasswordPolicy;
 
                $policy = $wgPasswordPolicy;
-               $policy['policies']['default'] += [
+               unset( $policy['policies'] );
+               $policy['policies']['default'] = [
                        'MinimalPasswordLength' => 1,
-                       'MinimalPasswordLengthToLogin' => 1,
+                       'MinimumPasswordLengthToLogin' => 1,
                ];
 
-               $this->setMwGlobals( 'wgPasswordPolicy', $policy );
+               $this->setMwGlobals( [
+                       'wgMinimalPasswordLength' => 10,
+                       'wgPasswordPolicy' => $policy,
+               ] );
 
                $ret1 = TemporaryPasswordAuthenticationRequest::newRandom();
                $ret2 = TemporaryPasswordAuthenticationRequest::newRandom();
-               $this->assertNotSame( '', $ret1->password );
-               $this->assertNotSame( '', $ret2->password );
+               $this->assertEquals( 10, strlen( $ret1->password ) );
+               $this->assertEquals( 10, strlen( $ret2->password ) );
                $this->assertNotSame( $ret1->password, $ret2->password );
+
+               $policy['policies']['default']['MinimalPasswordLength'] = 15;
+               $this->setMwGlobals( 'wgPasswordPolicy', $policy );
+               $ret = TemporaryPasswordAuthenticationRequest::newRandom();
+               $this->assertEquals( 15, strlen( $ret->password ) );
+
+               $policy['policies']['default']['MinimalPasswordLength'] = [ 'value' => 20 ];
+               $this->setMwGlobals( 'wgPasswordPolicy', $policy );
+               $ret = TemporaryPasswordAuthenticationRequest::newRandom();
+               $this->assertEquals( 20, strlen( $ret->password ) );
        }
 
        public function testNewInvalid() {