Do not insert page titles into querycache.qc_value
[lhc/web/wiklou.git] / tests / phpunit / includes / poolcounter / PoolWorkArticleViewTest.php
1 <?php
2
3 use MediaWiki\MediaWikiServices;
4 use MediaWiki\Revision\MutableRevisionRecord;
5 use MediaWiki\Revision\RevisionRecord;
6 use MediaWiki\Revision\SlotRecord;
7
8 /**
9 * @covers PoolWorkArticleView
10 * @group Database
11 */
12 class PoolWorkArticleViewTest extends MediaWikiTestCase {
13
14 private function makeRevision( WikiPage $page, $text ) {
15 $user = $this->getTestUser()->getUser();
16 $updater = $page->newPageUpdater( $user );
17
18 $updater->setContent( SlotRecord::MAIN, new WikitextContent( $text ) );
19 return $updater->saveRevision( CommentStoreComment::newUnsavedComment( 'testing' ) );
20 }
21
22 public function testDoWorkLoadRevision() {
23 $options = ParserOptions::newCanonical( 'canonical' );
24 $page = $this->getExistingTestPage( __METHOD__ );
25 $rev1 = $this->makeRevision( $page, 'First!' );
26 $rev2 = $this->makeRevision( $page, 'Second!' );
27
28 $work = new PoolWorkArticleView( $page, $options, $rev1->getId(), false );
29 $work->execute();
30 $this->assertContains( 'First', $work->getParserOutput()->getText() );
31
32 $work = new PoolWorkArticleView( $page, $options, $rev2->getId(), false );
33 $work->execute();
34 $this->assertContains( 'Second', $work->getParserOutput()->getText() );
35 }
36
37 public function testDoWorkParserCache() {
38 $options = ParserOptions::newCanonical( 'canonical' );
39 $page = $this->getExistingTestPage( __METHOD__ );
40 $rev1 = $this->makeRevision( $page, 'First!' );
41
42 $work = new PoolWorkArticleView( $page, $options, $rev1->getId(), true );
43 $work->execute();
44
45 $cache = MediaWikiServices::getInstance()->getParserCache();
46 $out = $cache->get( $page, $options );
47
48 $this->assertNotNull( $out );
49 $this->assertNotFalse( $out );
50 $this->assertContains( 'First', $out->getText() );
51 }
52
53 public function testDoWorkWithExplicitRevision() {
54 $options = ParserOptions::newCanonical( 'canonical' );
55 $page = $this->getExistingTestPage( __METHOD__ );
56 $rev = $this->makeRevision( $page, 'NOPE' );
57
58 // make a fake revision with different content, so we know it's actually being used!
59 $fakeRev = new MutableRevisionRecord( $page->getTitle() );
60 $fakeRev->setId( $rev->getId() );
61 $fakeRev->setPageId( $page->getId() );
62 $fakeRev->setContent( SlotRecord::MAIN, new WikitextContent( 'YES!' ) );
63
64 $work = new PoolWorkArticleView( $page, $options, $rev->getId(), false, $fakeRev );
65 $work->execute();
66
67 $text = $work->getParserOutput()->getText();
68 $this->assertContains( 'YES!', $text );
69 $this->assertNotContains( 'NOPE', $text );
70 }
71
72 public function testDoWorkWithContent() {
73 $options = ParserOptions::newCanonical( 'canonical' );
74 $page = $this->getExistingTestPage( __METHOD__ );
75
76 $content = new WikitextContent( 'YES!' );
77
78 $work = new PoolWorkArticleView( $page, $options, $page->getLatest(), false, $content );
79 $work->execute();
80
81 $text = $work->getParserOutput()->getText();
82 $this->assertContains( 'YES!', $text );
83 }
84
85 public function testDoWorkWithString() {
86 $options = ParserOptions::newCanonical( 'canonical' );
87 $page = $this->getExistingTestPage( __METHOD__ );
88
89 $work = new PoolWorkArticleView( $page, $options, $page->getLatest(), false, 'YES!' );
90 $work->execute();
91
92 $text = $work->getParserOutput()->getText();
93 $this->assertContains( 'YES!', $text );
94 }
95
96 public function provideMagicWords() {
97 yield 'PAGEID' => [
98 'Test {{PAGEID}} Test',
99 function ( RevisionRecord $rev ) {
100 return $rev->getPageId();
101 }
102 ];
103 yield 'REVISIONID' => [
104 'Test {{REVISIONID}} Test',
105 function ( RevisionRecord $rev ) {
106 return $rev->getId();
107 }
108 ];
109 yield 'REVISIONUSER' => [
110 'Test {{REVISIONUSER}} Test',
111 function ( RevisionRecord $rev ) {
112 return $rev->getUser()->getName();
113 }
114 ];
115 yield 'REVISIONTIMESTAMP' => [
116 'Test {{REVISIONTIMESTAMP}} Test',
117 function ( RevisionRecord $rev ) {
118 return $rev->getTimestamp();
119 }
120 ];
121 }
122
123 /**
124 * @dataProvider provideMagicWords
125 */
126 public function testMagicWords( $wikitext, $callback ) {
127 $options = ParserOptions::newCanonical( 'canonical' );
128 $page = $this->getExistingTestPage( __METHOD__ );
129 $rev = $page->getRevision()->getRevisionRecord();
130
131 // NOTE: provide the input as a string and let the PoolWorkArticleView create a fake
132 // revision internally, to see if the magic words work with that fake. They should
133 // work if the Parser causes the actual revision to be loaded when needed.
134 $work = new PoolWorkArticleView( $page, $options, $page->getLatest(), false, $wikitext );
135 $work->execute();
136
137 $expected = strval( $callback( $rev ) );
138 $output = $work->getParserOutput();
139
140 $this->assertContains( $expected, $output->getText() );
141 }
142
143 public function testDoWorkMissingPage() {
144 $options = ParserOptions::newCanonical( 'canonical' );
145 $page = $this->getNonexistingTestPage();
146
147 $work = new PoolWorkArticleView( $page, $options, '667788', false );
148 $this->assertFalse( $work->execute() );
149 }
150
151 public function testDoWorkDeletedContent() {
152 $options = ParserOptions::newCanonical( 'canonical' );
153 $page = $this->getExistingTestPage( __METHOD__ );
154 $rev1 = $page->getRevision()->getRevisionRecord();
155
156 // make another revision, since the latest revision cannot be deleted.
157 $rev2 = $this->makeRevision( $page, 'Next' );
158
159 // make a fake revision with deleted different content
160 $fakeRev = new MutableRevisionRecord( $page->getTitle() );
161 $fakeRev->setId( $rev1->getId() );
162 $fakeRev->setPageId( $page->getId() );
163 $fakeRev->setContent( SlotRecord::MAIN, new WikitextContent( 'SECRET' ) );
164 $fakeRev->setVisibility( RevisionRecord::DELETED_TEXT );
165
166 $work = new PoolWorkArticleView( $page, $options, $rev1->getId(), false, $fakeRev );
167 $this->assertFalse( $work->execute() );
168
169 $work = new PoolWorkArticleView( $page, $options, $rev1->getId(), false, $fakeRev,
170 RevisionRecord::RAW );
171 $this->assertNotFalse( $work->execute() );
172
173 // a deleted current revision should still be show
174 $fakeRev->setId( $rev2->getId() );
175 $work = new PoolWorkArticleView( $page, $options, $rev2->getId(), false, $fakeRev );
176 $this->assertNotFalse( $work->execute() );
177 }
178
179 }