Debian Codegraph @

extensions/Linter/tests/phpunit/LintUpdateTest.php

raw symbols SCIP
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 */
20
21 namespace MediaWiki\Linter\Test;
22
23 use MediaWiki\Content\JavaScriptContent;
24 use MediaWiki\Content\JavaScriptContentHandler;
25 use MediaWiki\Content\WikitextContent;
26 use MediaWiki\Content\WikitextContentHandler;
27 use MediaWiki\Linter\LintUpdate;
28 use MediaWiki\MainConfigNames;
29 use MediaWiki\Parser\ParserOutput;
30 use MediaWiki\Revision\MutableRevisionRecord;
31 use MediaWiki\Revision\RenderedRevision;
32 use MediaWiki\Revision\RevisionRecord;
33 use MediaWiki\Revision\SlotRecord;
34 use MediaWikiIntegrationTestCase;
35 use RefreshLinksJob;
36 use WikiPage;
37
38 /**
39 * @group Database
40 * @covers \MediaWiki\Linter\LintUpdate
41 */
42 class LintUpdateTest extends MediaWikiIntegrationTestCase {
43
44 protected function setUp(): void {
45 parent::setUp();
46
47 $this->overrideConfigValues( [
48 MainConfigNames::ParsoidSettings => [
49 'linting' => true
50 ],
51 'LinterParseOnDerivedDataUpdate' => true,
52 // Ensure that parser cache contents don't
53 // affect tests.
54 'ParserCacheType' => CACHE_NONE,
55 ] );
56 }
57
58 /**
59 * Assert that we trigger a parse, and that parse triggers onParserLogLinterData,
60 * and in turn triggers the ParserLogLinterData hook via Parsoid.
61 */
62 public function testUpdate() {
63 // NOTE: This performs an edit, so do it before installing the temp hook below!
64 $rrev = $this->newRenderedRevision();
65
66 $hookCalled = false;
67 $this->setTemporaryHook( 'ParserLogLinterData', static function () use ( &$hookCalled ) {
68 $hookCalled = true;
69 }, false );
70
71 $update = $this->newLintUpdate( $rrev );
72 $update->doUpdate();
73
74 $this->assertTrue( $hookCalled );
75 }
76
77 /**
78 * Assert that we don't parse if the content model is not supported.
79 */
80 public function testSkipModel() {
81 $contentHandler = $this->createNoOpMock(
82 JavaScriptContentHandler::class,
83 [ 'getParserOutput' ]
84 );
85 $contentHandler->expects( $this->never() )->method( 'getParserOutput' );
86 $contentHandlers = $this->getConfVar( MainConfigNames::ContentHandlers );
87 $this->overrideConfigValue( MainConfigNames::ContentHandlers, [
88 CONTENT_MODEL_JAVASCRIPT => [
89 'factory' => fn () => $contentHandler,
90 ],
91 ] + $contentHandlers );
92
93 $page = $this->getExistingTestPage();
94 $rev = new MutableRevisionRecord( $page );
95 $rev->setSlot(
96 SlotRecord::newUnsaved(
97 SlotRecord::MAIN,
98 new JavascriptContent( '{}' )
99 )
100 );
101 // Clear the local cache in the ParserOutputAccess
102 $this->resetServices();
103
104 $update = $this->newLintUpdate( $this->newRenderedRevision( $page, $rev ) );
105 $update->doUpdate();
106 }
107
108 /**
109 * Assert that we don't parse if the given revision is no longer the
110 * latest revision.
111 */
112 public function testSkipOld() {
113 // This may use the "real" wikitext content handler
114 $page = $this->getExistingTestPage();
115 $rev = new MutableRevisionRecord( $page );
116 $rev->setSlot(
117 SlotRecord::newUnsaved(
118 SlotRecord::MAIN,
119 new WikitextContent( 'bla bla' )
120 )
121 );
122
123 // make it not the current revision
124 $rev->setId( $page->getLatest() - 1 );
125 $newRev = $this->newRenderedRevision( $page, $rev );
126
127 // Ok, now set up a mock content handler for the remainder
128 $contentHandler = $this->createNoOpMock(
129 WikitextContentHandler::class,
130 [ 'getParserOutput' ]
131 );
132 $contentHandler->expects( $this->never() )->method( 'getParserOutput' );
133 $contentHandlers = $this->getConfVar( MainConfigNames::ContentHandlers );
134 $this->overrideConfigValue( MainConfigNames::ContentHandlers, [
135 CONTENT_MODEL_WIKITEXT => [
136 'factory' => fn () => $contentHandler,
137 ],
138 ] + $contentHandlers );
139
140 // Clear the local cache in the ParserOutputAccess
141 $this->resetServices();
142
143 $update = $this->newLintUpdate( $newRev );
144 $update->doUpdate();
145 }
146
147 /**
148 * Assert that a LintUpdate is triggered on edit through the RevisionDataUpdates
149 * hook, and in turn triggers the ParserLogLinterData hook via Parsoid.
150 *
151 * @covers \MediaWiki\Linter\Hooks::onRevisionDataUpdates
152 */
153 public function testPageEditIntegration() {
154 // Clear the local cache in the ParserOutputAccess
155 $this->resetServices();
156
157 $hookCalled = 0;
158 $this->setTemporaryHook( 'ParserLogLinterData', static function () use ( &$hookCalled ) {
159 $hookCalled++;
160 }, false );
161
162 $status = $this->editPage( 'JustSomePage', new WikitextContent( 'hello world' ) );
163
164 $this->assertSame( 1, $hookCalled );
165
166 $page = $this->getServiceContainer()->getWikiPageFactory()
167 ->newFromTitle( $status->getNewRevision()->getPage() );
168 }
169
170 /**
171 * Assert that a LintUpdate is triggered from RefreshLinksJob through the
172 * RevisionDataUpdates hook, and in turn triggers the ParserLogLinterData
173 * hook via Parsoid.
174 *
175 * @covers \MediaWiki\Linter\Hooks::onRevisionDataUpdates
176 */
177 public function testRefreshLinksJobIntegration() {
178 // NOTE: This performs an edit, so do it before installing the temp hook below!
179 $page = $this->getExistingTestPage();
180 // Clear the local cache in the ParserOutputAccess
181 $this->resetServices();
182
183 $hookCalled = 0;
184 $this->setTemporaryHook( 'ParserLogLinterData', static function () use ( &$hookCalled ) {
185 $hookCalled++;
186 }, false );
187
188 $job = new RefreshLinksJob( $page, [] );
189 $job->run();
190 $this->assertSame( 1, $hookCalled );
191 }
192
193 private function newRenderedRevision( ?WikiPage $page = null, ?RevisionRecord $rev = null ) {
194 $page = $this->getExistingTestPage();
195 $title = $page->getTitle();
196
197 $rev ??= $this->getServiceContainer()->getRevisionLookup()->getRevisionByTitle( $title );
198 $pOpt = $page->makeParserOptions( 'canonical' );
199
200 $rrev = new RenderedRevision(
201 $rev,
202 $pOpt,
203 $this->getServiceContainer()->getContentRenderer(),
204 static function () {
205 }
206 );
207
208 $rrev->setRevisionParserOutput( new ParserOutput( 'testing' ) );
209 // Clear the local cache in the ParserOutputAccess
210 $this->resetServices();
211
212 return $rrev;
213 }
214
215 private function newLintUpdate( RenderedRevision $renderedRevision ) {
216 $wikiPageFactory = $this->getServiceContainer()->getWikiPageFactory();
217 $parserOutputAccess = $this->getServiceContainer()->getParserOutputAccess();
218
219 return new LintUpdate(
220 $wikiPageFactory,
221 $parserOutputAccess,
222 $renderedRevision
223 );
224 }
225
226 }