wiki.techinc.nl/tests/phpunit/includes/poolcounter/PoolWorkArticleViewOldTest.php
Brian Wolff 024915bbcd Separate RevisionOutputCache::makeParserOutputKey from no revid case
If the revision id is null, the item should not be cachable.
However, you still need a poolcounter work key for such cases,
and this method was used for both. This is confusing and seems
dangerous, so split into two methods, one where a revision id
is required, and another one where it is optional that should
only be used when it is not for a cache key.

This also fixes a warning about null revId on php 8.1 in tests.

Bug: T313663
Change-Id: Id685caeecf21d058bfd8446d9b5e21f0f11e0177
2022-09-09 18:24:27 -07:00

110 lines
2.7 KiB
PHP

<?php
use MediaWiki\Json\JsonCodec;
use MediaWiki\Parser\RevisionOutputCache;
use MediaWiki\Revision\RevisionRecord;
use Psr\Log\NullLogger;
/**
* @covers PoolWorkArticleViewOld
* @group Database
*/
class PoolWorkArticleViewOldTest extends PoolWorkArticleViewTest {
/** @var RevisionOutputCache */
private $cache = null;
/**
* @param WikiPage $page
* @param RevisionRecord|null $rev
* @param ParserOptions|null $options
*
* @return PoolWorkArticleViewOld
*/
protected function newPoolWorkArticleView(
WikiPage $page,
RevisionRecord $rev = null,
$options = null
) {
if ( !$options ) {
$options = ParserOptions::newFromAnon();
}
if ( !$rev ) {
$rev = $page->getRevisionRecord();
}
if ( !$this->cache ) {
$this->installRevisionOutputCache();
}
$renderer = $this->getServiceContainer()->getRevisionRenderer();
return new PoolWorkArticleViewOld(
'test:' . $rev->getId(),
$this->cache,
$rev,
$options,
$renderer,
$this->getLoggerSpi()
);
}
/**
* @param BagOStuff|null $bag
*
* @return RevisionOutputCache
*/
private function installRevisionOutputCache( $bag = null ) {
$this->cache = new RevisionOutputCache(
'test',
new WANObjectCache( [ 'cache' => $bag ?: new HashBagOStuff() ] ),
60 * 60,
'20200101223344',
new JsonCodec(),
new NullStatsdDataFactory(),
new NullLogger()
);
return $this->cache;
}
public function testUpdateCachedOutput() {
$options = ParserOptions::newFromAnon();
$page = $this->getExistingTestPage( __METHOD__ );
$cache = $this->installRevisionOutputCache();
$work = $this->newPoolWorkArticleView( $page, null, $options );
/** @var Status $status */
$status = $work->execute();
$this->assertTrue( $status->isGood() );
$cachedOutput = $cache->get( $page->getRevisionRecord(), $options );
$this->assertNotEmpty( $cachedOutput );
$this->assertSame( $status->getValue()->getText(), $cachedOutput->getText() );
}
public function testDoesNotCacheNotSafe() {
$page = $this->getExistingTestPage( __METHOD__ );
$cache = $this->installRevisionOutputCache();
$parserOptions = ParserOptions::newFromAnon();
$parserOptions->setWrapOutputClass( 'wrapwrap' ); // Not safe to cache!
$work = $this->newPoolWorkArticleView( $page, null, $parserOptions );
/** @var Status $status */
$status = $work->execute();
$this->assertTrue( $status->isGood() );
$this->assertFalse( $cache->get( $page->getRevisionRecord(), $parserOptions ) );
}
public function testDoWorkWithFakeRevision() {
// PoolWorkArticleViewOld caches the results, but things with null revid should
// not be cached.
$this->expectException( InvalidArgumentException::class );
parent::testDoWorkWithFakeRevision();
}
}