Merge "Revert "Log the reason why revision->getContent() returns null""
[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 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
23 /**
24 * @expectedException Exception
25 */
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
51 public function provideNewFromCiphertextErrors() {
52 return [ [ 'blah' ], [ ':blah:' ] ];
53 }
54
55 /**
56 * @dataProvider provideNewFromCiphertextErrors
57 * @expectedException PasswordError
58 */
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
72 /**
73 * @expectedException PasswordError
74 */
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
103 public function testGenerateRandomPasswordString() {
104 $this->assertSame( 13, strlen( PasswordFactory::generateRandomPasswordString( 13 ) ) );
105 }
106
107 public function testNewInvalidPassword() {
108 $this->assertInstanceOf( InvalidPassword::class, PasswordFactory::newInvalidPassword() );
109 }
110 }