Localisation updates from https://translatewiki.net.
[mediawiki.git] / tests / phpunit / includes / jobqueue / jobs / ParsoidCachePrewarmJobTest.php
blobca0097e38b6bb2278d952bea1e7a3e0dc58771fc
1 <?php
3 use MediaWiki\Config\ServiceOptions;
4 use MediaWiki\MediaWikiServices;
5 use MediaWiki\OutputTransform\Stages\RenderDebugInfo;
6 use MediaWiki\Page\PageIdentity;
7 use MediaWiki\Page\PageIdentityValue;
8 use MediaWiki\Page\PageRecord;
9 use MediaWiki\Parser\ParserOptions;
10 use Psr\Log\NullLogger;
11 use Wikimedia\TestingAccessWrapper;
13 /**
14 * @group JobQueue
15 * @group Database
17 * @license GPL-2.0-or-later
19 class ParsoidCachePrewarmJobTest extends MediaWikiIntegrationTestCase {
21 private const NON_JOB_QUEUE_EDIT = 'parsoid edit not executed by job queue';
22 private const JOB_QUEUE_EDIT = 'parsoid edit executed by job queue';
24 private function getPageIdentity( PageRecord $page ): PageIdentityValue {
25 return PageIdentityValue::localIdentity(
26 $page->getId(),
27 $page->getNamespace(),
28 $page->getDBkey()
32 private function getPageRecord( PageIdentity $page ): PageRecord {
33 return $this->getServiceContainer()->getPageStore()
34 ->getPageByReference( $page );
37 /**
38 * @covers \ParsoidCachePrewarmJob::doParsoidCacheUpdate
39 * @covers \ParsoidCachePrewarmJob::newSpec
40 * @covers \ParsoidCachePrewarmJob::run
42 public function testRun() {
43 $page = $this->getExistingTestPage( 'ParsoidPrewarmJob' )->toPageRecord();
44 $rev1 = $this->editPage( $page, self::NON_JOB_QUEUE_EDIT )->getNewRevision();
46 $parsoidPrewarmJob = new ParsoidCachePrewarmJob(
47 [ 'revId' => $rev1->getId(), 'pageId' => $page->getId() ],
48 $this->getServiceContainer()->getParserOutputAccess(),
49 $this->getServiceContainer()->getPageStore(),
50 $this->getServiceContainer()->getRevisionLookup(),
51 $this->getServiceContainer()->getParsoidSiteConfig()
54 // NOTE: calling ->run() will not run the job scheduled in the queue but will
55 // instead call doParsoidCacheUpdate() directly. Will run the job and assert
56 // below.
57 $execStatus = $parsoidPrewarmJob->run();
58 $this->assertTrue( $execStatus );
60 $popts = ParserOptions::newFromAnon();
61 $popts->setUseParsoid();
62 $parsoidOutput = $this->getServiceContainer()->getParserOutputAccess()->getCachedParserOutput(
63 $this->getPageRecord( $this->getPageIdentity( $page ) ),
64 $popts,
65 $rev1
68 // Ensure we have the parsoid output in parser cache as an HTML document
69 $this->assertStringContainsString( '<html', $parsoidOutput->getRawText() );
70 $this->assertStringContainsString( self::NON_JOB_QUEUE_EDIT, $parsoidOutput->getRawText() );
72 $rev2 = $this->editPage( $page, self::JOB_QUEUE_EDIT )->getNewRevision();
73 // Post-edit, reset all services!
74 // ParserOutputAccess has a localCache which can incorrectly return stale
75 // content for the previous revision! Resetting ensures that ParsoidCachePrewarmJob
76 // gets a fresh copy of ParserOutputAccess.
77 $this->resetServices();
79 $parsoidPrewarmJob = new ParsoidCachePrewarmJob(
80 [ 'revId' => $rev2->getId(), 'pageId' => $page->getId(), 'causeAction' => 'just for testing' ],
81 $this->getServiceContainer()->getParserOutputAccess(),
82 $this->getServiceContainer()->getPageStore(),
83 $this->getServiceContainer()->getRevisionLookup(),
84 $this->getServiceContainer()->getParsoidSiteConfig()
87 $jobQueueGroup = $this->getServiceContainer()->getJobQueueGroup();
88 $jobQueueGroup->get( $parsoidPrewarmJob->getType() )->delete();
89 $jobQueueGroup->push( $parsoidPrewarmJob );
91 // At this point, we have 1 job scheduled for this job type.
92 $this->assertSame( 1, $jobQueueGroup->getQueueSizes()['parsoidCachePrewarm'] );
94 // doParsoidCacheUpdate() now with a job queue instead of calling directly.
95 $this->runJobs( [ 'maxJobs' => 1 ], [ 'type' => 'parsoidCachePrewarm' ] );
97 // At this point, we have 0 jobs scheduled for this job type.
98 $this->assertSame( 0, $jobQueueGroup->getQueueSizes()['parsoidCachePrewarm'] );
100 $parsoidOutput = $this->getServiceContainer()->getParserOutputAccess()->getCachedParserOutput(
101 $this->getPageRecord( $this->getPageIdentity( $page ) ),
102 $popts,
103 $rev2
106 // Ensure we have the parsoid output in parser cache as an HTML document
107 $this->assertStringContainsString( '<html', $parsoidOutput->getRawText() );
108 $this->assertStringContainsString( self::JOB_QUEUE_EDIT, $parsoidOutput->getRawText() );
110 $services = MediaWikiServices::getInstance();
111 $servicesOptions = new ServiceOptions(
112 RenderDebugInfo::CONSTRUCTOR_OPTIONS, $services->getMainConfig()
114 $rdi = TestingAccessWrapper::newFromObject(
115 new RenderDebugInfo( $servicesOptions, new NullLogger(), $services->getHookContainer() )
117 // Check that the causeAction was looped through as the render reason
118 $this->assertStringContainsString(
119 'triggered because: just for testing',
120 $rdi->debugInfo( $parsoidOutput )
125 * @covers \ParsoidCachePrewarmJob::newSpec
127 public function testEnqueueSpec() {
128 $page = $this->getExistingTestPage( 'ParsoidPrewarmJob' )->toPageRecord();
129 $rev1 = $this->editPage( $page, self::NON_JOB_QUEUE_EDIT )->getNewRevision();
131 $parsoidPrewarmSpec = ParsoidCachePrewarmJob::newSpec(
132 $rev1->getId(), $page,
135 $this->assertSame( 'parsoidCachePrewarm', $parsoidPrewarmSpec->getType(), 'getType' );
137 $dedupeInfo = $parsoidPrewarmSpec->getDeduplicationInfo();
138 $this->assertTrue( $dedupeInfo['params']['rootJobIsSelf'] );
139 $this->assertSame( $page->getTouched(), $dedupeInfo['params']['page_touched'] );
140 $this->assertSame( $rev1->getId(), $dedupeInfo['params']['revId'] );
141 $this->assertSame( $page->getId(), $dedupeInfo['params']['pageId'] );
143 $jobQueueGroup = $this->getServiceContainer()->getJobQueueGroup();
144 $jobQueueGroup->get( $parsoidPrewarmSpec->getType() )->delete();
146 $jobQueueGroup->push( $parsoidPrewarmSpec );
148 // At this point, we have 1 job scheduled for this job type.
149 $this->assertSame( 1, $jobQueueGroup->getQueueSizes()['parsoidCachePrewarm'] );
151 // Push again times, deduplication should apply!
152 $jobQueueGroup->push( $parsoidPrewarmSpec );
153 $jobQueueGroup->push( $parsoidPrewarmSpec );
155 // We should still have just 1 job scheduled for this job type.
156 $this->assertSame( 1, $jobQueueGroup->getQueueSizes()['parsoidCachePrewarm'] );