Debian Codegraph @

tests/phpunit/unit/includes/password/PasswordFactoryTest.php

raw symbols SCIP
1 <?php
2
3 use MediaWiki\Config\HashConfig;
4 use MediaWiki\MainConfigNames;
5 use MediaWiki\Password\InvalidPassword;
6 use MediaWiki\Password\MWOldPassword;
7 use MediaWiki\Password\MWSaltedPassword;
8 use MediaWiki\Password\PasswordError;
9 use MediaWiki\Password\PasswordFactory;
10
11 /**
12 * @covers \MediaWiki\Password\PasswordFactory
13 */
14 class PasswordFactoryTest extends MediaWikiUnitTestCase {
15 public function testConstruct() {
16 $pf = new PasswordFactory();
17 $this->assertEquals( [ '' ], array_keys( $pf->getTypes() ) );
18 $this->assertSame( '', $pf->getDefaultType() );
19
20 $pf = new PasswordFactory( [
21 'foo' => [ 'class' => 'FooPassword' ],
22 'bar' => [ 'class' => 'BarPassword', 'baz' => 'boom' ],
23 ], 'foo' );
24 $this->assertEquals( [ '', 'foo', 'bar' ], array_keys( $pf->getTypes() ) );
25 $bar = $pf->getTypes()['bar'];
26 $expected = [ 'class' => 'BarPassword', 'baz' => 'boom' ];
27 $this->assertArrayEquals( $expected, array_intersect( $bar, $expected ) );
28 $this->assertEquals( 'foo', $pf->getDefaultType() );
29 }
30
31 public function testRegister() {
32 $pf = new PasswordFactory;
33 $pf->register( 'foo', [ 'class' => InvalidPassword::class ] );
34 $this->assertArrayHasKey( 'foo', $pf->getTypes() );
35 }
36
37 public function testSetDefaultType() {
38 $pf = new PasswordFactory;
39 $pf->register( '1', [ 'class' => InvalidPassword::class ] );
40 $pf->register( '2', [ 'class' => InvalidPassword::class ] );
41 $pf->setDefaultType( '1' );
42 $this->assertSame( '1', $pf->getDefaultType() );
43 $pf->setDefaultType( '2' );
44 $this->assertSame( '2', $pf->getDefaultType() );
45 }
46
47 public function testSetDefaultTypeError() {
48 $pf = new PasswordFactory;
49 $this->expectException( Exception::class );
50 $pf->setDefaultType( 'bogus' );
51 }
52
53 public function testInit() {
54 $this->expectDeprecationAndContinue( '/deprecated in MediaWiki 1.32/' );
55 $config = new HashConfig( [
56 MainConfigNames::PasswordConfig => [
57 'foo' => [ 'class' => InvalidPassword::class ],
58 ],
59 MainConfigNames::PasswordDefault => 'foo'
60 ] );
61 $pf = new PasswordFactory;
62 $pf->init( $config );
63 $this->assertSame( 'foo', $pf->getDefaultType() );
64 $this->assertArrayHasKey( 'foo', $pf->getTypes() );
65 }
66
67 public function testNewFromCiphertext() {
68 $pf = new PasswordFactory;
69 $pf->register( 'B', [ 'class' => MWSaltedPassword::class ] );
70 $pw = $pf->newFromCiphertext( ':B:salt:d529e941509eb9e9b9cfaeae1fe7ca23' );
71 $this->assertInstanceOf( MWSaltedPassword::class, $pw );
72 }
73
74 public static function provideNewFromCiphertextErrors() {
75 return [ [ 'blah' ], [ ':blah:' ] ];
76 }
77
78 /**
79 * @dataProvider provideNewFromCiphertextErrors
80 */
81 public function testNewFromCiphertextErrors( $hash ) {
82 $pf = new PasswordFactory;
83 $pf->register( 'B', [ 'class' => MWSaltedPassword::class ] );
84 $this->expectException( PasswordError::class );
85 $pf->newFromCiphertext( $hash );
86 }
87
88 public function testNewFromType() {
89 $pf = new PasswordFactory;
90 $pf->register( 'B', [ 'class' => MWSaltedPassword::class ] );
91 $pw = $pf->newFromType( 'B' );
92 $this->assertInstanceOf( MWSaltedPassword::class, $pw );
93 }
94
95 public function testNewFromTypeError() {
96 $pf = new PasswordFactory;
97 $pf->register( 'B', [ 'class' => MWSaltedPassword::class ] );
98 $this->expectException( PasswordError::class );
99 $pf->newFromType( 'bogus' );
100 }
101
102 public function testNewFromPlaintext() {
103 $pf = new PasswordFactory;
104 $pf->register( 'A', [ 'class' => MWOldPassword::class ] );
105 $pf->register( 'B', [ 'class' => MWSaltedPassword::class ] );
106 $pf->setDefaultType( 'A' );
107
108 $this->assertInstanceOf( InvalidPassword::class, $pf->newFromPlaintext( null ) );
109 $this->assertInstanceOf( MWOldPassword::class, $pf->newFromPlaintext( 'password' ) );
110 $this->assertInstanceOf( MWSaltedPassword::class,
111 $pf->newFromPlaintext( 'password', $pf->newFromType( 'B' ) ) );
112 }
113
114 public function testNeedsUpdate() {
115 $pf = new PasswordFactory;
116 $pf->register( 'A', [ 'class' => MWOldPassword::class ] );
117 $pf->register( 'B', [ 'class' => MWSaltedPassword::class ] );
118 $pf->setDefaultType( 'A' );
119
120 $this->assertFalse( $pf->needsUpdate( $pf->newFromType( 'A' ) ) );
121 $this->assertTrue( $pf->needsUpdate( $pf->newFromType( 'B' ) ) );
122 }
123
124 public function testGenerateRandomPasswordString() {
125 $this->assertSame( 13, strlen( PasswordFactory::generateRandomPasswordString( 13 ) ) );
126 }
127
128 public function testNewInvalidPassword() {
129 $this->assertInstanceOf( InvalidPassword::class, PasswordFactory::newInvalidPassword() );
130 }
131 }