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