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 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068
|
<?php
use MediaWiki\Content\WikitextContent;
use MediaWiki\Json\JsonCodec;
use MediaWiki\Logger\LoggerFactory;
use MediaWiki\Logger\Spi as LoggerSpi;
use MediaWiki\MainConfigNames;
use MediaWiki\MediaWikiServices;
use MediaWiki\Page\Hook\OpportunisticLinksUpdateHook;
use MediaWiki\Page\PageRecord;
use MediaWiki\Page\ParserOutputAccess;
use MediaWiki\Page\WikiPageFactory;
use MediaWiki\Parser\ParserCache;
use MediaWiki\Parser\ParserCacheFactory;
use MediaWiki\Parser\ParserOptions;
use MediaWiki\Parser\ParserOutput;
use MediaWiki\Parser\RevisionOutputCache;
use MediaWiki\PoolCounter\PoolCounter;
use MediaWiki\PoolCounter\PoolCounterWork;
use MediaWiki\Revision\MutableRevisionRecord;
use MediaWiki\Revision\RevisionLookup;
use MediaWiki\Revision\RevisionRecord;
use MediaWiki\Revision\RevisionRenderer;
use MediaWiki\Revision\RevisionStore;
use MediaWiki\Revision\SlotRecord;
use MediaWiki\Status\Status;
use MediaWiki\Title\TitleFormatter;
use MediaWiki\Utils\MWTimestamp;
use Psr\Log\NullLogger;
use Wikimedia\ObjectCache\EmptyBagOStuff;
use Wikimedia\ObjectCache\HashBagOStuff;
use Wikimedia\ObjectCache\WANObjectCache;
use Wikimedia\Rdbms\ChronologyProtector;
use Wikimedia\Rdbms\ILBFactory;
use Wikimedia\Stats\StatsFactory;
use Wikimedia\TestingAccessWrapper;
/**
* @covers \MediaWiki\Page\ParserOutputAccess
* @group Database
*/
class ParserOutputAccessTest extends MediaWikiIntegrationTestCase {
public int $actualCallsToPoolWorkArticleView = 0;
public int $expectedCallsToPoolWorkArticleView = 0;
public function tearDown(): void {
$this->assertSame(
$this->expectedCallsToPoolWorkArticleView,
$this->actualCallsToPoolWorkArticleView,
'Calls to newPoolWorkArticleView'
);
parent::tearDown();
}
private function getHtml( $value ) {
if ( $value instanceof StatusValue ) {
$value = $value->getValue();
}
if ( $value instanceof ParserOutput ) {
$pipeline = MediaWikiServices::getInstance()->getDefaultOutputPipeline();
$value = $pipeline->run( $value, $this->getParserOptions(), [] )->getContentHolderText();
}
$html = preg_replace( '/<!--.*?-->/s', '', $value );
$html = trim( preg_replace( '/[\r\n]{2,}/', "\n", $html ) );
$html = trim( preg_replace( '/\s{2,}/', ' ', $html ) );
return $html;
}
private function assertContainsHtml( $needle, $actual, $msg = '' ) {
$this->assertNotNull( $actual );
if ( $actual instanceof StatusValue ) {
$this->assertStatusOK( $actual, 'isOK' );
}
$this->assertStringContainsString( $needle, $this->getHtml( $actual ), $msg );
}
private function assertSameHtml( $expected, $actual, $msg = '' ) {
$this->assertNotNull( $actual );
if ( $actual instanceof StatusValue ) {
$this->assertStatusOK( $actual, 'isOK' );
}
$this->assertSame( $this->getHtml( $expected ), $this->getHtml( $actual ), $msg );
}
private function assertNotSameHtml( $expected, $actual, $msg = '' ) {
$this->assertNotNull( $actual );
if ( $actual instanceof StatusValue ) {
$this->assertStatusOK( $actual, 'isOK' );
}
$this->assertNotSame( $this->getHtml( $expected ), $this->getHtml( $actual ), $msg );
}
private function getParserCache( $bag = null ) {
$parserCache = new ParserCache(
'test',
$bag ?: new HashBagOStuff(),
'19900220000000',
$this->getServiceContainer()->getHookContainer(),
new JsonCodec( $this->getServiceContainer() ),
StatsFactory::newNull(),
new NullLogger(),
$this->getServiceContainer()->getTitleFactory(),
$this->getServiceContainer()->getWikiPageFactory(),
$this->getServiceContainer()->getGlobalIdGenerator()
);
return $parserCache;
}
private function getRevisionOutputCache( $bag = null, $expiry = 3600 ) {
$wanCache = new WANObjectCache( [ 'cache' => $bag ?: new HashBagOStuff() ] );
$revisionOutputCache = new RevisionOutputCache(
'test',
$wanCache,
$expiry,
'19900220000000',
new JsonCodec( $this->getServiceContainer() ),
StatsFactory::newNull(),
new NullLogger(),
$this->getServiceContainer()->getGlobalIdGenerator()
);
return $revisionOutputCache;
}
/**
* @param ParserCache|null $parserCache
* @param RevisionOutputCache|null $revisionOutputCache
* @param int|bool $maxRenderCalls
*
* @return ParserOutputAccess
* @throws Exception
*/
private function getParserOutputAccessWithCache(
$parserCache = null,
$revisionOutputCache = null,
$maxRenderCalls = false
): ParserOutputAccess {
return $this->getParserOutputAccess( [
'parserCache' => $parserCache ?? new HashBagOStuff(),
'revisionOutputCache' => $revisionOutputCache ?? new HashBagOStuff(),
'maxRenderCalls' => $maxRenderCalls
] );
}
/**
* @param array $options
*
* @return ParserOutputAccess
* @throws Exception
*/
private function getParserOutputAccess( array $options = [] ): ParserOutputAccess {
$parserCacheFactory = $options['parserCacheFactory'] ?? null;
$maxRenderCalls = $options['maxRenderCalls'] ?? null;
$parserCache = $options['parserCache'] ?? null;
$revisionOutputCache = $options['revisionOutputCache'] ?? null;
$expectPoolCounterCalls = $options['expectPoolCounterCalls'] ?? 0;
if ( !$parserCacheFactory ) {
if ( !$parserCache instanceof ParserCache ) {
$parserCache = $this->getParserCache(
$parserCache ?? new EmptyBagOStuff()
);
}
if ( !$revisionOutputCache instanceof RevisionOutputCache ) {
$revisionOutputCache = $this->getRevisionOutputCache(
$revisionOutputCache ?? new EmptyBagOStuff()
);
}
$parserCacheFactory = $this->createMock( ParserCacheFactory::class );
$parserCacheFactory->method( 'getParserCache' )
->willReturn( $parserCache );
$parserCacheFactory->method( 'getRevisionOutputCache' )
->willReturn( $revisionOutputCache );
}
$revRenderer = $this->getServiceContainer()->getRevisionRenderer();
if ( $maxRenderCalls ) {
$realRevRenderer = $revRenderer;
$revRenderer =
$this->createNoOpMock( RevisionRenderer::class, [ 'getRenderedRevision' ] );
$revRenderer->expects( $this->atMost( $maxRenderCalls ) )
->method( 'getRenderedRevision' )
->willReturnCallback( [ $realRevRenderer, 'getRenderedRevision' ] );
}
$mock = new class (
$parserCacheFactory,
$this->getServiceContainer()->getRevisionLookup(),
$revRenderer,
$this->getServiceContainer()->getStatsFactory(),
$this->getServiceContainer()->getDBLoadBalancerFactory(),
$this->getServiceContainer()->getChronologyProtector(),
LoggerFactory::getProvider(),
$this->getServiceContainer()->getWikiPageFactory(),
$this->getServiceContainer()->getTitleFormatter(),
$this
) extends ParserOutputAccess {
private ParserOutputAccessTest $test;
public function __construct(
ParserCacheFactory $parserCacheFactory,
RevisionLookup $revisionLookup,
RevisionRenderer $revisionRenderer,
StatsFactory $statsFactory,
ILBFactory $lbFactory,
ChronologyProtector $chronologyProtector,
LoggerSpi $loggerSpi,
WikiPageFactory $wikiPageFactory,
TitleFormatter $titleFormatter,
ParserOutputAccessTest $test
) {
parent::__construct(
$parserCacheFactory,
$revisionLookup,
$revisionRenderer,
$statsFactory,
$lbFactory,
$chronologyProtector,
$loggerSpi,
$wikiPageFactory,
$titleFormatter
);
$this->test = $test;
}
protected function newPoolWorkArticleView(
PageRecord $page,
ParserOptions $parserOptions,
RevisionRecord $revision,
int $options
): PoolCounterWork {
$this->test->actualCallsToPoolWorkArticleView++;
return parent::newPoolWorkArticleView( $page, $parserOptions, $revision, $options );
}
};
$this->expectedCallsToPoolWorkArticleView += $expectPoolCounterCalls;
return $mock;
}
/**
* @param WikiPage $page
* @param string $text
*
* @return RevisionRecord
*/
private function makeFakeRevision( WikiPage $page, $text ) {
// construct fake revision with no ID
$content = new WikitextContent( $text );
$rev = new MutableRevisionRecord( $page->getTitle() );
$rev->setPageId( $page->getId() );
$rev->setContent( SlotRecord::MAIN, $content );
return $rev;
}
/**
* @return ParserOptions
*/
private function getParserOptions() {
return ParserOptions::newFromAnon();
}
/**
* Install OpportunisticLinksUpdateHook to inspect whether WikiPage::triggerOpportunisticLinksUpdate
* is called or not, the hook implementation will return false disabling the
* WikiPage::triggerOpportunisticLinksUpdate to proceed completely.
* @param bool $called whether WikiPage::triggerOpportunisticLinksUpdate is expected to be called or not
* @return void
*/
private function installOpportunisticUpdateHook( bool $called ): void {
$opportunisticUpdateHook =
$this->createMock( OpportunisticLinksUpdateHook::class );
// WikiPage::triggerOpportunisticLinksUpdate is not called by default
$opportunisticUpdateHook->expects( $this->exactly( $called ? 1 : 0 ) )
->method( 'onOpportunisticLinksUpdate' )
->willReturn( false );
$this->setTemporaryHook( 'OpportunisticLinksUpdate', $opportunisticUpdateHook );
}
/**
* Tests that we can get rendered output for the latest revision.
*/
public function testOutputForLatestRevision() {
$access = $this->getParserOutputAccess( [
'parserCache' => new HashBagOStuff()
] );
$page = $this->getNonexistingTestPage( __METHOD__ );
$this->editPage( $page, 'Hello \'\'World\'\'!' );
$parserOptions = $this->getParserOptions();
// WikiPage::triggerOpportunisticLinksUpdate is not called by default
$this->installOpportunisticUpdateHook( false );
$status = $access->getParserOutput( $page, $parserOptions );
$this->assertContainsHtml( 'Hello <i>World</i>!', $status );
$this->assertNotNull( $access->getCachedParserOutput( $page, $parserOptions ) );
}
/**
* Tests that we can get rendered output for the latest revision.
*/
public function testOutputForLatestRevisionUsingPoolCounter() {
$access = $this->getParserOutputAccess( [
'expectPoolCounterCalls' => 1
] );
$page = $this->getNonexistingTestPage( __METHOD__ );
$this->editPage( $page, 'Hello \'\'World\'\'!' );
$parserOptions = $this->getParserOptions();
// WikiPage::triggerOpportunisticLinksUpdate is not called by default
$this->installOpportunisticUpdateHook( false );
$status = $access->getParserOutput(
$page,
$parserOptions,
null,
ParserOutputAccess::OPT_FOR_ARTICLE_VIEW
);
$this->assertContainsHtml( 'Hello <i>World</i>!', $status );
}
/**
* Tests that we can get rendered output for the latest revision.
*/
public function testOutputForLatestRevisionWithLinksUpdate() {
$access = $this->getParserOutputAccess();
$page = $this->getNonexistingTestPage( __METHOD__ );
$this->editPage( $page, 'Hello \'\'World\'\'!' );
$parserOptions = $this->getParserOptions();
// With ParserOutputAccess::OPT_LINKS_UPDATE WikiPage::triggerOpportunisticLinksUpdate can be called
$this->installOpportunisticUpdateHook( true );
$status = $access->getParserOutput( $page, $parserOptions, null, ParserOutputAccess::OPT_LINKS_UPDATE );
$this->assertContainsHtml( 'Hello <i>World</i>!', $status );
}
/**
* Tests that we can get rendered output for the latest revision.
*/
public function testOutputForLatestRevisionWithLinksUpdateWithPoolCounter() {
$access = $this->getParserOutputAccess( [
'expectPoolCounterCalls' => 1
] );
$page = $this->getNonexistingTestPage( __METHOD__ );
$this->editPage( $page, 'Hello \'\'World\'\'!' );
$parserOptions = $this->getParserOptions();
// With ParserOutputAccess::OPT_LINKS_UPDATE WikiPage::triggerOpportunisticLinksUpdate can be called
$this->installOpportunisticUpdateHook( true );
$status = $access->getParserOutput(
$page,
$parserOptions,
null,
ParserOutputAccess::OPT_LINKS_UPDATE | ParserOutputAccess::OPT_FOR_ARTICLE_VIEW
);
$this->assertContainsHtml( 'Hello <i>World</i>!', $status );
}
/**
* Tests that cached output in the ParserCache will be used for the latest revision.
*/
public function testLatestRevisionUseCached() {
// Allow only one render call, use default caches
$access = $this->getParserOutputAccessWithCache( null, null, 1 );
$parserOptions = $this->getParserOptions();
$page = $this->getNonexistingTestPage( __METHOD__ );
$this->editPage( $page, 'Hello \'\'World\'\'!' );
$access->getParserOutput( $page, $parserOptions );
// The second call should use cached output
$status = $access->getParserOutput( $page, $parserOptions );
$this->assertContainsHtml( 'Hello <i>World</i>!', $status );
}
/**
* Tests that cached output in the ParserCache will not be used
* for the latest revision if the FORCE_PARSE option is given.
*/
public function testLatestRevisionForceParse() {
$parserCache = $this->getParserCache( new HashBagOStuff() );
$access = $this->getParserOutputAccessWithCache( $parserCache );
$parserOptions = ParserOptions::newFromAnon();
$page = $this->getNonexistingTestPage( __METHOD__ );
$this->editPage( $page, 'Hello \'\'World\'\'!' );
// Put something else into the cache, so we'd notice if it got used
$cachedOutput = new ParserOutput( 'Cached Text' );
$parserCache->save( $cachedOutput, $page, $parserOptions );
$status = $access->getParserOutput(
$page,
$parserOptions,
null,
ParserOutputAccess::OPT_FORCE_PARSE
);
$this->assertNotSameHtml( $cachedOutput, $status );
$this->assertContainsHtml( 'Hello <i>World</i>!', $status );
}
/**
* Tests that an error is reported if the latest revision cannot be loaded.
*/
public function testLatestRevisionCantLoad() {
$page = $this->getNonexistingTestPage( __METHOD__ );
$this->editPage( $page, 'Hello \'\'World\'\'!' );
$revisionStore = $this->createNoOpMock(
RevisionStore::class,
[ 'getRevisionByTitle', 'getKnownCurrentRevision', 'getRevisionById' ]
);
$revisionStore->method( 'getRevisionById' )->willReturn( null );
$revisionStore->method( 'getRevisionByTitle' )->willReturn( null );
$revisionStore->method( 'getKnownCurrentRevision' )->willReturn( false );
$this->setService( 'RevisionStore', $revisionStore );
$this->setService( 'RevisionLookup', $revisionStore );
$page->clear();
$access = $this->getParserOutputAccess();
$parserOptions = $this->getParserOptions();
$status = $access->getParserOutput( $page, $parserOptions );
$this->assertStatusError( 'missing-revision', $status );
}
/**
* Tests that getCachedParserOutput() will return previously generated output.
*/
public function testGetCachedParserOutput() {
$access = $this->getParserOutputAccessWithCache();
$parserOptions = $this->getParserOptions();
$page = $this->getNonexistingTestPage( __METHOD__ );
$output = $access->getCachedParserOutput( $page, $parserOptions );
$this->assertNull( $output );
$this->editPage( $page, 'Hello \'\'World\'\'!' );
$access->getParserOutput( $page, $parserOptions );
$output = $access->getCachedParserOutput( $page, $parserOptions );
$this->assertNotNull( $output );
$this->assertContainsHtml( 'Hello <i>World</i>!', $output );
}
/**
* Tests that getCachedParserOutput() will not return output for current revision when
* a fake revision with no ID is supplied.
*/
public function testGetCachedParserOutputForFakeRevision() {
$access = $this->getParserOutputAccessWithCache();
$page = $this->getNonexistingTestPage( __METHOD__ );
$this->editPage( $page, 'Hello \'\'World\'\'!' );
$parserOptions = $this->getParserOptions();
$access->getParserOutput( $page, $parserOptions );
$rev = $this->makeFakeRevision( $page, 'fake text' );
$output = $access->getCachedParserOutput( $page, $parserOptions, $rev );
$this->assertNull( $output );
}
/**
* Tests that getPageOutput() will place the generated output for the latest revision
* in the parser cache.
*/
public function testLatestRevisionIsCached() {
$access = $this->getParserOutputAccessWithCache();
$page = $this->getNonexistingTestPage( __METHOD__ );
$this->editPage( $page, 'Hello \'\'World\'\'!' );
$parserOptions = $this->getParserOptions();
$access->getParserOutput( $page, $parserOptions );
$cachedOutput = $access->getCachedParserOutput( $page, $parserOptions );
$this->assertContainsHtml( 'World', $cachedOutput );
}
/**
* Tests that the cache for the current revision is split on parser options.
*/
public function testLatestRevisionCacheSplit() {
$access = $this->getParserOutputAccessWithCache();
$frenchOptions = ParserOptions::newFromAnon();
$frenchOptions->setUserLang( 'fr' );
$tongaOptions = ParserOptions::newFromAnon();
$tongaOptions->setUserLang( 'to' );
$page = $this->getNonexistingTestPage( __METHOD__ );
$this->editPage( $page, 'Test {{int:ok}}!' );
$frenchResult = $access->getParserOutput( $page, $frenchOptions );
$this->assertContainsHtml( 'Test', $frenchResult );
// Check that French output was cached
$cachedFrenchOutput =
$access->getCachedParserOutput( $page, $frenchOptions );
$this->assertNotNull( $cachedFrenchOutput, 'French output should be in the cache' );
// check that we don't get the French output when asking for Tonga
$cachedTongaOutput =
$access->getCachedParserOutput( $page, $tongaOptions );
$this->assertNull( $cachedTongaOutput, 'Tonga output should not be in the cache yet' );
// check that we can generate the Tonga output, and it's different from French
$tongaResult = $access->getParserOutput( $page, $tongaOptions );
$this->assertContainsHtml( 'Test', $tongaResult );
$this->assertNotSameHtml(
$frenchResult,
$tongaResult,
'Tonga output should be different from French'
);
// check that the Tonga output is cached
$cachedTongaOutput =
$access->getCachedParserOutput( $page, $tongaOptions );
$this->assertNotNull( $cachedTongaOutput, 'Tonga output should be in the cache' );
}
/**
* Tests that getPageOutput() will place the generated output in the parser cache if the
* latest revision is passed explicitly. In other words, thins ensures that the current
* revision won't get treated like an old revision.
*/
public function testLatestRevisionIsDetectedAndCached() {
$access = $this->getParserOutputAccessWithCache();
$page = $this->getNonexistingTestPage( __METHOD__ );
$rev = $this->editPage( $page, 'Hello \'\'World\'\'!' )->getNewRevision();
// When $rev is passed, it should be detected to be the latest revision.
$parserOptions = $this->getParserOptions();
$access->getParserOutput( $page, $parserOptions, $rev );
$cachedOutput = $access->getCachedParserOutput( $page, $parserOptions );
$this->assertContainsHtml( 'World', $cachedOutput );
}
/**
* Tests that getPageOutput() will generate output for an old revision, and
* that we still have the output for the current revision cached afterwards.
*/
public function testOutputForOldRevision() {
$access = $this->getParserOutputAccessWithCache();
$page = $this->getNonexistingTestPage( __METHOD__ );
$firstRev = $this->editPage( $page, 'First' )->getNewRevision();
$secondRev = $this->editPage( $page, 'Second' )->getNewRevision();
// output is for the second revision (write to ParserCache)
$parserOptions = $this->getParserOptions();
$status = $access->getParserOutput( $page, $parserOptions );
$this->assertContainsHtml( 'Second', $status );
// output is for the first revision (not written to ParserCache)
$status = $access->getParserOutput( $page, $parserOptions, $firstRev );
$this->assertContainsHtml( 'First', $status );
// Latest revision is still the one in the ParserCache
$output = $access->getCachedParserOutput( $page, $parserOptions );
$this->assertContainsHtml( 'Second', $output );
}
/**
* Tests that getPageOutput() will generate output for an old revision, and
* that we still have the output for the current revision cached afterwards.
*/
public function testOutputForOldRevisionUsingPoolCounter() {
$access = $this->getParserOutputAccess( [
'expectPoolCounterCalls' => 2,
'parserCache' => new HashBagOStuff(),
'revisionOutputCache' => new HashBagOStuff()
] );
$page = $this->getNonexistingTestPage( __METHOD__ );
$firstRev = $this->editPage( $page, 'First' )->getNewRevision();
$secondRev = $this->editPage( $page, 'Second' )->getNewRevision();
// output is for the second revision (write to ParserCache)
$parserOptions = $this->getParserOptions();
$status = $access->getParserOutput(
$page,
$parserOptions,
null,
ParserOutputAccess::OPT_FOR_ARTICLE_VIEW
);
$this->assertContainsHtml( 'Second', $status );
// output is for the first revision (not written to ParserCache)
$status = $access->getParserOutput(
$page,
$parserOptions,
$firstRev,
ParserOutputAccess::OPT_FOR_ARTICLE_VIEW
);
$this->assertContainsHtml( 'First', $status );
// Latest revision is still the one in the ParserCache
$output = $access->getCachedParserOutput( $page, $parserOptions );
$this->assertContainsHtml( 'Second', $output );
}
/**
* Tests that trying to get output for a suppressed old revision is denied.
*/
public function testOldRevisionSuppressedDenied() {
$access = $this->getParserOutputAccess();
$page = $this->getNonexistingTestPage( __METHOD__ );
$firstRev = $this->editPage( $page, 'First' )->getNewRevision();
$secondRev = $this->editPage( $page, 'Second' )->getNewRevision();
$this->revisionDelete( $firstRev );
$firstRev =
$this->getServiceContainer()->getRevisionStore()->getRevisionById( $firstRev->getId() );
// output is for the first revision denied
$parserOptions = $this->getParserOptions();
$status = $access->getParserOutput( $page, $parserOptions, $firstRev );
$this->assertStatusError( 'missing-revision-permission', $status );
// TODO: Once PoolWorkArticleView properly reports errors, check that the correct error
// is propagated.
}
/**
* Tests that getting output for a suppressed old revision is possible when NO_AUDIENCE_CHECK
* is set.
*/
public function testOldRevisionSuppressedAllowed() {
$access = $this->getParserOutputAccess();
$page = $this->getNonexistingTestPage( __METHOD__ );
$firstRev = $this->editPage( $page, 'First' )->getNewRevision();
$secondRev = $this->editPage( $page, 'Second' )->getNewRevision();
$this->revisionDelete( $firstRev );
$firstRev =
$this->getServiceContainer()->getRevisionStore()->getRevisionById( $firstRev->getId() );
// output is for the first revision (even though it's suppressed)
$parserOptions = $this->getParserOptions();
$status = $access->getParserOutput(
$page,
$parserOptions,
$firstRev,
ParserOutputAccess::OPT_NO_AUDIENCE_CHECK
);
$this->assertContainsHtml( 'First', $status );
// even though the output was generated, it wasn't cached, since it's not public
$cachedOutput = $access->getCachedParserOutput( $page, $parserOptions, $firstRev );
$this->assertNull( $cachedOutput );
}
/**
* Tests that output for an old revision is fetched from the secondary parser cache if possible.
*/
public function testOldRevisionUseCached() {
// Allow only one render call, use default caches
$access = $this->getParserOutputAccessWithCache( null, null, 1 );
$parserOptions = $this->getParserOptions();
$page = $this->getNonexistingTestPage( __METHOD__ );
$this->editPage( $page, 'First' );
$oldRev = $page->getRevisionRecord();
$this->editPage( $page, 'Second' );
$firstStatus = $access->getParserOutput( $page, $parserOptions, $oldRev );
// The second call should use cached output
$secondStatus = $access->getParserOutput( $page, $parserOptions, $oldRev );
$this->assertSameHtml( $firstStatus, $secondStatus );
}
/**
* Tests that output for an old revision is fetched from the secondary parser cache if possible.
*/
public function testOldRevisionDisableCached() {
// Use default caches, but expiry 0 for the secondary cache
$access = $this->getParserOutputAccessWithCache(
null,
$this->getRevisionOutputCache( null, 0 )
);
$parserOptions = $this->getParserOptions();
$page = $this->getNonexistingTestPage( __METHOD__ );
$this->editPage( $page, 'First' );
$oldRev = $page->getRevisionRecord();
$this->editPage( $page, 'Second' );
$access->getParserOutput( $page, $parserOptions, $oldRev );
// Should not be cached!
$cachedOutput = $access->getCachedParserOutput( $page, $parserOptions, $oldRev );
$this->assertNull( $cachedOutput );
}
/**
* Tests that the secondary cache for output for old revisions is split on parser options.
*/
public function testOldRevisionCacheSplit() {
$access = $this->getParserOutputAccessWithCache();
$frenchOptions = ParserOptions::newFromAnon();
$frenchOptions->setUserLang( 'fr' );
$tongaOptions = ParserOptions::newFromAnon();
$tongaOptions->setUserLang( 'to' );
$page = $this->getNonexistingTestPage( __METHOD__ );
$this->editPage( $page, 'Test {{int:ok}}!' );
$oldRev = $page->getRevisionRecord();
$this->editPage( $page, 'Latest Test' );
$frenchResult = $access->getParserOutput( $page, $frenchOptions, $oldRev );
$this->assertContainsHtml( 'Test', $frenchResult );
// Check that French output was cached
$cachedFrenchOutput =
$access->getCachedParserOutput( $page, $frenchOptions, $oldRev );
$this->assertNotNull( $cachedFrenchOutput, 'French output should be in the cache' );
// check that we don't get the French output when asking for Tonga
$cachedTongaOutput =
$access->getCachedParserOutput( $page, $tongaOptions, $oldRev );
$this->assertNull( $cachedTongaOutput, 'Tonga output should not be in the cache yet' );
// check that we can generate the Tonga output, and it's different from French
$tongaResult = $access->getParserOutput( $page, $tongaOptions, $oldRev );
$this->assertContainsHtml( 'Test', $tongaResult );
$this->assertNotSameHtml(
$frenchResult,
$tongaResult,
'Tonga output should be different from French'
);
// check that the Tonga output is cached
$cachedTongaOutput =
$access->getCachedParserOutput( $page, $tongaOptions, $oldRev );
$this->assertNotNull( $cachedTongaOutput, 'Tonga output should be in the cache' );
}
/**
* Tests that a RevisionRecord with no ID can be rendered if OPT_NO_CACHE is set.
*/
public function testFakeRevisionNoCache() {
$access = $this->getParserOutputAccessWithCache();
$page = $this->getExistingTestPage( __METHOD__ );
$rev = $this->makeFakeRevision( $page, 'fake text' );
// Render fake
$parserOptions = $this->getParserOptions();
$fakeResult = $access->getParserOutput(
$page,
$parserOptions,
$rev,
ParserOutputAccess::OPT_NO_CACHE
);
$this->assertContainsHtml( 'fake text', $fakeResult );
// check that fake output isn't cached
$cachedOutput = $access->getCachedParserOutput( $page, $parserOptions );
if ( $cachedOutput ) {
// we may have a cache entry for original edit
$this->assertNotSameHtml( $fakeResult, $cachedOutput );
}
}
/**
* Tests that a RevisionRecord with no ID cannot be rendered if OPT_NO_CACHE is not set.
*/
public function testFakeRevisionError() {
$access = $this->getParserOutputAccess();
$parserOptions = $this->getParserOptions();
$page = $this->getExistingTestPage( __METHOD__ );
$rev = $this->makeFakeRevision( $page, 'fake text' );
// Render should fail
$this->expectException( InvalidArgumentException::class );
$access->getParserOutput( $page, $parserOptions, $rev );
}
/**
* Tests that trying to render a RevisionRecord for another page will throw an exception.
*/
public function testPageIdMismatchError() {
$access = $this->getParserOutputAccess();
$parserOptions = $this->getParserOptions();
$page1 = $this->getExistingTestPage( __METHOD__ . '-1' );
$page2 = $this->getExistingTestPage( __METHOD__ . '-2' );
$this->expectException( InvalidArgumentException::class );
$access->getParserOutput( $page1, $parserOptions, $page2->getRevisionRecord() );
}
/**
* Tests that trying to render a non-existing page will be reported as an error.
*/
public function testNonExistingPage() {
$access = $this->getParserOutputAccess();
$page = $this->getNonexistingTestPage( __METHOD__ );
$parserOptions = $this->getParserOptions();
$status = $access->getParserOutput( $page, $parserOptions );
$this->assertStatusError( 'nopagetext', $status );
}
/**
* @param Status $status
* @param bool $fastStale
*/
private function setPoolCounterFactory( $status, $fastStale = false ) {
$this->overrideConfigValue( MainConfigNames::PoolCounterConf, [
'ArticleView' => [
'class' => MockPoolCounterFailing::class,
'fastStale' => $fastStale,
'mockAcquire' => $status,
'mockRelease' => Status::newGood( PoolCounter::RELEASED ),
],
] );
}
public static function providePoolWorkDirty() {
yield [ Status::newGood( PoolCounter::QUEUE_FULL ), false, 'view-pool-overload' ];
yield [ Status::newGood( PoolCounter::TIMEOUT ), false, 'view-pool-overload' ];
yield [ Status::newGood( PoolCounter::TIMEOUT ), true, 'view-pool-contention' ];
}
/**
* Tests that under some circumstances, stale cache entries will be returned, but get
* flagged as "dirty".
*
* @dataProvider providePoolWorkDirty
*/
public function testPoolWorkDirty( $status, $fastStale, $expectedMessage ) {
$this->overrideConfigValues( [
MainConfigNames::ParserCacheExpireTime => 60,
] );
$this->setPoolCounterFactory( Status::newGood( PoolCounter::LOCKED ), $fastStale );
MWTimestamp::setFakeTime( '2020-04-04T01:02:03' );
$access = $this->getParserOutputAccess( [
'expectPoolCounterCalls' => 2,
'parserCache' => new HashBagOStuff()
] );
$page = $this->getNonexistingTestPage( __METHOD__ );
$this->editPage( $page, 'Hello \'\'World\'\'!' );
$parserOptions = $this->getParserOptions();
$result = $access->getParserOutput(
$page,
$parserOptions,
null,
ParserOutputAccess::OPT_FOR_ARTICLE_VIEW
);
$this->assertContainsHtml( 'World', $result, 'fresh result' );
$testingAccess = TestingAccessWrapper::newFromObject( $access );
$testingAccess->localCache->clear();
$this->setPoolCounterFactory( $status, $fastStale );
// expire parser cache
MWTimestamp::setFakeTime( '2020-05-05T01:02:03' );
$parserOptions = $this->getParserOptions();
$cachedResult = $access->getParserOutput(
$page,
$parserOptions,
null,
ParserOutputAccess::OPT_FOR_ARTICLE_VIEW
);
$this->assertContainsHtml( 'World', $cachedResult, 'cached result' );
$this->assertStatusWarning( $expectedMessage, $cachedResult );
$this->assertStatusWarning( 'view-pool-dirty-output', $cachedResult );
}
/**
* Tests that a failure to acquire a work lock will be reported as an error if no
* stale output can be returned.
*/
public function testPoolWorkTimeout() {
$this->overrideConfigValues( [
MainConfigNames::ParserCacheExpireTime => 60,
] );
$this->setPoolCounterFactory( Status::newGood( PoolCounter::TIMEOUT ) );
$access = $this->getParserOutputAccess( [
'expectPoolCounterCalls' => 1
] );
$page = $this->getNonexistingTestPage( __METHOD__ );
$this->editPage( $page, 'Hello \'\'World\'\'!' );
$parserOptions = $this->getParserOptions();
$result = $access->getParserOutput(
$page,
$parserOptions,
null,
ParserOutputAccess::OPT_FOR_ARTICLE_VIEW
);
$this->assertStatusError( 'pool-timeout', $result );
}
/**
* Tests that a PoolCounter error does not prevent output from being generated.
*/
public function testPoolWorkError() {
$this->overrideConfigValues( [
MainConfigNames::ParserCacheExpireTime => 60,
] );
$this->setPoolCounterFactory( Status::newFatal( 'some-error' ) );
$access = $this->getParserOutputAccess();
$page = $this->getNonexistingTestPage( __METHOD__ );
$this->editPage( $page, 'Hello \'\'World\'\'!' );
$parserOptions = $this->getParserOptions();
$result = $access->getParserOutput( $page, $parserOptions );
$this->assertContainsHtml( 'World', $result );
}
public function testParsoidCacheSplit() {
$parserCacheFactory = $this->createMock( ParserCacheFactory::class );
$revisionOutputCache = $this->getRevisionOutputCache( new HashBagOStuff() );
$caches = [
$this->getParserCache( new HashBagOStuff() ),
$this->getParserCache( new HashBagOStuff() ),
];
$calls = [];
$parserCacheFactory
->method( 'getParserCache' )
->willReturnCallback( static function ( $cacheName ) use ( &$calls, $caches ) {
static $cacheList = [];
$calls[] = $cacheName;
$which = array_search( $cacheName, $cacheList );
if ( $which === false ) {
$which = count( $cacheList );
$cacheList[] = $cacheName;
}
return $caches[$which];
} );
$parserCacheFactory
->method( 'getRevisionOutputCache' )
->willReturn( $revisionOutputCache );
$access = $this->getParserOutputAccess( [
'parserCacheFactory' => $parserCacheFactory
] );
$parserOptions0 = $this->getParserOptions();
$page = $this->getNonexistingTestPage( __METHOD__ );
$output = $access->getCachedParserOutput( $page, $parserOptions0 );
$this->assertNull( $output );
// $calls[0] will remember what cache name we used.
$this->assertCount( 1, $calls );
$parserOptions1 = $this->getParserOptions();
$parserOptions1->setUseParsoid();
$output = $access->getCachedParserOutput( $page, $parserOptions1 );
$this->assertNull( $output );
$this->assertCount( 2, $calls );
// Check that we used a different cache name this time.
$this->assertNotEquals( $calls[1], $calls[0], "Should use different caches" );
// Try this again, with actual content.
$calls = [];
$this->editPage( $page, "__NOTOC__" );
$status0 = $access->getParserOutput( $page, $parserOptions0 );
$this->assertContainsHtml( '<div class="mw-content-ltr mw-parser-output" lang="en" dir="ltr"></div>', $status0 );
$status1 = $access->getParserOutput( $page, $parserOptions1 );
$this->assertContainsHtml( '<meta property="mw:PageProp/notoc"', $status1 );
$this->assertNotSameHtml( $status0, $status1 );
}
public function testParsoidRevisionCacheSplit() {
$parserCacheFactory = $this->createMock( ParserCacheFactory::class );
$parserCache = $this->getParserCache( new HashBagOStuff() );
$caches = [
$this->getRevisionOutputCache( new HashBagOStuff() ),
$this->getRevisionOutputCache( new HashBagOStuff() ),
];
$calls = [];
$parserCacheFactory
->method( 'getParserCache' )
->willReturn( $parserCache );
$parserCacheFactory
->method( 'getRevisionOutputCache' )
->willReturnCallback( static function ( $cacheName ) use ( &$calls, $caches ) {
static $cacheList = [];
$calls[] = $cacheName;
$which = array_search( $cacheName, $cacheList );
if ( $which === false ) {
$which = count( $cacheList );
$cacheList[] = $cacheName;
}
return $caches[$which];
} );
$access = $this->getParserOutputAccess( [
'parserCacheFactory' => $parserCacheFactory
] );
$page = $this->getNonexistingTestPage( __METHOD__ );
$firstRev = $this->editPage( $page, 'First __NOTOC__' )->getNewRevision();
$secondRev = $this->editPage( $page, 'Second __NOTOC__' )->getNewRevision();
$parserOptions0 = $this->getParserOptions();
$status = $access->getParserOutput( $page, $parserOptions0, $firstRev );
$this->assertContainsHtml( 'First', $status );
// Check that we used the "not parsoid" revision cache
$this->assertNotEmpty( $calls );
$notParsoid = $calls[0];
$this->assertEquals( array_fill( 0, count( $calls ), $notParsoid ), $calls );
$calls = [];
$parserOptions1 = $this->getParserOptions();
$parserOptions1->setUseParsoid();
$status = $access->getParserOutput( $page, $parserOptions1, $firstRev );
$this->assertContainsHtml( 'First', $status );
$this->assertContainsHtml( '<meta property="mw:PageProp/notoc"', $status );
$this->assertNotEmpty( $calls );
$parsoid = $calls[0];
$this->assertNotEquals( $notParsoid, $parsoid, "Should use different caches" );
$this->assertEquals( array_fill( 0, count( $calls ), $parsoid ), $calls );
}
}
|