wiki.techinc.nl/tests/phpunit/includes/poolcounter/PoolWorkArticleViewOldTest.php
C. Scott Ananian 0de13d7662 Add ParserOutput::{get,set}RenderId() and set render id in ContentRenderer
Set the render ID for each parse stored into cache so that we are able
to identify a specific parse when there are dependencies (for example
in an edit based on that parse).  This is recorded as a property added
to the ParserOutput, not the parent CacheTime interface.  Even though
the render ID is /related/ to the CacheTime interface, CacheTime is
also used directly as a parser cache key, and the UUID should not be
part of the lookup key.

In general we are trying to move the location where these cache
properties are set as early as possible, so we check at each location
to ensure we don't overwrite a previously-set value.  Eventually we
can convert most of these checks into assertions that the cache
properties have already been set (T350538).  The primary location for
setting cache properties is the ContentRenderer.

Moved setting the revision timestamp into ContentRenderer as well, as
it was set along the same code paths.  An extra parameter was added to
ContentRenderer::getParserOutput() to support this.

Added merge code to ParserOutput::mergeInternalMetaDataFrom() which
should ensure that cache time, revision, timestamp, and render id are
all set properly when multiple slots are combined together in MCR.

In order to ensure the render ID is set on all codepaths we needed to
plumb the GlobalIdGenerator service into ContentRenderer, ParserCache,
ParserCacheFactory, and RevisionOutputCache.  Eventually (T350538) it
should only be necessary in the ContentRenderer.

Bug: T350538
Bug: T349868
Followup-To: Ic9b7cc0fcf365e772b7d080d76a065e3fd585f80
Change-Id: I72c5e6f86b7f081ab5ce7a56f5365d2f75067a78
2024-02-07 21:22:06 -05:00

116 lines
3 KiB
PHP

<?php
use MediaWiki\Json\JsonCodec;
use MediaWiki\Parser\RevisionOutputCache;
use MediaWiki\PoolCounter\PoolWorkArticleViewOld;
use MediaWiki\Revision\RevisionRecord;
use MediaWiki\Status\Status;
use Psr\Log\NullLogger;
use Wikimedia\UUID\GlobalIdGenerator;
/**
* @covers \MediaWiki\PoolCounter\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 ) {
$globalIdGenerator = $this->createMock( GlobalIdGenerator::class );
$globalIdGenerator->method( 'newUUIDv1' )->willReturn( 'uuid-uuid' );
$this->cache = new RevisionOutputCache(
'test',
new WANObjectCache( [ 'cache' => $bag ?: new HashBagOStuff() ] ),
60 * 60,
'20200101223344',
new JsonCodec(),
new NullStatsdDataFactory(),
new NullLogger(),
$globalIdGenerator
);
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->assertStatusGood( $status );
$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->assertStatusGood( $status );
$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();
}
}