Add PasswordFactory to MediaWikiServices
[lhc/web/wiklou.git] / tests / phpunit / includes / password / PasswordFactoryTest.php
1 <?php
2
3 /**
4 * @covers PasswordFactory
5 */
6 class PasswordFactoryTest extends MediaWikiTestCase {
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
37 /**
38 * @expectedException Exception
39 */
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
69 /**
70 * @dataProvider provideNewFromCiphertextErrors
71 * @expectedException PasswordError
72 */
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
86 /**
87 * @expectedException PasswordError
88 */
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
117 public function testGenerateRandomPasswordString() {
118 $this->assertSame( 13, strlen( PasswordFactory::generateRandomPasswordString( 13 ) ) );
119 }
120
121 public function testNewInvalidPassword() {
122 $this->assertInstanceOf( InvalidPassword::class, PasswordFactory::newInvalidPassword() );
123 }
124 }