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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
|
<?php
use MediaWiki\MediaWikiServices;
/**
* @group Database
*/
class MergeHistoryTest extends MediaWikiTestCase {
/**
* Make some pages to work with
*/
public function addDBDataOnce() {
// Pages that won't actually be merged
$this->insertPage( 'Test' );
$this->insertPage( 'Test2' );
// Pages that will be merged
$this->insertPage( 'Merge1' );
$this->insertPage( 'Merge2' );
}
/**
* @dataProvider provideIsValidMerge
* @covers MergeHistory::isValidMerge
* @param string $source Source page
* @param string $dest Destination page
* @param string|bool $timestamp Timestamp up to which revisions are merged (or false for all)
* @param string|bool $error Expected error for test (or true for no error)
*/
public function testIsValidMerge( $source, $dest, $timestamp, $error ) {
if ( $timestamp === true ) {
// Although this timestamp is after the latest timestamp of both pages,
// MergeHistory should select the latest source timestamp up to this which should
// still work for the merge.
$timestamp = time() + ( 24 * 3600 );
}
$factory = MediaWikiServices::getInstance()->getMergeHistoryFactory();
$mh = $factory->newMergeHistory(
Title::newFromText( $source ),
Title::newFromText( $dest ),
$timestamp
);
$status = $mh->isValidMerge();
if ( $error === true ) {
$this->assertTrue( $status->isGood() );
} else {
$this->assertTrue( $status->hasMessage( $error ) );
}
}
public static function provideIsValidMerge() {
return [
// for MergeHistory::isValidMerge
[ 'Test', 'Test2', false, true ],
// Timestamp of `true` is a placeholder for "in the future""
[ 'Test', 'Test2', true, true ],
[ 'Test', 'Test', false, 'mergehistory-fail-self-merge' ],
[ 'Nonexistant', 'Test2', false, 'mergehistory-fail-invalid-source' ],
[ 'Test', 'Nonexistant', false, 'mergehistory-fail-invalid-dest' ],
[
'Test',
'Test2',
'This is obviously an invalid timestamp',
'mergehistory-fail-bad-timestamp'
],
];
}
/**
* Test merge revision limit checking
* @covers MergeHistory::isValidMerge
*/
public function testIsValidMergeRevisionLimit() {
$this->hideDeprecated( 'MergeHistory being constructed directly' );
$limit = MergeHistory::REVISION_LIMIT;
$mh = $this->getMockBuilder( MergeHistory::class )
->setMethods( [ 'getRevisionCount' ] )
->setConstructorArgs( [
Title::newFromText( 'Test' ),
Title::newFromText( 'Test2' ),
] )
->getMock();
$mh->expects( $this->once() )
->method( 'getRevisionCount' )
->will( $this->returnValue( $limit + 1 ) );
$status = $mh->isValidMerge();
$this->assertTrue( $status->hasMessage( 'mergehistory-fail-toobig' ) );
$errors = $status->getErrorsByType( 'error' );
$params = $errors[0]['params'];
$this->assertEquals( $params[0], Message::numParam( $limit ) );
}
/**
* Test user permission checking
* @covers MergeHistory::checkPermissions
*/
public function testCheckPermissions() {
$factory = MediaWikiServices::getInstance()->getMergeHistoryFactory();
$mh = $factory->newMergeHistory(
Title::newFromText( 'Test' ),
Title::newFromText( 'Test2' )
);
// Sysop with mergehistory permission
$sysop = static::getTestSysop()->getUser();
$status = $mh->checkPermissions( $sysop, '' );
$this->assertTrue( $status->isOK() );
// Normal user
$notSysop = static::getTestUser()->getUser();
$status = $mh->checkPermissions( $notSysop, '' );
$this->assertTrue( $status->hasMessage( 'mergehistory-fail-permission' ) );
}
/**
* Test merged revision count
* @covers MergeHistory::getMergedRevisionCount
*/
public function testGetMergedRevisionCount() {
$factory = MediaWikiServices::getInstance()->getMergeHistoryFactory();
$mh = $factory->newMergeHistory(
Title::newFromText( 'Merge1' ),
Title::newFromText( 'Merge2' )
);
$sysop = static::getTestSysop()->getUser();
$mh->merge( $sysop );
$this->assertEquals( $mh->getMergedRevisionCount(), 1 );
}
/**
* Test the old and new constructors work (though the old is deprecated)
* @covers MergeHistory::__construct
*/
public function testConstructor() {
$services = MediaWikiServices::getInstance();
$source = Title::newFromText( 'Merge1' );
$destination = Title::newFromText( 'Merge2' );
$timestamp = false;
// Old method: No dependencies injected
$this->hideDeprecated( 'MergeHistory being constructed directly' );
$mergeHistory = new MergeHistory( $source, $destination, $timestamp );
$this->assertInstanceOf(
MergeHistory::class,
$mergeHistory
);
// New method: all dependencies injected
$mergeHistory = new MergeHistory(
$source,
$destination,
$timestamp,
$services->getDBLoadBalancer(),
$services->getPermissionManager(),
$services->getContentHandlerFactory(),
$services->getRevisionStore(),
$services->getWatchedItemStore()
);
$this->assertInstanceOf(
MergeHistory::class,
$mergeHistory
);
}
}
|