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