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