Debian Codegraph @

tests/phpunit/includes/page/MergeHistoryTest.php

raw symbols SCIP
1 <?php
2
3 use MediaWiki\CommentStore\CommentStoreComment;
4 use MediaWiki\MainConfigNames;
5 use MediaWiki\Message\Message;
6 use MediaWiki\Page\MergeHistory;
7 use MediaWiki\Revision\MutableRevisionRecord;
8 use MediaWiki\Tests\Unit\Permissions\MockAuthorityTrait;
9 use MediaWiki\Title\Title;
10 use MediaWiki\Utils\MWTimestamp;
11
12 /**
13 * @group Database
14 */
15 class MergeHistoryTest extends MediaWikiIntegrationTestCase {
16 use MockAuthorityTrait;
17
18 /**
19 * Make some pages to work with
20 */
21 public function addDBDataOnce() {
22 // Pages that won't actually be merged
23 $this->insertPage( 'Test' );
24 $this->insertPage( 'Test2' );
25
26 // Pages that will be merged
27 $this->insertPage( 'Merge1' );
28 $this->insertPage( 'Merge2' );
29
30 // Exclusive for testSourceUpdateForNoRedirectSupport()
31 $this->insertPage( 'Merge3' );
32 $this->insertPage( 'Merge4' );
33
34 // Exclusive for testSourceUpdateWithRedirectSupport()
35 $this->insertPage( 'Merge5' );
36 $this->insertPage( 'Merge6' );
37 }
38
39 /**
40 * @dataProvider provideIsValidMerge
41 * @covers \MediaWiki\Page\MergeHistory::isValidMerge
42 * @param string $source Source page
43 * @param string $dest Destination page
44 * @param string|bool $timestamp Timestamp up to which revisions are merged (or false for all)
45 * @param string|bool $error Expected error for test (or true for no error)
46 */
47 public function testIsValidMerge( $source, $dest, $timestamp, $error ) {
48 if ( $timestamp === true ) {
49 // Although this timestamp is after the latest timestamp of both pages,
50 // MergeHistory should select the latest source timestamp up to this which should
51 // still work for the merge.
52 $timestamp = time() + ( 24 * 3600 );
53 }
54 $factory = $this->getServiceContainer()->getMergeHistoryFactory();
55 $mh = $factory->newMergeHistory(
56 Title::newFromText( $source ),
57 Title::newFromText( $dest ),
58 $timestamp
59 );
60 $status = $mh->isValidMerge();
61 if ( $error === true ) {
62 $this->assertStatusGood( $status );
63 } else {
64 $this->assertStatusError( $error, $status );
65 }
66 }
67
68 public static function provideIsValidMerge() {
69 return [
70 // for MergeHistory::isValidMerge
71 [ 'Test', 'Test2', false, true ],
72 // Timestamp of `true` is a placeholder for "in the future""
73 [ 'Test', 'Test2', true, true ],
74 [ 'Test', 'Test', false, 'mergehistory-fail-self-merge' ],
75 [ 'Nonexistant', 'Test2', false, 'mergehistory-fail-invalid-source' ],
76 [ 'Test', 'Nonexistant', false, 'mergehistory-fail-invalid-dest' ],
77 [
78 'Test',
79 'Test2',
80 'This is obviously an invalid timestamp',
81 'mergehistory-fail-bad-timestamp'
82 ],
83 ];
84 }
85
86 /**
87 * Test merge revision limit checking
88 * @covers \MediaWiki\Page\MergeHistory::isValidMerge
89 */
90 public function testIsValidMergeRevisionLimit() {
91 $limit = MergeHistory::REVISION_LIMIT;
92 $mh = $this->getMockBuilder( MergeHistory::class )
93 ->onlyMethods( [ 'getRevisionCount' ] )
94 ->setConstructorArgs( [
95 Title::makeTitle( NS_MAIN, 'Test' ),
96 Title::makeTitle( NS_MAIN, 'Test2' ),
97 null,
98 $this->getServiceContainer()->getConnectionProvider(),
99 $this->getServiceContainer()->getContentHandlerFactory(),
100 $this->getServiceContainer()->getRevisionStore(),
101 $this->getServiceContainer()->getWatchedItemStore(),
102 $this->getServiceContainer()->getSpamChecker(),
103 $this->getServiceContainer()->getHookContainer(),
104 $this->getServiceContainer()->getWikiPageFactory(),
105 $this->getServiceContainer()->getTitleFormatter(),
106 $this->getServiceContainer()->getTitleFactory(),
107 $this->getServiceContainer()->getLinkTargetLookup(),
108 $this->getServiceContainer()->getDeletePageFactory(),
109 ] )
110 ->getMock();
111 $mh->expects( $this->once() )
112 ->method( 'getRevisionCount' )
113 ->willReturn( $limit + 1 );
114
115 $status = $mh->isValidMerge();
116
117 $this->assertStatusNotOK( $status );
118 $this->assertStatusMessagesExactly(
119 StatusValue::newFatal( 'mergehistory-fail-toobig', Message::numParam( $limit ) ),
120 $status
121 );
122 }
123
124 /**
125 * Test user permission checking
126 * @covers \MediaWiki\Page\MergeHistory::authorizeMerge
127 * @covers \MediaWiki\Page\MergeHistory::probablyCanMerge
128 */
129 public function testCheckPermissions() {
130 $factory = $this->getServiceContainer()->getMergeHistoryFactory();
131 $mh = $factory->newMergeHistory(
132 Title::makeTitle( NS_MAIN, 'Test' ),
133 Title::makeTitle( NS_MAIN, 'Test2' )
134 );
135
136 foreach ( [ 'authorizeMerge', 'probablyCanMerge' ] as $method ) {
137 // Sysop with mergehistory permission
138 $status = $mh->$method(
139 $this->mockRegisteredUltimateAuthority(),
140 ''
141 );
142 $this->assertStatusOK( $status );
143
144 $status = $mh->$method(
145 $this->mockRegisteredAuthorityWithoutPermissions( [ 'mergehistory' ] ),
146 ''
147 );
148 $this->assertStatusError( 'mergehistory-fail-permission', $status );
149 }
150 }
151
152 /**
153 * Test merged revision count
154 * @covers \MediaWiki\Page\MergeHistory::getMergedRevisionCount
155 */
156 public function testGetMergedRevisionCount() {
157 $factory = $this->getServiceContainer()->getMergeHistoryFactory();
158 $mh = $factory->newMergeHistory(
159 Title::makeTitle( NS_MAIN, 'Merge1' ),
160 Title::makeTitle( NS_MAIN, 'Merge2' )
161 );
162
163 $sysop = static::getTestSysop()->getUser();
164 $mh->merge( $sysop );
165 $this->assertSame( 1, $mh->getMergedRevisionCount() );
166 }
167
168 /**
169 * Test update to source page for pages with
170 * content model that supports redirects
171 *
172 * @covers \MediaWiki\Page\MergeHistory::merge
173 */
174 public function testSourceUpdateWithRedirectSupport() {
175 $title = Title::makeTitle( NS_MAIN, 'Merge5' );
176 $title2 = Title::makeTitle( NS_MAIN, 'Merge6' );
177
178 $factory = $this->getServiceContainer()->getMergeHistoryFactory();
179 $mh = $factory->newMergeHistory( $title, $title2 );
180
181 $this->assertTrue( $title->exists() );
182
183 $status = $mh->merge( static::getTestSysop()->getUser() );
184 $this->assertStatusOK( $status );
185
186 $this->assertTrue( $title->exists() );
187 }
188
189 /**
190 * Test update to source page for pages with
191 * content model that does not support redirects
192 *
193 * @covers \MediaWiki\Page\MergeHistory::merge
194 */
195 public function testSourceUpdateForNoRedirectSupport() {
196 $this->overrideConfigValues( [
197 MainConfigNames::ExtraNamespaces => [
198 2030 => 'NoRedirect',
199 2031 => 'NoRedirect_talk'
200 ],
201
202 MainConfigNames::NamespaceContentModels => [
203 2030 => 'testing'
204 ],
205 MainConfigNames::ContentHandlers => [
206 // Relies on the DummyContentHandlerForTesting not
207 // supporting redirects by default. If this ever gets
208 // changed this test has to be fixed.
209 'testing' => DummyContentHandlerForTesting::class
210 ]
211 ] );
212
213 $title = Title::makeTitle( NS_MAIN, 'Merge3' );
214 $title->setContentModel( 'testing' );
215 $title2 = Title::makeTitle( NS_MAIN, 'Merge4' );
216 $title2->setContentModel( 'testing' );
217
218 $factory = $this->getServiceContainer()->getMergeHistoryFactory();
219 $mh = $factory->newMergeHistory( $title, $title2 );
220
221 $this->assertTrue( $title->exists() );
222
223 $status = $mh->merge( static::getTestSysop()->getUser() );
224 $this->assertStatusOK( $status );
225
226 $this->assertFalse( $title->exists() );
227 }
228
229 /**
230 * @covers \MediaWiki\Page\MergeHistory::initTimestampLimits
231 */
232 public function testSplitTimestamp() {
233 // Create the source page with two revisions with the same timestamp
234 $user = static::getTestSysop()->getUser();
235 $title1 = $this->insertPage( "Merge7" )["title"];
236 $timestamp = MWTimestamp::now( TS_MW );
237 $store = $this->getServiceContainer()->getRevisionStore();
238 $revision = MutableRevisionRecord::newFromParentRevision( $store->getFirstRevision( $title1 ) );
239 $revision->setTimestamp( $timestamp );
240 $revision->setComment( CommentStoreComment::newUnsavedComment( "testing" ) );
241 $revision->setUser( $user );
242 $dbw = $this->getDB();
243 $revid1 = $store->insertRevisionOn( $revision, $dbw )->getID();
244
245 $revision2 = MutableRevisionRecord::newFromParentRevision( $store->getFirstRevision( $title1 ) );
246 $revision2->setTimestamp( $timestamp );
247 $revision2->setComment( CommentStoreComment::newUnsavedComment( "testing" ) );
248 $revision2->setUser( $user );
249 $revid2 = $store->insertRevisionOn( $revision2, $dbw )->getID();
250 // Create the destination page (here to ensure its timestamp is the same or later than the above)
251 $title2 = $this->insertPage( "Merge8" )["title"];
252
253 // Now do the merge
254 $factory = $this->getServiceContainer()->getMergeHistoryFactory();
255 $mh = $factory->newMergeHistory( $title1, $title2, $timestamp . '|' . $revid1 );
256 $status = $mh->merge( $user );
257 $this->assertStatusOK( $status );
258
259 $this->assertNull( $store->getRevisionByPageId( $title1->getId(), $revid1 ) );
260 $this->assertNotNull( $store->getRevisionByPageId( $title1->getId(), $revid2 ) );
261 $this->assertNotNull( $store->getRevisionByPageId( $title2->getId(), $revid1 ) );
262 $this->assertNull( $store->getRevisionByPageId( $title2->getId(), $revid2 ) );
263 }
264 }