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 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
|
<?php
use MediaWiki\Cache\LinkBatchFactory;
use MediaWiki\CommentFormatter\CommentFormatter;
use MediaWiki\Context\RequestContext;
use MediaWiki\HookContainer\HookContainer;
use MediaWiki\Linker\LinkRenderer;
use MediaWiki\Pager\DeletedContribsPager;
use MediaWiki\Revision\RevisionRecord;
use MediaWiki\Revision\RevisionStore;
use MediaWiki\Title\NamespaceInfo;
use MediaWiki\Title\Title;
use MediaWiki\User\User;
use MediaWiki\User\UserFactory;
use MediaWiki\User\UserIdentityValue;
use Wikimedia\Rdbms\IConnectionProvider;
use Wikimedia\TestingAccessWrapper;
/**
* @group Database
* @covers \MediaWiki\Pager\DeletedContribsPager
*/
class DeletedContribsPagerTest extends MediaWikiIntegrationTestCase {
private static User $user;
/** @var DeletedContribsPager */
private $pager;
/** @var HookContainer */
private $hookContainer;
/** @var LinkRenderer */
private $linkRenderer;
/** @var IConnectionProvider */
private $dbProvider;
/** @var RevisionStore */
private $revisionStore;
/** @var NamespaceInfo */
private $namespaceInfo;
/** @var CommentFormatter */
private $commentFormatter;
/** @var LinkBatchFactory */
private $linkBatchFactory;
/** @var UserFactory */
private $userFactory;
protected function setUp(): void {
parent::setUp();
$services = $this->getServiceContainer();
$this->hookContainer = $services->getHookContainer();
$this->linkRenderer = $services->getLinkRenderer();
$this->dbProvider = $services->getConnectionProvider();
$this->revisionStore = $services->getRevisionStore();
$this->namespaceInfo = $services->getNamespaceInfo();
$this->commentFormatter = $services->getCommentFormatter();
$this->linkBatchFactory = $services->getLinkBatchFactory();
$this->userFactory = $services->getUserFactory();
$this->pager = $this->getDeletedContribsPager();
}
private function getDeletedContribsPager( $target = 'Some test user', $namespace = 0 ) {
$target = UserIdentityValue::newAnonymous( $target );
return new DeletedContribsPager(
$this->hookContainer,
$this->linkRenderer,
$this->dbProvider,
$this->revisionStore,
$this->namespaceInfo,
$this->commentFormatter,
$this->linkBatchFactory,
$this->userFactory,
RequestContext::getMain(),
[ 'namespace' => $namespace ],
$target
);
}
/**
* Flow uses DeletedContribsPager::reallyDoQuery hook to provide something other then
* stdClass as a row, and then manually formats its own row in ContributionsLineEnding.
* Emulate this behaviour and check that it works.
*/
public function testDeletedContribProvidedByHook() {
$this->setTemporaryHook( 'DeletedContribsPager::reallyDoQuery', static function ( &$data ) {
$data = [ [ new class() {
public $ar_timestamp = 12345;
public $testing = 'TESTING';
public $ar_namespace = NS_MAIN;
public $ar_title = 'Test';
public $ar_rev_id = null;
} ] ];
} );
$this->setTemporaryHook( 'DeletedContributionsLineEnding', function ( $pager, &$ret, $row ) {
$this->assertSame( 'TESTING', $row->testing );
$ret .= 'FROM_HOOK!';
} );
$pager = $this->getDeletedContribsPager();
$this->assertStringContainsString( 'FROM_HOOK!', $pager->getBody() );
}
public static function provideEmptyResultIntegration() {
$cases = [
[ 'target' => '', 'namespace' => '' ],
[ 'target' => '127.0.0.1', 'namespace' => '' ],
[ 'target' => 'UserWithNoEdits', 'namespace' => 1 ],
];
foreach ( $cases as $case ) {
yield [ $case ];
}
}
/**
* Confirm that the query is valid for various filter options.
*
* @dataProvider provideEmptyResultIntegration
*/
public function testEmptyResultIntegration( $options ) {
$pager = $this->getDeletedContribsPager(
$options['target'],
$options['namespace'],
);
$this->assertIsString( $pager->getBody() );
$this->assertSame( 0, $pager->getNumRows() );
}
public function testPopulatedIntegrationNoPermissions() {
$pager = $this->getDeletedContribsPager( self::$user->getName() );
$this->assertIsString( $pager->getBody() );
$this->assertSame( 1, $pager->getNumRows() );
}
public function testPopulatedIntegrationWithPermissions() {
$this->setGroupPermissions( [ '*' => [
'deletedhistory' => true,
'deletedtext' => true,
'undelete' => true,
] ] );
$pager = $this->getDeletedContribsPager( self::$user->getName() );
$this->assertIsString( $pager->getBody() );
$this->assertSame( 2, $pager->getNumRows() );
$this->assertStringContainsString( '>+9<', $pager->getBody() );
}
public function testParentRevisionSizePreloading() {
$this->setGroupPermissions( [ '*' => [
'deletedhistory' => true,
'deletedtext' => true,
'undelete' => true,
] ] );
$pager = $this->getDeletedContribsPager( self::$user->getName() );
// Make sure the query leaves (at least) one row unselected
// so that we can test loading from parent revision ids
TestingAccessWrapper::newFromObject( $pager )->deletedOnly = true;
$this->assertIsString( $pager->getBody() );
$this->assertSame( 1, $pager->getNumRows() );
$this->assertStringContainsString( '>+9<', $pager->getBody() );
}
public function addDBDataOnce() {
self::$user = $this->getTestUser()->getUser();
$title = Title::makeTitle( NS_MAIN, 'DeletedContribsPagerTest' );
// Make two edits (one will be revdel'd)
$this->editPage( $title, 'Test', '', NS_MAIN, self::$user );
$status = $this->editPage( $title, 'Test content.', '', NS_MAIN, self::$user );
// Delete the page where the edits were made
$page = $this->getServiceContainer()->getWikiPageFactory()->newFromTitle( $title );
$this->deletePage( $page );
// Suppress the second edit
$this->getDb()->newUpdateQueryBuilder()
->update( 'archive' )
->set( [
'ar_deleted' => RevisionRecord::DELETED_USER | RevisionRecord::DELETED_TEXT,
// This is to ensure the minor edits path doesn't encounter an error
'ar_minor_edit' => 1,
] )
->where( [
'ar_rev_id' => $status->getNewRevision()->getId()
] )
->execute();
}
}
|