MediaWiki  1.30.0
PasswordFactoryTest.php
Go to the documentation of this file.
1 <?php
2 
7  public function testRegister() {
8  $pf = new PasswordFactory;
9  $pf->register( 'foo', [ 'class' => 'InvalidPassword' ] );
10  $this->assertArrayHasKey( 'foo', $pf->getTypes() );
11  }
12 
13  public function testSetDefaultType() {
14  $pf = new PasswordFactory;
15  $pf->register( '1', [ 'class' => 'InvalidPassword' ] );
16  $pf->register( '2', [ 'class' => 'InvalidPassword' ] );
17  $pf->setDefaultType( '1' );
18  $this->assertSame( '1', $pf->getDefaultType() );
19  $pf->setDefaultType( '2' );
20  $this->assertSame( '2', $pf->getDefaultType() );
21  }
22 
26  public function testSetDefaultTypeError() {
27  $pf = new PasswordFactory;
28  $pf->setDefaultType( 'bogus' );
29  }
30 
31  public function testInit() {
32  $config = new HashConfig( [
33  'PasswordConfig' => [
34  'foo' => [ 'class' => 'InvalidPassword' ],
35  ],
36  'PasswordDefault' => 'foo'
37  ] );
38  $pf = new PasswordFactory;
39  $pf->init( $config );
40  $this->assertSame( 'foo', $pf->getDefaultType() );
41  $this->assertArrayHasKey( 'foo', $pf->getTypes() );
42  }
43 
44  public function testNewFromCiphertext() {
45  $pf = new PasswordFactory;
46  $pf->register( 'B', [ 'class' => 'MWSaltedPassword' ] );
47  $pw = $pf->newFromCiphertext( ':B:salt:d529e941509eb9e9b9cfaeae1fe7ca23' );
48  $this->assertInstanceOf( MWSaltedPassword::class, $pw );
49  }
50 
51  public function provideNewFromCiphertextErrors() {
52  return [ [ 'blah' ], [ ':blah:' ] ];
53  }
54 
59  public function testNewFromCiphertextErrors( $hash ) {
60  $pf = new PasswordFactory;
61  $pf->register( 'B', [ 'class' => 'MWSaltedPassword' ] );
62  $pf->newFromCiphertext( $hash );
63  }
64 
65  public function testNewFromType() {
66  $pf = new PasswordFactory;
67  $pf->register( 'B', [ 'class' => 'MWSaltedPassword' ] );
68  $pw = $pf->newFromType( 'B' );
69  $this->assertInstanceOf( MWSaltedPassword::class, $pw );
70  }
71 
75  public function testNewFromTypeError() {
76  $pf = new PasswordFactory;
77  $pf->register( 'B', [ 'class' => 'MWSaltedPassword' ] );
78  $pf->newFromType( 'bogus' );
79  }
80 
81  public function testNewFromPlaintext() {
82  $pf = new PasswordFactory;
83  $pf->register( 'A', [ 'class' => 'MWOldPassword' ] );
84  $pf->register( 'B', [ 'class' => 'MWSaltedPassword' ] );
85  $pf->setDefaultType( 'A' );
86 
87  $this->assertInstanceOf( 'InvalidPassword', $pf->newFromPlaintext( null ) );
88  $this->assertInstanceOf( 'MWOldPassword', $pf->newFromPlaintext( 'password' ) );
89  $this->assertInstanceOf( 'MWSaltedPassword',
90  $pf->newFromPlaintext( 'password', $pf->newFromType( 'B' ) ) );
91  }
92 
93  public function testNeedsUpdate() {
94  $pf = new PasswordFactory;
95  $pf->register( 'A', [ 'class' => 'MWOldPassword' ] );
96  $pf->register( 'B', [ 'class' => 'MWSaltedPassword' ] );
97  $pf->setDefaultType( 'A' );
98 
99  $this->assertFalse( $pf->needsUpdate( $pf->newFromType( 'A' ) ) );
100  $this->assertTrue( $pf->needsUpdate( $pf->newFromType( 'B' ) ) );
101  }
102 
104  $this->assertSame( 13, strlen( PasswordFactory::generateRandomPasswordString( 13 ) ) );
105  }
106 
107  public function testNewInvalidPassword() {
108  $this->assertInstanceOf( 'InvalidPassword', PasswordFactory::newInvalidPassword() );
109  }
110 }
HashConfig
A Config instance which stores all settings as a member variable.
Definition: HashConfig.php:28
PasswordFactory\setDefaultType
setDefaultType( $type)
Set the default password type.
Definition: PasswordFactory.php:64
PasswordFactory\generateRandomPasswordString
static generateRandomPasswordString( $minLength=10)
Generate a random string suitable for a password.
Definition: PasswordFactory.php:198
PasswordFactoryTest\testNewInvalidPassword
testNewInvalidPassword()
Definition: PasswordFactoryTest.php:107
PasswordFactoryTest\provideNewFromCiphertextErrors
provideNewFromCiphertextErrors()
Definition: PasswordFactoryTest.php:51
PasswordFactoryTest\testNewFromTypeError
testNewFromTypeError()
PasswordError.
Definition: PasswordFactoryTest.php:75
PasswordFactoryTest\testInit
testInit()
Definition: PasswordFactoryTest.php:31
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
PasswordFactoryTest\testNewFromPlaintext
testNewFromPlaintext()
Definition: PasswordFactoryTest.php:81
PasswordFactoryTest\testRegister
testRegister()
Definition: PasswordFactoryTest.php:7
PasswordFactoryTest\testNeedsUpdate
testNeedsUpdate()
Definition: PasswordFactoryTest.php:93
PasswordFactoryTest\testSetDefaultTypeError
testSetDefaultTypeError()
Exception.
Definition: PasswordFactoryTest.php:26
PasswordFactoryTest\testNewFromCiphertextErrors
testNewFromCiphertextErrors( $hash)
provideNewFromCiphertextErrors PasswordError
Definition: PasswordFactoryTest.php:59
MediaWikiTestCase
Definition: MediaWikiTestCase.php:15
PasswordFactoryTest\testGenerateRandomPasswordString
testGenerateRandomPasswordString()
Definition: PasswordFactoryTest.php:103
PasswordFactoryTest\testSetDefaultType
testSetDefaultType()
Definition: PasswordFactoryTest.php:13
PasswordFactory\register
register( $type, array $config)
Register a new type of password hash.
Definition: PasswordFactory.php:53
PasswordFactoryTest\testNewFromCiphertext
testNewFromCiphertext()
Definition: PasswordFactoryTest.php:44
PasswordFactoryTest\testNewFromType
testNewFromType()
Definition: PasswordFactoryTest.php:65
PasswordFactory\newInvalidPassword
static newInvalidPassword()
Create an InvalidPassword.
Definition: PasswordFactory.php:214
PasswordFactory\init
init(Config $config)
Initialize the internal static variables using the global variables.
Definition: PasswordFactory.php:85
PasswordFactory
Factory class for creating and checking Password objects.
Definition: PasswordFactory.php:28
class
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
Definition: maintenance.txt:52
PasswordFactoryTest
PasswordFactory.
Definition: PasswordFactoryTest.php:6