Debian Codegraph @

tests/phpunit/unit/includes/page/UndeletePageTest.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Tests\Unit\Page;
4
5 use Generator;
6 use JobQueueGroup;
7 use MediaWiki\Config\ServiceOptions;
8 use MediaWiki\Content\IContentHandlerFactory;
9 use MediaWiki\Linker\LinkTarget;
10 use MediaWiki\Page\PageIdentity;
11 use MediaWiki\Page\PageIdentityValue;
12 use MediaWiki\Page\ProperPageIdentity;
13 use MediaWiki\Page\UndeletePage;
14 use MediaWiki\Page\WikiPageFactory;
15 use MediaWiki\Permissions\Authority;
16 use MediaWiki\Revision\ArchivedRevisionLookup;
17 use MediaWiki\Revision\RevisionStore;
18 use MediaWiki\Storage\PageUpdaterFactory;
19 use MediaWiki\Title\NamespaceInfo;
20 use MediaWiki\Title\Title;
21 use MediaWikiUnitTestCase;
22 use Psr\Log\NullLogger;
23 use RepoGroup;
24 use Wikimedia\Message\ITextFormatter;
25 use Wikimedia\Rdbms\IConnectionProvider;
26 use Wikimedia\Rdbms\ReadOnlyMode;
27 use WikiPage;
28
29 /**
30 * @coversDefaultClass \MediaWiki\Page\UndeletePage
31 */
32 class UndeletePageTest extends MediaWikiUnitTestCase {
33 /**
34 * @param ProperPageIdentity|null $page
35 * @param WikiPageFactory|null $wpFactory
36 * @param NamespaceInfo|null $namespaceInfo
37 * @param ArchivedRevisionLookup|null $archivedRevisionLookup
38 * @return UndeletePage
39 */
40 private function getUndeletePage(
41 ?ProperPageIdentity $page = null,
42 ?WikiPageFactory $wpFactory = null,
43 ?NamespaceInfo $namespaceInfo = null,
44 ?ArchivedRevisionLookup $archivedRevisionLookup = null
45 ): UndeletePage {
46 return new UndeletePage(
47 $this->createHookContainer(),
48 $this->createMock( JobQueueGroup::class ),
49 $this->createMock( IConnectionProvider::class ),
50 $this->createMock( ReadOnlyMode::class ),
51 $this->createMock( RepoGroup::class ),
52 new NullLogger(),
53 $this->createMock( RevisionStore::class ),
54 $wpFactory ?? $this->createMock( WikiPageFactory::class ),
55 $this->createMock( PageUpdaterFactory::class ),
56 $this->createMock( IContentHandlerFactory::class ),
57 $archivedRevisionLookup ?? $this->createMock( ArchivedRevisionLookup::class ),
58 $namespaceInfo ?? $this->createMock( NamespaceInfo::class ),
59 $this->createMock( ITextFormatter::class ),
60 $page ?? $this->createMock( ProperPageIdentity::class ),
61 $this->createMock( Authority::class )
62 );
63 }
64
65 /**
66 * @param ProperPageIdentity $page
67 * @param WikiPageFactory $wpFactory
68 * @param NamespaceInfo|null $nsInfo
69 * @param ArchivedRevisionLookup $archivedRevisionLookup
70 * @param string|null $expectedMsg
71 * @covers ::canProbablyUndeleteAssociatedTalk
72 * @dataProvider provideAssociatedTalk
73 */
74 public function testCanProbablyUndeleteAssociatedTalk(
75 ProperPageIdentity $page,
76 WikiPageFactory $wpFactory,
77 ?NamespaceInfo $nsInfo,
78 ArchivedRevisionLookup $archivedRevisionLookup,
79 ?string $expectedMsg
80 ): void {
81 $res = $this->getUndeletePage( $page, $wpFactory, $nsInfo, $archivedRevisionLookup )
82 ->canProbablyUndeleteAssociatedTalk();
83 if ( $expectedMsg === null ) {
84 $this->assertStatusGood( $res );
85 } else {
86 $this->assertStatusError( $expectedMsg, $res );
87 }
88 }
89
90 public function provideAssociatedTalk(): Generator {
91 $getWpFactory = function ( bool $talkExists ): WikiPageFactory {
92 $wpFactory = $this->createMock( WikiPageFactory::class );
93 $wpFactory->method( 'newFromTitle' )->willReturnCallback( function ( $t ) {
94 $title = Title::castFromPageReference( $t );
95 $wikiPage = $this->createMock( WikiPage::class );
96 $wikiPage->method( 'getTitle' )->willReturn( $title );
97 return $wikiPage;
98 } );
99 $wpFactory->method( 'newFromLinkTarget' )->willReturnCallback(
100 function ( LinkTarget $t ) use ( $talkExists ) {
101 $existingTalk = $this->createMock( WikiPage::class );
102 $existingTalk->method( 'exists' )->willReturn( $talkExists );
103 return $existingTalk;
104 }
105 );
106 return $wpFactory;
107 };
108 $getArchiveLookup = function ( bool $hasDeletedRevs ): ArchivedRevisionLookup {
109 $ret = $this->createMock( ArchivedRevisionLookup::class );
110 $ret->method( 'hasArchivedRevisions' )->willReturn( $hasDeletedRevs );
111 return $ret;
112 };
113 $nsInfo = new NamespaceInfo( $this->createMock( ServiceOptions::class ), $this->createHookContainer(), [], [] );
114
115 $talkPage = new PageIdentityValue( 42, NS_TALK, 'Test talk page', PageIdentity::LOCAL );
116 yield 'Talk page' => [
117 $talkPage,
118 $getWpFactory( false ),
119 $nsInfo,
120 $getArchiveLookup( false ),
121 'undelete-error-associated-alreadytalk'
122 ];
123
124 $nonTalkPage = new PageIdentityValue( 44, NS_MAIN, 'Test article', PageIdentity::LOCAL );
125
126 yield 'Article whose talk page exists and does not have deleted revisions' => [
127 $nonTalkPage,
128 $getWpFactory( true ),
129 $nsInfo,
130 $getArchiveLookup( false ),
131 'undelete-error-associated-notdeleted'
132 ];
133
134 yield 'Article whose talk page does not exist and does not have deleted revisions' => [
135 $nonTalkPage,
136 $getWpFactory( false ),
137 $nsInfo,
138 $getArchiveLookup( false ),
139 'undelete-error-associated-notdeleted'
140 ];
141
142 yield 'Article whose talk page exists and has deleted revisions' =>
143 [ $nonTalkPage, $getWpFactory( true ), $nsInfo, $getArchiveLookup( true ), null ];
144
145 yield 'Article whose talk page does not exist and has deleted revisions' =>
146 [ $nonTalkPage, $getWpFactory( false ), $nsInfo, $getArchiveLookup( true ), null ];
147 }
148 }