Remove hard deprecation of PasswordPolicyChecks::checkPopularPasswordBlacklist
[lhc/web/wiklou.git] / tests / phpunit / includes / MergeHistoryTest.php
1 <?php
2
3 /**
4 * @group Database
5 */
6 class MergeHistoryTest extends MediaWikiTestCase {
7
8 /**
9 * Make some pages to work with
10 */
11 public function addDBDataOnce() {
12 // Pages that won't actually be merged
13 $this->insertPage( 'Test' );
14 $this->insertPage( 'Test2' );
15
16 // Pages that will be merged
17 $this->insertPage( 'Merge1' );
18 $this->insertPage( 'Merge2' );
19 }
20
21 /**
22 * @dataProvider provideIsValidMerge
23 * @covers MergeHistory::isValidMerge
24 * @param string $source Source page
25 * @param string $dest Destination page
26 * @param string|bool $timestamp Timestamp up to which revisions are merged (or false for all)
27 * @param string|bool $error Expected error for test (or true for no error)
28 */
29 public function testIsValidMerge( $source, $dest, $timestamp, $error ) {
30 if ( $timestamp === true ) {
31 // Although this timestamp is after the latest timestamp of both pages,
32 // MergeHistory should select the latest source timestamp up to this which should
33 // still work for the merge.
34 $timestamp = time() + ( 24 * 3600 );
35 }
36 $mh = new MergeHistory(
37 Title::newFromText( $source ),
38 Title::newFromText( $dest ),
39 $timestamp
40 );
41 $status = $mh->isValidMerge();
42 if ( $error === true ) {
43 $this->assertTrue( $status->isGood() );
44 } else {
45 $this->assertTrue( $status->hasMessage( $error ) );
46 }
47 }
48
49 public static function provideIsValidMerge() {
50 return [
51 // for MergeHistory::isValidMerge
52 [ 'Test', 'Test2', false, true ],
53 // Timestamp of `true` is a placeholder for "in the future""
54 [ 'Test', 'Test2', true, true ],
55 [ 'Test', 'Test', false, 'mergehistory-fail-self-merge' ],
56 [ 'Nonexistant', 'Test2', false, 'mergehistory-fail-invalid-source' ],
57 [ 'Test', 'Nonexistant', false, 'mergehistory-fail-invalid-dest' ],
58 [
59 'Test',
60 'Test2',
61 'This is obviously an invalid timestamp',
62 'mergehistory-fail-bad-timestamp'
63 ],
64 ];
65 }
66
67 /**
68 * Test merge revision limit checking
69 * @covers MergeHistory::isValidMerge
70 */
71 public function testIsValidMergeRevisionLimit() {
72 $limit = MergeHistory::REVISION_LIMIT;
73
74 $mh = $this->getMockBuilder( MergeHistory::class )
75 ->setMethods( [ 'getRevisionCount' ] )
76 ->setConstructorArgs( [
77 Title::newFromText( 'Test' ),
78 Title::newFromText( 'Test2' ),
79 ] )
80 ->getMock();
81 $mh->expects( $this->once() )
82 ->method( 'getRevisionCount' )
83 ->will( $this->returnValue( $limit + 1 ) );
84
85 $status = $mh->isValidMerge();
86 $this->assertTrue( $status->hasMessage( 'mergehistory-fail-toobig' ) );
87 $errors = $status->getErrorsByType( 'error' );
88 $params = $errors[0]['params'];
89 $this->assertEquals( $params[0], Message::numParam( $limit ) );
90 }
91
92 /**
93 * Test user permission checking
94 * @covers MergeHistory::checkPermissions
95 */
96 public function testCheckPermissions() {
97 $mh = new MergeHistory(
98 Title::newFromText( 'Test' ),
99 Title::newFromText( 'Test2' )
100 );
101
102 // Sysop with mergehistory permission
103 $sysop = static::getTestSysop()->getUser();
104 $status = $mh->checkPermissions( $sysop, '' );
105 $this->assertTrue( $status->isOK() );
106
107 // Normal user
108 $notSysop = static::getTestUser()->getUser();
109 $status = $mh->checkPermissions( $notSysop, '' );
110 $this->assertTrue( $status->hasMessage( 'mergehistory-fail-permission' ) );
111 }
112
113 /**
114 * Test merged revision count
115 * @covers MergeHistory::getMergedRevisionCount
116 */
117 public function testGetMergedRevisionCount() {
118 $mh = new MergeHistory(
119 Title::newFromText( 'Merge1' ),
120 Title::newFromText( 'Merge2' )
121 );
122
123 $sysop = static::getTestSysop()->getUser();
124 $mh->merge( $sysop );
125 $this->assertEquals( $mh->getMergedRevisionCount(), 1 );
126 }
127 }