| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 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 |
|
| 40 |
|
| 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 |
|
| 53 |
|
| 54 |
'ParserCacheType' => CACHE_NONE, |
| 55 |
] ); |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
public function testUpdate() { |
| 63 |
|
| 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 |
|
| 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 |
|
| 102 |
$this->resetServices(); |
| 103 |
|
| 104 |
$update = $this->newLintUpdate( $this->newRenderedRevision( $page, $rev ) ); |
| 105 |
$update->doUpdate(); |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
public function testSkipOld() { |
| 113 |
|
| 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 |
|
| 124 |
$rev->setId( $page->getLatest() - 1 ); |
| 125 |
$newRev = $this->newRenderedRevision( $page, $rev ); |
| 126 |
|
| 127 |
|
| 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 |
|
| 141 |
$this->resetServices(); |
| 142 |
|
| 143 |
$update = $this->newLintUpdate( $newRev ); |
| 144 |
$update->doUpdate(); |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
public function testPageEditIntegration() { |
| 154 |
|
| 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 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
public function testRefreshLinksJobIntegration() { |
| 178 |
|
| 179 |
$page = $this->getExistingTestPage(); |
| 180 |
|
| 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 |
|
| 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 |
} |