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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
|
<?php
namespace MediaWiki\Tests\Parser;
use InvalidArgumentException;
use MediaWiki\HookContainer\HookContainer;
use MediaWiki\Json\JsonCodec;
use MediaWiki\Page\PageRecord;
use MediaWiki\Page\PageStoreRecord;
use MediaWiki\Page\WikiPageFactory;
use MediaWiki\Parser\CacheTime;
use MediaWiki\Parser\ParserCache;
use MediaWiki\Parser\ParserCacheFilter;
use MediaWiki\Parser\ParserOptions;
use MediaWiki\Parser\ParserOutput;
use MediaWiki\Tests\Json\JsonDeserializableSuperClass;
use MediaWiki\Title\Title;
use MediaWiki\Title\TitleFactory;
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\EmptyBagOStuff;
use Wikimedia\ObjectCache\HashBagOStuff;
use Wikimedia\Stats\StatsFactory;
use Wikimedia\TestingAccessWrapper;
use Wikimedia\UUID\GlobalIdGenerator;
use WikiPage;
/**
* @covers \MediaWiki\Parser\ParserCache
*/
class ParserCacheTest extends MediaWikiIntegrationTestCase {
/** @var int */
private $time;
/** @var string */
private $cacheTime;
/** @var PageRecord */
private $page;
protected function setUp(): void {
parent::setUp();
$this->time = time();
$this->cacheTime = MWTimestamp::convert( TS_MW, $this->time + 1 );
$this->page = $this->createPageRecord();
MWTimestamp::setFakeTime( $this->time );
}
/**
* @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 );
}
/**
* @param HookContainer|null $hookContainer
* @param BagOStuff|null $storage
* @param LoggerInterface|null $logger
* @param WikiPageFactory|null $wikiPageFactory
* @return ParserCache
*/
private function createParserCache(
?HookContainer $hookContainer = null,
?BagOStuff $storage = null,
?LoggerInterface $logger = null,
?WikiPageFactory $wikiPageFactory = null
): ParserCache {
if ( !$wikiPageFactory ) {
$wikiPageMock = $this->createMock( WikiPage::class );
$wikiPageMock->method( 'getContentModel' )->willReturn( CONTENT_MODEL_WIKITEXT );
$wikiPageFactory = $this->createMock( WikiPageFactory::class );
$wikiPageFactory->method( 'newFromTitle' )->willReturn( $wikiPageMock );
}
$globalIdGenerator = $this->createMock( GlobalIdGenerator::class );
$globalIdGenerator->method( 'newUUIDv1' )->willReturn( 'uuid-uuid' );
return new ParserCache(
'test',
$storage ?: new HashBagOStuff(),
'19900220000000',
$hookContainer ?: $this->createHookContainer( [] ),
new JsonCodec(),
StatsFactory::newNull(),
$logger ?: new NullLogger(),
$this->createMock( TitleFactory::class ),
$wikiPageFactory,
$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 );
$parserOutput->setRenderId( 'dummy-render-id' );
$parserOutput->setCacheRevisionId( 0 );
// ParserOutput::getCacheTime() also sets it as a side effect
$parserOutput->setRevisionTimestamp( $parserOutput->getCacheTime() );
return $parserOutput;
}
/**
* @covers \MediaWiki\Parser\ParserCache::getMetadata
*/
public function testGetMetadataMissing() {
$cache = $this->createParserCache();
$metadataFromCache = $cache->getMetadata( $this->page, ParserCache::USE_CURRENT_ONLY );
$this->assertNull( $metadataFromCache );
}
/**
* @covers \MediaWiki\Parser\ParserCache::getMetadata
*/
public function testGetMetadataAllGood() {
$cache = $this->createParserCache();
$parserOutput = $this->createDummyParserOutput();
$cache->save( $parserOutput, $this->page, ParserOptions::newFromAnon(), $this->cacheTime );
$metadataFromCache = $cache->getMetadata( $this->page, ParserCache::USE_CURRENT_ONLY );
$this->assertNotNull( $metadataFromCache );
$this->assertSame( $this->getDummyUsedOptions(), $metadataFromCache->getUsedOptions() );
$this->assertSame( 4242, $metadataFromCache->getCacheExpiry() );
$this->assertSame( $this->page->getLatest(), $metadataFromCache->getCacheRevisionId() );
$this->assertSame( $this->cacheTime, $metadataFromCache->getCacheTime() );
}
/**
* @covers \MediaWiki\Parser\ParserCache::getMetadata
*/
public function testGetMetadataExpired() {
$cache = $this->createParserCache();
$parserOutput = $this->createDummyParserOutput();
$cache->save( $parserOutput, $this->page, ParserOptions::newFromAnon(), $this->cacheTime );
$this->page = $this->createPageRecord( [ 'page_touched' => $this->time + 10000 ] );
$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() );
$this->assertSame( $this->page->getLatest(), $metadataFromCache->getCacheRevisionId() );
$this->assertSame( $this->cacheTime, $metadataFromCache->getCacheTime() );
}
/**
* @covers \MediaWiki\Parser\ParserCache::getMetadata
*/
public function testGetMetadataOutdated() {
$cache = $this->createParserCache();
$parserOutput = $this->createDummyParserOutput();
$cache->save( $parserOutput, $this->page, ParserOptions::newFromAnon(), $this->cacheTime );
$this->page = $this->createPageRecord( [ 'page_latest' => $this->page->getLatest() + 1 ] );
$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() );
$this->assertNotSame( $this->page->getLatest(), $metadataFromCache->getCacheRevisionId() );
$this->assertSame( $this->cacheTime, $metadataFromCache->getCacheTime() );
}
/**
* @covers \MediaWiki\Parser\ParserCache::makeParserOutputKey
*/
public function testMakeParserOutputKey() {
$cache = $this->createParserCache();
$options1 = ParserOptions::newFromAnon();
$options1->setOption( $this->getDummyUsedOptions()[0], 'value1' );
$key1 = $cache->makeParserOutputKey( $this->page, $options1, $this->getDummyUsedOptions() );
$this->assertNotNull( $key1 );
$options2 = ParserOptions::newFromAnon();
$options2->setOption( $this->getDummyUsedOptions()[0], 'value2' );
$key2 = $cache->makeParserOutputKey( $this->page, $options2, $this->getDummyUsedOptions() );
$this->assertNotNull( $key2 );
$this->assertNotSame( $key1, $key2 );
}
/*
* Test that fetching without storing first returns false.
* @covers \ParserCache::get
*/
public function testGetEmpty() {
$cache = $this->createParserCache();
$options = ParserOptions::newFromAnon();
$this->assertFalse( $cache->get( $this->page, $options ) );
}
/**
* Test that fetching with the same options return the saved value.
* @covers \MediaWiki\Parser\ParserCache::get
* @covers \MediaWiki\Parser\ParserCache::save
*/
public function testSaveGetSameOptions() {
$cache = $this->createParserCache();
$parserOutput = new ParserOutput( 'TEST_TEXT' );
$options1 = ParserOptions::newFromAnon();
$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->getRawText() );
$this->assertSame( $this->cacheTime, $savedOutput->getCacheTime() );
$this->assertSame( $this->page->getLatest(), $savedOutput->getCacheRevisionId() );
}
/**
* Test that fetching with different unused option returns a value.
* @covers \MediaWiki\Parser\ParserCache::get
* @covers \MediaWiki\Parser\ParserCache::save
*/
public function testSaveGetDifferentUnusedOption() {
$cache = $this->createParserCache();
$optionName = $this->getDummyUsedOptions()[0];
$parserOutput = new ParserOutput( 'TEST_TEXT' );
$options1 = ParserOptions::newFromAnon();
$options1->setOption( $optionName, 'value1' );
$cache->save( $parserOutput, $this->page, $options1, $this->cacheTime );
$options2 = ParserOptions::newFromAnon();
$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->getRawText() );
$this->assertSame( $this->cacheTime, $savedOutput->getCacheTime() );
$this->assertSame( $this->page->getLatest(), $savedOutput->getCacheRevisionId() );
}
/**
* Test that non-cacheable output is not stored
* @covers \MediaWiki\Parser\ParserCache::save
* @covers \MediaWiki\Parser\ParserCache::get
*/
public function testDoesNotStoreNonCacheable() {
$cache = $this->createParserCache();
$parserOutput = new ParserOutput( 'TEST_TEXT' );
$parserOutput->updateCacheExpiry( 0 );
$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 ) );
$this->assertFalse( $cache->getDirty( $this->page, $options1 ) );
}
/**
* Test that ParserCacheFilter can be used to prevent content from being cached
* @covers \MediaWiki\Parser\ParserCache::save
* @covers \MediaWiki\Parser\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 ) );
$this->assertFalse( $cache->getDirty( $this->page, $options1 ) );
}
/**
* Test that ParserOptions::isSafeToCache is respected on save
* @covers \MediaWiki\Parser\ParserCache::save
*/
public function testDoesNotStoreNotSafeToCacheAndUsed() {
$cache = $this->createParserCache();
$parserOutput = new ParserOutput( 'TEST_TEXT' );
$parserOutput->recordOption( 'wrapclass' );
$options = ParserOptions::newFromAnon();
$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 \MediaWiki\Parser\ParserCache::get
*/
public function testDoesNotGetNotSafeToCache() {
$cache = $this->createParserCache();
$parserOutput = new ParserOutput( 'TEST_TEXT' );
$parserOutput->recordOption( 'wrapclass' );
$cache->save( $parserOutput, $this->page, ParserOptions::newFromAnon(), $this->cacheTime );
$otherOptions = ParserOptions::newFromAnon();
$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 ) );
}
/**
* Test that ParserOptions::isSafeToCache is respected on save
* @covers \MediaWiki\Parser\ParserCache::save
* @covers \MediaWiki\Parser\ParserCache::get
*/
public function testStoresNotSafeToCacheAndUnused() {
$cache = $this->createParserCache();
$parserOutput = new ParserOutput( 'TEST_TEXT' );
$options = ParserOptions::newFromAnon();
$options->setOption( 'wrapclass', 'wrapwrap' );
$cache->save( $parserOutput, $this->page, $options, $this->cacheTime );
$this->assertStringContainsString( 'TEST_TEXT', $cache->get( $this->page, $options )
->getRawText() );
}
/**
* Test that fetching with different used option don't return a value.
* @covers \MediaWiki\Parser\ParserCache::get
* @covers \MediaWiki\Parser\ParserCache::save
*/
public function testSaveGetDifferentUsedOption() {
$cache = $this->createParserCache();
$parserOutput = new ParserOutput( 'TEST_TEXT' );
$optionName = $this->getDummyUsedOptions()[0];
$parserOutput->recordOption( $optionName );
$options1 = ParserOptions::newFromAnon();
$options1->setOption( $optionName, 'value1' );
$cache->save( $parserOutput, $this->page, $options1, $this->cacheTime );
$options2 = ParserOptions::newFromAnon();
$options2->setOption( $optionName, 'value2' );
$this->assertFalse( $cache->get( $this->page, $options2 ) );
}
/**
* Test that output with expired metadata can be retrieved with getDirty
* @covers \MediaWiki\Parser\ParserCache::getDirty
* @covers \MediaWiki\Parser\ParserCache::get
*/
public function testGetExpiredMetadata() {
$cache = $this->createParserCache();
$parserOutput = new ParserOutput( 'TEST_TEXT' );
$parserOutput->updateCacheExpiry( 10 );
$options1 = ParserOptions::newFromAnon();
$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 \MediaWiki\Parser\ParserCache::getDirty
* @covers \MediaWiki\Parser\ParserCache::get
*/
public function testGetExpiredContent() {
$cache = $this->createParserCache();
$optionName = $this->getDummyUsedOptions()[0];
$parserOutput1 = new ParserOutput( 'TEST_TEXT1' );
$parserOutput1->recordOption( $optionName );
$parserOutput1->updateCacheExpiry( 10 );
$options1 = ParserOptions::newFromAnon();
$options1->setOption( $optionName, 'value1' );
$cache->save( $parserOutput1, $this->page, $options1, $this->cacheTime );
$parserOutput2 = new ParserOutput( 'TEST_TEXT2' );
$parserOutput2->recordOption( $optionName );
$parserOutput2->updateCacheExpiry( 100500600 );
$options2 = ParserOptions::newFromAnon();
$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 \MediaWiki\Parser\ParserCache::getDirty
* @covers \MediaWiki\Parser\ParserCache::get
*/
public function testGetOutdatedMetadata() {
$cache = $this->createParserCache();
$parserOutput = new ParserOutput( 'TEST_TEXT' );
$options1 = ParserOptions::newFromAnon();
$cache->save( $parserOutput, $this->page, $options1, $this->cacheTime );
$this->assertInstanceOf( ParserOutput::class,
$cache->get( $this->page, $options1 ) );
$this->page = $this->createPageRecord( [ 'page_latest' => $this->page->getLatest() + 1 ] );
$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 \MediaWiki\Parser\ParserCache::getDirty
* @covers \MediaWiki\Parser\ParserCache::get
*/
public function testGetOutdatedContent() {
$cache = $this->createParserCache();
$optionName = $this->getDummyUsedOptions()[0];
$parserOutput1 = new ParserOutput( 'TEST_TEXT' );
$parserOutput1->recordOption( $optionName );
$options1 = ParserOptions::newFromAnon();
$options1->setOption( $optionName, 'value1' );
$cache->save( $parserOutput1, $this->page, $options1, $this->cacheTime );
$this->page = $this->createPageRecord( [ 'page_latest' => $this->page->getLatest() + 1 ] );
$parserOutput2 = new ParserOutput( 'TEST_TEXT' );
$parserOutput2->recordOption( $optionName );
$options2 = ParserOptions::newFromAnon();
$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 \MediaWiki\Parser\ParserCache::deleteOptionsKey
*/
public function testDeleteOptionsKey() {
$cache = $this->createParserCache();
$parserOutput = new ParserOutput( 'TEST_TEXT' );
$options1 = ParserOptions::newFromAnon();
$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 \MediaWiki\Parser\ParserCache::get
*/
public function testRejectedByHook() {
$parserOutput = new ParserOutput( 'TEST_TEXT' );
$options = ParserOptions::newFromAnon();
$options->setOption( $this->getDummyUsedOptions()[0], 'value1' );
$wikiPageMock = $this->createMock( WikiPage::class );
$wikiPageMock->method( 'getContentModel' )->willReturn( CONTENT_MODEL_WIKITEXT );
$wikiPageFactory = $this->createMock( WikiPageFactory::class );
$wikiPageFactory->method( 'newFromTitle' )
->with( $this->page )
->willReturn( $wikiPageMock );
$hookContainer = $this->createHookContainer( [
'RejectParserCacheValue' =>
function ( ParserOutput $value, WikiPage $hookPage, ParserOptions $popts )
use ( $wikiPageMock, $parserOutput, $options ) {
// parse start time is not saved/restored in the cache
$parserOutput->clearParseStartTime();
$this->assertEquals( $parserOutput, $value );
$this->assertSame( $wikiPageMock, $hookPage );
$this->assertSame( $options, $popts );
return false;
}
] );
$cache = $this->createParserCache( $hookContainer, null, null, $wikiPageFactory );
$cache->save( $parserOutput, $this->page, $options, $this->cacheTime );
$this->assertFalse( $cache->get( $this->page, $options ) );
}
/**
* Test that ParserCacheSaveComplete hook is run
* @covers \MediaWiki\Parser\ParserCache::save
*/
public function testParserCacheSaveCompleteHook() {
$parserOutput = new ParserOutput( 'TEST_TEXT' );
$options = ParserOptions::newFromAnon();
$options->setOption( $this->getDummyUsedOptions()[0], 'value1' );
$hookContainer = $this->createHookContainer( [
'ParserCacheSaveComplete' =>
function (
ParserCache $hookCache, ParserOutput $value, Title $hookTitle, ParserOptions $popts, int $revId
) use ( $parserOutput, $options ) {
$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 );
}
/**
* Tests that parser cache respects skipped if page does not exist
* @covers \MediaWiki\Parser\ParserCache::get
*/
public function testSkipIfNotExist() {
$mockPage = $this->createNoOpMock( PageRecord::class, [ 'exists', 'assertWiki' ] );
$mockPage->method( 'exists' )->willReturn( false );
$wikiPageMock = $this->createMock( WikiPage::class );
$wikiPageMock->method( 'getContentModel' )->willReturn( 'wikitext' );
$wikiPageFactoryMock = $this->createMock( WikiPageFactory::class );
$wikiPageFactoryMock->method( 'newFromTitle' )
->with( $mockPage )
->willReturn( $wikiPageMock );
$cache = $this->createParserCache( null, null, null, $wikiPageFactoryMock );
$this->assertFalse( $cache->get( $mockPage, ParserOptions::newFromAnon() ) );
}
/**
* Tests that parser cache respects skipped if page is redirect
* @covers \MediaWiki\Parser\ParserCache::get
*/
public function testSkipIfRedirect() {
$cache = $this->createParserCache();
$page = $this->createPageRecord( [
'page_is_redirect' => true
] );
$this->assertFalse( $cache->get( $page, ParserOptions::newFromAnon() ) );
}
/**
* Tests that getCacheStorage returns underlying BagOStuff
* @covers \MediaWiki\Parser\ParserCache::getCacheStorage
*/
public function testGetCacheStorage() {
$storage = new EmptyBagOStuff();
$cache = $this->createParserCache( null, $storage );
$this->assertSame( $storage, $cache->getCacheStorage() );
}
/**
* @covers \MediaWiki\Parser\ParserCache::save
*/
public function testSaveNoText() {
$this->expectException( InvalidArgumentException::class );
$this->createParserCache()->save(
new ParserOutput( null ),
$this->page,
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\ParserCache::get
* @covers \MediaWiki\Parser\ParserCache::restoreFromJson
* @param string $data
*/
public function testCorruptData( string $data ) {
$cache = $this->createParserCache( null, new HashBagOStuff() );
$parserOutput = new ParserOutput( 'TEST_TEXT' );
$options1 = ParserOptions::newFromAnon();
$cache->save( $parserOutput, $this->page, $options1, $this->cacheTime );
$outputKey = $cache->makeParserOutputKey(
$this->page,
$options1,
$parserOutput->getUsedOptions()
);
$cache->getCacheStorage()->set( $outputKey, $data );
// just make sure we don't crash and burn
$this->assertFalse( $cache->get( $this->page, $options1 ) );
}
/**
* 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.
*
* @covers \MediaWiki\Parser\ParserCache::getMetadata
*/
public function testCorruptMetadata() {
$cacheStorage = new HashBagOStuff();
$cache = $this->createParserCache( null, $cacheStorage );
$parserOutput = new ParserOutput( 'TEST_TEXT' );
$options1 = ParserOptions::newFromAnon();
$cache->save( $parserOutput, $this->page, $options1, $this->cacheTime );
// Mess up the metadata
$optionsKey = TestingAccessWrapper::newFromObject( $cache )->makeMetadataKey(
$this->page
);
$cacheStorage->set( $optionsKey, 'bad data' );
// Recreate the cache to drop in-memory cached metadata.
$cache = $this->createParserCache( null, $cacheStorage );
// just make sure we don't crash and burn
$this->assertNull( $cache->getMetadata( $this->page ) );
}
/**
* Test what happens when upgrading from 1.35 or earlier,
* when old cache entries do not yet use JSON.
*
* @covers \MediaWiki\Parser\ParserCache::get
*/
public function testMigrationToJson() {
$bagOStuff = new HashBagOStuff();
$wikiPageMock = $this->createMock( WikiPage::class );
$wikiPageMock->method( 'getContentModel' )->willReturn( CONTENT_MODEL_WIKITEXT );
$wikiPageFactory = $this->createMock( WikiPageFactory::class );
$wikiPageFactory->method( 'newFromTitle' )->willReturn( $wikiPageMock );
$globalIdGenerator = $this->createMock( GlobalIdGenerator::class );
$globalIdGenerator->method( 'newUUIDv1' )->willReturn( 'uuid-uuid' );
$cache = $this->getMockBuilder( ParserCache::class )
->setConstructorArgs( [
'test',
$bagOStuff,
'19900220000000',
$this->createHookContainer( [] ),
new JsonCodec(),
StatsFactory::newNull(),
new NullLogger(),
$this->createMock( TitleFactory::class ),
$wikiPageFactory,
$globalIdGenerator
] )
->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;
}
);
$parserOutput1 = new ParserOutput( 'Lorem Ipsum' );
$options = ParserOptions::newFromAnon();
$cache->save( $parserOutput1, $this->page, $options, $this->cacheTime );
// emulate migration to JSON
$cache = $this->createParserCache( null, $bagOStuff );
// make sure we can load non-json cache data
$cachedOutput = $cache->get( $this->page, $options );
$parserOutput1->clearParseStartTime(); // not saved/restored
$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 );
$parserOutput2->clearParseStartTime(); // not saved/restored
$this->assertEquals( $parserOutput2, $cachedOutput );
}
/**
* @covers \MediaWiki\Parser\ParserCache::convertForCache
*/
public function testNonSerializableJsonIsReported() {
$testLogger = new TestLogger( true );
$cache = $this->createParserCache( null, null, $testLogger );
$parserOutput = $this->createDummyParserOutput();
$parserOutput->setExtensionData( 'test', new User() );
$cache->save( $parserOutput, $this->page, ParserOptions::newFromAnon() );
$this->assertArraySubmapSame(
[ [ LogLevel::ERROR, 'Unable to serialize JSON' ] ],
$testLogger->getBuffer()
);
}
/**
* @covers \MediaWiki\Parser\ParserCache::convertForCache
*/
public function testCyclicStructuresDoNotBlowUpInJson() {
$this->markTestSkipped( 'Temporarily disabled: T314338' );
$testLogger = new TestLogger( true );
$cache = $this->createParserCache( null, null, $testLogger );
$parserOutput = $this->createDummyParserOutput();
$cyclicArray = [ 'a' => 'b' ];
$cyclicArray['c'] = &$cyclicArray;
$parserOutput->setExtensionData( 'test', $cyclicArray );
$cache->save( $parserOutput, $this->page, ParserOptions::newFromAnon() );
$this->assertArraySubmapSame(
[ [ LogLevel::ERROR, 'Unable to serialize JSON' ] ],
$testLogger->getBuffer()
);
}
/**
* Tests that unicode characters are not \u escaped
*
* @covers \MediaWiki\Parser\ParserCache::convertForCache
*/
public function testJsonEncodeUnicode() {
$unicodeCharacter = "Э";
$cache = $this->createParserCache( null, new HashBagOStuff() );
$parserOutput = $this->createDummyParserOutput();
$parserOutput->setRawText( $unicodeCharacter );
$cache->save( $parserOutput, $this->page, ParserOptions::newFromAnon() );
$json = $cache->getCacheStorage()->get(
$cache->makeParserOutputKey( $this->page, ParserOptions::newFromAnon() )
);
$this->assertStringNotContainsString( "\u003E", $json );
$this->assertStringContainsString( $unicodeCharacter, $json );
}
}
|