1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
|
<?php
namespace MediaWiki\Tests\Parser;
use InvalidArgumentException;
use MediaWiki\Json\JsonCodec;
use MediaWiki\Page\PageIdentity;
use MediaWiki\Page\PageIdentityValue;
use MediaWiki\Parser\ParserOptions;
use MediaWiki\Parser\ParserOutput;
use MediaWiki\Parser\RevisionOutputCache;
use MediaWiki\Revision\MutableRevisionRecord;
use MediaWiki\Revision\RevisionRecord;
use MediaWiki\Tests\Json\JsonDeserializableSuperClass;
use MediaWiki\User\User;
use MediaWiki\Utils\MWTimestamp;
use MediaWikiIntegrationTestCase;
use Psr\Log\LoggerInterface;
use Psr\Log\LogLevel;
use Psr\Log\NullLogger;
use TestLogger;
use Wikimedia\ObjectCache\BagOStuff;
use Wikimedia\ObjectCache\HashBagOStuff;
use Wikimedia\ObjectCache\WANObjectCache;
use Wikimedia\Stats\StatsFactory;
use Wikimedia\TestingAccessWrapper;
use Wikimedia\UUID\GlobalIdGenerator;
/**
* @covers \MediaWiki\Parser\RevisionOutputCache
*/
class RevisionOutputCacheTest extends MediaWikiIntegrationTestCase {
/** @var int */
private $time;
/** @var string */
private $cacheTime;
/** @var RevisionRecord */
private $revision;
protected function setUp(): void {
parent::setUp();
$this->time = time();
$this->cacheTime = MWTimestamp::convert( TS_MW, $this->time + 1 );
MWTimestamp::setFakeTime( $this->time );
$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 ) );
}
/**
* @param BagOStuff|null $storage
* @param LoggerInterface|null $logger
* @param int $expiry
* @param string $epoch
*
* @return RevisionOutputCache
*/
private function createRevisionOutputCache(
?BagOStuff $storage = null,
?LoggerInterface $logger = null,
$expiry = 3600,
$epoch = '19900220000000'
): RevisionOutputCache {
$globalIdGenerator = $this->createMock( GlobalIdGenerator::class );
$globalIdGenerator->method( 'newUUIDv1' )->willReturn( 'uuid-uuid' );
return new RevisionOutputCache(
'test',
new WANObjectCache( [ 'cache' => $storage ?: new HashBagOStuff() ] ),
$expiry,
$epoch,
new JsonCodec(),
StatsFactory::newNull(),
$logger ?: new NullLogger(),
$globalIdGenerator
);
}
/**
* @return array
*/
private function getDummyUsedOptions(): array {
return array_slice(
ParserOptions::allCacheVaryingOptions(),
0,
2
);
}
/**
* @return ParserOutput
*/
private function createDummyParserOutput(): ParserOutput {
$parserOutput = new ParserOutput();
$parserOutput->setRawText( 'TEST' );
foreach ( $this->getDummyUsedOptions() as $option ) {
$parserOutput->recordOption( $option );
}
$parserOutput->updateCacheExpiry( 4242 );
return $parserOutput;
}
/**
* @covers \MediaWiki\Parser\RevisionOutputCache::makeParserOutputKey
*/
public function testMakeParserOutputKey() {
$cache = $this->createRevisionOutputCache();
$options1 = ParserOptions::newFromAnon();
$options1->setOption( $this->getDummyUsedOptions()[0], 'value1' );
$key1 = $cache->makeParserOutputKey( $this->revision, $options1, $this->getDummyUsedOptions() );
$this->assertNotNull( $key1 );
$options2 = ParserOptions::newFromAnon();
$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();
$options = ParserOptions::newFromAnon();
$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' );
$options1 = ParserOptions::newFromAnon();
$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.
$this->assertStringStartsWith( 'TEST_TEXT',
$savedOutput->getRawText() );
$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 );
$options1 = ParserOptions::newFromAnon();
$cache->save( $parserOutput, $this->revision, $options1, $this->cacheTime );
$this->assertFalse( $cache->get( $this->revision, $options1 ) );
}
/**
* 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' );
$options = ParserOptions::newFromAnon();
$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' );
$options = ParserOptions::newFromAnon();
$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 ) );
}
/**
* Test that ParserOptions::isSafeToCache is respected on save
* @covers \MediaWiki\Parser\RevisionOutputCache::save
*/
public function testDoesNotStoreNotSafeToCache() {
$cache = $this->createRevisionOutputCache();
$parserOutput = new ParserOutput( 'TEST_TEXT' );
$options = ParserOptions::newFromAnon();
$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' );
$cache->save( $parserOutput, $this->revision, ParserOptions::newFromAnon(), $this->cacheTime );
$otherOptions = ParserOptions::newFromAnon();
$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 );
$options1 = ParserOptions::newFromAnon();
$options1->setOption( $optionName, 'value1' );
$cache->save( $parserOutput, $this->revision, $options1, $this->cacheTime );
$options2 = ParserOptions::newFromAnon();
$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,
ParserOptions::newFromAnon()
);
}
public static function provideCorruptData() {
yield 'JSON serialization, bad data' => [ 'bla bla' ];
yield 'JSON serialization, no _class_' => [ '{"test":"test"}' ];
yield 'JSON serialization, non-existing _class_' => [ '{"_class_":"NonExistentBogusClass"}' ];
$wrongInstance = new JsonDeserializableSuperClass( 'test' );
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' );
$options1 = ParserOptions::newFromAnon();
$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() );
$cache->save( $parserOutput, $this->revision, ParserOptions::newFromAnon() );
$this->assertArraySubmapSame(
[ [ LogLevel::ERROR, 'Unable to serialize JSON' ] ],
$testLogger->getBuffer()
);
}
/**
* @covers \MediaWiki\Parser\RevisionOutputCache::encodeAsJson
*/
public function testCyclicStructuresDoNotBlowUpInJson() {
$this->markTestSkipped( 'Temporarily disabled: T314338' );
$testLogger = new TestLogger( true );
$cache = $this->createRevisionOutputCache( null, $testLogger );
$parserOutput = $this->createDummyParserOutput();
$cyclicArray = [ 'a' => 'b' ];
$cyclicArray['c'] = &$cyclicArray;
$parserOutput->setExtensionData( 'test', $cyclicArray );
$cache->save( $parserOutput, $this->revision, ParserOptions::newFromAnon() );
$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();
$parserOutput->setRawText( $unicodeCharacter );
$cache->save( $parserOutput, $this->revision, ParserOptions::newFromAnon() );
$backend = TestingAccessWrapper::newFromObject( $cache )->cache;
$json = $backend->get(
$cache->makeParserOutputKey( $this->revision, ParserOptions::newFromAnon() )
);
$this->assertStringNotContainsString( "\u003E", $json );
$this->assertStringContainsString( $unicodeCharacter, $json );
}
}
|