| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Tests\Parser; |
| 4 |
|
| 5 |
use InvalidArgumentException; |
| 6 |
use MediaWiki\HookContainer\HookContainer; |
| 7 |
use MediaWiki\Json\JsonCodec; |
| 8 |
use MediaWiki\Page\PageRecord; |
| 9 |
use MediaWiki\Page\PageStoreRecord; |
| 10 |
use MediaWiki\Page\WikiPageFactory; |
| 11 |
use MediaWiki\Parser\CacheTime; |
| 12 |
use MediaWiki\Parser\ParserCache; |
| 13 |
use MediaWiki\Parser\ParserCacheFilter; |
| 14 |
use MediaWiki\Parser\ParserOptions; |
| 15 |
use MediaWiki\Parser\ParserOutput; |
| 16 |
use MediaWiki\Tests\Json\JsonDeserializableSuperClass; |
| 17 |
use MediaWiki\Title\Title; |
| 18 |
use MediaWiki\Title\TitleFactory; |
| 19 |
use MediaWiki\User\User; |
| 20 |
use MediaWiki\Utils\MWTimestamp; |
| 21 |
use MediaWikiIntegrationTestCase; |
| 22 |
use Psr\Log\LoggerInterface; |
| 23 |
use Psr\Log\LogLevel; |
| 24 |
use Psr\Log\NullLogger; |
| 25 |
use TestLogger; |
| 26 |
use Wikimedia\ObjectCache\BagOStuff; |
| 27 |
use Wikimedia\ObjectCache\EmptyBagOStuff; |
| 28 |
use Wikimedia\ObjectCache\HashBagOStuff; |
| 29 |
use Wikimedia\Stats\StatsFactory; |
| 30 |
use Wikimedia\TestingAccessWrapper; |
| 31 |
use Wikimedia\UUID\GlobalIdGenerator; |
| 32 |
use WikiPage; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
class ParserCacheTest extends MediaWikiIntegrationTestCase { |
| 38 |
|
| 39 |
|
| 40 |
private $time; |
| 41 |
|
| 42 |
|
| 43 |
private $cacheTime; |
| 44 |
|
| 45 |
|
| 46 |
private $page; |
| 47 |
|
| 48 |
protected function setUp(): void { |
| 49 |
parent::setUp(); |
| 50 |
$this->time = time(); |
| 51 |
$this->cacheTime = MWTimestamp::convert( TS_MW, $this->time + 1 ); |
| 52 |
$this->page = $this->createPageRecord(); |
| 53 |
|
| 54 |
MWTimestamp::setFakeTime( $this->time ); |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
private function createPageRecord( array $overrides = [] ): PageRecord { |
| 62 |
return new PageStoreRecord( (object)array_merge( [ |
| 63 |
'page_id' => 42, |
| 64 |
'page_namespace' => NS_MAIN, |
| 65 |
'page_title' => 'Testing_Testing', |
| 66 |
'page_latest' => 24, |
| 67 |
'page_is_new' => false, |
| 68 |
'page_is_redirect' => false, |
| 69 |
'page_touched' => $this->time, |
| 70 |
'page_lang' => 'qqx', |
| 71 |
], $overrides ), PageRecord::LOCAL ); |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
private function createParserCache( |
| 82 |
?HookContainer $hookContainer = null, |
| 83 |
?BagOStuff $storage = null, |
| 84 |
?LoggerInterface $logger = null, |
| 85 |
?WikiPageFactory $wikiPageFactory = null |
| 86 |
): ParserCache { |
| 87 |
if ( !$wikiPageFactory ) { |
| 88 |
$wikiPageMock = $this->createMock( WikiPage::class ); |
| 89 |
$wikiPageMock->method( 'getContentModel' )->willReturn( CONTENT_MODEL_WIKITEXT ); |
| 90 |
$wikiPageFactory = $this->createMock( WikiPageFactory::class ); |
| 91 |
$wikiPageFactory->method( 'newFromTitle' )->willReturn( $wikiPageMock ); |
| 92 |
} |
| 93 |
$globalIdGenerator = $this->createMock( GlobalIdGenerator::class ); |
| 94 |
$globalIdGenerator->method( 'newUUIDv1' )->willReturn( 'uuid-uuid' ); |
| 95 |
return new ParserCache( |
| 96 |
'test', |
| 97 |
$storage ?: new HashBagOStuff(), |
| 98 |
'19900220000000', |
| 99 |
$hookContainer ?: $this->createHookContainer( [] ), |
| 100 |
new JsonCodec(), |
| 101 |
StatsFactory::newNull(), |
| 102 |
$logger ?: new NullLogger(), |
| 103 |
$this->createMock( TitleFactory::class ), |
| 104 |
$wikiPageFactory, |
| 105 |
$globalIdGenerator |
| 106 |
); |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
private function getDummyUsedOptions(): array { |
| 113 |
return array_slice( |
| 114 |
ParserOptions::allCacheVaryingOptions(), |
| 115 |
0, |
| 116 |
2 |
| 117 |
); |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
private function createDummyParserOutput(): ParserOutput { |
| 124 |
$parserOutput = new ParserOutput(); |
| 125 |
$parserOutput->setRawText( 'TEST' ); |
| 126 |
foreach ( $this->getDummyUsedOptions() as $option ) { |
| 127 |
$parserOutput->recordOption( $option ); |
| 128 |
} |
| 129 |
$parserOutput->updateCacheExpiry( 4242 ); |
| 130 |
$parserOutput->setRenderId( 'dummy-render-id' ); |
| 131 |
$parserOutput->setCacheRevisionId( 0 ); |
| 132 |
|
| 133 |
$parserOutput->setRevisionTimestamp( $parserOutput->getCacheTime() ); |
| 134 |
return $parserOutput; |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
public function testGetMetadataMissing() { |
| 141 |
$cache = $this->createParserCache(); |
| 142 |
$metadataFromCache = $cache->getMetadata( $this->page, ParserCache::USE_CURRENT_ONLY ); |
| 143 |
$this->assertNull( $metadataFromCache ); |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
public function testGetMetadataAllGood() { |
| 150 |
$cache = $this->createParserCache(); |
| 151 |
$parserOutput = $this->createDummyParserOutput(); |
| 152 |
|
| 153 |
$cache->save( $parserOutput, $this->page, ParserOptions::newFromAnon(), $this->cacheTime ); |
| 154 |
|
| 155 |
$metadataFromCache = $cache->getMetadata( $this->page, ParserCache::USE_CURRENT_ONLY ); |
| 156 |
$this->assertNotNull( $metadataFromCache ); |
| 157 |
$this->assertSame( $this->getDummyUsedOptions(), $metadataFromCache->getUsedOptions() ); |
| 158 |
$this->assertSame( 4242, $metadataFromCache->getCacheExpiry() ); |
| 159 |
$this->assertSame( $this->page->getLatest(), $metadataFromCache->getCacheRevisionId() ); |
| 160 |
$this->assertSame( $this->cacheTime, $metadataFromCache->getCacheTime() ); |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
public function testGetMetadataExpired() { |
| 167 |
$cache = $this->createParserCache(); |
| 168 |
$parserOutput = $this->createDummyParserOutput(); |
| 169 |
$cache->save( $parserOutput, $this->page, ParserOptions::newFromAnon(), $this->cacheTime ); |
| 170 |
|
| 171 |
$this->page = $this->createPageRecord( [ 'page_touched' => $this->time + 10000 ] ); |
| 172 |
$this->assertNull( $cache->getMetadata( $this->page, ParserCache::USE_CURRENT_ONLY ) ); |
| 173 |
$metadataFromCache = $cache->getMetadata( $this->page, ParserCache::USE_EXPIRED ); |
| 174 |
$this->assertNotNull( $metadataFromCache ); |
| 175 |
$this->assertSame( $this->getDummyUsedOptions(), $metadataFromCache->getUsedOptions() ); |
| 176 |
$this->assertSame( 4242, $metadataFromCache->getCacheExpiry() ); |
| 177 |
$this->assertSame( $this->page->getLatest(), $metadataFromCache->getCacheRevisionId() ); |
| 178 |
$this->assertSame( $this->cacheTime, $metadataFromCache->getCacheTime() ); |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
public function testGetMetadataOutdated() { |
| 185 |
$cache = $this->createParserCache(); |
| 186 |
$parserOutput = $this->createDummyParserOutput(); |
| 187 |
$cache->save( $parserOutput, $this->page, ParserOptions::newFromAnon(), $this->cacheTime ); |
| 188 |
|
| 189 |
$this->page = $this->createPageRecord( [ 'page_latest' => $this->page->getLatest() + 1 ] ); |
| 190 |
$this->assertNull( $cache->getMetadata( $this->page, ParserCache::USE_CURRENT_ONLY ) ); |
| 191 |
$this->assertNull( $cache->getMetadata( $this->page, ParserCache::USE_EXPIRED ) ); |
| 192 |
$metadataFromCache = $cache->getMetadata( $this->page, ParserCache::USE_OUTDATED ); |
| 193 |
$this->assertSame( $this->getDummyUsedOptions(), $metadataFromCache->getUsedOptions() ); |
| 194 |
$this->assertSame( 4242, $metadataFromCache->getCacheExpiry() ); |
| 195 |
$this->assertNotSame( $this->page->getLatest(), $metadataFromCache->getCacheRevisionId() ); |
| 196 |
$this->assertSame( $this->cacheTime, $metadataFromCache->getCacheTime() ); |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
public function testMakeParserOutputKey() { |
| 203 |
$cache = $this->createParserCache(); |
| 204 |
|
| 205 |
$options1 = ParserOptions::newFromAnon(); |
| 206 |
$options1->setOption( $this->getDummyUsedOptions()[0], 'value1' ); |
| 207 |
$key1 = $cache->makeParserOutputKey( $this->page, $options1, $this->getDummyUsedOptions() ); |
| 208 |
$this->assertNotNull( $key1 ); |
| 209 |
|
| 210 |
$options2 = ParserOptions::newFromAnon(); |
| 211 |
$options2->setOption( $this->getDummyUsedOptions()[0], 'value2' ); |
| 212 |
$key2 = $cache->makeParserOutputKey( $this->page, $options2, $this->getDummyUsedOptions() ); |
| 213 |
$this->assertNotNull( $key2 ); |
| 214 |
$this->assertNotSame( $key1, $key2 ); |
| 215 |
} |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
public function testGetEmpty() { |
| 222 |
$cache = $this->createParserCache(); |
| 223 |
$options = ParserOptions::newFromAnon(); |
| 224 |
|
| 225 |
$this->assertFalse( $cache->get( $this->page, $options ) ); |
| 226 |
} |
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
public function testSaveGetSameOptions() { |
| 234 |
$cache = $this->createParserCache(); |
| 235 |
$parserOutput = new ParserOutput( 'TEST_TEXT' ); |
| 236 |
|
| 237 |
$options1 = ParserOptions::newFromAnon(); |
| 238 |
$options1->setOption( $this->getDummyUsedOptions()[0], 'value1' ); |
| 239 |
$cache->save( $parserOutput, $this->page, $options1, $this->cacheTime ); |
| 240 |
|
| 241 |
$savedOutput = $cache->get( $this->page, $options1 ); |
| 242 |
$this->assertInstanceOf( ParserOutput::class, $savedOutput ); |
| 243 |
|
| 244 |
$this->assertStringStartsWith( 'TEST_TEXT', $savedOutput->getRawText() ); |
| 245 |
$this->assertSame( $this->cacheTime, $savedOutput->getCacheTime() ); |
| 246 |
$this->assertSame( $this->page->getLatest(), $savedOutput->getCacheRevisionId() ); |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
public function testSaveGetDifferentUnusedOption() { |
| 255 |
$cache = $this->createParserCache(); |
| 256 |
$optionName = $this->getDummyUsedOptions()[0]; |
| 257 |
$parserOutput = new ParserOutput( 'TEST_TEXT' ); |
| 258 |
|
| 259 |
$options1 = ParserOptions::newFromAnon(); |
| 260 |
$options1->setOption( $optionName, 'value1' ); |
| 261 |
$cache->save( $parserOutput, $this->page, $options1, $this->cacheTime ); |
| 262 |
|
| 263 |
$options2 = ParserOptions::newFromAnon(); |
| 264 |
$options2->setOption( $optionName, 'value2' ); |
| 265 |
$savedOutput = $cache->get( $this->page, $options2 ); |
| 266 |
$this->assertInstanceOf( ParserOutput::class, $savedOutput ); |
| 267 |
|
| 268 |
$this->assertStringStartsWith( 'TEST_TEXT', $savedOutput->getRawText() ); |
| 269 |
$this->assertSame( $this->cacheTime, $savedOutput->getCacheTime() ); |
| 270 |
$this->assertSame( $this->page->getLatest(), $savedOutput->getCacheRevisionId() ); |
| 271 |
} |
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
public function testDoesNotStoreNonCacheable() { |
| 279 |
$cache = $this->createParserCache(); |
| 280 |
$parserOutput = new ParserOutput( 'TEST_TEXT' ); |
| 281 |
$parserOutput->updateCacheExpiry( 0 ); |
| 282 |
|
| 283 |
$options1 = ParserOptions::newFromAnon(); |
| 284 |
$cache->save( $parserOutput, $this->page, $options1, $this->cacheTime ); |
| 285 |
|
| 286 |
$this->assertFalse( $cache->get( $this->page, $options1 ) ); |
| 287 |
$this->assertFalse( $cache->get( $this->page, $options1, true ) ); |
| 288 |
$this->assertFalse( $cache->getDirty( $this->page, $options1 ) ); |
| 289 |
} |
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
public function testDoesNotStoreFiltered() { |
| 297 |
$cache = $this->createParserCache(); |
| 298 |
$parserOutput = new ParserOutput( 'TEST_TEXT' ); |
| 299 |
$parserOutput->resetParseStartTime(); |
| 300 |
$parserOutput->recordTimeProfile(); |
| 301 |
|
| 302 |
|
| 303 |
$cache->setFilter( new ParserCacheFilter( [ |
| 304 |
'default' => [ 'minCpuTime' => 100 ] |
| 305 |
] ) ); |
| 306 |
|
| 307 |
$options1 = ParserOptions::newFromAnon(); |
| 308 |
$cache->save( $parserOutput, $this->page, $options1, $this->cacheTime ); |
| 309 |
|
| 310 |
$this->assertFalse( $cache->get( $this->page, $options1 ) ); |
| 311 |
$this->assertFalse( $cache->get( $this->page, $options1, true ) ); |
| 312 |
$this->assertFalse( $cache->getDirty( $this->page, $options1 ) ); |
| 313 |
} |
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
public function testDoesNotStoreNotSafeToCacheAndUsed() { |
| 320 |
$cache = $this->createParserCache(); |
| 321 |
$parserOutput = new ParserOutput( 'TEST_TEXT' ); |
| 322 |
$parserOutput->recordOption( 'wrapclass' ); |
| 323 |
|
| 324 |
$options = ParserOptions::newFromAnon(); |
| 325 |
$options->setOption( 'wrapclass', 'wrapwrap' ); |
| 326 |
|
| 327 |
$cache->save( $parserOutput, $this->page, $options, $this->cacheTime ); |
| 328 |
|
| 329 |
$this->assertFalse( $cache->get( $this->page, $options ) ); |
| 330 |
$this->assertFalse( $cache->get( $this->page, $options, true ) ); |
| 331 |
$this->assertFalse( $cache->getDirty( $this->page, $options ) ); |
| 332 |
} |
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
public function testDoesNotGetNotSafeToCache() { |
| 339 |
$cache = $this->createParserCache(); |
| 340 |
$parserOutput = new ParserOutput( 'TEST_TEXT' ); |
| 341 |
$parserOutput->recordOption( 'wrapclass' ); |
| 342 |
|
| 343 |
$cache->save( $parserOutput, $this->page, ParserOptions::newFromAnon(), $this->cacheTime ); |
| 344 |
|
| 345 |
$otherOptions = ParserOptions::newFromAnon(); |
| 346 |
$otherOptions->setOption( 'wrapclass', 'wrapwrap' ); |
| 347 |
|
| 348 |
$this->assertFalse( $cache->get( $this->page, $otherOptions ) ); |
| 349 |
$this->assertFalse( $cache->get( $this->page, $otherOptions, true ) ); |
| 350 |
$this->assertFalse( $cache->getDirty( $this->page, $otherOptions ) ); |
| 351 |
} |
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
public function testStoresNotSafeToCacheAndUnused() { |
| 359 |
$cache = $this->createParserCache(); |
| 360 |
$parserOutput = new ParserOutput( 'TEST_TEXT' ); |
| 361 |
|
| 362 |
$options = ParserOptions::newFromAnon(); |
| 363 |
$options->setOption( 'wrapclass', 'wrapwrap' ); |
| 364 |
|
| 365 |
$cache->save( $parserOutput, $this->page, $options, $this->cacheTime ); |
| 366 |
$this->assertStringContainsString( 'TEST_TEXT', $cache->get( $this->page, $options ) |
| 367 |
->getRawText() ); |
| 368 |
} |
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
public function testSaveGetDifferentUsedOption() { |
| 376 |
$cache = $this->createParserCache(); |
| 377 |
$parserOutput = new ParserOutput( 'TEST_TEXT' ); |
| 378 |
$optionName = $this->getDummyUsedOptions()[0]; |
| 379 |
$parserOutput->recordOption( $optionName ); |
| 380 |
|
| 381 |
$options1 = ParserOptions::newFromAnon(); |
| 382 |
$options1->setOption( $optionName, 'value1' ); |
| 383 |
$cache->save( $parserOutput, $this->page, $options1, $this->cacheTime ); |
| 384 |
|
| 385 |
$options2 = ParserOptions::newFromAnon(); |
| 386 |
$options2->setOption( $optionName, 'value2' ); |
| 387 |
$this->assertFalse( $cache->get( $this->page, $options2 ) ); |
| 388 |
} |
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
public function testGetExpiredMetadata() { |
| 396 |
$cache = $this->createParserCache(); |
| 397 |
$parserOutput = new ParserOutput( 'TEST_TEXT' ); |
| 398 |
$parserOutput->updateCacheExpiry( 10 ); |
| 399 |
|
| 400 |
$options1 = ParserOptions::newFromAnon(); |
| 401 |
$cache->save( $parserOutput, $this->page, $options1, $this->cacheTime ); |
| 402 |
|
| 403 |
MWTimestamp::setFakeTime( $this->time + 15 * 1000 ); |
| 404 |
$this->assertFalse( $cache->get( $this->page, $options1 ) ); |
| 405 |
$this->assertInstanceOf( ParserOutput::class, |
| 406 |
$cache->get( $this->page, $options1, true ) ); |
| 407 |
$this->assertInstanceOf( ParserOutput::class, |
| 408 |
$cache->getDirty( $this->page, $options1 ) ); |
| 409 |
} |
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
public function testGetExpiredContent() { |
| 417 |
$cache = $this->createParserCache(); |
| 418 |
$optionName = $this->getDummyUsedOptions()[0]; |
| 419 |
|
| 420 |
$parserOutput1 = new ParserOutput( 'TEST_TEXT1' ); |
| 421 |
$parserOutput1->recordOption( $optionName ); |
| 422 |
$parserOutput1->updateCacheExpiry( 10 ); |
| 423 |
$options1 = ParserOptions::newFromAnon(); |
| 424 |
$options1->setOption( $optionName, 'value1' ); |
| 425 |
$cache->save( $parserOutput1, $this->page, $options1, $this->cacheTime ); |
| 426 |
|
| 427 |
$parserOutput2 = new ParserOutput( 'TEST_TEXT2' ); |
| 428 |
$parserOutput2->recordOption( $optionName ); |
| 429 |
$parserOutput2->updateCacheExpiry( 100500600 ); |
| 430 |
$options2 = ParserOptions::newFromAnon(); |
| 431 |
$options2->setOption( $optionName, 'value2' ); |
| 432 |
$cache->save( $parserOutput2, $this->page, $options2, $this->cacheTime ); |
| 433 |
|
| 434 |
MWTimestamp::setFakeTime( $this->time + 15 * 1000 ); |
| 435 |
$this->assertFalse( $cache->get( $this->page, $options1 ) ); |
| 436 |
$this->assertInstanceOf( ParserOutput::class, |
| 437 |
$cache->get( $this->page, $options1, true ) ); |
| 438 |
$this->assertInstanceOf( ParserOutput::class, |
| 439 |
$cache->getDirty( $this->page, $options1 ) ); |
| 440 |
} |
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
public function testGetOutdatedMetadata() { |
| 448 |
$cache = $this->createParserCache(); |
| 449 |
$parserOutput = new ParserOutput( 'TEST_TEXT' ); |
| 450 |
|
| 451 |
$options1 = ParserOptions::newFromAnon(); |
| 452 |
$cache->save( $parserOutput, $this->page, $options1, $this->cacheTime ); |
| 453 |
$this->assertInstanceOf( ParserOutput::class, |
| 454 |
$cache->get( $this->page, $options1 ) ); |
| 455 |
|
| 456 |
$this->page = $this->createPageRecord( [ 'page_latest' => $this->page->getLatest() + 1 ] ); |
| 457 |
$this->assertFalse( $cache->get( $this->page, $options1 ) ); |
| 458 |
$this->assertInstanceOf( ParserOutput::class, |
| 459 |
$cache->get( $this->page, $options1, true ) ); |
| 460 |
$this->assertInstanceOf( ParserOutput::class, |
| 461 |
$cache->getDirty( $this->page, $options1 ) ); |
| 462 |
} |
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
public function testGetOutdatedContent() { |
| 470 |
$cache = $this->createParserCache(); |
| 471 |
$optionName = $this->getDummyUsedOptions()[0]; |
| 472 |
|
| 473 |
$parserOutput1 = new ParserOutput( 'TEST_TEXT' ); |
| 474 |
$parserOutput1->recordOption( $optionName ); |
| 475 |
$options1 = ParserOptions::newFromAnon(); |
| 476 |
$options1->setOption( $optionName, 'value1' ); |
| 477 |
$cache->save( $parserOutput1, $this->page, $options1, $this->cacheTime ); |
| 478 |
|
| 479 |
$this->page = $this->createPageRecord( [ 'page_latest' => $this->page->getLatest() + 1 ] ); |
| 480 |
$parserOutput2 = new ParserOutput( 'TEST_TEXT' ); |
| 481 |
$parserOutput2->recordOption( $optionName ); |
| 482 |
$options2 = ParserOptions::newFromAnon(); |
| 483 |
$options2->setOption( $optionName, 'value2' ); |
| 484 |
$cache->save( $parserOutput2, $this->page, $options2, $this->cacheTime ); |
| 485 |
|
| 486 |
$this->assertFalse( $cache->get( $this->page, $options1 ) ); |
| 487 |
$this->assertInstanceOf( ParserOutput::class, |
| 488 |
$cache->get( $this->page, $options1, true ) ); |
| 489 |
$this->assertInstanceOf( ParserOutput::class, |
| 490 |
$cache->getDirty( $this->page, $options1 ) ); |
| 491 |
} |
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
public function testDeleteOptionsKey() { |
| 498 |
$cache = $this->createParserCache(); |
| 499 |
$parserOutput = new ParserOutput( 'TEST_TEXT' ); |
| 500 |
|
| 501 |
$options1 = ParserOptions::newFromAnon(); |
| 502 |
$cache->save( $parserOutput, $this->page, $options1, $this->cacheTime ); |
| 503 |
$this->assertInstanceOf( ParserOutput::class, |
| 504 |
$cache->get( $this->page, $options1 ) ); |
| 505 |
$cache->deleteOptionsKey( $this->page ); |
| 506 |
|
| 507 |
$this->assertFalse( $cache->get( $this->page, $options1 ) ); |
| 508 |
} |
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
public function testRejectedByHook() { |
| 515 |
$parserOutput = new ParserOutput( 'TEST_TEXT' ); |
| 516 |
$options = ParserOptions::newFromAnon(); |
| 517 |
$options->setOption( $this->getDummyUsedOptions()[0], 'value1' ); |
| 518 |
|
| 519 |
$wikiPageMock = $this->createMock( WikiPage::class ); |
| 520 |
$wikiPageMock->method( 'getContentModel' )->willReturn( CONTENT_MODEL_WIKITEXT ); |
| 521 |
$wikiPageFactory = $this->createMock( WikiPageFactory::class ); |
| 522 |
$wikiPageFactory->method( 'newFromTitle' ) |
| 523 |
->with( $this->page ) |
| 524 |
->willReturn( $wikiPageMock ); |
| 525 |
$hookContainer = $this->createHookContainer( [ |
| 526 |
'RejectParserCacheValue' => |
| 527 |
function ( ParserOutput $value, WikiPage $hookPage, ParserOptions $popts ) |
| 528 |
use ( $wikiPageMock, $parserOutput, $options ) { |
| 529 |
|
| 530 |
$parserOutput->clearParseStartTime(); |
| 531 |
$this->assertEquals( $parserOutput, $value ); |
| 532 |
$this->assertSame( $wikiPageMock, $hookPage ); |
| 533 |
$this->assertSame( $options, $popts ); |
| 534 |
return false; |
| 535 |
} |
| 536 |
] ); |
| 537 |
$cache = $this->createParserCache( $hookContainer, null, null, $wikiPageFactory ); |
| 538 |
$cache->save( $parserOutput, $this->page, $options, $this->cacheTime ); |
| 539 |
$this->assertFalse( $cache->get( $this->page, $options ) ); |
| 540 |
} |
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
public function testParserCacheSaveCompleteHook() { |
| 547 |
$parserOutput = new ParserOutput( 'TEST_TEXT' ); |
| 548 |
$options = ParserOptions::newFromAnon(); |
| 549 |
$options->setOption( $this->getDummyUsedOptions()[0], 'value1' ); |
| 550 |
|
| 551 |
$hookContainer = $this->createHookContainer( [ |
| 552 |
'ParserCacheSaveComplete' => |
| 553 |
function ( |
| 554 |
ParserCache $hookCache, ParserOutput $value, Title $hookTitle, ParserOptions $popts, int $revId |
| 555 |
) use ( $parserOutput, $options ) { |
| 556 |
$this->assertSame( $parserOutput, $value ); |
| 557 |
$this->assertSame( $options, $popts ); |
| 558 |
$this->assertSame( 42, $revId ); |
| 559 |
} |
| 560 |
] ); |
| 561 |
$cache = $this->createParserCache( $hookContainer ); |
| 562 |
$cache->save( $parserOutput, $this->page, $options, $this->cacheTime, 42 ); |
| 563 |
} |
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
public function testSkipIfNotExist() { |
| 570 |
$mockPage = $this->createNoOpMock( PageRecord::class, [ 'exists', 'assertWiki' ] ); |
| 571 |
$mockPage->method( 'exists' )->willReturn( false ); |
| 572 |
$wikiPageMock = $this->createMock( WikiPage::class ); |
| 573 |
$wikiPageMock->method( 'getContentModel' )->willReturn( 'wikitext' ); |
| 574 |
$wikiPageFactoryMock = $this->createMock( WikiPageFactory::class ); |
| 575 |
$wikiPageFactoryMock->method( 'newFromTitle' ) |
| 576 |
->with( $mockPage ) |
| 577 |
->willReturn( $wikiPageMock ); |
| 578 |
$cache = $this->createParserCache( null, null, null, $wikiPageFactoryMock ); |
| 579 |
$this->assertFalse( $cache->get( $mockPage, ParserOptions::newFromAnon() ) ); |
| 580 |
} |
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
public function testSkipIfRedirect() { |
| 587 |
$cache = $this->createParserCache(); |
| 588 |
$page = $this->createPageRecord( [ |
| 589 |
'page_is_redirect' => true |
| 590 |
] ); |
| 591 |
$this->assertFalse( $cache->get( $page, ParserOptions::newFromAnon() ) ); |
| 592 |
} |
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
public function testGetCacheStorage() { |
| 599 |
$storage = new EmptyBagOStuff(); |
| 600 |
$cache = $this->createParserCache( null, $storage ); |
| 601 |
$this->assertSame( $storage, $cache->getCacheStorage() ); |
| 602 |
} |
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
public function testSaveNoText() { |
| 608 |
$this->expectException( InvalidArgumentException::class ); |
| 609 |
$this->createParserCache()->save( |
| 610 |
new ParserOutput( null ), |
| 611 |
$this->page, |
| 612 |
ParserOptions::newFromAnon() |
| 613 |
); |
| 614 |
} |
| 615 |
|
| 616 |
public static function provideCorruptData() { |
| 617 |
yield 'JSON serialization, bad data' => [ 'bla bla' ]; |
| 618 |
yield 'JSON serialization, no _class_' => [ '{"test":"test"}' ]; |
| 619 |
yield 'JSON serialization, non-existing _class_' => [ '{"_class_":"NonExistentBogusClass"}' ]; |
| 620 |
$wrongInstance = new JsonDeserializableSuperClass( 'test' ); |
| 621 |
yield 'JSON serialization, wrong class' => [ json_encode( $wrongInstance->jsonSerialize() ) ]; |
| 622 |
} |
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
public function testCorruptData( string $data ) { |
| 636 |
$cache = $this->createParserCache( null, new HashBagOStuff() ); |
| 637 |
$parserOutput = new ParserOutput( 'TEST_TEXT' ); |
| 638 |
|
| 639 |
$options1 = ParserOptions::newFromAnon(); |
| 640 |
$cache->save( $parserOutput, $this->page, $options1, $this->cacheTime ); |
| 641 |
|
| 642 |
$outputKey = $cache->makeParserOutputKey( |
| 643 |
$this->page, |
| 644 |
$options1, |
| 645 |
$parserOutput->getUsedOptions() |
| 646 |
); |
| 647 |
|
| 648 |
$cache->getCacheStorage()->set( $outputKey, $data ); |
| 649 |
|
| 650 |
|
| 651 |
$this->assertFalse( $cache->get( $this->page, $options1 ) ); |
| 652 |
} |
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
public function testCorruptMetadata() { |
| 663 |
$cacheStorage = new HashBagOStuff(); |
| 664 |
$cache = $this->createParserCache( null, $cacheStorage ); |
| 665 |
$parserOutput = new ParserOutput( 'TEST_TEXT' ); |
| 666 |
|
| 667 |
$options1 = ParserOptions::newFromAnon(); |
| 668 |
$cache->save( $parserOutput, $this->page, $options1, $this->cacheTime ); |
| 669 |
|
| 670 |
|
| 671 |
$optionsKey = TestingAccessWrapper::newFromObject( $cache )->makeMetadataKey( |
| 672 |
$this->page |
| 673 |
); |
| 674 |
$cacheStorage->set( $optionsKey, 'bad data' ); |
| 675 |
|
| 676 |
|
| 677 |
$cache = $this->createParserCache( null, $cacheStorage ); |
| 678 |
|
| 679 |
|
| 680 |
$this->assertNull( $cache->getMetadata( $this->page ) ); |
| 681 |
} |
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
public function testMigrationToJson() { |
| 690 |
$bagOStuff = new HashBagOStuff(); |
| 691 |
|
| 692 |
$wikiPageMock = $this->createMock( WikiPage::class ); |
| 693 |
$wikiPageMock->method( 'getContentModel' )->willReturn( CONTENT_MODEL_WIKITEXT ); |
| 694 |
$wikiPageFactory = $this->createMock( WikiPageFactory::class ); |
| 695 |
$wikiPageFactory->method( 'newFromTitle' )->willReturn( $wikiPageMock ); |
| 696 |
$globalIdGenerator = $this->createMock( GlobalIdGenerator::class ); |
| 697 |
$globalIdGenerator->method( 'newUUIDv1' )->willReturn( 'uuid-uuid' ); |
| 698 |
$cache = $this->getMockBuilder( ParserCache::class ) |
| 699 |
->setConstructorArgs( [ |
| 700 |
'test', |
| 701 |
$bagOStuff, |
| 702 |
'19900220000000', |
| 703 |
$this->createHookContainer( [] ), |
| 704 |
new JsonCodec(), |
| 705 |
StatsFactory::newNull(), |
| 706 |
new NullLogger(), |
| 707 |
$this->createMock( TitleFactory::class ), |
| 708 |
$wikiPageFactory, |
| 709 |
$globalIdGenerator |
| 710 |
] ) |
| 711 |
->onlyMethods( [ 'convertForCache' ] ) |
| 712 |
->getMock(); |
| 713 |
|
| 714 |
|
| 715 |
|
| 716 |
|
| 717 |
|
| 718 |
$cache->method( 'convertForCache' )->willReturnCallback( |
| 719 |
static function ( CacheTime $obj, string $key ) { |
| 720 |
return $obj; |
| 721 |
} |
| 722 |
); |
| 723 |
|
| 724 |
$parserOutput1 = new ParserOutput( 'Lorem Ipsum' ); |
| 725 |
|
| 726 |
$options = ParserOptions::newFromAnon(); |
| 727 |
$cache->save( $parserOutput1, $this->page, $options, $this->cacheTime ); |
| 728 |
|
| 729 |
|
| 730 |
$cache = $this->createParserCache( null, $bagOStuff ); |
| 731 |
|
| 732 |
|
| 733 |
$cachedOutput = $cache->get( $this->page, $options ); |
| 734 |
$parserOutput1->clearParseStartTime(); |
| 735 |
$this->assertEquals( $parserOutput1, $cachedOutput ); |
| 736 |
|
| 737 |
|
| 738 |
$parserOutput2 = new ParserOutput( 'dolor sit amet' ); |
| 739 |
$cache->save( $parserOutput2, $this->page, $options, $this->cacheTime ); |
| 740 |
|
| 741 |
|
| 742 |
$cachedOutput = $cache->get( $this->page, $options ); |
| 743 |
$parserOutput2->clearParseStartTime(); |
| 744 |
$this->assertEquals( $parserOutput2, $cachedOutput ); |
| 745 |
} |
| 746 |
|
| 747 |
|
| 748 |
|
| 749 |
|
| 750 |
public function testNonSerializableJsonIsReported() { |
| 751 |
$testLogger = new TestLogger( true ); |
| 752 |
$cache = $this->createParserCache( null, null, $testLogger ); |
| 753 |
|
| 754 |
$parserOutput = $this->createDummyParserOutput(); |
| 755 |
$parserOutput->setExtensionData( 'test', new User() ); |
| 756 |
$cache->save( $parserOutput, $this->page, ParserOptions::newFromAnon() ); |
| 757 |
$this->assertArraySubmapSame( |
| 758 |
[ [ LogLevel::ERROR, 'Unable to serialize JSON' ] ], |
| 759 |
$testLogger->getBuffer() |
| 760 |
); |
| 761 |
} |
| 762 |
|
| 763 |
|
| 764 |
|
| 765 |
|
| 766 |
public function testCyclicStructuresDoNotBlowUpInJson() { |
| 767 |
$this->markTestSkipped( 'Temporarily disabled: T314338' ); |
| 768 |
$testLogger = new TestLogger( true ); |
| 769 |
$cache = $this->createParserCache( null, null, $testLogger ); |
| 770 |
|
| 771 |
$parserOutput = $this->createDummyParserOutput(); |
| 772 |
$cyclicArray = [ 'a' => 'b' ]; |
| 773 |
$cyclicArray['c'] = &$cyclicArray; |
| 774 |
$parserOutput->setExtensionData( 'test', $cyclicArray ); |
| 775 |
$cache->save( $parserOutput, $this->page, ParserOptions::newFromAnon() ); |
| 776 |
$this->assertArraySubmapSame( |
| 777 |
[ [ LogLevel::ERROR, 'Unable to serialize JSON' ] ], |
| 778 |
$testLogger->getBuffer() |
| 779 |
); |
| 780 |
} |
| 781 |
|
| 782 |
|
| 783 |
|
| 784 |
|
| 785 |
|
| 786 |
|
| 787 |
public function testJsonEncodeUnicode() { |
| 788 |
$unicodeCharacter = "Э"; |
| 789 |
$cache = $this->createParserCache( null, new HashBagOStuff() ); |
| 790 |
|
| 791 |
$parserOutput = $this->createDummyParserOutput(); |
| 792 |
$parserOutput->setRawText( $unicodeCharacter ); |
| 793 |
$cache->save( $parserOutput, $this->page, ParserOptions::newFromAnon() ); |
| 794 |
$json = $cache->getCacheStorage()->get( |
| 795 |
$cache->makeParserOutputKey( $this->page, ParserOptions::newFromAnon() ) |
| 796 |
); |
| 797 |
$this->assertStringNotContainsString( "\u003E", $json ); |
| 798 |
$this->assertStringContainsString( $unicodeCharacter, $json ); |
| 799 |
} |
| 800 |
} |