| 1 |
<?php |
| 2 |
|
| 3 |
use MediaWiki\Json\JsonCodec; |
| 4 |
use MediaWiki\Parser\ParserOptions; |
| 5 |
use MediaWiki\Parser\RevisionOutputCache; |
| 6 |
use MediaWiki\PoolCounter\PoolWorkArticleViewOld; |
| 7 |
use MediaWiki\Revision\RevisionRecord; |
| 8 |
use MediaWiki\Status\Status; |
| 9 |
use Psr\Log\NullLogger; |
| 10 |
use Wikimedia\ObjectCache\BagOStuff; |
| 11 |
use Wikimedia\ObjectCache\HashBagOStuff; |
| 12 |
use Wikimedia\ObjectCache\WANObjectCache; |
| 13 |
use Wikimedia\Stats\StatsFactory; |
| 14 |
use Wikimedia\UUID\GlobalIdGenerator; |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
class PoolWorkArticleViewOldTest extends PoolWorkArticleViewTest { |
| 21 |
|
| 22 |
|
| 23 |
private $cache = null; |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
protected function newPoolWorkArticleView( |
| 33 |
WikiPage $page, |
| 34 |
?RevisionRecord $rev = null, |
| 35 |
$options = null |
| 36 |
) { |
| 37 |
if ( !$options ) { |
| 38 |
$options = ParserOptions::newFromAnon(); |
| 39 |
} |
| 40 |
|
| 41 |
if ( !$rev ) { |
| 42 |
$rev = $page->getRevisionRecord(); |
| 43 |
} |
| 44 |
|
| 45 |
if ( !$this->cache ) { |
| 46 |
$this->installRevisionOutputCache(); |
| 47 |
} |
| 48 |
|
| 49 |
$renderer = $this->getServiceContainer()->getRevisionRenderer(); |
| 50 |
|
| 51 |
return new PoolWorkArticleViewOld( |
| 52 |
'test:' . $rev->getId(), |
| 53 |
$this->cache, |
| 54 |
$rev, |
| 55 |
$options, |
| 56 |
$renderer, |
| 57 |
$this->getLoggerSpi() |
| 58 |
); |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
private function installRevisionOutputCache( $bag = null ) { |
| 67 |
$globalIdGenerator = $this->createMock( GlobalIdGenerator::class ); |
| 68 |
$globalIdGenerator->method( 'newUUIDv1' )->willReturn( 'uuid-uuid' ); |
| 69 |
$this->cache = new RevisionOutputCache( |
| 70 |
'test', |
| 71 |
new WANObjectCache( [ 'cache' => $bag ?: new HashBagOStuff() ] ), |
| 72 |
60 * 60, |
| 73 |
'20200101223344', |
| 74 |
new JsonCodec(), |
| 75 |
StatsFactory::newNull(), |
| 76 |
new NullLogger(), |
| 77 |
$globalIdGenerator |
| 78 |
); |
| 79 |
|
| 80 |
return $this->cache; |
| 81 |
} |
| 82 |
|
| 83 |
public function testUpdateCachedOutput() { |
| 84 |
$options = ParserOptions::newFromAnon(); |
| 85 |
$page = $this->getExistingTestPage( __METHOD__ ); |
| 86 |
|
| 87 |
$cache = $this->installRevisionOutputCache(); |
| 88 |
|
| 89 |
$work = $this->newPoolWorkArticleView( $page, null, $options ); |
| 90 |
|
| 91 |
$status = $work->execute(); |
| 92 |
$this->assertStatusGood( $status ); |
| 93 |
|
| 94 |
$cachedOutput = $cache->get( $page->getRevisionRecord(), $options ); |
| 95 |
$this->assertNotEmpty( $cachedOutput ); |
| 96 |
$this->assertSame( $status->getValue()->getRawText(), |
| 97 |
$cachedOutput->getRawText() ); |
| 98 |
} |
| 99 |
|
| 100 |
public function testDoesNotCacheNotSafe() { |
| 101 |
$page = $this->getExistingTestPage( __METHOD__ ); |
| 102 |
|
| 103 |
$cache = $this->installRevisionOutputCache(); |
| 104 |
|
| 105 |
$parserOptions = ParserOptions::newFromAnon(); |
| 106 |
$parserOptions->setWrapOutputClass( 'wrapwrap' ); |
| 107 |
|
| 108 |
$work = $this->newPoolWorkArticleView( $page, null, $parserOptions ); |
| 109 |
|
| 110 |
$status = $work->execute(); |
| 111 |
$this->assertStatusGood( $status ); |
| 112 |
|
| 113 |
$this->assertFalse( $cache->get( $page->getRevisionRecord(), $parserOptions ) ); |
| 114 |
} |
| 115 |
|
| 116 |
public function testDoWorkWithFakeRevision() { |
| 117 |
|
| 118 |
|
| 119 |
$this->expectException( InvalidArgumentException::class ); |
| 120 |
parent::testDoWorkWithFakeRevision(); |
| 121 |
} |
| 122 |
} |