2015-12-30 04:53:34 +00:00
|
|
|
<?php
|
|
|
|
|
|
2020-04-28 22:54:18 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
|
|
|
|
|
2015-12-30 04:53:34 +00:00
|
|
|
/**
|
|
|
|
|
* @group Database
|
|
|
|
|
*/
|
|
|
|
|
class MergeHistoryTest extends MediaWikiTestCase {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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' );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @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() {
|
Introduce wfDeprecatedMsg()
Deprecating something means to say something nasty about it, or to draw
its character into question. For example, "this function is lazy and good
for nothing". Deprecatory remarks by a developer are generally taken as a
warning that violence will soon be done against the function in question.
Other developers are thus warned to avoid associating with the deprecated
function.
However, since wfDeprecated() was introduced, it has become obvious that
the targets of deprecation are not limited to functions. Developers can
deprecate literally anything: a parameter, a return value, a file
format, Mondays, the concept of being, etc. wfDeprecated() requires
every deprecatory statement to begin with "use of", leading to some
awkward sentences. For example, one might say: "Use of your mouth to
cough without it being covered by your arm is deprecated since 2020."
So, introduce wfDeprecatedMsg(), which allows deprecation messages to be
specified in plain text, with the caller description being optionally
appended. Migrate incorrect or gramatically awkward uses of wfDeprecated()
to wfDeprecatedMsg().
Change-Id: Ib3dd2fe37677d98425d0f3692db5c9e988943ae8
2020-06-12 04:18:35 +00:00
|
|
|
$this->filterDeprecated( '/Direct construction of MergeHistory/' );
|
2020-04-28 22:54:18 +00:00
|
|
|
|
2015-12-30 04:53:34 +00:00
|
|
|
$limit = MergeHistory::REVISION_LIMIT;
|
|
|
|
|
|
2018-01-13 00:02:09 +00:00
|
|
|
$mh = $this->getMockBuilder( MergeHistory::class )
|
2016-02-17 09:09:32 +00:00
|
|
|
->setMethods( [ 'getRevisionCount' ] )
|
|
|
|
|
->setConstructorArgs( [
|
2015-12-30 04:53:34 +00:00
|
|
|
Title::newFromText( 'Test' ),
|
|
|
|
|
Title::newFromText( 'Test2' ),
|
2016-02-17 09:09:32 +00:00
|
|
|
] )
|
2015-12-30 04:53:34 +00:00
|
|
|
->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() {
|
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' )
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Sysop with mergehistory permission
|
2016-05-18 09:19:20 +00:00
|
|
|
$sysop = static::getTestSysop()->getUser();
|
2015-12-30 04:53:34 +00:00
|
|
|
$status = $mh->checkPermissions( $sysop, '' );
|
|
|
|
|
$this->assertTrue( $status->isOK() );
|
|
|
|
|
|
|
|
|
|
// Normal user
|
2016-05-18 09:19:20 +00:00
|
|
|
$notSysop = static::getTestUser()->getUser();
|
2015-12-30 04:53:34 +00:00
|
|
|
$status = $mh->checkPermissions( $notSysop, '' );
|
|
|
|
|
$this->assertTrue( $status->hasMessage( 'mergehistory-fail-permission' ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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 );
|
2015-12-30 04:53:34 +00:00
|
|
|
$this->assertEquals( $mh->getMergedRevisionCount(), 1 );
|
|
|
|
|
}
|
2020-04-28 22:54:18 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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
|
Introduce wfDeprecatedMsg()
Deprecating something means to say something nasty about it, or to draw
its character into question. For example, "this function is lazy and good
for nothing". Deprecatory remarks by a developer are generally taken as a
warning that violence will soon be done against the function in question.
Other developers are thus warned to avoid associating with the deprecated
function.
However, since wfDeprecated() was introduced, it has become obvious that
the targets of deprecation are not limited to functions. Developers can
deprecate literally anything: a parameter, a return value, a file
format, Mondays, the concept of being, etc. wfDeprecated() requires
every deprecatory statement to begin with "use of", leading to some
awkward sentences. For example, one might say: "Use of your mouth to
cough without it being covered by your arm is deprecated since 2020."
So, introduce wfDeprecatedMsg(), which allows deprecation messages to be
specified in plain text, with the caller description being optionally
appended. Migrate incorrect or gramatically awkward uses of wfDeprecated()
to wfDeprecatedMsg().
Change-Id: Ib3dd2fe37677d98425d0f3692db5c9e988943ae8
2020-06-12 04:18:35 +00:00
|
|
|
$this->filterDeprecated( '/Direct construction of MergeHistory/' );
|
2020-04-28 22:54:18 +00:00
|
|
|
$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(),
|
2020-05-13 21:30:46 +00:00
|
|
|
$services->getWatchedItemStore(),
|
|
|
|
|
$services->getSpamChecker()
|
2020-04-28 22:54:18 +00:00
|
|
|
);
|
|
|
|
|
$this->assertInstanceOf(
|
|
|
|
|
MergeHistory::class,
|
|
|
|
|
$mergeHistory
|
|
|
|
|
);
|
|
|
|
|
}
|
2015-12-30 04:53:34 +00:00
|
|
|
}
|