| 1 |
<?php |
| 2 |
|
| 3 |
use MediaWiki\CommentStore\CommentStoreComment; |
| 4 |
use MediaWiki\Content\WikitextContent; |
| 5 |
use MediaWiki\Logger\Spi as LoggerSpi; |
| 6 |
use MediaWiki\Parser\ParserOptions; |
| 7 |
use MediaWiki\PoolCounter\PoolWorkArticleView; |
| 8 |
use MediaWiki\Revision\MutableRevisionRecord; |
| 9 |
use MediaWiki\Revision\RevisionRecord; |
| 10 |
use MediaWiki\Revision\SlotRecord; |
| 11 |
use MediaWiki\Status\Status; |
| 12 |
use Psr\Log\LoggerInterface; |
| 13 |
use Psr\Log\NullLogger; |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
class PoolWorkArticleViewTest extends MediaWikiIntegrationTestCase { |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
protected function getLoggerSpi( $logger = null ) { |
| 27 |
$spi = $this->createNoOpMock( LoggerSpi::class, [ 'getLogger' ] ); |
| 28 |
$spi->method( 'getLogger' )->willReturn( $logger ?? new NullLogger() ); |
| 29 |
return $spi; |
| 30 |
} |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
protected function newPoolWorkArticleView( |
| 40 |
WikiPage $page, |
| 41 |
?RevisionRecord $rev = null, |
| 42 |
$options = null |
| 43 |
) { |
| 44 |
if ( !$options ) { |
| 45 |
$options = ParserOptions::newFromAnon(); |
| 46 |
} |
| 47 |
|
| 48 |
if ( !$rev ) { |
| 49 |
$rev = $page->getRevisionRecord(); |
| 50 |
} |
| 51 |
|
| 52 |
$revisionRenderer = $this->getServiceContainer()->getRevisionRenderer(); |
| 53 |
|
| 54 |
return new PoolWorkArticleView( |
| 55 |
'test:' . $rev->getId(), |
| 56 |
$rev, |
| 57 |
$options, |
| 58 |
$revisionRenderer, |
| 59 |
$this->getLoggerSpi() |
| 60 |
); |
| 61 |
} |
| 62 |
|
| 63 |
private function makeRevision( WikiPage $page, $text ) { |
| 64 |
$user = $this->getTestUser()->getUser(); |
| 65 |
$revision = $page->newPageUpdater( $user ) |
| 66 |
->setContent( SlotRecord::MAIN, new WikitextContent( $text ) ) |
| 67 |
->saveRevision( CommentStoreComment::newUnsavedComment( 'testing' ) ); |
| 68 |
|
| 69 |
return $revision; |
| 70 |
} |
| 71 |
|
| 72 |
public function testDoWorkLoadRevision() { |
| 73 |
$options = ParserOptions::newFromAnon(); |
| 74 |
$page = $this->getExistingTestPage( __METHOD__ ); |
| 75 |
$rev1 = $this->makeRevision( $page, 'First!' ); |
| 76 |
$rev2 = $this->makeRevision( $page, 'Second!' ); |
| 77 |
|
| 78 |
$work = $this->newPoolWorkArticleView( $page, $rev1, $options ); |
| 79 |
|
| 80 |
$status = $work->execute(); |
| 81 |
$this->assertStringContainsString( 'First', $status->getValue()->getText() ); |
| 82 |
|
| 83 |
$work = $this->newPoolWorkArticleView( $page, $rev2, $options ); |
| 84 |
|
| 85 |
$status = $work->execute(); |
| 86 |
$this->assertStringContainsString( 'Second', $status->getValue()->getText() ); |
| 87 |
} |
| 88 |
|
| 89 |
public function testDoWorkParserCache() { |
| 90 |
$options = ParserOptions::newFromAnon(); |
| 91 |
$page = $this->getExistingTestPage( __METHOD__ ); |
| 92 |
$rev1 = $this->makeRevision( $page, 'First!' ); |
| 93 |
|
| 94 |
$work = $this->newPoolWorkArticleView( $page, $rev1, $options ); |
| 95 |
$work->execute(); |
| 96 |
|
| 97 |
$cache = $this->getServiceContainer()->getParserCache(); |
| 98 |
$out = $cache->get( $page, $options ); |
| 99 |
|
| 100 |
$this->assertNotNull( $out ); |
| 101 |
$this->assertNotFalse( $out ); |
| 102 |
$this->assertStringContainsString( 'First', $out->getRawText() ); |
| 103 |
} |
| 104 |
|
| 105 |
public function testDoWorkWithFakeRevision() { |
| 106 |
$options = ParserOptions::newFromAnon(); |
| 107 |
$page = $this->getExistingTestPage( __METHOD__ ); |
| 108 |
$rev = $this->makeRevision( $page, 'NOPE' ); |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
$fakeRev = new MutableRevisionRecord( $page->getTitle() ); |
| 113 |
$fakeRev->setContent( SlotRecord::MAIN, new WikitextContent( 'YES!' ) ); |
| 114 |
|
| 115 |
$work = $this->newPoolWorkArticleView( $page, $fakeRev, $options ); |
| 116 |
|
| 117 |
$status = $work->execute(); |
| 118 |
|
| 119 |
$text = $status->getValue()->getText(); |
| 120 |
$this->assertStringContainsString( 'YES!', $text ); |
| 121 |
$this->assertStringNotContainsString( 'NOPE', $text ); |
| 122 |
} |
| 123 |
|
| 124 |
public static function provideMagicWords() { |
| 125 |
yield 'PAGEID' => [ |
| 126 |
'Test {{PAGEID}} Test', |
| 127 |
static function ( RevisionRecord $rev ) { |
| 128 |
return $rev->getPageId(); |
| 129 |
} |
| 130 |
]; |
| 131 |
yield 'REVISIONID' => [ |
| 132 |
'Test {{REVISIONID}} Test', |
| 133 |
static function ( RevisionRecord $rev ) { |
| 134 |
return $rev->getId(); |
| 135 |
} |
| 136 |
]; |
| 137 |
yield 'REVISIONUSER' => [ |
| 138 |
'Test {{REVISIONUSER}} Test', |
| 139 |
static function ( RevisionRecord $rev ) { |
| 140 |
return $rev->getUser()->getName(); |
| 141 |
} |
| 142 |
]; |
| 143 |
yield 'REVISIONTIMESTAMP' => [ |
| 144 |
'Test {{REVISIONTIMESTAMP}} Test', |
| 145 |
static function ( RevisionRecord $rev ) { |
| 146 |
return $rev->getTimestamp(); |
| 147 |
} |
| 148 |
]; |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
public function testMagicWords( $wikitext, $callback ) { |
| 155 |
static $counter = 1; |
| 156 |
|
| 157 |
$options = ParserOptions::newFromAnon(); |
| 158 |
$page = $this->getNonexistingTestPage( __METHOD__ . $counter++ ); |
| 159 |
$this->editPage( $page, $wikitext ); |
| 160 |
$rev = $page->getRevisionRecord(); |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
$work = $this->newPoolWorkArticleView( |
| 166 |
$page, |
| 167 |
$page->getRevisionRecord(), |
| 168 |
$options, |
| 169 |
false |
| 170 |
); |
| 171 |
|
| 172 |
$status = $work->execute(); |
| 173 |
|
| 174 |
$expected = strval( $callback( $rev ) ); |
| 175 |
$output = $status->getValue(); |
| 176 |
|
| 177 |
$this->assertStringContainsString( $expected, $output->getText() ); |
| 178 |
} |
| 179 |
|
| 180 |
public function testDoWorkDeletedContent() { |
| 181 |
$options = ParserOptions::newFromAnon(); |
| 182 |
$page = $this->getExistingTestPage( __METHOD__ ); |
| 183 |
$rev1 = $page->getRevisionRecord(); |
| 184 |
|
| 185 |
|
| 186 |
$rev2 = $this->makeRevision( $page, 'Next' ); |
| 187 |
|
| 188 |
|
| 189 |
$fakeRev = new MutableRevisionRecord( $page->getTitle() ); |
| 190 |
$fakeRev->setId( $rev1->getId() ); |
| 191 |
$fakeRev->setPageId( $page->getId() ); |
| 192 |
$fakeRev->setContent( SlotRecord::MAIN, new WikitextContent( 'SECRET' ) ); |
| 193 |
$fakeRev->setVisibility( RevisionRecord::DELETED_TEXT ); |
| 194 |
|
| 195 |
|
| 196 |
$work = $this->newPoolWorkArticleView( $page, $fakeRev, $options ); |
| 197 |
|
| 198 |
$status = $work->execute(); |
| 199 |
$this->assertStatusGood( $status ); |
| 200 |
} |
| 201 |
|
| 202 |
} |