2016-12-12 14:26:15 +00:00
|
|
|
<?php
|
|
|
|
|
|
2019-08-19 21:41:04 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2018-12-17 14:03:44 +00:00
|
|
|
use Wikimedia\TestingAccessWrapper;
|
|
|
|
|
|
2016-12-12 14:26:15 +00:00
|
|
|
/**
|
|
|
|
|
* @group Database
|
|
|
|
|
*/
|
2020-06-30 15:09:24 +00:00
|
|
|
class ContribsPagerTest extends MediaWikiIntegrationTestCase {
|
2017-04-21 16:17:59 +00:00
|
|
|
/** @var ContribsPager */
|
|
|
|
|
private $pager;
|
|
|
|
|
|
2019-08-19 21:41:04 +00:00
|
|
|
/** @var LinkRenderer */
|
|
|
|
|
private $linkRenderer;
|
|
|
|
|
|
2020-09-19 22:43:46 +00:00
|
|
|
/** @var RevisionStore */
|
|
|
|
|
private $revisionStore;
|
|
|
|
|
|
|
|
|
|
/** @var LinkBatchFactory */
|
|
|
|
|
private $linkBatchFactory;
|
|
|
|
|
|
|
|
|
|
/** @var HookContainer */
|
|
|
|
|
private $hookContainer;
|
|
|
|
|
|
|
|
|
|
/** @var ILoadBalancer */
|
|
|
|
|
private $loadBalancer;
|
|
|
|
|
|
|
|
|
|
/** @var ActorMigration */
|
|
|
|
|
private $actorMigration;
|
|
|
|
|
|
|
|
|
|
/** @var NamespaceInfo */
|
|
|
|
|
private $namespaceInfo;
|
|
|
|
|
|
2020-06-14 10:51:39 +00:00
|
|
|
protected function setUp() : void {
|
2020-09-19 22:43:46 +00:00
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
|
|
$services = MediaWikiServices::getInstance();
|
|
|
|
|
$this->linkRenderer = $services->getLinkRenderer();
|
|
|
|
|
$this->revisionStore = $services->getRevisionStore();
|
|
|
|
|
$this->linkBatchFactory = $services->getLinkBatchFactory();
|
|
|
|
|
$this->hookContainer = $services->getHookContainer();
|
|
|
|
|
$this->loadBalancer = $services->getDBLoadBalancer();
|
|
|
|
|
$this->actorMigration = $services->getActorMigration();
|
|
|
|
|
$this->namespaceInfo = $services->getNamespaceInfo();
|
|
|
|
|
$this->pager = $this->getContribsPager( [
|
2017-04-21 16:17:59 +00:00
|
|
|
'start' => '2017-01-01',
|
|
|
|
|
'end' => '2017-02-02',
|
2020-09-19 22:43:46 +00:00
|
|
|
] );
|
|
|
|
|
}
|
2017-04-21 16:17:59 +00:00
|
|
|
|
2020-09-19 22:43:46 +00:00
|
|
|
private function getContribsPager( array $options ) {
|
|
|
|
|
return new ContribsPager(
|
|
|
|
|
new RequestContext(),
|
|
|
|
|
$options,
|
|
|
|
|
$this->linkRenderer,
|
|
|
|
|
$this->linkBatchFactory,
|
|
|
|
|
$this->hookContainer,
|
|
|
|
|
$this->loadBalancer,
|
|
|
|
|
$this->actorMigration,
|
|
|
|
|
$this->revisionStore,
|
|
|
|
|
$this->namespaceInfo
|
|
|
|
|
);
|
2017-04-21 16:17:59 +00:00
|
|
|
}
|
|
|
|
|
|
2020-07-16 17:25:15 +00:00
|
|
|
/**
|
|
|
|
|
* @covers ContribsPager::reallyDoQuery
|
|
|
|
|
* Tests enabling/disabling ContribsPager::reallyDoQuery hook via the revisionsOnly option to restrict
|
|
|
|
|
* extensions are able to insert their own revisions
|
|
|
|
|
*/
|
|
|
|
|
public function testRevisionsOnlyOption() {
|
2021-02-07 13:10:36 +00:00
|
|
|
$this->setTemporaryHook( 'ContribsPager::reallyDoQuery', static function ( &$data ) {
|
2020-07-16 17:25:15 +00:00
|
|
|
$fakeRow = (object)[ 'rev_timestamp' => '20200717192356' ];
|
|
|
|
|
$fakeRowWrapper = new FakeResultWrapper( [ $fakeRow ] );
|
|
|
|
|
$data[] = $fakeRowWrapper;
|
|
|
|
|
} );
|
|
|
|
|
|
2020-09-19 22:43:46 +00:00
|
|
|
$allContribsPager = $this->getContribsPager( [] );
|
2020-07-16 17:25:15 +00:00
|
|
|
$allContribsResults = $allContribsPager->reallyDoQuery( '', 2, IndexPager::QUERY_DESCENDING );
|
|
|
|
|
$this->assertEquals( $allContribsResults->numRows(), 1 );
|
|
|
|
|
|
2020-09-19 22:43:46 +00:00
|
|
|
$revOnlyPager = $this->getContribsPager( [ 'revisionsOnly' => true ] );
|
2020-07-16 17:25:15 +00:00
|
|
|
$revOnlyResults = $revOnlyPager->reallyDoQuery( '', 2, IndexPager::QUERY_DESCENDING );
|
|
|
|
|
$this->assertEquals( $revOnlyResults->numRows(), 0 );
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-12 14:26:15 +00:00
|
|
|
/**
|
2017-12-25 07:27:02 +00:00
|
|
|
* @covers ContribsPager::processDateFilter
|
2016-12-12 14:26:15 +00:00
|
|
|
* @dataProvider dateFilterOptionProcessingProvider
|
|
|
|
|
* @param array $inputOpts Input options
|
|
|
|
|
* @param array $expectedOpts Expected options
|
|
|
|
|
*/
|
2018-12-17 14:03:44 +00:00
|
|
|
public function testDateFilterOptionProcessing( array $inputOpts, array $expectedOpts ) {
|
2020-03-19 22:46:38 +00:00
|
|
|
$this->assertArraySubmapSame(
|
|
|
|
|
$expectedOpts,
|
|
|
|
|
ContribsPager::processDateFilter( $inputOpts ),
|
|
|
|
|
"Matching date filter options"
|
|
|
|
|
);
|
2016-12-12 14:26:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function dateFilterOptionProcessingProvider() {
|
|
|
|
|
return [
|
|
|
|
|
[ [ 'start' => '2016-05-01',
|
|
|
|
|
'end' => '2016-06-01',
|
|
|
|
|
'year' => null,
|
|
|
|
|
'month' => null ],
|
|
|
|
|
[ 'start' => '2016-05-01',
|
|
|
|
|
'end' => '2016-06-01' ] ],
|
|
|
|
|
[ [ 'start' => '2016-05-01',
|
|
|
|
|
'end' => '2016-06-01',
|
|
|
|
|
'year' => '',
|
|
|
|
|
'month' => '' ],
|
|
|
|
|
[ 'start' => '2016-05-01',
|
|
|
|
|
'end' => '2016-06-01' ] ],
|
|
|
|
|
[ [ 'start' => '2016-05-01',
|
|
|
|
|
'end' => '2016-06-01',
|
|
|
|
|
'year' => '2012',
|
|
|
|
|
'month' => '5' ],
|
|
|
|
|
[ 'start' => '',
|
|
|
|
|
'end' => '2012-05-31' ] ],
|
|
|
|
|
[ [ 'start' => '',
|
|
|
|
|
'end' => '',
|
|
|
|
|
'year' => '2012',
|
|
|
|
|
'month' => '5' ],
|
|
|
|
|
[ 'start' => '',
|
|
|
|
|
'end' => '2012-05-31' ] ],
|
|
|
|
|
[ [ 'start' => '',
|
|
|
|
|
'end' => '',
|
|
|
|
|
'year' => '2012',
|
|
|
|
|
'month' => '' ],
|
|
|
|
|
[ 'start' => '',
|
|
|
|
|
'end' => '2012-12-31' ] ],
|
|
|
|
|
];
|
|
|
|
|
}
|
2017-04-21 16:17:59 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers ContribsPager::isQueryableRange
|
|
|
|
|
* @dataProvider provideQueryableRanges
|
|
|
|
|
*/
|
|
|
|
|
public function testQueryableRanges( $ipRange ) {
|
|
|
|
|
$this->setMwGlobals( [
|
|
|
|
|
'wgRangeContributionsCIDRLimit' => [
|
|
|
|
|
'IPv4' => 16,
|
|
|
|
|
'IPv6' => 32,
|
|
|
|
|
],
|
|
|
|
|
] );
|
|
|
|
|
|
|
|
|
|
$this->assertTrue(
|
|
|
|
|
$this->pager->isQueryableRange( $ipRange ),
|
|
|
|
|
"$ipRange is a queryable IP range"
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function provideQueryableRanges() {
|
|
|
|
|
return [
|
|
|
|
|
[ '116.17.184.5/32' ],
|
|
|
|
|
[ '0.17.184.5/16' ],
|
|
|
|
|
[ '2000::/32' ],
|
|
|
|
|
[ '2001:db8::/128' ],
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers ContribsPager::isQueryableRange
|
|
|
|
|
* @dataProvider provideUnqueryableRanges
|
|
|
|
|
*/
|
|
|
|
|
public function testUnqueryableRanges( $ipRange ) {
|
|
|
|
|
$this->setMwGlobals( [
|
|
|
|
|
'wgRangeContributionsCIDRLimit' => [
|
|
|
|
|
'IPv4' => 16,
|
|
|
|
|
'IPv6' => 32,
|
|
|
|
|
],
|
|
|
|
|
] );
|
|
|
|
|
|
|
|
|
|
$this->assertFalse(
|
|
|
|
|
$this->pager->isQueryableRange( $ipRange ),
|
|
|
|
|
"$ipRange is not a queryable IP range"
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function provideUnqueryableRanges() {
|
|
|
|
|
return [
|
|
|
|
|
[ '116.17.184.5/33' ],
|
|
|
|
|
[ '0.17.184.5/15' ],
|
|
|
|
|
[ '2000::/31' ],
|
|
|
|
|
[ '2001:db8::/9999' ],
|
|
|
|
|
];
|
|
|
|
|
}
|
2018-12-17 14:03:44 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers \ContribsPager::getExtraSortFields
|
|
|
|
|
* @covers \ContribsPager::getIndexField
|
|
|
|
|
* @covers \ContribsPager::getQueryInfo
|
|
|
|
|
*/
|
|
|
|
|
public function testUniqueSortOrderWithoutIpChanges() {
|
2020-09-19 22:43:46 +00:00
|
|
|
$pager = $this->getContribsPager( [
|
2018-12-17 14:03:44 +00:00
|
|
|
'start' => '',
|
|
|
|
|
'end' => '',
|
2020-09-19 22:43:46 +00:00
|
|
|
] );
|
2018-12-17 14:03:44 +00:00
|
|
|
|
|
|
|
|
/** @var ContribsPager $pager */
|
|
|
|
|
$pager = TestingAccessWrapper::newFromObject( $pager );
|
|
|
|
|
$queryInfo = $pager->buildQueryInfo( '', 1, false );
|
|
|
|
|
|
|
|
|
|
$this->assertNotContains( 'ip_changes', $queryInfo[0] );
|
|
|
|
|
$this->assertArrayNotHasKey( 'ip_changes', $queryInfo[5] );
|
|
|
|
|
$this->assertContains( 'rev_timestamp', $queryInfo[1] );
|
|
|
|
|
$this->assertContains( 'rev_id', $queryInfo[1] );
|
|
|
|
|
$this->assertSame( [ 'rev_timestamp DESC', 'rev_id DESC' ], $queryInfo[4]['ORDER BY'] );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers \ContribsPager::getExtraSortFields
|
|
|
|
|
* @covers \ContribsPager::getIndexField
|
|
|
|
|
* @covers \ContribsPager::getQueryInfo
|
|
|
|
|
*/
|
|
|
|
|
public function testUniqueSortOrderOnIpChanges() {
|
2020-09-19 22:43:46 +00:00
|
|
|
$pager = $this->getContribsPager( [
|
2018-12-17 14:03:44 +00:00
|
|
|
'target' => '116.17.184.5/32',
|
|
|
|
|
'start' => '',
|
|
|
|
|
'end' => '',
|
2020-09-19 22:43:46 +00:00
|
|
|
] );
|
2018-12-17 14:03:44 +00:00
|
|
|
|
|
|
|
|
/** @var ContribsPager $pager */
|
|
|
|
|
$pager = TestingAccessWrapper::newFromObject( $pager );
|
|
|
|
|
$queryInfo = $pager->buildQueryInfo( '', 1, false );
|
|
|
|
|
|
|
|
|
|
$this->assertContains( 'ip_changes', $queryInfo[0] );
|
|
|
|
|
$this->assertArrayHasKey( 'ip_changes', $queryInfo[5] );
|
2019-04-23 16:13:08 +00:00
|
|
|
$this->assertSame( [ 'ipc_rev_timestamp DESC', 'ipc_rev_id DESC' ], $queryInfo[4]['ORDER BY'] );
|
2018-12-17 14:03:44 +00:00
|
|
|
}
|
|
|
|
|
|
2020-04-27 04:57:50 +00:00
|
|
|
/**
|
|
|
|
|
* @covers \ContribsPager::tryToCreateValidRevision
|
|
|
|
|
* @covers \ContribsPager::tryCreatingRevisionRecord
|
|
|
|
|
*/
|
|
|
|
|
public function testCreateRevision() {
|
2020-04-29 15:41:17 +00:00
|
|
|
$this->hideDeprecated( 'ContribsPager::tryToCreateValidRevision' );
|
2020-07-02 06:22:52 +00:00
|
|
|
$this->hideDeprecated( 'Revision::__construct' );
|
2020-10-29 20:31:51 +00:00
|
|
|
$title = Title::makeTitle( NS_MAIN, __METHOD__ );
|
2020-04-29 15:41:17 +00:00
|
|
|
|
2020-09-19 22:43:46 +00:00
|
|
|
$pager = $this->getContribsPager( [
|
2020-04-27 04:57:50 +00:00
|
|
|
'target' => '116.17.184.5/32',
|
|
|
|
|
'start' => '',
|
|
|
|
|
'end' => '',
|
2020-09-19 22:43:46 +00:00
|
|
|
] );
|
2020-04-27 04:57:50 +00:00
|
|
|
|
|
|
|
|
$invalidRow = (object)[
|
|
|
|
|
'foo' => 'bar'
|
|
|
|
|
];
|
|
|
|
|
|
2020-10-29 20:31:51 +00:00
|
|
|
$this->assertNull( $pager->tryToCreateValidRevision( $invalidRow, $title ) );
|
|
|
|
|
$this->assertNull( $pager->tryCreatingRevisionRecord( $invalidRow, $title ) );
|
2020-04-27 04:57:50 +00:00
|
|
|
|
|
|
|
|
$validRow = (object)[
|
|
|
|
|
'rev_id' => '2',
|
|
|
|
|
'rev_page' => '2',
|
2020-10-29 20:31:51 +00:00
|
|
|
'page_namespace' => $title->getNamespace(),
|
|
|
|
|
'page_title' => $title->getDBkey(),
|
2020-04-27 04:57:50 +00:00
|
|
|
'rev_text_id' => '47',
|
|
|
|
|
'rev_timestamp' => '20180528192356',
|
|
|
|
|
'rev_minor_edit' => '0',
|
|
|
|
|
'rev_deleted' => '0',
|
|
|
|
|
'rev_len' => '700',
|
|
|
|
|
'rev_parent_id' => '0',
|
|
|
|
|
'rev_sha1' => 'deadbeef',
|
|
|
|
|
'rev_comment_text' => 'whatever',
|
|
|
|
|
'rev_comment_data' => null,
|
|
|
|
|
'rev_comment_cid' => null,
|
2021-02-20 21:51:37 +00:00
|
|
|
'rev_user' => '1',
|
2020-04-27 04:57:50 +00:00
|
|
|
'rev_user_text' => 'Editor',
|
2021-02-20 21:51:37 +00:00
|
|
|
'rev_actor' => '11',
|
2020-04-27 04:57:50 +00:00
|
|
|
'rev_content_format' => null,
|
|
|
|
|
'rev_content_model' => null,
|
|
|
|
|
];
|
|
|
|
|
|
2020-10-29 20:31:51 +00:00
|
|
|
$this->assertNotNull( $pager->tryToCreateValidRevision( $validRow, $title ) );
|
|
|
|
|
$this->assertNotNull( $pager->tryCreatingRevisionRecord( $validRow, $title ) );
|
2020-04-27 04:57:50 +00:00
|
|
|
}
|
2016-12-12 14:26:15 +00:00
|
|
|
}
|