2 use MediaWiki\MediaWikiServices
;
3 use MediaWiki\Storage\MutableRevisionRecord
;
4 use MediaWiki\Storage\RevisionRecord
;
7 * @covers PoolWorkArticleView
9 class PoolWorkArticleViewTest
extends MediaWikiTestCase
{
11 private function makeRevision( WikiPage
$page, $text ) {
12 $user = $this->getTestUser()->getUser();
13 $updater = $page->newPageUpdater( $user );
15 $updater->setContent( 'main', new WikitextContent( $text ) );
16 return $updater->saveRevision( CommentStoreComment
::newUnsavedComment( 'testing' ) );
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!' );
25 $work = new PoolWorkArticleView( $page, $options, $rev1->getId(), false );
27 $this->assertContains( 'First', $work->getParserOutput()->getText() );
29 $work = new PoolWorkArticleView( $page, $options, $rev2->getId(), false );
31 $this->assertContains( 'Second', $work->getParserOutput()->getText() );
34 public function testDoWorkParserCache() {
35 $options = ParserOptions
::newCanonical( 'canonical' );
36 $page = $this->getExistingTestPage( __METHOD__
);
37 $rev1 = $this->makeRevision( $page, 'First!' );
39 $work = new PoolWorkArticleView( $page, $options, $rev1->getId(), true );
42 $cache = MediaWikiServices
::getInstance()->getParserCache();
43 $out = $cache->get( $page, $options );
45 $this->assertNotNull( $out );
46 $this->assertNotFalse( $out );
47 $this->assertContains( 'First', $out->getText() );
50 public function testDoWorkWithExplicitRevision() {
51 $options = ParserOptions
::newCanonical( 'canonical' );
52 $page = $this->getExistingTestPage( __METHOD__
);
53 $rev = $this->makeRevision( $page, 'NOPE' );
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!' ) );
61 $work = new PoolWorkArticleView( $page, $options, $rev->getId(), false, $fakeRev );
64 $text = $work->getParserOutput()->getText();
65 $this->assertContains( 'YES!', $text );
66 $this->assertNotContains( 'NOPE', $text );
69 public function testDoWorkWithContent() {
70 $options = ParserOptions
::newCanonical( 'canonical' );
71 $page = $this->getExistingTestPage( __METHOD__
);
73 $content = new WikitextContent( 'YES!' );
75 $work = new PoolWorkArticleView( $page, $options, $page->getLatest(), false, $content );
78 $text = $work->getParserOutput()->getText();
79 $this->assertContains( 'YES!', $text );
82 public function testDoWorkWithString() {
83 $options = ParserOptions
::newCanonical( 'canonical' );
84 $page = $this->getExistingTestPage( __METHOD__
);
86 $work = new PoolWorkArticleView( $page, $options, $page->getLatest(), false, 'YES!' );
89 $text = $work->getParserOutput()->getText();
90 $this->assertContains( 'YES!', $text );
93 public function provideMagicWords() {
95 'Test {{PAGEID}} Test',
96 function ( RevisionRecord
$rev ) {
97 return $rev->getPageId();
100 yield
'REVISIONID' => [
101 'Test {{REVISIONID}} Test',
102 function ( RevisionRecord
$rev ) {
103 return $rev->getId();
106 yield
'REVISIONUSER' => [
107 'Test {{REVISIONUSER}} Test',
108 function ( RevisionRecord
$rev ) {
109 return $rev->getUser()->getName();
112 yield
'REVISIONTIMESTAMP' => [
113 'Test {{REVISIONTIMESTAMP}} Test',
114 function ( RevisionRecord
$rev ) {
115 return $rev->getTimestamp();
120 * @dataProvider provideMagicWords
122 public function testMagicWords( $wikitext, $callback ) {
123 $options = ParserOptions
::newCanonical( 'canonical' );
124 $page = $this->getExistingTestPage( __METHOD__
);
125 $rev = $page->getRevision()->getRevisionRecord();
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 );
133 $expected = strval( $callback( $rev ) );
134 $output = $work->getParserOutput();
136 $this->assertContains( $expected, $output->getText() );
139 public function testDoWorkMissingPage() {
140 $options = ParserOptions
::newCanonical( 'canonical' );
141 $page = $this->getNonexistingTestPage();
143 $work = new PoolWorkArticleView( $page, $options, '667788', false );
144 $this->assertFalse( $work->execute() );
147 public function testDoWorkDeletedContent() {
148 $options = ParserOptions
::newCanonical( 'canonical' );
149 $page = $this->getExistingTestPage( __METHOD__
);
150 $rev1 = $page->getRevision()->getRevisionRecord();
152 // make another revision, since the latest revision cannot be deleted.
153 $rev2 = $this->makeRevision( $page, 'Next' );
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
);
162 $work = new PoolWorkArticleView( $page, $options, $rev1->getId(), false, $fakeRev );
163 $this->assertFalse( $work->execute() );
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() );