2015-12-30 04:53:34 +00:00
|
|
|
<?php
|
|
|
|
|
|
2020-04-28 22:54:18 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2021-03-02 21:46:06 +00:00
|
|
|
use MediaWiki\Tests\Unit\Permissions\MockAuthorityTrait;
|
2020-04-28 22:54:18 +00:00
|
|
|
|
2015-12-30 04:53:34 +00:00
|
|
|
/**
|
|
|
|
|
* @group Database
|
|
|
|
|
*/
|
2020-06-30 15:09:24 +00:00
|
|
|
class MergeHistoryTest extends MediaWikiIntegrationTestCase {
|
2021-03-02 21:46:06 +00:00
|
|
|
use MockAuthorityTrait;
|
2015-12-30 04:53:34 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Make some pages to work with
|
|
|
|
|
*/
|
2016-03-07 17:26:25 +00:00
|
|
|
public function addDBDataOnce() {
|
2015-12-30 04:53:34 +00:00
|
|
|
// Pages that won't actually be merged
|
|
|
|
|
$this->insertPage( 'Test' );
|
|
|
|
|
$this->insertPage( 'Test2' );
|
|
|
|
|
|
|
|
|
|
// Pages that will be merged
|
|
|
|
|
$this->insertPage( 'Merge1' );
|
|
|
|
|
$this->insertPage( 'Merge2' );
|
Improve handling of content models that do not support redirect.
To properly support content models that do not support redirects
during historymerge we have to do some cleanup, else after merging
all revisions of a page, a corrupted page will be left with a page id
(because it was not deleted) but no live revision (because they have
been all merged to the destination page).
This will lead to cascade of exceptions in Wikipage, RevisionStore,
RevisionStoreRecord, PoolWorkArticleView as well other various paths
that will attempt to interact with these, because page and revision
mismatch is considered a logic error almost everywhere.
The failure does not happen for content models that support redirects
because they are immediately creating new (latest) revision for the
old corrupted page. But we cannot require all content models to support
redirects, may not be feasible and can hinder forward compatibility.
This patch fixes this for content models that do not support redirect.
Now after merging all revisions of a page to another page, and the
source content model does not support redirect, empty content will be
created to aid proper deletion of the page afterwards.
Creating the content before deletion is necessary, else proper
deletion is not possible because many calls to revision-related methods
will throw exception during the deletion if we just use the original
corrupted page which does not have proper revisions now.
Bug: T93469
Bug: T263340
Change-Id: I07109445288633e3ddece4190f0c1c2b10372384
2020-09-28 09:08:00 +00:00
|
|
|
|
|
|
|
|
// Exclusive for testSourceUpdateForNoRedirectSupport()
|
|
|
|
|
$this->insertPage( 'Merge3' );
|
|
|
|
|
$this->insertPage( 'Merge4' );
|
2015-12-30 04:53:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider provideIsValidMerge
|
|
|
|
|
* @covers MergeHistory::isValidMerge
|
2016-04-30 10:10:17 +00:00
|
|
|
* @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)
|
2015-12-30 04:53:34 +00:00
|
|
|
*/
|
|
|
|
|
public function testIsValidMerge( $source, $dest, $timestamp, $error ) {
|
2018-08-15 00:11:43 +00:00
|
|
|
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 );
|
|
|
|
|
}
|
2020-04-28 22:54:18 +00:00
|
|
|
$factory = MediaWikiServices::getInstance()->getMergeHistoryFactory();
|
|
|
|
|
$mh = $factory->newMergeHistory(
|
2015-12-30 04:53:34 +00:00
|
|
|
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() {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [
|
2015-12-30 04:53:34 +00:00
|
|
|
// for MergeHistory::isValidMerge
|
2016-02-17 09:09:32 +00:00
|
|
|
[ 'Test', 'Test2', false, true ],
|
2018-08-15 00:11:43 +00:00
|
|
|
// Timestamp of `true` is a placeholder for "in the future""
|
|
|
|
|
[ 'Test', 'Test2', true, true ],
|
2016-02-17 09:09:32 +00:00
|
|
|
[ 'Test', 'Test', false, 'mergehistory-fail-self-merge' ],
|
|
|
|
|
[ 'Nonexistant', 'Test2', false, 'mergehistory-fail-invalid-source' ],
|
|
|
|
|
[ 'Test', 'Nonexistant', false, 'mergehistory-fail-invalid-dest' ],
|
|
|
|
|
[
|
2015-12-30 04:53:34 +00:00
|
|
|
'Test',
|
|
|
|
|
'Test2',
|
|
|
|
|
'This is obviously an invalid timestamp',
|
|
|
|
|
'mergehistory-fail-bad-timestamp'
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
];
|
2015-12-30 04:53:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Test merge revision limit checking
|
|
|
|
|
* @covers MergeHistory::isValidMerge
|
|
|
|
|
*/
|
|
|
|
|
public function testIsValidMergeRevisionLimit() {
|
|
|
|
|
$limit = MergeHistory::REVISION_LIMIT;
|
2018-01-13 00:02:09 +00:00
|
|
|
$mh = $this->getMockBuilder( MergeHistory::class )
|
2021-03-20 15:18:58 +00:00
|
|
|
->onlyMethods( [ 'getRevisionCount' ] )
|
2016-02-17 09:09:32 +00:00
|
|
|
->setConstructorArgs( [
|
2015-12-30 04:53:34 +00:00
|
|
|
Title::newFromText( 'Test' ),
|
|
|
|
|
Title::newFromText( 'Test2' ),
|
2021-06-22 14:35:13 +00:00
|
|
|
null,
|
|
|
|
|
$this->getServiceContainer()->getDBLoadBalancer(),
|
|
|
|
|
$this->getServiceContainer()->getContentHandlerFactory(),
|
|
|
|
|
$this->getServiceContainer()->getRevisionStore(),
|
|
|
|
|
$this->getServiceContainer()->getWatchedItemStore(),
|
|
|
|
|
$this->getServiceContainer()->getSpamChecker(),
|
|
|
|
|
$this->getServiceContainer()->getHookContainer(),
|
|
|
|
|
$this->getServiceContainer()->getWikiPageFactory(),
|
|
|
|
|
$this->getServiceContainer()->getTitleFormatter(),
|
|
|
|
|
$this->getServiceContainer()->getTitleFactory(),
|
2016-02-17 09:09:32 +00:00
|
|
|
] )
|
2015-12-30 04:53:34 +00:00
|
|
|
->getMock();
|
|
|
|
|
$mh->expects( $this->once() )
|
|
|
|
|
->method( 'getRevisionCount' )
|
2021-04-22 08:28:11 +00:00
|
|
|
->willReturn( $limit + 1 );
|
2015-12-30 04:53:34 +00:00
|
|
|
|
|
|
|
|
$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
|
2021-03-02 21:46:06 +00:00
|
|
|
* @covers MergeHistory::authorizeMerge
|
|
|
|
|
* @covers MergeHistory::probablyCanMerge
|
2015-12-30 04:53:34 +00:00
|
|
|
*/
|
|
|
|
|
public function testCheckPermissions() {
|
2020-04-28 22:54:18 +00:00
|
|
|
$factory = MediaWikiServices::getInstance()->getMergeHistoryFactory();
|
|
|
|
|
$mh = $factory->newMergeHistory(
|
2015-12-30 04:53:34 +00:00
|
|
|
Title::newFromText( 'Test' ),
|
|
|
|
|
Title::newFromText( 'Test2' )
|
|
|
|
|
);
|
|
|
|
|
|
2021-03-02 21:46:06 +00:00
|
|
|
foreach ( [ 'authorizeMerge', 'probablyCanMerge' ] as $method ) {
|
|
|
|
|
// Sysop with mergehistory permission
|
|
|
|
|
$status = $mh->$method(
|
|
|
|
|
$this->mockRegisteredUltimateAuthority(),
|
|
|
|
|
''
|
|
|
|
|
);
|
|
|
|
|
$this->assertTrue( $status->isOK() );
|
|
|
|
|
|
|
|
|
|
$status = $mh->$method(
|
|
|
|
|
$this->mockRegisteredAuthorityWithoutPermissions( [ 'mergehistory' ] ),
|
|
|
|
|
''
|
|
|
|
|
);
|
|
|
|
|
$this->assertTrue( $status->hasMessage( 'mergehistory-fail-permission' ) );
|
|
|
|
|
}
|
2015-12-30 04:53:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Test merged revision count
|
|
|
|
|
* @covers MergeHistory::getMergedRevisionCount
|
|
|
|
|
*/
|
|
|
|
|
public function testGetMergedRevisionCount() {
|
2020-04-28 22:54:18 +00:00
|
|
|
$factory = MediaWikiServices::getInstance()->getMergeHistoryFactory();
|
|
|
|
|
$mh = $factory->newMergeHistory(
|
2015-12-30 04:53:34 +00:00
|
|
|
Title::newFromText( 'Merge1' ),
|
|
|
|
|
Title::newFromText( 'Merge2' )
|
|
|
|
|
);
|
|
|
|
|
|
2016-05-18 09:19:20 +00:00
|
|
|
$sysop = static::getTestSysop()->getUser();
|
|
|
|
|
$mh->merge( $sysop );
|
2021-03-07 20:00:46 +00:00
|
|
|
$this->assertSame( 1, $mh->getMergedRevisionCount() );
|
2015-12-30 04:53:34 +00:00
|
|
|
}
|
2020-04-28 22:54:18 +00:00
|
|
|
|
Improve handling of content models that do not support redirect.
To properly support content models that do not support redirects
during historymerge we have to do some cleanup, else after merging
all revisions of a page, a corrupted page will be left with a page id
(because it was not deleted) but no live revision (because they have
been all merged to the destination page).
This will lead to cascade of exceptions in Wikipage, RevisionStore,
RevisionStoreRecord, PoolWorkArticleView as well other various paths
that will attempt to interact with these, because page and revision
mismatch is considered a logic error almost everywhere.
The failure does not happen for content models that support redirects
because they are immediately creating new (latest) revision for the
old corrupted page. But we cannot require all content models to support
redirects, may not be feasible and can hinder forward compatibility.
This patch fixes this for content models that do not support redirect.
Now after merging all revisions of a page to another page, and the
source content model does not support redirect, empty content will be
created to aid proper deletion of the page afterwards.
Creating the content before deletion is necessary, else proper
deletion is not possible because many calls to revision-related methods
will throw exception during the deletion if we just use the original
corrupted page which does not have proper revisions now.
Bug: T93469
Bug: T263340
Change-Id: I07109445288633e3ddece4190f0c1c2b10372384
2020-09-28 09:08:00 +00:00
|
|
|
/**
|
|
|
|
|
* Test update to source page for pages with
|
|
|
|
|
* content model that supports redirects
|
|
|
|
|
*
|
|
|
|
|
* @covers MergeHistory::merge
|
|
|
|
|
*/
|
|
|
|
|
public function testSourceUpdateWithRedirectSupport() {
|
|
|
|
|
$title = Title::newFromText( 'Merge1' );
|
|
|
|
|
$title2 = Title::newFromText( 'Merge2' );
|
|
|
|
|
|
|
|
|
|
$factory = MediaWikiServices::getInstance()->getMergeHistoryFactory();
|
|
|
|
|
$mh = $factory->newMergeHistory( $title, $title2 );
|
|
|
|
|
|
|
|
|
|
$this->assertTrue( $title->exists() );
|
|
|
|
|
|
|
|
|
|
$status = $mh->merge( static::getTestSysop()->getUser() );
|
|
|
|
|
|
|
|
|
|
$this->assertTrue( $title->exists() );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Test update to source page for pages with
|
|
|
|
|
* content model that does not support redirects
|
|
|
|
|
*
|
|
|
|
|
* @covers MergeHistory::merge
|
|
|
|
|
*/
|
|
|
|
|
public function testSourceUpdateForNoRedirectSupport() {
|
|
|
|
|
$this->setMwGlobals( [
|
|
|
|
|
'wgExtraNamespaces' => [
|
|
|
|
|
2030 => 'NoRedirect',
|
|
|
|
|
2030 => 'NoRedirect_talk'
|
|
|
|
|
],
|
|
|
|
|
|
|
|
|
|
'wgNamespaceContentModels' => [
|
|
|
|
|
2030 => 'testing'
|
|
|
|
|
],
|
|
|
|
|
'wgContentHandlers' => [
|
|
|
|
|
// Relies on the DummyContentHandlerForTesting not
|
|
|
|
|
// supporting redirects by default. If this ever gets
|
|
|
|
|
// changed this test has to be fixed.
|
|
|
|
|
'testing' => DummyContentHandlerForTesting::class
|
|
|
|
|
]
|
|
|
|
|
] );
|
|
|
|
|
|
|
|
|
|
$title = Title::newFromText( 'Merge3' );
|
|
|
|
|
$title->setContentModel( 'testing' );
|
|
|
|
|
$title2 = Title::newFromText( 'Merge4' );
|
|
|
|
|
$title2->setContentModel( 'testing' );
|
|
|
|
|
|
|
|
|
|
$factory = MediaWikiServices::getInstance()->getMergeHistoryFactory();
|
|
|
|
|
$mh = $factory->newMergeHistory( $title, $title2 );
|
|
|
|
|
|
|
|
|
|
$this->assertTrue( $title->exists() );
|
|
|
|
|
|
|
|
|
|
$status = $mh->merge( static::getTestSysop()->getUser() );
|
|
|
|
|
|
|
|
|
|
$this->assertFalse( $title->exists() );
|
|
|
|
|
}
|
2015-12-30 04:53:34 +00:00
|
|
|
}
|