Localisation updates from https://translatewiki.net.
[mediawiki.git] / tests / phpunit / includes / pager / ContributionsPagerTest.php
blob673c37b023b6e3d581e255540034072b4a1b88d0
1 <?php
3 use MediaWiki\Cache\LinkBatchFactory;
4 use MediaWiki\CommentFormatter\CommentFormatter;
5 use MediaWiki\Context\RequestContext;
6 use MediaWiki\HookContainer\HookContainer;
7 use MediaWiki\Linker\LinkRenderer;
8 use MediaWiki\Pager\ContributionsPager;
9 use MediaWiki\Revision\RevisionStore;
10 use MediaWiki\Title\NamespaceInfo;
11 use MediaWiki\Title\Title;
12 use MediaWiki\User\User;
13 use MediaWiki\User\UserFactory;
14 use MediaWiki\User\UserIdentity;
16 /**
17 * Test class for ContributionsPagerTest handling of revision and archive modes.
19 * @group Pager
20 * @group Database
21 * @covers \MediaWiki\Pager\ContributionsPager
23 class ContributionsPagerTest extends MediaWikiIntegrationTestCase {
25 private static User $user;
27 private function getPagerForTryCreatingRevisionRecord( $isArchive = false ) {
28 $revisionStore = $this->createMock( RevisionStore::class );
29 $revisionStore->method( 'isRevisionRow' )
30 ->willReturn( true );
31 $revisionStore->expects( $this->never() )
32 ->method( $isArchive ? 'newRevisionFromRow' : 'newRevisionFromArchiveRow' );
34 return $this->getMockForAbstractClass(
35 ContributionsPager::class,
37 $this->createMock( LinkRenderer::class ),
38 $this->createMock( LinkBatchFactory::class ),
39 $this->createMock( HookContainer::class ),
40 $this->createMock( RevisionStore::class ),
41 $this->createMock( NamespaceInfo::class ),
42 $this->createMock( CommentFormatter::class ),
43 $this->createMock( UserFactory::class ),
44 new RequestContext(),
46 'isArchive' => $isArchive,
48 $this->createMock( UserIdentity::class ),
53 /**
54 * @dataProvider provideIsArchive
56 public function testTryCreatingRevisionRecord( $isArchive, $row ) {
57 $pager = $this->getPagerForTryCreatingRevisionRecord( $isArchive );
58 $pager->tryCreatingRevisionRecord( $row );
61 /**
62 * @dataProvider provideIsArchive
64 public function testCreateRevisionRecord( $isArchive, $row ) {
65 $pager = $this->getPagerForTryCreatingRevisionRecord( $isArchive );
66 $pager->createRevisionRecord( $row );
69 public function provideIsArchive() {
70 return [
71 'Get revisions from the revision table' => [ false, [ 'rev_id' => 6789 ] ],
72 'Get revisions from the archive table' => [ true, [ 'ar_rev_id' => 9876 ] ],
76 private function getPager( $context, $target ) {
77 $services = $this->getServiceContainer();
79 // Define a pager in 'archive' mode.
80 $pager = new class(
81 $services->getLinkRenderer(),
82 $services->getLinkBatchFactory(),
83 $services->getHookContainer(),
84 $services->getRevisionStore(),
85 $services->getNamespaceInfo(),
86 $services->getCommentFormatter(),
87 $services->getUserFactory(),
88 $context,
90 'isArchive' => true,
91 'target' => $target,
92 // The topOnly filter should be ignored and not throw an error: T371495
93 'topOnly' => true,
95 $this->createMock( UserIdentity::class ),
96 ) extends ContributionsPager {
97 protected string $revisionIdField = 'ar_rev_id';
98 protected string $revisionParentIdField = 'ar_parent_id';
99 protected string $revisionTimestampField = 'ar_timestamp';
100 protected string $revisionLengthField = 'ar_len';
101 protected string $revisionDeletedField = 'ar_deleted';
102 protected string $revisionMinorField = 'ar_minor_edit';
103 protected string $userNameField = 'ar_user_text';
104 protected string $pageNamespaceField = 'ar_namespace';
105 protected string $pageTitleField = 'ar_title';
107 protected function getRevisionQuery() {
108 $revQuery = $this->revisionStore->newArchiveSelectQueryBuilder( $this->getDatabase() )
109 ->joinComment()
110 ->where( [ 'actor_name' => $this->target ] )
111 ->andWhere( $this->getNamespaceCond() )
112 ->getQueryInfo( 'joins' );
113 return [
114 'tables' => $revQuery['tables'],
115 'fields' => $revQuery['fields'],
116 'conds' => [],
117 'options' => [],
118 'join_conds' => $revQuery['joins'],
122 public function getIndexField() {
123 return 'ar_timestamp';
127 return $pager;
131 * Test the pager in 'archive' mode. This involves making a new class, since no
132 * concrete subclass in MediaWiki core currently uses this mode.
134 * In the future, SpecialDeletedContributions could use a subclass of ContributionsPager
135 * instead of DeletedContribsPager. In that case, this test can be moved to the tests
136 * for that class.
138 public function testFormatRow() {
139 $context = new RequestContext();
140 $context->setLanguage( 'qqx' );
142 $pager = $this->getPager( $context, self::$user->getName() );
144 // Perform assertions
145 $this->assertSame( 1, $pager->getNumRows() );
147 $html = $pager->getBody();
148 $this->assertStringContainsString( 'deletionlog', $html );
149 $this->assertStringContainsString( 'undeleteviewlink', $html );
151 // The performing user does not have the right to undelete
152 $this->assertStringNotContainsString( 'mw-changeslist-date', $html );
155 public function testFormatRowDateLinks() {
156 $context = new RequestContext();
157 $context->setUser( $this->getTestSysop()->getUser() );
159 $pager = $this->getPager( $context, self::$user->getName() );
161 $html = $pager->getBody();
162 $this->assertStringContainsString( 'mw-changeslist-date', $html );
165 public function addDbDataOnce() {
166 // Set up data
167 self::$user = $this->getTestUser()->getUser();
168 $this->editPage(
169 'Test page for deletion', 'Test Content', 'test', NS_MAIN, self::$user
171 $title = Title::newFromText( 'Test page for deletion' );
172 $page = $this->getServiceContainer()->getWikiPageFactory()->newFromTitle( $title );
173 $this->deletePage( $page );