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 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
|
<?php
/**
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
*
* @file
*/
namespace MediaWiki\Linter\Test;
use MediaWiki\Content\JavaScriptContent;
use MediaWiki\Content\JavaScriptContentHandler;
use MediaWiki\Content\WikitextContent;
use MediaWiki\Content\WikitextContentHandler;
use MediaWiki\Linter\LintUpdate;
use MediaWiki\MainConfigNames;
use MediaWiki\Parser\ParserOutput;
use MediaWiki\Revision\MutableRevisionRecord;
use MediaWiki\Revision\RenderedRevision;
use MediaWiki\Revision\RevisionRecord;
use MediaWiki\Revision\SlotRecord;
use MediaWikiIntegrationTestCase;
use RefreshLinksJob;
use WikiPage;
/**
* @group Database
* @covers \MediaWiki\Linter\LintUpdate
*/
class LintUpdateTest extends MediaWikiIntegrationTestCase {
protected function setUp(): void {
parent::setUp();
$this->overrideConfigValues( [
MainConfigNames::ParsoidSettings => [
'linting' => true
],
'LinterParseOnDerivedDataUpdate' => true,
// Ensure that parser cache contents don't
// affect tests.
'ParserCacheType' => CACHE_NONE,
] );
}
/**
* Assert that we trigger a parse, and that parse triggers onParserLogLinterData,
* and in turn triggers the ParserLogLinterData hook via Parsoid.
*/
public function testUpdate() {
// NOTE: This performs an edit, so do it before installing the temp hook below!
$rrev = $this->newRenderedRevision();
$hookCalled = false;
$this->setTemporaryHook( 'ParserLogLinterData', static function () use ( &$hookCalled ) {
$hookCalled = true;
}, false );
$update = $this->newLintUpdate( $rrev );
$update->doUpdate();
$this->assertTrue( $hookCalled );
}
/**
* Assert that we don't parse if the content model is not supported.
*/
public function testSkipModel() {
$contentHandler = $this->createNoOpMock(
JavaScriptContentHandler::class,
[ 'getParserOutput' ]
);
$contentHandler->expects( $this->never() )->method( 'getParserOutput' );
$contentHandlers = $this->getConfVar( MainConfigNames::ContentHandlers );
$this->overrideConfigValue( MainConfigNames::ContentHandlers, [
CONTENT_MODEL_JAVASCRIPT => [
'factory' => fn () => $contentHandler,
],
] + $contentHandlers );
$page = $this->getExistingTestPage();
$rev = new MutableRevisionRecord( $page );
$rev->setSlot(
SlotRecord::newUnsaved(
SlotRecord::MAIN,
new JavascriptContent( '{}' )
)
);
// Clear the local cache in the ParserOutputAccess
$this->resetServices();
$update = $this->newLintUpdate( $this->newRenderedRevision( $page, $rev ) );
$update->doUpdate();
}
/**
* Assert that we don't parse if the given revision is no longer the
* latest revision.
*/
public function testSkipOld() {
// This may use the "real" wikitext content handler
$page = $this->getExistingTestPage();
$rev = new MutableRevisionRecord( $page );
$rev->setSlot(
SlotRecord::newUnsaved(
SlotRecord::MAIN,
new WikitextContent( 'bla bla' )
)
);
// make it not the current revision
$rev->setId( $page->getLatest() - 1 );
$newRev = $this->newRenderedRevision( $page, $rev );
// Ok, now set up a mock content handler for the remainder
$contentHandler = $this->createNoOpMock(
WikitextContentHandler::class,
[ 'getParserOutput' ]
);
$contentHandler->expects( $this->never() )->method( 'getParserOutput' );
$contentHandlers = $this->getConfVar( MainConfigNames::ContentHandlers );
$this->overrideConfigValue( MainConfigNames::ContentHandlers, [
CONTENT_MODEL_WIKITEXT => [
'factory' => fn () => $contentHandler,
],
] + $contentHandlers );
// Clear the local cache in the ParserOutputAccess
$this->resetServices();
$update = $this->newLintUpdate( $newRev );
$update->doUpdate();
}
/**
* Assert that a LintUpdate is triggered on edit through the RevisionDataUpdates
* hook, and in turn triggers the ParserLogLinterData hook via Parsoid.
*
* @covers \MediaWiki\Linter\Hooks::onRevisionDataUpdates
*/
public function testPageEditIntegration() {
// Clear the local cache in the ParserOutputAccess
$this->resetServices();
$hookCalled = 0;
$this->setTemporaryHook( 'ParserLogLinterData', static function () use ( &$hookCalled ) {
$hookCalled++;
}, false );
$status = $this->editPage( 'JustSomePage', new WikitextContent( 'hello world' ) );
$this->assertSame( 1, $hookCalled );
$page = $this->getServiceContainer()->getWikiPageFactory()
->newFromTitle( $status->getNewRevision()->getPage() );
}
/**
* Assert that a LintUpdate is triggered from RefreshLinksJob through the
* RevisionDataUpdates hook, and in turn triggers the ParserLogLinterData
* hook via Parsoid.
*
* @covers \MediaWiki\Linter\Hooks::onRevisionDataUpdates
*/
public function testRefreshLinksJobIntegration() {
// NOTE: This performs an edit, so do it before installing the temp hook below!
$page = $this->getExistingTestPage();
// Clear the local cache in the ParserOutputAccess
$this->resetServices();
$hookCalled = 0;
$this->setTemporaryHook( 'ParserLogLinterData', static function () use ( &$hookCalled ) {
$hookCalled++;
}, false );
$job = new RefreshLinksJob( $page, [] );
$job->run();
$this->assertSame( 1, $hookCalled );
}
private function newRenderedRevision( ?WikiPage $page = null, ?RevisionRecord $rev = null ) {
$page = $this->getExistingTestPage();
$title = $page->getTitle();
$rev ??= $this->getServiceContainer()->getRevisionLookup()->getRevisionByTitle( $title );
$pOpt = $page->makeParserOptions( 'canonical' );
$rrev = new RenderedRevision(
$rev,
$pOpt,
$this->getServiceContainer()->getContentRenderer(),
static function () {
}
);
$rrev->setRevisionParserOutput( new ParserOutput( 'testing' ) );
// Clear the local cache in the ParserOutputAccess
$this->resetServices();
return $rrev;
}
private function newLintUpdate( RenderedRevision $renderedRevision ) {
$wikiPageFactory = $this->getServiceContainer()->getWikiPageFactory();
$parserOutputAccess = $this->getServiceContainer()->getParserOutputAccess();
return new LintUpdate(
$wikiPageFactory,
$parserOutputAccess,
$renderedRevision
);
}
}
|