summaryrefslogtreecommitdiff
blob: 6ae2e4e4094a28d62cb1606e30005cf04b22fbe3 (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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?php

/**
 * @todo this should use the Database group
 */
class MergeUserTest extends MediaWikiTestCase {

	private $counter = 0;

	private function getNewTestUser() {
		$this->counter++;
		$name = __CLASS__ . (string)$this->counter;
		$user = User::newFromName( $name );
		if ( $user->getId() ) { // Already exists, try again.
			return $this->getNewTestUser();
		}
		$user->setName( $name );
		$user->addToDatabase();
		return $user;
	}

	/**
	 * Clear all instance caches
	 *
	 * @param User $u
	 */
	private function reallyClearInstanceCache( User &$u ) {
		$u = User::newFromName( $u->getName() );
	}

	public function testBasicMerge() {
		$user1 = $this->getNewTestUser();
		$user1->addToDatabase();
		$user1->setOption( 'foo', 'baz' );
		$user1->saveSettings();
		$user2 = $this->getNewTestUser();
		$user2->addToDatabase();

		$mu = new MergeUser( $user1, $user2, $this->getMock( 'UserMergeLogger' ) );
		$mu->merge( $this->getMock( 'User' ) );

		$this->reallyClearInstanceCache( $user1 );
		$this->reallyClearInstanceCache( $user2 );
		$this->assertEquals( null, $user1->getOption( 'foo' ) );
		$this->assertEquals( 'baz', $user2->getOption( 'foo' ) );
	}

	public function testMergeOfUserGroups() {
		$user1 = $this->getNewTestUser();
		$user1->addGroup( 'group1' );
		$user1->addGroup( 'group2' );
		$user2 = $this->getNewTestUser();
		$user2->addGroup( 'group2' );

		$mu = new MergeUser( $user1, $user2, $this->getMock( 'UserMergeLogger' ) );
		$mu->merge( $this->getMock( 'User' ) );

		$this->reallyClearInstanceCache( $user1 );
		$this->reallyClearInstanceCache( $user2 );

		$this->assertArrayEquals( [ 'group2' ], $user1->getGroups() );
		$this->assertArrayEquals( [ 'group1', 'group2' ], $user2->getGroups() );
	}

	public function testDeleteUser() {
		$user1 = $this->getNewTestUser();
		$user2 = $this->getNewTestUser();

		$this->reallyClearInstanceCache( $user1 );
		$this->assertGreaterThan( 0, $user1->getId() );

		$mu = new MergeUser( $user1, $user2, $this->getMock( 'UserMergeLogger' ) );
		$mu->delete( $this->getMock( 'User' ), 'wfMessage' );

		$this->reallyClearInstanceCache( $user1 );
		$this->assertEquals( 0, $user1->getId() );
	}

	/**
	 * @covers MergeUser::mergeEditcount
	 */
	public function testMergeEditcount() {
		$user1 = $this->getNewTestUser();
		$user2 = $this->getNewTestUser();
		$count = 0;
		$user1->incEditCount();
		while ( $count < 10 ) {
			$user1->incEditCount();
			$user2->incEditCount();
			$count++;
		}

		$mu = new MergeUser( $user1, $user2, $this->getMock( 'UserMergeLogger' ) );
		$mu->merge( $this->getMock( 'User' ) );

		$this->reallyClearInstanceCache( $user1 );
		$this->reallyClearInstanceCache( $user2 );
		$this->assertEquals( 0, $user1->getEditCount() );
		$this->assertEquals( 21, $user2->getEditCount() );
	}
}