4 * @covers PasswordFactory
6 class PasswordFactoryTest
extends MediaWikiTestCase
{
7 public function testRegister() {
8 $pf = new PasswordFactory
;
9 $pf->register( 'foo', [ 'class' => 'InvalidPassword' ] );
10 $this->assertArrayHasKey( 'foo', $pf->getTypes() );
13 public function testSetDefaultType() {
14 $pf = new PasswordFactory
;
15 $pf->register( '1', [ 'class' => 'InvalidPassword' ] );
16 $pf->register( '2', [ 'class' => 'InvalidPassword' ] );
17 $pf->setDefaultType( '1' );
18 $this->assertSame( '1', $pf->getDefaultType() );
19 $pf->setDefaultType( '2' );
20 $this->assertSame( '2', $pf->getDefaultType() );
24 * @expectedException Exception
26 public function testSetDefaultTypeError() {
27 $pf = new PasswordFactory
;
28 $pf->setDefaultType( 'bogus' );
31 public function testInit() {
32 $config = new HashConfig( [
34 'foo' => [ 'class' => 'InvalidPassword' ],
36 'PasswordDefault' => 'foo'
38 $pf = new PasswordFactory
;
40 $this->assertSame( 'foo', $pf->getDefaultType() );
41 $this->assertArrayHasKey( 'foo', $pf->getTypes() );
44 public function testNewFromCiphertext() {
45 $pf = new PasswordFactory
;
46 $pf->register( 'B', [ 'class' => 'MWSaltedPassword' ] );
47 $pw = $pf->newFromCiphertext( ':B:salt:d529e941509eb9e9b9cfaeae1fe7ca23' );
48 $this->assertInstanceOf( MWSaltedPassword
::class, $pw );
51 public function provideNewFromCiphertextErrors() {
52 return [ [ 'blah' ], [ ':blah:' ] ];
56 * @dataProvider provideNewFromCiphertextErrors
57 * @expectedException PasswordError
59 public function testNewFromCiphertextErrors( $hash ) {
60 $pf = new PasswordFactory
;
61 $pf->register( 'B', [ 'class' => 'MWSaltedPassword' ] );
62 $pf->newFromCiphertext( $hash );
65 public function testNewFromType() {
66 $pf = new PasswordFactory
;
67 $pf->register( 'B', [ 'class' => 'MWSaltedPassword' ] );
68 $pw = $pf->newFromType( 'B' );
69 $this->assertInstanceOf( MWSaltedPassword
::class, $pw );
73 * @expectedException PasswordError
75 public function testNewFromTypeError() {
76 $pf = new PasswordFactory
;
77 $pf->register( 'B', [ 'class' => 'MWSaltedPassword' ] );
78 $pf->newFromType( 'bogus' );
81 public function testNewFromPlaintext() {
82 $pf = new PasswordFactory
;
83 $pf->register( 'A', [ 'class' => 'MWOldPassword' ] );
84 $pf->register( 'B', [ 'class' => 'MWSaltedPassword' ] );
85 $pf->setDefaultType( 'A' );
87 $this->assertInstanceOf( 'InvalidPassword', $pf->newFromPlaintext( null ) );
88 $this->assertInstanceOf( 'MWOldPassword', $pf->newFromPlaintext( 'password' ) );
89 $this->assertInstanceOf( 'MWSaltedPassword',
90 $pf->newFromPlaintext( 'password', $pf->newFromType( 'B' ) ) );
93 public function testNeedsUpdate() {
94 $pf = new PasswordFactory
;
95 $pf->register( 'A', [ 'class' => 'MWOldPassword' ] );
96 $pf->register( 'B', [ 'class' => 'MWSaltedPassword' ] );
97 $pf->setDefaultType( 'A' );
99 $this->assertFalse( $pf->needsUpdate( $pf->newFromType( 'A' ) ) );
100 $this->assertTrue( $pf->needsUpdate( $pf->newFromType( 'B' ) ) );
103 public function testGenerateRandomPasswordString() {
104 $this->assertSame( 13, strlen( PasswordFactory
::generateRandomPasswordString( 13 ) ) );
107 public function testNewInvalidPassword() {
108 $this->assertInstanceOf( 'InvalidPassword', PasswordFactory
::newInvalidPassword() );