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