test: Clean up data providers that should be static
[lhc/web/wiklou.git] / tests / phpunit / includes / password / PasswordTestCase.php
1 <?php
2 /**
3 * Testing framework for the password hashes
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 */
22
23 /**
24 * @since 1.24
25 */
26 abstract class PasswordTestCase extends MediaWikiTestCase {
27 /**
28 * @var PasswordFactory
29 */
30 protected $passwordFactory;
31
32 protected function setUp() {
33 parent::setUp();
34
35 $this->passwordFactory = new PasswordFactory();
36 foreach ( $this->getTypeConfigs() as $type => $config ) {
37 $this->passwordFactory->register( $type, $config );
38 }
39 }
40
41 /**
42 * Return an array of configs to be used for this class's password type.
43 *
44 * @return array[]
45 */
46 abstract protected function getTypeConfigs();
47
48 /**
49 * An array of tests in the form of (bool, string, string), where the first
50 * element is whether the second parameter (a password hash) and the third
51 * parameter (a password) should match.
52 *
53 * @return array
54 */
55 abstract public static function providePasswordTests();
56
57 /**
58 * @dataProvider providePasswordTests
59 */
60 public function testHashing( $shouldMatch, $hash, $password ) {
61 $hash = $this->passwordFactory->newFromCiphertext( $hash );
62 $password = $this->passwordFactory->newFromPlaintext( $password, $hash );
63 $this->assertSame( $shouldMatch, $hash->equals( $password ) );
64 }
65
66 /**
67 * @dataProvider providePasswordTests
68 */
69 public function testStringSerialization( $shouldMatch, $hash, $password ) {
70 $hashObj = $this->passwordFactory->newFromCiphertext( $hash );
71 $serialized = $hashObj->toString();
72 $unserialized = $this->passwordFactory->newFromCiphertext( $serialized );
73 $this->assertTrue( $hashObj->equals( $unserialized ) );
74 }
75
76 /**
77 * @dataProvider providePasswordTests
78 * @covers InvalidPassword::equals
79 * @covers InvalidPassword::toString
80 */
81 public function testInvalidUnequalNormal( $shouldMatch, $hash, $password ) {
82 $invalid = $this->passwordFactory->newFromCiphertext( null );
83 $normal = $this->passwordFactory->newFromCiphertext( $hash );
84
85 $this->assertFalse( $invalid->equals( $normal ) );
86 $this->assertFalse( $normal->equals( $invalid ) );
87 }
88 }