wiki.techinc.nl/tests/phpunit/unit/includes/parser/ParserCacheFactoryTest.php

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

78 lines
2.1 KiB
PHP
Raw Normal View History

<?php
namespace MediaWiki\Tests\Parser;
use MediaWiki\Config\ServiceOptions;
use MediaWiki\Json\JsonCodec;
use MediaWiki\MainConfigNames;
use MediaWiki\MainConfigSchema;
use MediaWiki\Page\WikiPageFactory;
use MediaWiki\Parser\ParserCacheFactory;
use MediaWiki\Parser\RevisionOutputCache;
use MediaWiki\Title\TitleFactory;
use MediaWikiUnitTestCase;
use ParserCache;
use Psr\Log\NullLogger;
use WANObjectCache;
use Wikimedia\ObjectCache\HashBagOStuff;
use Wikimedia\Stats\StatsFactory;
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
2023-09-14 16:11:20 +00:00
use Wikimedia\UUID\GlobalIdGenerator;
/**
* @covers \MediaWiki\Parser\ParserCacheFactory
*/
class ParserCacheFactoryTest extends MediaWikiUnitTestCase {
/**
* @return ParserCacheFactory
*/
private function newParserCacheFactory() {
$options = new ServiceOptions( ParserCacheFactory::CONSTRUCTOR_OPTIONS, [
MainConfigNames::CacheEpoch => '20200202112233',
MainConfigNames::OldRevisionParserCacheExpireTime => 60,
MainConfigNames::ParserCacheFilterConfig
=> MainConfigSchema::getDefaultValue( MainConfigNames::ParserCacheFilterConfig ),
] );
return new ParserCacheFactory(
new HashBagOStuff(),
new WANObjectCache( [ 'cache' => new HashBagOStuff() ] ),
$this->createHookContainer(),
new JsonCodec(),
StatsFactory::newNull(),
new NullLogger(),
$options,
$this->createNoOpMock( TitleFactory::class ),
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
2023-09-14 16:11:20 +00:00
$this->createNoOpMock( WikiPageFactory::class ),
$this->createNoOpMock( GlobalIdGenerator::class )
);
}
public function testGetParserCache() {
$factory = $this->newParserCacheFactory();
$a = $factory->getParserCache( 'test' );
$this->assertInstanceOf( ParserCache::class, $a );
$b = $factory->getParserCache( 'test' );
$this->assertSame( $a, $b );
$c = $factory->getParserCache( 'xyzzy' );
$this->assertNotSame( $a, $c );
}
public function testGetRevisionOutputCache() {
$factory = $this->newParserCacheFactory();
$a = $factory->getRevisionOutputCache( 'test' );
$this->assertInstanceOf( RevisionOutputCache::class, $a );
$b = $factory->getRevisionOutputCache( 'test' );
$this->assertSame( $a, $b );
$c = $factory->getRevisionOutputCache( 'xyzzy' );
$this->assertNotSame( $a, $c );
}
}