MediaWiki REL1_31
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::class ] );
10 $this->assertArrayHasKey( 'foo', $pf->getTypes() );
11 }
12
13 public function testSetDefaultType() {
14 $pf = new PasswordFactory;
15 $pf->register( '1', [ 'class' => InvalidPassword::class ] );
16 $pf->register( '2', [ 'class' => InvalidPassword::class ] );
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::class ],
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::class ] );
47 $pw = $pf->newFromCiphertext( ':B:salt:d529e941509eb9e9b9cfaeae1fe7ca23' );
48 $this->assertInstanceOf( MWSaltedPassword::class, $pw );
49 }
50
52 return [ [ 'blah' ], [ ':blah:' ] ];
53 }
54
59 public function testNewFromCiphertextErrors( $hash ) {
60 $pf = new PasswordFactory;
61 $pf->register( 'B', [ 'class' => MWSaltedPassword::class ] );
62 $pf->newFromCiphertext( $hash );
63 }
64
65 public function testNewFromType() {
66 $pf = new PasswordFactory;
67 $pf->register( 'B', [ 'class' => MWSaltedPassword::class ] );
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::class ] );
78 $pf->newFromType( 'bogus' );
79 }
80
81 public function testNewFromPlaintext() {
82 $pf = new PasswordFactory;
83 $pf->register( 'A', [ 'class' => MWOldPassword::class ] );
84 $pf->register( 'B', [ 'class' => MWSaltedPassword::class ] );
85 $pf->setDefaultType( 'A' );
86
87 $this->assertInstanceOf( InvalidPassword::class, $pf->newFromPlaintext( null ) );
88 $this->assertInstanceOf( MWOldPassword::class, $pf->newFromPlaintext( 'password' ) );
89 $this->assertInstanceOf( MWSaltedPassword::class,
90 $pf->newFromPlaintext( 'password', $pf->newFromType( 'B' ) ) );
91 }
92
93 public function testNeedsUpdate() {
94 $pf = new PasswordFactory;
95 $pf->register( 'A', [ 'class' => MWOldPassword::class ] );
96 $pf->register( 'B', [ 'class' => MWSaltedPassword::class ] );
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::class, PasswordFactory::newInvalidPassword() );
109 }
110}
A Config instance which stores all settings as a member variable.
testNewFromCiphertextErrors( $hash)
provideNewFromCiphertextErrors PasswordError
testNewFromTypeError()
PasswordError.
testSetDefaultTypeError()
Exception.
Factory class for creating and checking Password objects.
register( $type, array $config)
Register a new type of password hash.
setDefaultType( $type)
Set the default password type.
init(Config $config)
Initialize the internal static variables using the global variables.
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:37