Debian Codegraph @

tests/phpunit/includes/poolcounter/PoolWorkArticleViewOldTest.php

raw symbols SCIP
1 <?php
2
3 use MediaWiki\Json\JsonCodec;
4 use MediaWiki\Parser\ParserOptions;
5 use MediaWiki\Parser\RevisionOutputCache;
6 use MediaWiki\PoolCounter\PoolWorkArticleViewOld;
7 use MediaWiki\Revision\RevisionRecord;
8 use MediaWiki\Status\Status;
9 use Psr\Log\NullLogger;
10 use Wikimedia\ObjectCache\BagOStuff;
11 use Wikimedia\ObjectCache\HashBagOStuff;
12 use Wikimedia\ObjectCache\WANObjectCache;
13 use Wikimedia\Stats\StatsFactory;
14 use Wikimedia\UUID\GlobalIdGenerator;
15
16 /**
17 * @covers \MediaWiki\PoolCounter\PoolWorkArticleViewOld
18 * @group Database
19 */
20 class PoolWorkArticleViewOldTest extends PoolWorkArticleViewTest {
21
22 /** @var RevisionOutputCache */
23 private $cache = null;
24
25 /**
26 * @param WikiPage $page
27 * @param RevisionRecord|null $rev
28 * @param ParserOptions|null $options
29 *
30 * @return PoolWorkArticleViewOld
31 */
32 protected function newPoolWorkArticleView(
33 WikiPage $page,
34 ?RevisionRecord $rev = null,
35 $options = null
36 ) {
37 if ( !$options ) {
38 $options = ParserOptions::newFromAnon();
39 }
40
41 if ( !$rev ) {
42 $rev = $page->getRevisionRecord();
43 }
44
45 if ( !$this->cache ) {
46 $this->installRevisionOutputCache();
47 }
48
49 $renderer = $this->getServiceContainer()->getRevisionRenderer();
50
51 return new PoolWorkArticleViewOld(
52 'test:' . $rev->getId(),
53 $this->cache,
54 $rev,
55 $options,
56 $renderer,
57 $this->getLoggerSpi()
58 );
59 }
60
61 /**
62 * @param BagOStuff|null $bag
63 *
64 * @return RevisionOutputCache
65 */
66 private function installRevisionOutputCache( $bag = null ) {
67 $globalIdGenerator = $this->createMock( GlobalIdGenerator::class );
68 $globalIdGenerator->method( 'newUUIDv1' )->willReturn( 'uuid-uuid' );
69 $this->cache = new RevisionOutputCache(
70 'test',
71 new WANObjectCache( [ 'cache' => $bag ?: new HashBagOStuff() ] ),
72 60 * 60,
73 '20200101223344',
74 new JsonCodec(),
75 StatsFactory::newNull(),
76 new NullLogger(),
77 $globalIdGenerator
78 );
79
80 return $this->cache;
81 }
82
83 public function testUpdateCachedOutput() {
84 $options = ParserOptions::newFromAnon();
85 $page = $this->getExistingTestPage( __METHOD__ );
86
87 $cache = $this->installRevisionOutputCache();
88
89 $work = $this->newPoolWorkArticleView( $page, null, $options );
90 /** @var Status $status */
91 $status = $work->execute();
92 $this->assertStatusGood( $status );
93
94 $cachedOutput = $cache->get( $page->getRevisionRecord(), $options );
95 $this->assertNotEmpty( $cachedOutput );
96 $this->assertSame( $status->getValue()->getRawText(),
97 $cachedOutput->getRawText() );
98 }
99
100 public function testDoesNotCacheNotSafe() {
101 $page = $this->getExistingTestPage( __METHOD__ );
102
103 $cache = $this->installRevisionOutputCache();
104
105 $parserOptions = ParserOptions::newFromAnon();
106 $parserOptions->setWrapOutputClass( 'wrapwrap' ); // Not safe to cache!
107
108 $work = $this->newPoolWorkArticleView( $page, null, $parserOptions );
109 /** @var Status $status */
110 $status = $work->execute();
111 $this->assertStatusGood( $status );
112
113 $this->assertFalse( $cache->get( $page->getRevisionRecord(), $parserOptions ) );
114 }
115
116 public function testDoWorkWithFakeRevision() {
117 // PoolWorkArticleViewOld caches the results, but things with null revid should
118 // not be cached.
119 $this->expectException( InvalidArgumentException::class );
120 parent::testDoWorkWithFakeRevision();
121 }
122 }