| 1 |
<?php |
| 2 |
|
| 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; |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
class ParsoidCachePrewarmJobTest extends MediaWikiIntegrationTestCase { |
| 20 |
|
| 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'; |
| 23 |
|
| 24 |
private function getPageIdentity( PageRecord $page ): PageIdentityValue { |
| 25 |
return PageIdentityValue::localIdentity( |
| 26 |
$page->getId(), |
| 27 |
$page->getNamespace(), |
| 28 |
$page->getDBkey() |
| 29 |
); |
| 30 |
} |
| 31 |
|
| 32 |
private function getPageRecord( PageIdentity $page ): PageRecord { |
| 33 |
return $this->getServiceContainer()->getPageStore() |
| 34 |
->getPageByReference( $page ); |
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
public function testRun() { |
| 43 |
$page = $this->getExistingTestPage( 'ParsoidPrewarmJob' )->toPageRecord(); |
| 44 |
$rev1 = $this->editPage( $page, self::NON_JOB_QUEUE_EDIT )->getNewRevision(); |
| 45 |
|
| 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() |
| 52 |
); |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
$execStatus = $parsoidPrewarmJob->run(); |
| 58 |
$this->assertTrue( $execStatus ); |
| 59 |
|
| 60 |
$popts = ParserOptions::newFromAnon(); |
| 61 |
$popts->setUseParsoid(); |
| 62 |
$parsoidOutput = $this->getServiceContainer()->getParserOutputAccess()->getCachedParserOutput( |
| 63 |
$this->getPageRecord( $this->getPageIdentity( $page ) ), |
| 64 |
$popts, |
| 65 |
$rev1 |
| 66 |
); |
| 67 |
|
| 68 |
|
| 69 |
$this->assertStringContainsString( '<html', $parsoidOutput->getRawText() ); |
| 70 |
$this->assertStringContainsString( self::NON_JOB_QUEUE_EDIT, $parsoidOutput->getRawText() ); |
| 71 |
|
| 72 |
$rev2 = $this->editPage( $page, self::JOB_QUEUE_EDIT )->getNewRevision(); |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
$this->resetServices(); |
| 78 |
|
| 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() |
| 85 |
); |
| 86 |
|
| 87 |
$jobQueueGroup = $this->getServiceContainer()->getJobQueueGroup(); |
| 88 |
$jobQueueGroup->get( $parsoidPrewarmJob->getType() )->delete(); |
| 89 |
$jobQueueGroup->push( $parsoidPrewarmJob ); |
| 90 |
|
| 91 |
|
| 92 |
$this->assertSame( 1, $jobQueueGroup->getQueueSizes()['parsoidCachePrewarm'] ); |
| 93 |
|
| 94 |
|
| 95 |
$this->runJobs( [ 'maxJobs' => 1 ], [ 'type' => 'parsoidCachePrewarm' ] ); |
| 96 |
|
| 97 |
|
| 98 |
$this->assertSame( 0, $jobQueueGroup->getQueueSizes()['parsoidCachePrewarm'] ); |
| 99 |
|
| 100 |
$parsoidOutput = $this->getServiceContainer()->getParserOutputAccess()->getCachedParserOutput( |
| 101 |
$this->getPageRecord( $this->getPageIdentity( $page ) ), |
| 102 |
$popts, |
| 103 |
$rev2 |
| 104 |
); |
| 105 |
|
| 106 |
|
| 107 |
$this->assertStringContainsString( '<html', $parsoidOutput->getRawText() ); |
| 108 |
$this->assertStringContainsString( self::JOB_QUEUE_EDIT, $parsoidOutput->getRawText() ); |
| 109 |
|
| 110 |
$services = MediaWikiServices::getInstance(); |
| 111 |
$servicesOptions = new ServiceOptions( |
| 112 |
RenderDebugInfo::CONSTRUCTOR_OPTIONS, $services->getMainConfig() |
| 113 |
); |
| 114 |
$rdi = TestingAccessWrapper::newFromObject( |
| 115 |
new RenderDebugInfo( $servicesOptions, new NullLogger(), $services->getHookContainer() ) |
| 116 |
); |
| 117 |
|
| 118 |
$this->assertStringContainsString( |
| 119 |
'triggered because: just for testing', |
| 120 |
$rdi->debugInfo( $parsoidOutput ) |
| 121 |
); |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
public function testEnqueueSpec() { |
| 128 |
$page = $this->getExistingTestPage( 'ParsoidPrewarmJob' )->toPageRecord(); |
| 129 |
$rev1 = $this->editPage( $page, self::NON_JOB_QUEUE_EDIT )->getNewRevision(); |
| 130 |
|
| 131 |
$parsoidPrewarmSpec = ParsoidCachePrewarmJob::newSpec( |
| 132 |
$rev1->getId(), $page, |
| 133 |
); |
| 134 |
|
| 135 |
$this->assertSame( 'parsoidCachePrewarm', $parsoidPrewarmSpec->getType(), 'getType' ); |
| 136 |
|
| 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'] ); |
| 142 |
|
| 143 |
$jobQueueGroup = $this->getServiceContainer()->getJobQueueGroup(); |
| 144 |
$jobQueueGroup->get( $parsoidPrewarmSpec->getType() )->delete(); |
| 145 |
|
| 146 |
$jobQueueGroup->push( $parsoidPrewarmSpec ); |
| 147 |
|
| 148 |
|
| 149 |
$this->assertSame( 1, $jobQueueGroup->getQueueSizes()['parsoidCachePrewarm'] ); |
| 150 |
|
| 151 |
|
| 152 |
$jobQueueGroup->push( $parsoidPrewarmSpec ); |
| 153 |
$jobQueueGroup->push( $parsoidPrewarmSpec ); |
| 154 |
|
| 155 |
|
| 156 |
$this->assertSame( 1, $jobQueueGroup->getQueueSizes()['parsoidCachePrewarm'] ); |
| 157 |
} |
| 158 |
|
| 159 |
} |