2 use MediaWiki\MediaWikiServices
;
3 use MediaWiki\Storage\MutableRevisionRecord
;
4 use MediaWiki\Storage\RevisionRecord
;
7 * @covers PoolWorkArticleView
10 class PoolWorkArticleViewTest
extends MediaWikiTestCase
{
12 private function makeRevision( WikiPage
$page, $text ) {
13 $user = $this->getTestUser()->getUser();
14 $updater = $page->newPageUpdater( $user );
16 $updater->setContent( 'main', new WikitextContent( $text ) );
17 return $updater->saveRevision( CommentStoreComment
::newUnsavedComment( 'testing' ) );
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!' );
26 $work = new PoolWorkArticleView( $page, $options, $rev1->getId(), false );
28 $this->assertContains( 'First', $work->getParserOutput()->getText() );
30 $work = new PoolWorkArticleView( $page, $options, $rev2->getId(), false );
32 $this->assertContains( 'Second', $work->getParserOutput()->getText() );
35 public function testDoWorkParserCache() {
36 $options = ParserOptions
::newCanonical( 'canonical' );
37 $page = $this->getExistingTestPage( __METHOD__
);
38 $rev1 = $this->makeRevision( $page, 'First!' );
40 $work = new PoolWorkArticleView( $page, $options, $rev1->getId(), true );
43 $cache = MediaWikiServices
::getInstance()->getParserCache();
44 $out = $cache->get( $page, $options );
46 $this->assertNotNull( $out );
47 $this->assertNotFalse( $out );
48 $this->assertContains( 'First', $out->getText() );
51 public function testDoWorkWithExplicitRevision() {
52 $options = ParserOptions
::newCanonical( 'canonical' );
53 $page = $this->getExistingTestPage( __METHOD__
);
54 $rev = $this->makeRevision( $page, 'NOPE' );
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!' ) );
62 $work = new PoolWorkArticleView( $page, $options, $rev->getId(), false, $fakeRev );
65 $text = $work->getParserOutput()->getText();
66 $this->assertContains( 'YES!', $text );
67 $this->assertNotContains( 'NOPE', $text );
70 public function testDoWorkWithContent() {
71 $options = ParserOptions
::newCanonical( 'canonical' );
72 $page = $this->getExistingTestPage( __METHOD__
);
74 $content = new WikitextContent( 'YES!' );
76 $work = new PoolWorkArticleView( $page, $options, $page->getLatest(), false, $content );
79 $text = $work->getParserOutput()->getText();
80 $this->assertContains( 'YES!', $text );
83 public function testDoWorkWithString() {
84 $options = ParserOptions
::newCanonical( 'canonical' );
85 $page = $this->getExistingTestPage( __METHOD__
);
87 $work = new PoolWorkArticleView( $page, $options, $page->getLatest(), false, 'YES!' );
90 $text = $work->getParserOutput()->getText();
91 $this->assertContains( 'YES!', $text );
94 public function provideMagicWords() {
96 'Test {{PAGEID}} Test',
97 function ( RevisionRecord
$rev ) {
98 return $rev->getPageId();
101 yield
'REVISIONID' => [
102 'Test {{REVISIONID}} Test',
103 function ( RevisionRecord
$rev ) {
104 return $rev->getId();
107 yield
'REVISIONUSER' => [
108 'Test {{REVISIONUSER}} Test',
109 function ( RevisionRecord
$rev ) {
110 return $rev->getUser()->getName();
113 yield
'REVISIONTIMESTAMP' => [
114 'Test {{REVISIONTIMESTAMP}} Test',
115 function ( RevisionRecord
$rev ) {
116 return $rev->getTimestamp();
121 * @dataProvider provideMagicWords
123 public function testMagicWords( $wikitext, $callback ) {
124 $options = ParserOptions
::newCanonical( 'canonical' );
125 $page = $this->getExistingTestPage( __METHOD__
);
126 $rev = $page->getRevision()->getRevisionRecord();
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 );
134 $expected = strval( $callback( $rev ) );
135 $output = $work->getParserOutput();
137 $this->assertContains( $expected, $output->getText() );
140 public function testDoWorkMissingPage() {
141 $options = ParserOptions
::newCanonical( 'canonical' );
142 $page = $this->getNonexistingTestPage();
144 $work = new PoolWorkArticleView( $page, $options, '667788', false );
145 $this->assertFalse( $work->execute() );
148 public function testDoWorkDeletedContent() {
149 $options = ParserOptions
::newCanonical( 'canonical' );
150 $page = $this->getExistingTestPage( __METHOD__
);
151 $rev1 = $page->getRevision()->getRevisionRecord();
153 // make another revision, since the latest revision cannot be deleted.
154 $rev2 = $this->makeRevision( $page, 'Next' );
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
);
163 $work = new PoolWorkArticleView( $page, $options, $rev1->getId(), false, $fakeRev );
164 $this->assertFalse( $work->execute() );
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() );