summaryrefslogtreecommitdiff
blob: a84e50c331d2da7395e4f2f5e393797b519198d4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?php

/**
 * @covers SpoofUser
 * @group Database
 */
class SpoofUserTest extends MediaWikiTestCase {

	protected $tablesUsed = [ 'user', 'spoofuser' ];

	private static $usernames = [
		'UserFoo',
		'UserF00',
		'FooBaz',
		'FOOBAZ',
		'F00BAZ',
		'BazFoo',
		'BAZFOO',
		'BazF00',
		'ILIKECAPSLOCKS',
	];

	public function setUp() {
		parent::setUp();

		// Put some stuff in the database
		foreach ( self::$usernames as $user ) {
			$s = new SpoofUser( $user );
			$s->record();
			$user = User::newFromName( $user );
			$user->addToDatabase();
		}

		$s = new SpoofUser( 'NotInTheUserTable' );
		$s->record();
	}

	/**
	 * @covers SpoofUser::record
	 */
	public function testRecord() {
		$user = User::newFromName( 'SomeUsername' );
		$user->addToDatabase();
		$s = new SpoofUser( 'SomeUsername' );
		$this->assertTrue( $s->record() );
		// Check that it made it into the database
		$this->assertArrayEquals( [ 'SomeUsername' ], $s->getConflicts() );
	}

	/**
	 * @covers SpoofUser::getConflicts
	 * @dataProvider provideGetConflicts
	 */
	public function testGetConflicts( $user, $conflicts ) {
		$s = new SpoofUser( $user );
		$this->assertArrayEquals( $conflicts, $s->getConflicts() );
	}

	public static function provideGetConflicts() {
		return [
			[ 'UserFoo', [ 'UserF00', 'UserFoo' ] ],
			[ 'FooBaz', [ 'F00BAZ', 'FOOBAZ', 'FooBaz' ] ],
			[ 'ILIKECAPSLOCKS', [ 'ILIKECAPSLOCKS' ] ],
			[ 'NotInTheUserTable', [] ],
			[ 'UsErFoO', [ 'UserF00', 'UserFoo' ] ],
		];
	}

	/**
	 * @covers SpoofUser::update
	 */
	public function testUpdate() {
		$user = User::newFromName( 'MyNewUserName' );
		$user->addToDatabase();
		$s = new SpoofUser( 'MyNewUserName' );
		$s->update( 'BAZFOO' );
		$this->assertArrayEquals( [ 'MyNewUserName' ], $s->getConflicts() );
		$foobaz = new SpoofUser( 'BAZFOO' );
		$this->assertArrayEquals( [ 'BazF00', 'BazFoo' ], $foobaz->getConflicts() );
	}
}