wiki.techinc.nl/tests/phpunit/includes/poolcounter/PoolWorkArticleViewOldTest.php
Thiemo Kreuz c5fdb1c8ba Change ParserOutputAccess workers to work with Status objects
All these methods have been written to return true, but that value was
never used for anything other than realizing that the method succeeded.
The ParserOutput object we are interested in was stuck in a property.
Why not return the ParserOutput object?

I wrapped it in a Status object to be able to pass warning messages
along with the actual result. There was even more specialized code to
do that via dedicated setters and getters. All this can be removed now.

Bug: T304813
Change-Id: I6fd3745835dfcaec94695469498a2662f8317c35
2022-04-08 15:47:59 +02:00

103 lines
2.5 KiB
PHP

<?php
use MediaWiki\Json\JsonCodec;
use MediaWiki\Parser\RevisionOutputCache;
use MediaWiki\Revision\RevisionRecord;
use Psr\Log\NullLogger;
/**
* @covers PoolWorkArticleViewOld
* @group Database
*/
class PoolWorkArticleViewOldTest extends PoolWorkArticleViewTest {
/** @var RevisionOutputCache */
private $cache = null;
/**
* @param WikiPage $page
* @param RevisionRecord|null $rev
* @param ParserOptions|null $options
*
* @return PoolWorkArticleViewOld
*/
protected function newPoolWorkArticleView(
WikiPage $page,
RevisionRecord $rev = null,
$options = null
) {
if ( !$options ) {
$options = ParserOptions::newCanonical( 'canonical' );
}
if ( !$rev ) {
$rev = $page->getRevisionRecord();
}
if ( !$this->cache ) {
$this->installRevisionOutputCache();
}
$renderer = $this->getServiceContainer()->getRevisionRenderer();
return new PoolWorkArticleViewOld(
'test:' . $rev->getId(),
$this->cache,
$rev,
$options,
$renderer,
$this->getLoggerSpi()
);
}
/**
* @param BagOStuff|null $bag
*
* @return RevisionOutputCache
*/
private function installRevisionOutputCache( $bag = null ) {
$this->cache = new RevisionOutputCache(
'test',
new WANObjectCache( [ 'cache' => $bag ?: new HashBagOStuff() ] ),
60 * 60,
'20200101223344',
new JsonCodec(),
new NullStatsdDataFactory(),
new NullLogger()
);
return $this->cache;
}
public function testUpdateCachedOutput() {
$options = ParserOptions::newCanonical( 'canonical' );
$page = $this->getExistingTestPage( __METHOD__ );
$cache = $this->installRevisionOutputCache();
$work = $this->newPoolWorkArticleView( $page, null, $options );
/** @var Status $status */
$status = $work->execute();
$this->assertTrue( $status->isGood() );
$cachedOutput = $cache->get( $page->getRevisionRecord(), $options );
$this->assertNotEmpty( $cachedOutput );
$this->assertSame( $status->getValue()->getText(), $cachedOutput->getText() );
}
public function testDoesNotCacheNotSafe() {
$page = $this->getExistingTestPage( __METHOD__ );
$cache = $this->installRevisionOutputCache();
$parserOptions = ParserOptions::newCanonical( 'canonical' );
$parserOptions->setWrapOutputClass( 'wrapwrap' ); // Not safe to cache!
$work = $this->newPoolWorkArticleView( $page, null, $parserOptions );
/** @var Status $status */
$status = $work->execute();
$this->assertTrue( $status->isGood() );
$this->assertFalse( $cache->get( $page->getRevisionRecord(), $parserOptions ) );
}
}