2020-09-27 17:04:26 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace MediaWiki\Tests\Parser;
|
|
|
|
|
|
|
|
|
|
use BagOStuff;
|
2022-05-09 15:51:35 +00:00
|
|
|
use CacheTime;
|
2020-09-27 17:04:26 +00:00
|
|
|
use EmptyBagOStuff;
|
|
|
|
|
use HashBagOStuff;
|
2020-10-23 02:52:57 +00:00
|
|
|
use InvalidArgumentException;
|
2020-09-27 17:04:26 +00:00
|
|
|
use MediaWiki\HookContainer\HookContainer;
|
2020-11-18 00:26:53 +00:00
|
|
|
use MediaWiki\Json\JsonCodec;
|
2021-03-24 14:05:44 +00:00
|
|
|
use MediaWiki\Page\PageRecord;
|
|
|
|
|
use MediaWiki\Page\PageStoreRecord;
|
|
|
|
|
use MediaWiki\Page\WikiPageFactory;
|
2023-09-28 15:03:33 +00:00
|
|
|
use MediaWiki\Parser\ParserCacheFilter;
|
2023-12-14 19:20:33 +00:00
|
|
|
use MediaWiki\Parser\ParserOutput;
|
2020-10-23 00:17:31 +00:00
|
|
|
use MediaWiki\Tests\Json\JsonUnserializableSuperClass;
|
2023-03-01 20:33:26 +00:00
|
|
|
use MediaWiki\Title\Title;
|
2023-07-21 00:29:59 +00:00
|
|
|
use MediaWiki\Title\TitleFactory;
|
2023-09-19 12:13:45 +00:00
|
|
|
use MediaWiki\User\User;
|
2023-08-19 03:35:06 +00:00
|
|
|
use MediaWiki\Utils\MWTimestamp;
|
2020-09-27 17:04:26 +00:00
|
|
|
use MediaWikiIntegrationTestCase;
|
|
|
|
|
use NullStatsdDataFactory;
|
|
|
|
|
use ParserCache;
|
|
|
|
|
use ParserOptions;
|
2020-10-14 14:54:56 +00:00
|
|
|
use Psr\Log\LoggerInterface;
|
|
|
|
|
use Psr\Log\LogLevel;
|
2020-09-27 17:04:26 +00:00
|
|
|
use Psr\Log\NullLogger;
|
2020-10-14 14:54:56 +00:00
|
|
|
use TestLogger;
|
2020-10-01 15:30:58 +00:00
|
|
|
use Wikimedia\TestingAccessWrapper;
|
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;
|
2020-09-27 17:04:26 +00:00
|
|
|
use WikiPage;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers ParserCache
|
|
|
|
|
* @package MediaWiki\Tests\Parser
|
|
|
|
|
*/
|
|
|
|
|
class ParserCacheTest extends MediaWikiIntegrationTestCase {
|
|
|
|
|
|
|
|
|
|
/** @var int */
|
|
|
|
|
private $time;
|
|
|
|
|
|
|
|
|
|
/** @var string */
|
|
|
|
|
private $cacheTime;
|
|
|
|
|
|
2021-03-24 14:05:44 +00:00
|
|
|
/** @var PageRecord */
|
2020-09-27 17:04:26 +00:00
|
|
|
private $page;
|
|
|
|
|
|
2021-07-22 03:11:47 +00:00
|
|
|
protected function setUp(): void {
|
2020-09-27 17:04:26 +00:00
|
|
|
parent::setUp();
|
|
|
|
|
$this->time = time();
|
2021-03-24 14:05:44 +00:00
|
|
|
$this->cacheTime = MWTimestamp::convert( TS_MW, $this->time + 1 );
|
|
|
|
|
$this->page = $this->createPageRecord();
|
2020-09-27 17:04:26 +00:00
|
|
|
|
2021-03-24 14:05:44 +00:00
|
|
|
MWTimestamp::setFakeTime( $this->time );
|
2020-09-27 17:04:26 +00:00
|
|
|
}
|
|
|
|
|
|
2021-03-24 14:05:44 +00:00
|
|
|
/**
|
|
|
|
|
* @param array $overrides
|
|
|
|
|
* @return PageRecord
|
|
|
|
|
*/
|
|
|
|
|
private function createPageRecord( array $overrides = [] ): PageRecord {
|
|
|
|
|
return new PageStoreRecord( (object)array_merge( [
|
|
|
|
|
'page_id' => 42,
|
|
|
|
|
'page_namespace' => NS_MAIN,
|
|
|
|
|
'page_title' => 'Testing_Testing',
|
|
|
|
|
'page_latest' => 24,
|
|
|
|
|
'page_is_new' => false,
|
|
|
|
|
'page_is_redirect' => false,
|
|
|
|
|
'page_touched' => $this->time,
|
|
|
|
|
'page_lang' => 'qqx',
|
|
|
|
|
], $overrides ), PageRecord::LOCAL );
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-27 17:04:26 +00:00
|
|
|
/**
|
|
|
|
|
* @param HookContainer|null $hookContainer
|
|
|
|
|
* @param BagOStuff|null $storage
|
2020-10-14 14:54:56 +00:00
|
|
|
* @param LoggerInterface|null $logger
|
2021-03-24 14:05:44 +00:00
|
|
|
* @param WikiPageFactory|null $wikiPageFactory
|
2020-09-27 17:04:26 +00:00
|
|
|
* @return ParserCache
|
|
|
|
|
*/
|
|
|
|
|
private function createParserCache(
|
|
|
|
|
HookContainer $hookContainer = null,
|
2020-10-14 14:54:56 +00:00
|
|
|
BagOStuff $storage = null,
|
2021-03-24 14:05:44 +00:00
|
|
|
LoggerInterface $logger = null,
|
|
|
|
|
WikiPageFactory $wikiPageFactory = null
|
2020-09-27 17:04:26 +00:00
|
|
|
): ParserCache {
|
2023-07-21 00:29:59 +00:00
|
|
|
if ( !$wikiPageFactory ) {
|
|
|
|
|
$wikiPageMock = $this->createMock( WikiPage::class );
|
|
|
|
|
$wikiPageMock->method( 'getContentModel' )->willReturn( CONTENT_MODEL_WIKITEXT );
|
|
|
|
|
$wikiPageFactory = $this->createMock( WikiPageFactory::class );
|
|
|
|
|
$wikiPageFactory->method( 'newFromTitle' )->willReturn( $wikiPageMock );
|
|
|
|
|
}
|
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
|
|
|
$globalIdGenerator = $this->createMock( GlobalIdGenerator::class );
|
|
|
|
|
$globalIdGenerator->method( 'newUUIDv1' )->willReturn( 'uuid-uuid' );
|
2020-09-27 17:04:26 +00:00
|
|
|
return new ParserCache(
|
|
|
|
|
'test',
|
|
|
|
|
$storage ?: new HashBagOStuff(),
|
|
|
|
|
'19900220000000',
|
|
|
|
|
$hookContainer ?: $this->createHookContainer( [] ),
|
2020-11-18 00:26:53 +00:00
|
|
|
new JsonCodec(),
|
2020-09-27 17:04:26 +00:00
|
|
|
new NullStatsdDataFactory(),
|
2021-03-24 14:05:44 +00:00
|
|
|
$logger ?: new NullLogger(),
|
2023-07-21 00:29:59 +00:00
|
|
|
$this->createMock( 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
|
|
|
$wikiPageFactory,
|
|
|
|
|
$globalIdGenerator
|
2020-09-27 17:04:26 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
private function getDummyUsedOptions(): array {
|
|
|
|
|
return array_slice(
|
|
|
|
|
ParserOptions::allCacheVaryingOptions(),
|
|
|
|
|
0,
|
|
|
|
|
2
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return ParserOutput
|
|
|
|
|
*/
|
|
|
|
|
private function createDummyParserOutput(): ParserOutput {
|
|
|
|
|
$parserOutput = new ParserOutput();
|
|
|
|
|
$parserOutput->setText( 'TEST' );
|
|
|
|
|
foreach ( $this->getDummyUsedOptions() as $option ) {
|
|
|
|
|
$parserOutput->recordOption( $option );
|
|
|
|
|
}
|
|
|
|
|
$parserOutput->updateCacheExpiry( 4242 );
|
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
|
|
|
$parserOutput->setRenderId( 'dummy-render-id' );
|
|
|
|
|
$parserOutput->setCacheRevisionId( 0 );
|
|
|
|
|
// ParserOutput::getCacheTime() also sets it as a side effect
|
2023-11-06 21:25:07 +00:00
|
|
|
$parserOutput->setRevisionTimestamp( $parserOutput->getCacheTime() );
|
2020-09-27 17:04:26 +00:00
|
|
|
return $parserOutput;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2020-09-23 21:31:14 +00:00
|
|
|
* @covers ParserCache::getMetadata
|
|
|
|
|
*/
|
|
|
|
|
public function testGetMetadataMissing() {
|
|
|
|
|
$cache = $this->createParserCache();
|
|
|
|
|
$metadataFromCache = $cache->getMetadata( $this->page, ParserCache::USE_CURRENT_ONLY );
|
|
|
|
|
$this->assertNull( $metadataFromCache );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers ParserCache::getMetadata
|
|
|
|
|
*/
|
|
|
|
|
public function testGetMetadataAllGood() {
|
|
|
|
|
$cache = $this->createParserCache();
|
|
|
|
|
$parserOutput = $this->createDummyParserOutput();
|
2021-03-24 14:05:44 +00:00
|
|
|
|
2022-06-16 13:22:24 +00:00
|
|
|
$cache->save( $parserOutput, $this->page, ParserOptions::newFromAnon(), $this->cacheTime );
|
2020-09-23 21:31:14 +00:00
|
|
|
|
|
|
|
|
$metadataFromCache = $cache->getMetadata( $this->page, ParserCache::USE_CURRENT_ONLY );
|
|
|
|
|
$this->assertNotNull( $metadataFromCache );
|
|
|
|
|
$this->assertSame( $this->getDummyUsedOptions(), $metadataFromCache->getUsedOptions() );
|
|
|
|
|
$this->assertSame( 4242, $metadataFromCache->getCacheExpiry() );
|
2021-03-24 14:05:44 +00:00
|
|
|
$this->assertSame( $this->page->getLatest(), $metadataFromCache->getCacheRevisionId() );
|
2020-09-23 21:31:14 +00:00
|
|
|
$this->assertSame( $this->cacheTime, $metadataFromCache->getCacheTime() );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers ParserCache::getMetadata
|
|
|
|
|
*/
|
|
|
|
|
public function testGetMetadataExpired() {
|
|
|
|
|
$cache = $this->createParserCache();
|
|
|
|
|
$parserOutput = $this->createDummyParserOutput();
|
2022-06-16 13:22:24 +00:00
|
|
|
$cache->save( $parserOutput, $this->page, ParserOptions::newFromAnon(), $this->cacheTime );
|
2020-09-23 21:31:14 +00:00
|
|
|
|
2021-03-24 14:05:44 +00:00
|
|
|
$this->page = $this->createPageRecord( [ 'page_touched' => $this->time + 10000 ] );
|
2020-09-23 21:31:14 +00:00
|
|
|
$this->assertNull( $cache->getMetadata( $this->page, ParserCache::USE_CURRENT_ONLY ) );
|
|
|
|
|
$metadataFromCache = $cache->getMetadata( $this->page, ParserCache::USE_EXPIRED );
|
|
|
|
|
$this->assertNotNull( $metadataFromCache );
|
|
|
|
|
$this->assertSame( $this->getDummyUsedOptions(), $metadataFromCache->getUsedOptions() );
|
|
|
|
|
$this->assertSame( 4242, $metadataFromCache->getCacheExpiry() );
|
2021-03-24 14:05:44 +00:00
|
|
|
$this->assertSame( $this->page->getLatest(), $metadataFromCache->getCacheRevisionId() );
|
2020-09-23 21:31:14 +00:00
|
|
|
$this->assertSame( $this->cacheTime, $metadataFromCache->getCacheTime() );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers ParserCache::getMetadata
|
|
|
|
|
*/
|
|
|
|
|
public function testGetMetadataOutdated() {
|
|
|
|
|
$cache = $this->createParserCache();
|
|
|
|
|
$parserOutput = $this->createDummyParserOutput();
|
2022-06-16 13:22:24 +00:00
|
|
|
$cache->save( $parserOutput, $this->page, ParserOptions::newFromAnon(), $this->cacheTime );
|
2020-09-23 21:31:14 +00:00
|
|
|
|
2021-03-24 14:05:44 +00:00
|
|
|
$this->page = $this->createPageRecord( [ 'page_latest' => $this->page->getLatest() + 1 ] );
|
2020-09-23 21:31:14 +00:00
|
|
|
$this->assertNull( $cache->getMetadata( $this->page, ParserCache::USE_CURRENT_ONLY ) );
|
|
|
|
|
$this->assertNull( $cache->getMetadata( $this->page, ParserCache::USE_EXPIRED ) );
|
|
|
|
|
$metadataFromCache = $cache->getMetadata( $this->page, ParserCache::USE_OUTDATED );
|
|
|
|
|
$this->assertSame( $this->getDummyUsedOptions(), $metadataFromCache->getUsedOptions() );
|
|
|
|
|
$this->assertSame( 4242, $metadataFromCache->getCacheExpiry() );
|
2021-03-24 14:05:44 +00:00
|
|
|
$this->assertNotSame( $this->page->getLatest(), $metadataFromCache->getCacheRevisionId() );
|
2020-09-23 21:31:14 +00:00
|
|
|
$this->assertSame( $this->cacheTime, $metadataFromCache->getCacheTime() );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers ParserCache::makeParserOutputKey
|
|
|
|
|
*/
|
|
|
|
|
public function testMakeParserOutputKey() {
|
|
|
|
|
$cache = $this->createParserCache();
|
|
|
|
|
|
2022-06-16 13:22:24 +00:00
|
|
|
$options1 = ParserOptions::newFromAnon();
|
2020-09-23 21:31:14 +00:00
|
|
|
$options1->setOption( $this->getDummyUsedOptions()[0], 'value1' );
|
|
|
|
|
$key1 = $cache->makeParserOutputKey( $this->page, $options1, $this->getDummyUsedOptions() );
|
|
|
|
|
$this->assertNotNull( $key1 );
|
|
|
|
|
|
2022-06-16 13:22:24 +00:00
|
|
|
$options2 = ParserOptions::newFromAnon();
|
2020-09-23 21:31:14 +00:00
|
|
|
$options2->setOption( $this->getDummyUsedOptions()[0], 'value2' );
|
|
|
|
|
$key2 = $cache->makeParserOutputKey( $this->page, $options2, $this->getDummyUsedOptions() );
|
|
|
|
|
$this->assertNotNull( $key2 );
|
|
|
|
|
$this->assertNotSame( $key1, $key2 );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
2020-09-27 17:04:26 +00:00
|
|
|
* Test that fetching without storing first returns false.
|
|
|
|
|
* @covers ParserCache::get
|
|
|
|
|
*/
|
|
|
|
|
public function testGetEmpty() {
|
|
|
|
|
$cache = $this->createParserCache();
|
2022-06-16 13:22:24 +00:00
|
|
|
$options = ParserOptions::newFromAnon();
|
2020-09-27 17:04:26 +00:00
|
|
|
|
|
|
|
|
$this->assertFalse( $cache->get( $this->page, $options ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Test that fetching with the same options return the saved value.
|
|
|
|
|
* @covers ParserCache::get
|
|
|
|
|
* @covers ParserCache::save
|
|
|
|
|
*/
|
|
|
|
|
public function testSaveGetSameOptions() {
|
|
|
|
|
$cache = $this->createParserCache();
|
|
|
|
|
$parserOutput = new ParserOutput( 'TEST_TEXT' );
|
|
|
|
|
|
2022-06-16 13:22:24 +00:00
|
|
|
$options1 = ParserOptions::newFromAnon();
|
2020-09-27 17:04:26 +00:00
|
|
|
$options1->setOption( $this->getDummyUsedOptions()[0], 'value1' );
|
|
|
|
|
$cache->save( $parserOutput, $this->page, $options1, $this->cacheTime );
|
|
|
|
|
|
|
|
|
|
$savedOutput = $cache->get( $this->page, $options1 );
|
|
|
|
|
$this->assertInstanceOf( ParserOutput::class, $savedOutput );
|
|
|
|
|
// ParserCache adds a comment to the HTML, so check if the result starts with page content.
|
|
|
|
|
$this->assertStringStartsWith( 'TEST_TEXT', $savedOutput->getText() );
|
|
|
|
|
$this->assertSame( $this->cacheTime, $savedOutput->getCacheTime() );
|
2021-03-24 14:05:44 +00:00
|
|
|
$this->assertSame( $this->page->getLatest(), $savedOutput->getCacheRevisionId() );
|
2020-09-27 17:04:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Test that fetching with different unused option returns a value.
|
|
|
|
|
* @covers ParserCache::get
|
|
|
|
|
* @covers ParserCache::save
|
|
|
|
|
*/
|
|
|
|
|
public function testSaveGetDifferentUnusedOption() {
|
|
|
|
|
$cache = $this->createParserCache();
|
|
|
|
|
$optionName = $this->getDummyUsedOptions()[0];
|
|
|
|
|
$parserOutput = new ParserOutput( 'TEST_TEXT' );
|
|
|
|
|
|
2022-06-16 13:22:24 +00:00
|
|
|
$options1 = ParserOptions::newFromAnon();
|
2020-09-27 17:04:26 +00:00
|
|
|
$options1->setOption( $optionName, 'value1' );
|
|
|
|
|
$cache->save( $parserOutput, $this->page, $options1, $this->cacheTime );
|
|
|
|
|
|
2022-06-16 13:22:24 +00:00
|
|
|
$options2 = ParserOptions::newFromAnon();
|
2020-09-27 17:04:26 +00:00
|
|
|
$options2->setOption( $optionName, 'value2' );
|
|
|
|
|
$savedOutput = $cache->get( $this->page, $options2 );
|
|
|
|
|
$this->assertInstanceOf( ParserOutput::class, $savedOutput );
|
|
|
|
|
// ParserCache adds a comment to the HTML, so check if the result starts with page content.
|
|
|
|
|
$this->assertStringStartsWith( 'TEST_TEXT', $savedOutput->getText() );
|
|
|
|
|
$this->assertSame( $this->cacheTime, $savedOutput->getCacheTime() );
|
2021-03-24 14:05:44 +00:00
|
|
|
$this->assertSame( $this->page->getLatest(), $savedOutput->getCacheRevisionId() );
|
2020-09-27 17:04:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Test that non-cacheable output is not stored
|
|
|
|
|
* @covers ParserCache::save
|
2020-12-15 18:56:00 +00:00
|
|
|
* @covers ParserCache::get
|
2020-09-27 17:04:26 +00:00
|
|
|
*/
|
|
|
|
|
public function testDoesNotStoreNonCacheable() {
|
|
|
|
|
$cache = $this->createParserCache();
|
|
|
|
|
$parserOutput = new ParserOutput( 'TEST_TEXT' );
|
|
|
|
|
$parserOutput->updateCacheExpiry( 0 );
|
|
|
|
|
|
2022-06-16 13:22:24 +00:00
|
|
|
$options1 = ParserOptions::newFromAnon();
|
2020-09-27 17:04:26 +00:00
|
|
|
$cache->save( $parserOutput, $this->page, $options1, $this->cacheTime );
|
|
|
|
|
|
|
|
|
|
$this->assertFalse( $cache->get( $this->page, $options1 ) );
|
|
|
|
|
$this->assertFalse( $cache->get( $this->page, $options1, true ) );
|
2023-09-28 15:03:33 +00:00
|
|
|
$this->assertFalse( $cache->getDirty( $this->page, $options1 ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Test that ParserCacheFilter can be used to prevent content from being cached
|
|
|
|
|
* @covers ParserCache::save
|
|
|
|
|
* @covers ParserCache::get
|
|
|
|
|
*/
|
|
|
|
|
public function testDoesNotStoreFiltered() {
|
|
|
|
|
$cache = $this->createParserCache();
|
|
|
|
|
$parserOutput = new ParserOutput( 'TEST_TEXT' );
|
|
|
|
|
$parserOutput->resetParseStartTime();
|
|
|
|
|
$parserOutput->recordTimeProfile();
|
|
|
|
|
|
|
|
|
|
// Only cache output that took at least 100 seconds of CPU to generate
|
|
|
|
|
$cache->setFilter( new ParserCacheFilter( [
|
|
|
|
|
'default' => [ 'minCpuTime' => 100 ]
|
|
|
|
|
] ) );
|
|
|
|
|
|
|
|
|
|
$options1 = ParserOptions::newFromAnon();
|
|
|
|
|
$cache->save( $parserOutput, $this->page, $options1, $this->cacheTime );
|
|
|
|
|
|
|
|
|
|
$this->assertFalse( $cache->get( $this->page, $options1 ) );
|
|
|
|
|
$this->assertFalse( $cache->get( $this->page, $options1, true ) );
|
2020-09-27 17:04:26 +00:00
|
|
|
$this->assertFalse( $cache->getDirty( $this->page, $options1 ) );
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-01 22:22:31 +00:00
|
|
|
/**
|
|
|
|
|
* Test that ParserOptions::isSafeToCache is respected on save
|
|
|
|
|
* @covers ParserCache::save
|
|
|
|
|
*/
|
2020-12-15 18:56:00 +00:00
|
|
|
public function testDoesNotStoreNotSafeToCacheAndUsed() {
|
2020-12-01 22:22:31 +00:00
|
|
|
$cache = $this->createParserCache();
|
|
|
|
|
$parserOutput = new ParserOutput( 'TEST_TEXT' );
|
2020-12-15 18:56:00 +00:00
|
|
|
$parserOutput->recordOption( 'wrapclass' );
|
2020-12-01 22:22:31 +00:00
|
|
|
|
2022-06-16 13:22:24 +00:00
|
|
|
$options = ParserOptions::newFromAnon();
|
2020-12-01 22:22:31 +00:00
|
|
|
$options->setOption( 'wrapclass', 'wrapwrap' );
|
|
|
|
|
|
|
|
|
|
$cache->save( $parserOutput, $this->page, $options, $this->cacheTime );
|
|
|
|
|
|
|
|
|
|
$this->assertFalse( $cache->get( $this->page, $options ) );
|
|
|
|
|
$this->assertFalse( $cache->get( $this->page, $options, true ) );
|
|
|
|
|
$this->assertFalse( $cache->getDirty( $this->page, $options ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Test that ParserOptions::isSafeToCache is respected on get
|
|
|
|
|
* @covers ParserCache::get
|
|
|
|
|
*/
|
|
|
|
|
public function testDoesNotGetNotSafeToCache() {
|
|
|
|
|
$cache = $this->createParserCache();
|
|
|
|
|
$parserOutput = new ParserOutput( 'TEST_TEXT' );
|
2020-12-15 18:56:00 +00:00
|
|
|
$parserOutput->recordOption( 'wrapclass' );
|
2020-12-01 22:22:31 +00:00
|
|
|
|
2022-06-16 13:22:24 +00:00
|
|
|
$cache->save( $parserOutput, $this->page, ParserOptions::newFromAnon(), $this->cacheTime );
|
2020-12-01 22:22:31 +00:00
|
|
|
|
2022-06-16 13:22:24 +00:00
|
|
|
$otherOptions = ParserOptions::newFromAnon();
|
2020-12-01 22:22:31 +00:00
|
|
|
$otherOptions->setOption( 'wrapclass', 'wrapwrap' );
|
|
|
|
|
|
|
|
|
|
$this->assertFalse( $cache->get( $this->page, $otherOptions ) );
|
|
|
|
|
$this->assertFalse( $cache->get( $this->page, $otherOptions, true ) );
|
|
|
|
|
$this->assertFalse( $cache->getDirty( $this->page, $otherOptions ) );
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-15 18:56:00 +00:00
|
|
|
/**
|
|
|
|
|
* Test that ParserOptions::isSafeToCache is respected on save
|
|
|
|
|
* @covers ParserCache::save
|
|
|
|
|
* @covers ParserCache::get
|
|
|
|
|
*/
|
|
|
|
|
public function testStoresNotSafeToCacheAndUnused() {
|
|
|
|
|
$cache = $this->createParserCache();
|
|
|
|
|
$parserOutput = new ParserOutput( 'TEST_TEXT' );
|
|
|
|
|
|
2022-06-16 13:22:24 +00:00
|
|
|
$options = ParserOptions::newFromAnon();
|
2020-12-15 18:56:00 +00:00
|
|
|
$options->setOption( 'wrapclass', 'wrapwrap' );
|
|
|
|
|
|
|
|
|
|
$cache->save( $parserOutput, $this->page, $options, $this->cacheTime );
|
|
|
|
|
$this->assertStringContainsString( 'TEST_TEXT', $cache->get( $this->page, $options )->getText() );
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-27 17:04:26 +00:00
|
|
|
/**
|
|
|
|
|
* Test that fetching with different used option don't return a value.
|
|
|
|
|
* @covers ParserCache::get
|
|
|
|
|
* @covers ParserCache::save
|
|
|
|
|
*/
|
|
|
|
|
public function testSaveGetDifferentUsedOption() {
|
|
|
|
|
$cache = $this->createParserCache();
|
|
|
|
|
$parserOutput = new ParserOutput( 'TEST_TEXT' );
|
|
|
|
|
$optionName = $this->getDummyUsedOptions()[0];
|
|
|
|
|
$parserOutput->recordOption( $optionName );
|
|
|
|
|
|
2022-06-16 13:22:24 +00:00
|
|
|
$options1 = ParserOptions::newFromAnon();
|
2020-09-27 17:04:26 +00:00
|
|
|
$options1->setOption( $optionName, 'value1' );
|
|
|
|
|
$cache->save( $parserOutput, $this->page, $options1, $this->cacheTime );
|
|
|
|
|
|
2022-06-16 13:22:24 +00:00
|
|
|
$options2 = ParserOptions::newFromAnon();
|
2020-09-27 17:04:26 +00:00
|
|
|
$options2->setOption( $optionName, 'value2' );
|
|
|
|
|
$this->assertFalse( $cache->get( $this->page, $options2 ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Test that output with expired metadata can be retrieved with getDirty
|
|
|
|
|
* @covers ParserCache::getDirty
|
|
|
|
|
* @covers ParserCache::get
|
|
|
|
|
*/
|
|
|
|
|
public function testGetExpiredMetadata() {
|
|
|
|
|
$cache = $this->createParserCache();
|
|
|
|
|
$parserOutput = new ParserOutput( 'TEST_TEXT' );
|
|
|
|
|
$parserOutput->updateCacheExpiry( 10 );
|
|
|
|
|
|
2022-06-16 13:22:24 +00:00
|
|
|
$options1 = ParserOptions::newFromAnon();
|
2020-09-27 17:04:26 +00:00
|
|
|
$cache->save( $parserOutput, $this->page, $options1, $this->cacheTime );
|
|
|
|
|
|
|
|
|
|
MWTimestamp::setFakeTime( $this->time + 15 * 1000 );
|
|
|
|
|
$this->assertFalse( $cache->get( $this->page, $options1 ) );
|
|
|
|
|
$this->assertInstanceOf( ParserOutput::class,
|
|
|
|
|
$cache->get( $this->page, $options1, true ) );
|
|
|
|
|
$this->assertInstanceOf( ParserOutput::class,
|
|
|
|
|
$cache->getDirty( $this->page, $options1 ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Test that expired output with not expired metadata can be retrieved with getDirty
|
|
|
|
|
* @covers ParserCache::getDirty
|
|
|
|
|
* @covers ParserCache::get
|
|
|
|
|
*/
|
|
|
|
|
public function testGetExpiredContent() {
|
|
|
|
|
$cache = $this->createParserCache();
|
|
|
|
|
$optionName = $this->getDummyUsedOptions()[0];
|
|
|
|
|
|
|
|
|
|
$parserOutput1 = new ParserOutput( 'TEST_TEXT1' );
|
|
|
|
|
$parserOutput1->recordOption( $optionName );
|
|
|
|
|
$parserOutput1->updateCacheExpiry( 10 );
|
2022-06-16 13:22:24 +00:00
|
|
|
$options1 = ParserOptions::newFromAnon();
|
2020-09-27 17:04:26 +00:00
|
|
|
$options1->setOption( $optionName, 'value1' );
|
|
|
|
|
$cache->save( $parserOutput1, $this->page, $options1, $this->cacheTime );
|
|
|
|
|
|
|
|
|
|
$parserOutput2 = new ParserOutput( 'TEST_TEXT2' );
|
|
|
|
|
$parserOutput2->recordOption( $optionName );
|
|
|
|
|
$parserOutput2->updateCacheExpiry( 100500600 );
|
2022-06-16 13:22:24 +00:00
|
|
|
$options2 = ParserOptions::newFromAnon();
|
2020-09-27 17:04:26 +00:00
|
|
|
$options2->setOption( $optionName, 'value2' );
|
|
|
|
|
$cache->save( $parserOutput2, $this->page, $options2, $this->cacheTime );
|
|
|
|
|
|
|
|
|
|
MWTimestamp::setFakeTime( $this->time + 15 * 1000 );
|
|
|
|
|
$this->assertFalse( $cache->get( $this->page, $options1 ) );
|
|
|
|
|
$this->assertInstanceOf( ParserOutput::class,
|
|
|
|
|
$cache->get( $this->page, $options1, true ) );
|
|
|
|
|
$this->assertInstanceOf( ParserOutput::class,
|
|
|
|
|
$cache->getDirty( $this->page, $options1 ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Test that output with outdated metadata can be retrieved with getDirty
|
|
|
|
|
* @covers ParserCache::getDirty
|
|
|
|
|
* @covers ParserCache::get
|
|
|
|
|
*/
|
|
|
|
|
public function testGetOutdatedMetadata() {
|
|
|
|
|
$cache = $this->createParserCache();
|
|
|
|
|
$parserOutput = new ParserOutput( 'TEST_TEXT' );
|
|
|
|
|
|
2022-06-16 13:22:24 +00:00
|
|
|
$options1 = ParserOptions::newFromAnon();
|
2020-09-27 17:04:26 +00:00
|
|
|
$cache->save( $parserOutput, $this->page, $options1, $this->cacheTime );
|
|
|
|
|
$this->assertInstanceOf( ParserOutput::class,
|
|
|
|
|
$cache->get( $this->page, $options1 ) );
|
|
|
|
|
|
2021-03-24 14:05:44 +00:00
|
|
|
$this->page = $this->createPageRecord( [ 'page_latest' => $this->page->getLatest() + 1 ] );
|
2020-09-27 17:04:26 +00:00
|
|
|
$this->assertFalse( $cache->get( $this->page, $options1 ) );
|
|
|
|
|
$this->assertInstanceOf( ParserOutput::class,
|
|
|
|
|
$cache->get( $this->page, $options1, true ) );
|
|
|
|
|
$this->assertInstanceOf( ParserOutput::class,
|
|
|
|
|
$cache->getDirty( $this->page, $options1 ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Test that outdated output with good metadata can be retrieved with getDirty
|
|
|
|
|
* @covers ParserCache::getDirty
|
|
|
|
|
* @covers ParserCache::get
|
|
|
|
|
*/
|
|
|
|
|
public function testGetOutdatedContent() {
|
|
|
|
|
$cache = $this->createParserCache();
|
|
|
|
|
$optionName = $this->getDummyUsedOptions()[0];
|
|
|
|
|
|
|
|
|
|
$parserOutput1 = new ParserOutput( 'TEST_TEXT' );
|
|
|
|
|
$parserOutput1->recordOption( $optionName );
|
2022-06-16 13:22:24 +00:00
|
|
|
$options1 = ParserOptions::newFromAnon();
|
2020-09-27 17:04:26 +00:00
|
|
|
$options1->setOption( $optionName, 'value1' );
|
|
|
|
|
$cache->save( $parserOutput1, $this->page, $options1, $this->cacheTime );
|
|
|
|
|
|
2021-03-24 14:05:44 +00:00
|
|
|
$this->page = $this->createPageRecord( [ 'page_latest' => $this->page->getLatest() + 1 ] );
|
2020-09-27 17:04:26 +00:00
|
|
|
$parserOutput2 = new ParserOutput( 'TEST_TEXT' );
|
|
|
|
|
$parserOutput2->recordOption( $optionName );
|
2022-06-16 13:22:24 +00:00
|
|
|
$options2 = ParserOptions::newFromAnon();
|
2020-09-27 17:04:26 +00:00
|
|
|
$options2->setOption( $optionName, 'value2' );
|
|
|
|
|
$cache->save( $parserOutput2, $this->page, $options2, $this->cacheTime );
|
|
|
|
|
|
|
|
|
|
$this->assertFalse( $cache->get( $this->page, $options1 ) );
|
|
|
|
|
$this->assertInstanceOf( ParserOutput::class,
|
|
|
|
|
$cache->get( $this->page, $options1, true ) );
|
|
|
|
|
$this->assertInstanceOf( ParserOutput::class,
|
|
|
|
|
$cache->getDirty( $this->page, $options1 ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Test that fetching after deleting a key returns false.
|
|
|
|
|
* @covers ParserCache::deleteOptionsKey
|
|
|
|
|
*/
|
|
|
|
|
public function testDeleteOptionsKey() {
|
|
|
|
|
$cache = $this->createParserCache();
|
|
|
|
|
$parserOutput = new ParserOutput( 'TEST_TEXT' );
|
|
|
|
|
|
2022-06-16 13:22:24 +00:00
|
|
|
$options1 = ParserOptions::newFromAnon();
|
2020-09-27 17:04:26 +00:00
|
|
|
$cache->save( $parserOutput, $this->page, $options1, $this->cacheTime );
|
|
|
|
|
$this->assertInstanceOf( ParserOutput::class,
|
|
|
|
|
$cache->get( $this->page, $options1 ) );
|
|
|
|
|
$cache->deleteOptionsKey( $this->page );
|
|
|
|
|
|
|
|
|
|
$this->assertFalse( $cache->get( $this->page, $options1 ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Test that RejectParserCacheValue hook can reject ParserOutput
|
|
|
|
|
* @covers ParserCache::get
|
|
|
|
|
*/
|
|
|
|
|
public function testRejectedByHook() {
|
|
|
|
|
$parserOutput = new ParserOutput( 'TEST_TEXT' );
|
2022-06-16 13:22:24 +00:00
|
|
|
$options = ParserOptions::newFromAnon();
|
2020-09-27 17:04:26 +00:00
|
|
|
$options->setOption( $this->getDummyUsedOptions()[0], 'value1' );
|
2021-03-24 14:05:44 +00:00
|
|
|
|
|
|
|
|
$wikiPageMock = $this->createMock( WikiPage::class );
|
2023-07-21 00:29:59 +00:00
|
|
|
$wikiPageMock->method( 'getContentModel' )->willReturn( CONTENT_MODEL_WIKITEXT );
|
|
|
|
|
$wikiPageFactory = $this->createMock( WikiPageFactory::class );
|
|
|
|
|
$wikiPageFactory->method( 'newFromTitle' )
|
2021-03-24 14:05:44 +00:00
|
|
|
->with( $this->page )
|
|
|
|
|
->willReturn( $wikiPageMock );
|
2020-09-27 17:04:26 +00:00
|
|
|
$hookContainer = $this->createHookContainer( [
|
|
|
|
|
'RejectParserCacheValue' =>
|
|
|
|
|
function ( ParserOutput $value, WikiPage $hookPage, ParserOptions $popts )
|
2021-03-24 14:05:44 +00:00
|
|
|
use ( $wikiPageMock, $parserOutput, $options ) {
|
2022-05-09 15:51:35 +00:00
|
|
|
$this->assertEquals( $parserOutput, $value );
|
2021-03-24 14:05:44 +00:00
|
|
|
$this->assertSame( $wikiPageMock, $hookPage );
|
2020-09-27 17:04:26 +00:00
|
|
|
$this->assertSame( $options, $popts );
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
] );
|
2023-07-21 00:29:59 +00:00
|
|
|
$cache = $this->createParserCache( $hookContainer, null, null, $wikiPageFactory );
|
2020-09-27 17:04:26 +00:00
|
|
|
$cache->save( $parserOutput, $this->page, $options, $this->cacheTime );
|
|
|
|
|
$this->assertFalse( $cache->get( $this->page, $options ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Test that ParserCacheSaveComplete hook is run
|
|
|
|
|
* @covers ParserCache::save
|
|
|
|
|
*/
|
|
|
|
|
public function testParserCacheSaveCompleteHook() {
|
|
|
|
|
$parserOutput = new ParserOutput( 'TEST_TEXT' );
|
2022-06-16 13:22:24 +00:00
|
|
|
$options = ParserOptions::newFromAnon();
|
2020-09-27 17:04:26 +00:00
|
|
|
$options->setOption( $this->getDummyUsedOptions()[0], 'value1' );
|
2021-03-24 14:05:44 +00:00
|
|
|
|
2020-09-27 17:04:26 +00:00
|
|
|
$hookContainer = $this->createHookContainer( [
|
|
|
|
|
'ParserCacheSaveComplete' =>
|
2021-09-03 22:52:31 +00:00
|
|
|
function (
|
|
|
|
|
ParserCache $hookCache, ParserOutput $value, Title $hookTitle, ParserOptions $popts, int $revId
|
|
|
|
|
) use ( $parserOutput, $options ) {
|
2020-09-27 17:04:26 +00:00
|
|
|
$this->assertSame( $parserOutput, $value );
|
|
|
|
|
$this->assertSame( $options, $popts );
|
|
|
|
|
$this->assertSame( 42, $revId );
|
|
|
|
|
}
|
|
|
|
|
] );
|
|
|
|
|
$cache = $this->createParserCache( $hookContainer );
|
|
|
|
|
$cache->save( $parserOutput, $this->page, $options, $this->cacheTime, 42 );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2021-03-24 14:05:44 +00:00
|
|
|
* Tests that parser cache respects skipped if page does not exist
|
2020-09-27 17:04:26 +00:00
|
|
|
* @covers ParserCache::get
|
|
|
|
|
*/
|
2021-03-24 14:05:44 +00:00
|
|
|
public function testSkipIfNotExist() {
|
|
|
|
|
$mockPage = $this->createNoOpMock( PageRecord::class, [ 'exists', 'assertWiki' ] );
|
|
|
|
|
$mockPage->method( 'exists' )->willReturn( false );
|
|
|
|
|
$wikiPageMock = $this->createMock( WikiPage::class );
|
2022-07-25 20:46:30 +00:00
|
|
|
$wikiPageMock->method( 'getContentModel' )->willReturn( 'wikitext' );
|
2021-03-24 14:05:44 +00:00
|
|
|
$wikiPageFactoryMock = $this->createMock( WikiPageFactory::class );
|
|
|
|
|
$wikiPageFactoryMock->method( 'newFromTitle' )
|
|
|
|
|
->with( $mockPage )
|
|
|
|
|
->willReturn( $wikiPageMock );
|
|
|
|
|
$cache = $this->createParserCache( null, null, null, $wikiPageFactoryMock );
|
2022-06-16 13:22:24 +00:00
|
|
|
$this->assertFalse( $cache->get( $mockPage, ParserOptions::newFromAnon() ) );
|
2020-09-27 17:04:26 +00:00
|
|
|
}
|
|
|
|
|
|
2021-03-24 14:05:44 +00:00
|
|
|
/**
|
|
|
|
|
* Tests that parser cache respects skipped if page is redirect
|
|
|
|
|
* @covers ParserCache::get
|
|
|
|
|
*/
|
|
|
|
|
public function testSkipIfRedirect() {
|
|
|
|
|
$cache = $this->createParserCache();
|
|
|
|
|
$page = $this->createPageRecord( [
|
|
|
|
|
'page_is_redirect' => true
|
|
|
|
|
] );
|
2022-06-16 13:22:24 +00:00
|
|
|
$this->assertFalse( $cache->get( $page, ParserOptions::newFromAnon() ) );
|
2021-03-24 14:05:44 +00:00
|
|
|
}
|
|
|
|
|
|
2020-09-27 17:04:26 +00:00
|
|
|
/**
|
|
|
|
|
* Tests that getCacheStorage returns underlying BagOStuff
|
|
|
|
|
* @covers ParserCache::getCacheStorage
|
|
|
|
|
*/
|
|
|
|
|
public function testGetCacheStorage() {
|
|
|
|
|
$storage = new EmptyBagOStuff();
|
|
|
|
|
$cache = $this->createParserCache( null, $storage );
|
|
|
|
|
$this->assertSame( $storage, $cache->getCacheStorage() );
|
|
|
|
|
}
|
2020-10-01 15:30:58 +00:00
|
|
|
|
2020-10-23 02:52:57 +00:00
|
|
|
/**
|
|
|
|
|
* @covers ParserCache::save
|
|
|
|
|
*/
|
|
|
|
|
public function testSaveNoText() {
|
|
|
|
|
$this->expectException( InvalidArgumentException::class );
|
|
|
|
|
$this->createParserCache()->save(
|
|
|
|
|
new ParserOutput( null ),
|
|
|
|
|
$this->page,
|
2022-06-16 13:22:24 +00:00
|
|
|
ParserOptions::newFromAnon()
|
2020-10-23 02:52:57 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-23 11:36:19 +00:00
|
|
|
public static function provideCorruptData() {
|
2022-05-09 15:51:35 +00:00
|
|
|
yield 'JSON serialization, bad data' => [ 'bla bla' ];
|
|
|
|
|
yield 'JSON serialization, no _class_' => [ '{"test":"test"}' ];
|
|
|
|
|
yield 'JSON serialization, non-existing _class_' => [ '{"_class_":"NonExistentBogusClass"}' ];
|
2020-10-23 00:17:31 +00:00
|
|
|
$wrongInstance = new JsonUnserializableSuperClass( 'test' );
|
2022-05-09 15:51:35 +00:00
|
|
|
yield 'JSON serialization, wrong class' => [ json_encode( $wrongInstance->jsonSerialize() ) ];
|
2020-10-23 02:52:57 +00:00
|
|
|
}
|
|
|
|
|
|
2020-10-01 15:30:58 +00:00
|
|
|
/**
|
|
|
|
|
* Test that we handle corrupt data gracefully.
|
|
|
|
|
* This is important for forward-compatibility with JSON serialization.
|
|
|
|
|
* We want to be sure that we don't crash horribly if we have to roll
|
|
|
|
|
* back to a version of the code that doesn't know about JSON.
|
|
|
|
|
*
|
2020-10-23 02:52:57 +00:00
|
|
|
* @dataProvider provideCorruptData
|
2020-10-01 15:30:58 +00:00
|
|
|
* @covers ParserCache::get
|
2020-10-23 02:52:57 +00:00
|
|
|
* @covers ParserCache::restoreFromJson
|
|
|
|
|
* @param string $data
|
2020-10-01 15:30:58 +00:00
|
|
|
*/
|
2022-05-09 15:51:35 +00:00
|
|
|
public function testCorruptData( string $data ) {
|
2020-12-07 15:35:18 +00:00
|
|
|
$cache = $this->createParserCache( null, new HashBagOStuff() );
|
2020-10-01 15:30:58 +00:00
|
|
|
$parserOutput = new ParserOutput( 'TEST_TEXT' );
|
|
|
|
|
|
2022-06-16 13:22:24 +00:00
|
|
|
$options1 = ParserOptions::newFromAnon();
|
2020-10-01 15:30:58 +00:00
|
|
|
$cache->save( $parserOutput, $this->page, $options1, $this->cacheTime );
|
|
|
|
|
|
2020-10-02 14:07:37 +00:00
|
|
|
$outputKey = $cache->makeParserOutputKey(
|
2020-10-01 15:30:58 +00:00
|
|
|
$this->page,
|
2020-10-02 14:07:37 +00:00
|
|
|
$options1,
|
|
|
|
|
$parserOutput->getUsedOptions()
|
2020-10-01 15:30:58 +00:00
|
|
|
);
|
|
|
|
|
|
2020-12-07 15:35:18 +00:00
|
|
|
$cache->getCacheStorage()->set( $outputKey, $data );
|
2020-10-01 15:30:58 +00:00
|
|
|
|
|
|
|
|
// just make sure we don't crash and burn
|
2020-10-23 02:52:57 +00:00
|
|
|
$this->assertFalse( $cache->get( $this->page, $options1 ) );
|
2020-10-01 15:30:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Test that we handle corrupt data gracefully.
|
|
|
|
|
* This is important for forward-compatibility with JSON serialization.
|
|
|
|
|
* We want to be sure that we don't crash horribly if we have to roll
|
|
|
|
|
* back to a version of the code that doesn't know about JSON.
|
|
|
|
|
*
|
2020-10-02 14:07:37 +00:00
|
|
|
* @covers ParserCache::getMetadata
|
2020-10-01 15:30:58 +00:00
|
|
|
*/
|
|
|
|
|
public function testCorruptMetadata() {
|
2021-04-06 16:12:26 +00:00
|
|
|
$cacheStorage = new HashBagOStuff();
|
|
|
|
|
$cache = $this->createParserCache( null, $cacheStorage );
|
2020-10-01 15:30:58 +00:00
|
|
|
$parserOutput = new ParserOutput( 'TEST_TEXT' );
|
|
|
|
|
|
2022-06-16 13:22:24 +00:00
|
|
|
$options1 = ParserOptions::newFromAnon();
|
2020-10-01 15:30:58 +00:00
|
|
|
$cache->save( $parserOutput, $this->page, $options1, $this->cacheTime );
|
|
|
|
|
|
2021-04-06 16:12:26 +00:00
|
|
|
// Mess up the metadata
|
2020-10-02 14:07:37 +00:00
|
|
|
$optionsKey = TestingAccessWrapper::newFromObject( $cache )->makeMetadataKey(
|
2020-10-01 15:30:58 +00:00
|
|
|
$this->page
|
|
|
|
|
);
|
2021-04-06 16:12:26 +00:00
|
|
|
$cacheStorage->set( $optionsKey, 'bad data' );
|
2020-10-01 15:30:58 +00:00
|
|
|
|
2021-04-06 16:12:26 +00:00
|
|
|
// Recreate the cache to drop in-memory cached metadata.
|
|
|
|
|
$cache = $this->createParserCache( null, $cacheStorage );
|
2020-10-01 15:30:58 +00:00
|
|
|
|
|
|
|
|
// just make sure we don't crash and burn
|
2020-10-02 14:07:37 +00:00
|
|
|
$this->assertNull( $cache->getMetadata( $this->page ) );
|
2020-10-01 15:30:58 +00:00
|
|
|
}
|
|
|
|
|
|
2020-09-29 19:18:00 +00:00
|
|
|
/**
|
2022-05-09 15:51:35 +00:00
|
|
|
* Test what happens when upgrading from 1.35 or earlier,
|
|
|
|
|
* when old cache entries do not yet use JSON.
|
2020-09-29 19:18:00 +00:00
|
|
|
*
|
|
|
|
|
* @covers ParserCache::get
|
|
|
|
|
*/
|
|
|
|
|
public function testMigrationToJson() {
|
2022-05-09 15:51:35 +00:00
|
|
|
$bagOStuff = new HashBagOStuff();
|
|
|
|
|
|
2023-07-21 00:29:59 +00:00
|
|
|
$wikiPageMock = $this->createMock( WikiPage::class );
|
|
|
|
|
$wikiPageMock->method( 'getContentModel' )->willReturn( CONTENT_MODEL_WIKITEXT );
|
|
|
|
|
$wikiPageFactory = $this->createMock( WikiPageFactory::class );
|
|
|
|
|
$wikiPageFactory->method( 'newFromTitle' )->willReturn( $wikiPageMock );
|
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
|
|
|
$globalIdGenerator = $this->createMock( GlobalIdGenerator::class );
|
|
|
|
|
$globalIdGenerator->method( 'newUUIDv1' )->willReturn( 'uuid-uuid' );
|
2022-05-09 15:51:35 +00:00
|
|
|
$cache = $this->getMockBuilder( ParserCache::class )
|
|
|
|
|
->setConstructorArgs( [
|
|
|
|
|
'test',
|
|
|
|
|
$bagOStuff,
|
|
|
|
|
'19900220000000',
|
|
|
|
|
$this->createHookContainer( [] ),
|
|
|
|
|
new JsonCodec(),
|
|
|
|
|
new NullStatsdDataFactory(),
|
|
|
|
|
new NullLogger(),
|
2023-07-21 00:29:59 +00:00
|
|
|
$this->createMock( 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
|
|
|
$wikiPageFactory,
|
|
|
|
|
$globalIdGenerator
|
2022-05-09 15:51:35 +00:00
|
|
|
] )
|
|
|
|
|
->onlyMethods( [ 'convertForCache' ] )
|
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
|
|
// Emulate pre-1.36 behavior: rely on native PHP serialization.
|
|
|
|
|
// Note that backwards compatibility of the actual serialization is covered
|
|
|
|
|
// by ParserOutputTest which uses various versions of serialized data
|
|
|
|
|
// under tests/phpunit/data/ParserCache.
|
|
|
|
|
$cache->method( 'convertForCache' )->willReturnCallback(
|
|
|
|
|
static function ( CacheTime $obj, string $key ) {
|
|
|
|
|
return $obj;
|
|
|
|
|
}
|
|
|
|
|
);
|
2020-09-29 19:18:00 +00:00
|
|
|
|
|
|
|
|
$parserOutput1 = new ParserOutput( 'Lorem Ipsum' );
|
|
|
|
|
|
2022-06-16 13:22:24 +00:00
|
|
|
$options = ParserOptions::newFromAnon();
|
2020-09-29 19:18:00 +00:00
|
|
|
$cache->save( $parserOutput1, $this->page, $options, $this->cacheTime );
|
|
|
|
|
|
|
|
|
|
// emulate migration to JSON
|
2022-05-09 15:51:35 +00:00
|
|
|
$cache = $this->createParserCache( null, $bagOStuff );
|
2020-09-29 19:18:00 +00:00
|
|
|
|
|
|
|
|
// make sure we can load non-json cache data
|
|
|
|
|
$cachedOutput = $cache->get( $this->page, $options );
|
|
|
|
|
$this->assertEquals( $parserOutput1, $cachedOutput );
|
|
|
|
|
|
|
|
|
|
// now test that the cache works when using JSON
|
|
|
|
|
$parserOutput2 = new ParserOutput( 'dolor sit amet' );
|
|
|
|
|
$cache->save( $parserOutput2, $this->page, $options, $this->cacheTime );
|
|
|
|
|
|
|
|
|
|
// make sure we can load json cache data
|
|
|
|
|
$cachedOutput = $cache->get( $this->page, $options );
|
|
|
|
|
$this->assertEquals( $parserOutput2, $cachedOutput );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2022-05-09 15:51:35 +00:00
|
|
|
* @covers ParserCache::convertForCache
|
2020-10-14 14:54:56 +00:00
|
|
|
*/
|
|
|
|
|
public function testNonSerializableJsonIsReported() {
|
|
|
|
|
$testLogger = new TestLogger( true );
|
|
|
|
|
$cache = $this->createParserCache( null, null, $testLogger );
|
|
|
|
|
|
|
|
|
|
$parserOutput = $this->createDummyParserOutput();
|
|
|
|
|
$parserOutput->setExtensionData( 'test', new User() );
|
2022-06-16 13:22:24 +00:00
|
|
|
$cache->save( $parserOutput, $this->page, ParserOptions::newFromAnon() );
|
2020-12-01 22:22:31 +00:00
|
|
|
$this->assertArraySubmapSame(
|
2020-11-18 00:26:53 +00:00
|
|
|
[ [ LogLevel::ERROR, 'Unable to serialize JSON' ] ],
|
2020-10-14 14:54:56 +00:00
|
|
|
$testLogger->getBuffer()
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2022-05-09 15:51:35 +00:00
|
|
|
* @covers ParserCache::convertForCache
|
2020-10-14 14:54:56 +00:00
|
|
|
*/
|
|
|
|
|
public function testCyclicStructuresDoNotBlowUpInJson() {
|
2022-07-07 20:32:08 +00:00
|
|
|
$this->markTestSkipped( 'Temporarily disabled: T314338' );
|
2020-10-14 14:54:56 +00:00
|
|
|
$testLogger = new TestLogger( true );
|
|
|
|
|
$cache = $this->createParserCache( null, null, $testLogger );
|
|
|
|
|
|
|
|
|
|
$parserOutput = $this->createDummyParserOutput();
|
|
|
|
|
$cyclicArray = [ 'a' => 'b' ];
|
|
|
|
|
$cyclicArray['c'] = &$cyclicArray;
|
|
|
|
|
$parserOutput->setExtensionData( 'test', $cyclicArray );
|
2022-06-16 13:22:24 +00:00
|
|
|
$cache->save( $parserOutput, $this->page, ParserOptions::newFromAnon() );
|
2020-12-01 22:22:31 +00:00
|
|
|
$this->assertArraySubmapSame(
|
2020-11-18 00:26:53 +00:00
|
|
|
[ [ LogLevel::ERROR, 'Unable to serialize JSON' ] ],
|
2020-10-14 14:54:56 +00:00
|
|
|
$testLogger->getBuffer()
|
|
|
|
|
);
|
|
|
|
|
}
|
2020-10-23 00:48:59 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Tests that unicode characters are not \u escaped
|
2022-05-09 15:51:35 +00:00
|
|
|
*
|
|
|
|
|
* @covers ParserCache::convertForCache
|
2020-10-23 00:48:59 +00:00
|
|
|
*/
|
|
|
|
|
public function testJsonEncodeUnicode() {
|
|
|
|
|
$unicodeCharacter = "Э";
|
|
|
|
|
$cache = $this->createParserCache( null, new HashBagOStuff() );
|
2021-03-24 14:05:44 +00:00
|
|
|
|
2020-10-23 00:48:59 +00:00
|
|
|
$parserOutput = $this->createDummyParserOutput();
|
|
|
|
|
$parserOutput->setText( $unicodeCharacter );
|
2022-06-16 13:22:24 +00:00
|
|
|
$cache->save( $parserOutput, $this->page, ParserOptions::newFromAnon() );
|
2020-10-23 00:48:59 +00:00
|
|
|
$json = $cache->getCacheStorage()->get(
|
2022-06-16 13:22:24 +00:00
|
|
|
$cache->makeParserOutputKey( $this->page, ParserOptions::newFromAnon() )
|
2020-10-23 00:48:59 +00:00
|
|
|
);
|
|
|
|
|
$this->assertStringNotContainsString( "\u003E", $json );
|
|
|
|
|
$this->assertStringContainsString( $unicodeCharacter, $json );
|
|
|
|
|
}
|
2020-09-27 17:04:26 +00:00
|
|
|
}
|