2020-12-04 15:28:25 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace MediaWiki\Tests\Parser;
|
|
|
|
|
|
|
|
|
|
use InvalidArgumentException;
|
|
|
|
|
use MediaWiki\Json\JsonCodec;
|
2021-03-24 14:05:44 +00:00
|
|
|
use MediaWiki\Page\PageIdentity;
|
|
|
|
|
use MediaWiki\Page\PageIdentityValue;
|
2023-12-14 19:20:33 +00:00
|
|
|
use MediaWiki\Parser\ParserOutput;
|
2020-12-04 15:28:25 +00:00
|
|
|
use MediaWiki\Parser\RevisionOutputCache;
|
2022-04-14 20:54:04 +00:00
|
|
|
use MediaWiki\Revision\MutableRevisionRecord;
|
2020-12-04 15:28:25 +00:00
|
|
|
use MediaWiki\Revision\RevisionRecord;
|
2024-05-04 08:58:57 +00:00
|
|
|
use MediaWiki\Tests\Json\JsonDeserializableSuperClass;
|
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-12-04 15:28:25 +00:00
|
|
|
use MediaWikiIntegrationTestCase;
|
|
|
|
|
use ParserOptions;
|
|
|
|
|
use Psr\Log\LoggerInterface;
|
|
|
|
|
use Psr\Log\LogLevel;
|
|
|
|
|
use Psr\Log\NullLogger;
|
|
|
|
|
use TestLogger;
|
|
|
|
|
use WANObjectCache;
|
2024-07-09 13:37:44 +00:00
|
|
|
use Wikimedia\ObjectCache\BagOStuff;
|
|
|
|
|
use Wikimedia\ObjectCache\HashBagOStuff;
|
2024-03-15 11:18:10 +00:00
|
|
|
use Wikimedia\Stats\StatsFactory;
|
2020-12-04 15:28:25 +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-12-04 15:28:25 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers \MediaWiki\Parser\RevisionOutputCache
|
|
|
|
|
*/
|
|
|
|
|
class RevisionOutputCacheTest extends MediaWikiIntegrationTestCase {
|
|
|
|
|
|
|
|
|
|
/** @var int */
|
|
|
|
|
private $time;
|
|
|
|
|
|
|
|
|
|
/** @var string */
|
|
|
|
|
private $cacheTime;
|
|
|
|
|
|
|
|
|
|
/** @var RevisionRecord */
|
|
|
|
|
private $revision;
|
|
|
|
|
|
2021-07-22 03:11:47 +00:00
|
|
|
protected function setUp(): void {
|
2020-12-04 15:28:25 +00:00
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
|
|
$this->time = time();
|
2021-03-24 14:05:44 +00:00
|
|
|
$this->cacheTime = MWTimestamp::convert( TS_MW, $this->time + 1 );
|
2020-12-04 15:28:25 +00:00
|
|
|
MWTimestamp::setFakeTime( $this->time );
|
|
|
|
|
|
2021-03-24 14:05:44 +00:00
|
|
|
$this->revision = new MutableRevisionRecord(
|
|
|
|
|
new PageIdentityValue(
|
|
|
|
|
42,
|
|
|
|
|
NS_MAIN,
|
|
|
|
|
'Testing_Testing',
|
|
|
|
|
PageIdentity::LOCAL
|
|
|
|
|
),
|
|
|
|
|
RevisionRecord::LOCAL
|
|
|
|
|
);
|
|
|
|
|
$this->revision->setId( 24 );
|
|
|
|
|
$this->revision->setTimestamp( MWTimestamp::convert( TS_MW, $this->time ) );
|
2020-12-04 15:28:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param BagOStuff|null $storage
|
|
|
|
|
* @param LoggerInterface|null $logger
|
2020-12-15 22:12:49 +00:00
|
|
|
* @param int $expiry
|
|
|
|
|
* @param string $epoch
|
|
|
|
|
*
|
2020-12-04 15:28:25 +00:00
|
|
|
* @return RevisionOutputCache
|
|
|
|
|
*/
|
|
|
|
|
private function createRevisionOutputCache(
|
|
|
|
|
BagOStuff $storage = null,
|
2020-12-15 22:12:49 +00:00
|
|
|
LoggerInterface $logger = null,
|
|
|
|
|
$expiry = 3600,
|
|
|
|
|
$epoch = '19900220000000'
|
2020-12-04 15:28:25 +00:00
|
|
|
): RevisionOutputCache {
|
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-12-04 15:28:25 +00:00
|
|
|
return new RevisionOutputCache(
|
|
|
|
|
'test',
|
|
|
|
|
new WANObjectCache( [ 'cache' => $storage ?: new HashBagOStuff() ] ),
|
2020-12-15 22:12:49 +00:00
|
|
|
$expiry,
|
|
|
|
|
$epoch,
|
2020-12-04 15:28:25 +00:00
|
|
|
new JsonCodec(),
|
2024-03-15 11:18:10 +00:00
|
|
|
StatsFactory::newNull(),
|
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
|
|
|
$logger ?: new NullLogger(),
|
|
|
|
|
$globalIdGenerator
|
2020-12-04 15:28:25 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
private function getDummyUsedOptions(): array {
|
|
|
|
|
return array_slice(
|
|
|
|
|
ParserOptions::allCacheVaryingOptions(),
|
|
|
|
|
0,
|
|
|
|
|
2
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return ParserOutput
|
|
|
|
|
*/
|
|
|
|
|
private function createDummyParserOutput(): ParserOutput {
|
|
|
|
|
$parserOutput = new ParserOutput();
|
2024-02-09 22:27:00 +00:00
|
|
|
$parserOutput->setRawText( 'TEST' );
|
2020-12-04 15:28:25 +00:00
|
|
|
foreach ( $this->getDummyUsedOptions() as $option ) {
|
|
|
|
|
$parserOutput->recordOption( $option );
|
|
|
|
|
}
|
|
|
|
|
$parserOutput->updateCacheExpiry( 4242 );
|
|
|
|
|
return $parserOutput;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers \MediaWiki\Parser\RevisionOutputCache::makeParserOutputKey
|
|
|
|
|
*/
|
|
|
|
|
public function testMakeParserOutputKey() {
|
|
|
|
|
$cache = $this->createRevisionOutputCache();
|
|
|
|
|
|
2022-06-16 13:22:24 +00:00
|
|
|
$options1 = ParserOptions::newFromAnon();
|
2020-12-04 15:28:25 +00:00
|
|
|
$options1->setOption( $this->getDummyUsedOptions()[0], 'value1' );
|
|
|
|
|
$key1 = $cache->makeParserOutputKey( $this->revision, $options1, $this->getDummyUsedOptions() );
|
|
|
|
|
$this->assertNotNull( $key1 );
|
|
|
|
|
|
2022-06-16 13:22:24 +00:00
|
|
|
$options2 = ParserOptions::newFromAnon();
|
2020-12-04 15:28:25 +00:00
|
|
|
$options2->setOption( $this->getDummyUsedOptions()[0], 'value2' );
|
|
|
|
|
$key2 = $cache->makeParserOutputKey( $this->revision, $options2, $this->getDummyUsedOptions() );
|
|
|
|
|
$this->assertNotNull( $key2 );
|
|
|
|
|
$this->assertNotSame( $key1, $key2 );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Test that fetching without storing first returns false.
|
|
|
|
|
* @covers \MediaWiki\Parser\RevisionOutputCache::get
|
|
|
|
|
*/
|
|
|
|
|
public function testGetEmpty() {
|
|
|
|
|
$cache = $this->createRevisionOutputCache();
|
2022-06-16 13:22:24 +00:00
|
|
|
$options = ParserOptions::newFromAnon();
|
2020-12-04 15:28:25 +00:00
|
|
|
|
|
|
|
|
$this->assertFalse( $cache->get( $this->revision, $options ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Test that fetching with the same options return the saved value.
|
|
|
|
|
* @covers \MediaWiki\Parser\RevisionOutputCache::get
|
|
|
|
|
* @covers \MediaWiki\Parser\RevisionOutputCache::save
|
|
|
|
|
*/
|
|
|
|
|
public function testSaveGetSameOptions() {
|
|
|
|
|
$cache = $this->createRevisionOutputCache();
|
|
|
|
|
$parserOutput = new ParserOutput( 'TEST_TEXT' );
|
|
|
|
|
|
2022-06-16 13:22:24 +00:00
|
|
|
$options1 = ParserOptions::newFromAnon();
|
2020-12-04 15:28:25 +00:00
|
|
|
$options1->setOption( $this->getDummyUsedOptions()[0], 'value1' );
|
|
|
|
|
$cache->save( $parserOutput, $this->revision, $options1, $this->cacheTime );
|
|
|
|
|
|
|
|
|
|
$savedOutput = $cache->get( $this->revision, $options1 );
|
|
|
|
|
$this->assertInstanceOf( ParserOutput::class, $savedOutput );
|
|
|
|
|
// RevisionOutputCache adds a comment to the HTML, so check if the result starts with page content.
|
2024-07-30 16:49:20 +00:00
|
|
|
$this->assertStringStartsWith( 'TEST_TEXT',
|
|
|
|
|
$savedOutput->getRawText() );
|
2020-12-04 15:28:25 +00:00
|
|
|
$this->assertSame( $this->cacheTime, $savedOutput->getCacheTime() );
|
|
|
|
|
$this->assertSame( $this->revision->getId(), $savedOutput->getCacheRevisionId() );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Test that non-cacheable output is not stored
|
|
|
|
|
* @covers \MediaWiki\Parser\RevisionOutputCache::save
|
|
|
|
|
*/
|
|
|
|
|
public function testDoesNotStoreNonCacheable() {
|
|
|
|
|
$cache = $this->createRevisionOutputCache();
|
|
|
|
|
$parserOutput = new ParserOutput( 'TEST_TEXT' );
|
|
|
|
|
$parserOutput->updateCacheExpiry( 0 );
|
|
|
|
|
|
2022-06-16 13:22:24 +00:00
|
|
|
$options1 = ParserOptions::newFromAnon();
|
2020-12-04 15:28:25 +00:00
|
|
|
$cache->save( $parserOutput, $this->revision, $options1, $this->cacheTime );
|
|
|
|
|
|
|
|
|
|
$this->assertFalse( $cache->get( $this->revision, $options1 ) );
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-15 22:12:49 +00:00
|
|
|
/**
|
|
|
|
|
* Test that setting the cache epoch will cause outdated entries to be ignored
|
|
|
|
|
* @covers \MediaWiki\Parser\RevisionOutputCache::get
|
|
|
|
|
*/
|
|
|
|
|
public function testExpiresByEpoch() {
|
|
|
|
|
$store = new HashBagOStuff();
|
|
|
|
|
$cache = $this->createRevisionOutputCache( $store );
|
|
|
|
|
$parserOutput = new ParserOutput( 'TEST_TEXT' );
|
|
|
|
|
|
2022-06-16 13:22:24 +00:00
|
|
|
$options = ParserOptions::newFromAnon();
|
2020-12-15 22:12:49 +00:00
|
|
|
$cache->save( $parserOutput, $this->revision, $options, $this->cacheTime );
|
|
|
|
|
|
|
|
|
|
// determine cache epoch younger than cache time
|
|
|
|
|
$cacheTime = MWTimestamp::convert( TS_UNIX, $parserOutput->getCacheTime() );
|
|
|
|
|
$epoch = MWTimestamp::convert( TS_MW, $cacheTime + 60 );
|
|
|
|
|
|
|
|
|
|
// create a cache with the new epoch
|
|
|
|
|
$cache = $this->createRevisionOutputCache( $store, null, 60 * 60, $epoch );
|
|
|
|
|
$this->assertFalse( $cache->get( $this->revision, $options ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Test that setting the cache expiry period will cause outdated entries to be ignored
|
|
|
|
|
* @covers \MediaWiki\Parser\RevisionOutputCache::get
|
|
|
|
|
*/
|
|
|
|
|
public function testExpiresByDuration() {
|
|
|
|
|
$store = new HashBagOStuff();
|
|
|
|
|
|
|
|
|
|
// original cache is good for an hour
|
|
|
|
|
$cache = $this->createRevisionOutputCache( $store );
|
|
|
|
|
$parserOutput = new ParserOutput( 'TEST_TEXT' );
|
|
|
|
|
|
2022-06-16 13:22:24 +00:00
|
|
|
$options = ParserOptions::newFromAnon();
|
2020-12-15 22:12:49 +00:00
|
|
|
$cache->save( $parserOutput, $this->revision, $options, $this->cacheTime );
|
|
|
|
|
|
|
|
|
|
// move the clock forward by 60 seconds
|
|
|
|
|
$cacheTime = MWTimestamp::convert( TS_UNIX, $parserOutput->getCacheTime() );
|
|
|
|
|
MWTimestamp::setFakeTime( $cacheTime + 60 );
|
|
|
|
|
|
|
|
|
|
// create a cache that expires after 30 seconds
|
|
|
|
|
$cache = $this->createRevisionOutputCache( $store, null, 30 );
|
|
|
|
|
$this->assertFalse( $cache->get( $this->revision, $options ) );
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-04 15:28:25 +00:00
|
|
|
/**
|
|
|
|
|
* Test that ParserOptions::isSafeToCache is respected on save
|
|
|
|
|
* @covers \MediaWiki\Parser\RevisionOutputCache::save
|
|
|
|
|
*/
|
|
|
|
|
public function testDoesNotStoreNotSafeToCache() {
|
|
|
|
|
$cache = $this->createRevisionOutputCache();
|
|
|
|
|
$parserOutput = new ParserOutput( 'TEST_TEXT' );
|
|
|
|
|
|
2022-06-16 13:22:24 +00:00
|
|
|
$options = ParserOptions::newFromAnon();
|
2020-12-04 15:28:25 +00:00
|
|
|
$options->setOption( 'wrapclass', 'wrapwrap' );
|
|
|
|
|
|
|
|
|
|
$cache->save( $parserOutput, $this->revision, $options, $this->cacheTime );
|
|
|
|
|
|
|
|
|
|
$this->assertFalse( $cache->get( $this->revision, $options ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Test that ParserOptions::isSafeToCache is respected on get
|
|
|
|
|
* @covers \MediaWiki\Parser\RevisionOutputCache::get
|
|
|
|
|
*/
|
|
|
|
|
public function testDoesNotGetNotSafeToCache() {
|
|
|
|
|
$cache = $this->createRevisionOutputCache();
|
|
|
|
|
$parserOutput = new ParserOutput( 'TEST_TEXT' );
|
|
|
|
|
|
2022-06-16 13:22:24 +00:00
|
|
|
$cache->save( $parserOutput, $this->revision, ParserOptions::newFromAnon(), $this->cacheTime );
|
2020-12-04 15:28:25 +00:00
|
|
|
|
2022-06-16 13:22:24 +00:00
|
|
|
$otherOptions = ParserOptions::newFromAnon();
|
2020-12-04 15:28:25 +00:00
|
|
|
$otherOptions->setOption( 'wrapclass', 'wrapwrap' );
|
|
|
|
|
|
|
|
|
|
$this->assertFalse( $cache->get( $this->revision, $otherOptions ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Test that fetching with different used option don't return a value.
|
|
|
|
|
* @covers \MediaWiki\Parser\RevisionOutputCache::get
|
|
|
|
|
* @covers \MediaWiki\Parser\RevisionOutputCache::save
|
|
|
|
|
*/
|
|
|
|
|
public function testSaveGetDifferentUsedOption() {
|
|
|
|
|
$cache = $this->createRevisionOutputCache();
|
|
|
|
|
$parserOutput = new ParserOutput( 'TEST_TEXT' );
|
|
|
|
|
$optionName = $this->getDummyUsedOptions()[0];
|
|
|
|
|
$parserOutput->recordOption( $optionName );
|
|
|
|
|
|
2022-06-16 13:22:24 +00:00
|
|
|
$options1 = ParserOptions::newFromAnon();
|
2020-12-04 15:28:25 +00:00
|
|
|
$options1->setOption( $optionName, 'value1' );
|
|
|
|
|
$cache->save( $parserOutput, $this->revision, $options1, $this->cacheTime );
|
|
|
|
|
|
2022-06-16 13:22:24 +00:00
|
|
|
$options2 = ParserOptions::newFromAnon();
|
2020-12-04 15:28:25 +00:00
|
|
|
$options2->setOption( $optionName, 'value2' );
|
|
|
|
|
$this->assertFalse( $cache->get( $this->revision, $options2 ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers \MediaWiki\Parser\RevisionOutputCache::save
|
|
|
|
|
*/
|
|
|
|
|
public function testSaveNoText() {
|
|
|
|
|
$this->expectException( InvalidArgumentException::class );
|
|
|
|
|
$this->createRevisionOutputCache()->save(
|
|
|
|
|
new ParserOutput( null ),
|
|
|
|
|
$this->revision,
|
2022-06-16 13:22:24 +00:00
|
|
|
ParserOptions::newFromAnon()
|
2020-12-04 15:28:25 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-23 11:36:19 +00:00
|
|
|
public static function provideCorruptData() {
|
2020-12-04 15:28:25 +00:00
|
|
|
yield 'JSON serialization, bad data' => [ 'bla bla' ];
|
|
|
|
|
yield 'JSON serialization, no _class_' => [ '{"test":"test"}' ];
|
|
|
|
|
yield 'JSON serialization, non-existing _class_' => [ '{"_class_":"NonExistentBogusClass"}' ];
|
|
|
|
|
|
2024-05-04 08:58:57 +00:00
|
|
|
$wrongInstance = new JsonDeserializableSuperClass( 'test' );
|
2020-12-04 15:28:25 +00:00
|
|
|
yield 'JSON serialization, wrong class' => [ json_encode( $wrongInstance->jsonSerialize() ) ];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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.
|
|
|
|
|
*
|
|
|
|
|
* @dataProvider provideCorruptData
|
|
|
|
|
* @covers \MediaWiki\Parser\RevisionOutputCache::get
|
|
|
|
|
* @covers \MediaWiki\Parser\RevisionOutputCache::restoreFromJson
|
|
|
|
|
* @param string $data
|
|
|
|
|
*/
|
|
|
|
|
public function testCorruptData( string $data ) {
|
|
|
|
|
$cache = $this->createRevisionOutputCache();
|
|
|
|
|
$parserOutput = new ParserOutput( 'TEST_TEXT' );
|
|
|
|
|
|
2022-06-16 13:22:24 +00:00
|
|
|
$options1 = ParserOptions::newFromAnon();
|
2020-12-04 15:28:25 +00:00
|
|
|
$cache->save( $parserOutput, $this->revision, $options1, $this->cacheTime );
|
|
|
|
|
|
|
|
|
|
$outputKey = $cache->makeParserOutputKey(
|
|
|
|
|
$this->revision,
|
|
|
|
|
$options1,
|
|
|
|
|
$parserOutput->getUsedOptions()
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$backend = TestingAccessWrapper::newFromObject( $cache )->cache;
|
|
|
|
|
$backend->set( $outputKey, $data );
|
|
|
|
|
|
|
|
|
|
// just make sure we don't crash and burn
|
|
|
|
|
$this->assertFalse( $cache->get( $this->revision, $options1 ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers \MediaWiki\Parser\RevisionOutputCache::encodeAsJson
|
|
|
|
|
*/
|
|
|
|
|
public function testNonSerializableJsonIsReported() {
|
|
|
|
|
$testLogger = new TestLogger( true );
|
|
|
|
|
$cache = $this->createRevisionOutputCache( null, $testLogger );
|
|
|
|
|
|
|
|
|
|
$parserOutput = $this->createDummyParserOutput();
|
|
|
|
|
$parserOutput->setExtensionData( 'test', new User() );
|
2022-06-16 13:22:24 +00:00
|
|
|
$cache->save( $parserOutput, $this->revision, ParserOptions::newFromAnon() );
|
2020-12-04 15:28:25 +00:00
|
|
|
$this->assertArraySubmapSame(
|
|
|
|
|
[ [ LogLevel::ERROR, 'Unable to serialize JSON' ] ],
|
|
|
|
|
$testLogger->getBuffer()
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers \MediaWiki\Parser\RevisionOutputCache::encodeAsJson
|
|
|
|
|
*/
|
|
|
|
|
public function testCyclicStructuresDoNotBlowUpInJson() {
|
2022-07-07 20:32:08 +00:00
|
|
|
$this->markTestSkipped( 'Temporarily disabled: T314338' );
|
2020-12-04 15:28:25 +00:00
|
|
|
$testLogger = new TestLogger( true );
|
|
|
|
|
$cache = $this->createRevisionOutputCache( 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->revision, ParserOptions::newFromAnon() );
|
2020-12-04 15:28:25 +00:00
|
|
|
$this->assertArraySubmapSame(
|
|
|
|
|
[ [ LogLevel::ERROR, 'Unable to serialize JSON' ] ],
|
|
|
|
|
$testLogger->getBuffer()
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Tests that unicode characters are not \u escaped
|
|
|
|
|
* @covers \MediaWiki\Parser\RevisionOutputCache::encodeAsJson
|
|
|
|
|
*/
|
|
|
|
|
public function testJsonEncodeUnicode() {
|
|
|
|
|
$unicodeCharacter = "Э";
|
|
|
|
|
$cache = $this->createRevisionOutputCache( new HashBagOStuff() );
|
|
|
|
|
$parserOutput = $this->createDummyParserOutput();
|
2024-02-09 22:27:00 +00:00
|
|
|
$parserOutput->setRawText( $unicodeCharacter );
|
2022-06-16 13:22:24 +00:00
|
|
|
$cache->save( $parserOutput, $this->revision, ParserOptions::newFromAnon() );
|
2020-12-04 15:28:25 +00:00
|
|
|
|
|
|
|
|
$backend = TestingAccessWrapper::newFromObject( $cache )->cache;
|
|
|
|
|
$json = $backend->get(
|
2022-06-16 13:22:24 +00:00
|
|
|
$cache->makeParserOutputKey( $this->revision, ParserOptions::newFromAnon() )
|
2020-12-04 15:28:25 +00:00
|
|
|
);
|
|
|
|
|
$this->assertStringNotContainsString( "\u003E", $json );
|
|
|
|
|
$this->assertStringContainsString( $unicodeCharacter, $json );
|
|
|
|
|
}
|
|
|
|
|
}
|