MediaWiki  master
PasswordFactoryTest.php
Go to the documentation of this file.
1 <?php
2 
7  public function testConstruct() {
8  $pf = new PasswordFactory();
9  $this->assertEquals( [ '' ], array_keys( $pf->getTypes() ) );
10  $this->assertEquals( '', $pf->getDefaultType() );
11 
12  $pf = new PasswordFactory( [
13  'foo' => [ 'class' => 'FooPassword' ],
14  'bar' => [ 'class' => 'BarPassword', 'baz' => 'boom' ],
15  ], 'foo' );
16  $this->assertEquals( [ '', 'foo', 'bar' ], array_keys( $pf->getTypes() ) );
17  $this->assertArraySubset( [ 'class' => 'BarPassword', 'baz' => 'boom' ], $pf->getTypes()['bar'] );
18  $this->assertEquals( 'foo', $pf->getDefaultType() );
19  }
20 
21  public function testRegister() {
22  $pf = new PasswordFactory;
23  $pf->register( 'foo', [ 'class' => InvalidPassword::class ] );
24  $this->assertArrayHasKey( 'foo', $pf->getTypes() );
25  }
26 
27  public function testSetDefaultType() {
28  $pf = new PasswordFactory;
29  $pf->register( '1', [ 'class' => InvalidPassword::class ] );
30  $pf->register( '2', [ 'class' => InvalidPassword::class ] );
31  $pf->setDefaultType( '1' );
32  $this->assertSame( '1', $pf->getDefaultType() );
33  $pf->setDefaultType( '2' );
34  $this->assertSame( '2', $pf->getDefaultType() );
35  }
36 
40  public function testSetDefaultTypeError() {
41  $pf = new PasswordFactory;
42  $pf->setDefaultType( 'bogus' );
43  }
44 
45  public function testInit() {
46  $config = new HashConfig( [
47  'PasswordConfig' => [
48  'foo' => [ 'class' => InvalidPassword::class ],
49  ],
50  'PasswordDefault' => 'foo'
51  ] );
52  $pf = new PasswordFactory;
53  $pf->init( $config );
54  $this->assertSame( 'foo', $pf->getDefaultType() );
55  $this->assertArrayHasKey( 'foo', $pf->getTypes() );
56  }
57 
58  public function testNewFromCiphertext() {
59  $pf = new PasswordFactory;
60  $pf->register( 'B', [ 'class' => MWSaltedPassword::class ] );
61  $pw = $pf->newFromCiphertext( ':B:salt:d529e941509eb9e9b9cfaeae1fe7ca23' );
62  $this->assertInstanceOf( MWSaltedPassword::class, $pw );
63  }
64 
65  public function provideNewFromCiphertextErrors() {
66  return [ [ 'blah' ], [ ':blah:' ] ];
67  }
68 
73  public function testNewFromCiphertextErrors( $hash ) {
74  $pf = new PasswordFactory;
75  $pf->register( 'B', [ 'class' => MWSaltedPassword::class ] );
76  $pf->newFromCiphertext( $hash );
77  }
78 
79  public function testNewFromType() {
80  $pf = new PasswordFactory;
81  $pf->register( 'B', [ 'class' => MWSaltedPassword::class ] );
82  $pw = $pf->newFromType( 'B' );
83  $this->assertInstanceOf( MWSaltedPassword::class, $pw );
84  }
85 
89  public function testNewFromTypeError() {
90  $pf = new PasswordFactory;
91  $pf->register( 'B', [ 'class' => MWSaltedPassword::class ] );
92  $pf->newFromType( 'bogus' );
93  }
94 
95  public function testNewFromPlaintext() {
96  $pf = new PasswordFactory;
97  $pf->register( 'A', [ 'class' => MWOldPassword::class ] );
98  $pf->register( 'B', [ 'class' => MWSaltedPassword::class ] );
99  $pf->setDefaultType( 'A' );
100 
101  $this->assertInstanceOf( InvalidPassword::class, $pf->newFromPlaintext( null ) );
102  $this->assertInstanceOf( MWOldPassword::class, $pf->newFromPlaintext( 'password' ) );
103  $this->assertInstanceOf( MWSaltedPassword::class,
104  $pf->newFromPlaintext( 'password', $pf->newFromType( 'B' ) ) );
105  }
106 
107  public function testNeedsUpdate() {
108  $pf = new PasswordFactory;
109  $pf->register( 'A', [ 'class' => MWOldPassword::class ] );
110  $pf->register( 'B', [ 'class' => MWSaltedPassword::class ] );
111  $pf->setDefaultType( 'A' );
112 
113  $this->assertFalse( $pf->needsUpdate( $pf->newFromType( 'A' ) ) );
114  $this->assertTrue( $pf->needsUpdate( $pf->newFromType( 'B' ) ) );
115  }
116 
118  $this->assertSame( 13, strlen( PasswordFactory::generateRandomPasswordString( 13 ) ) );
119  }
120 
121  public function testNewInvalidPassword() {
122  $this->assertInstanceOf( InvalidPassword::class, PasswordFactory::newInvalidPassword() );
123  }
124 }
static generateRandomPasswordString( $minLength=10)
Generate a random string suitable for a password.
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not null
Definition: hooks.txt:780
static newInvalidPassword()
Create an InvalidPassword.
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
testNewFromCiphertextErrors( $hash)
provideNewFromCiphertextErrors PasswordError
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
testSetDefaultTypeError()
Exception.
A Config instance which stores all settings as a member variable.
Definition: HashConfig.php:28
testNewFromTypeError()
PasswordError.