3 use MediaWiki\MediaWikiServices
;
4 use MediaWiki\Revision\MutableRevisionRecord
;
5 use MediaWiki\Revision\RevisionRecord
;
6 use MediaWiki\Revision\SlotRecord
;
9 * @covers PoolWorkArticleView
12 class PoolWorkArticleViewTest
extends MediaWikiTestCase
{
14 private function makeRevision( WikiPage
$page, $text ) {
15 $user = $this->getTestUser()->getUser();
16 $updater = $page->newPageUpdater( $user );
18 $updater->setContent( SlotRecord
::MAIN
, new WikitextContent( $text ) );
19 return $updater->saveRevision( CommentStoreComment
::newUnsavedComment( 'testing' ) );
22 public function testDoWorkLoadRevision() {
23 $options = ParserOptions
::newCanonical( 'canonical' );
24 $page = $this->getExistingTestPage( __METHOD__
);
25 $rev1 = $this->makeRevision( $page, 'First!' );
26 $rev2 = $this->makeRevision( $page, 'Second!' );
28 $work = new PoolWorkArticleView( $page, $options, $rev1->getId(), false );
30 $this->assertContains( 'First', $work->getParserOutput()->getText() );
32 $work = new PoolWorkArticleView( $page, $options, $rev2->getId(), false );
34 $this->assertContains( 'Second', $work->getParserOutput()->getText() );
37 public function testDoWorkParserCache() {
38 $options = ParserOptions
::newCanonical( 'canonical' );
39 $page = $this->getExistingTestPage( __METHOD__
);
40 $rev1 = $this->makeRevision( $page, 'First!' );
42 $work = new PoolWorkArticleView( $page, $options, $rev1->getId(), true );
45 $cache = MediaWikiServices
::getInstance()->getParserCache();
46 $out = $cache->get( $page, $options );
48 $this->assertNotNull( $out );
49 $this->assertNotFalse( $out );
50 $this->assertContains( 'First', $out->getText() );
53 public function testDoWorkWithExplicitRevision() {
54 $options = ParserOptions
::newCanonical( 'canonical' );
55 $page = $this->getExistingTestPage( __METHOD__
);
56 $rev = $this->makeRevision( $page, 'NOPE' );
58 // make a fake revision with different content, so we know it's actually being used!
59 $fakeRev = new MutableRevisionRecord( $page->getTitle() );
60 $fakeRev->setId( $rev->getId() );
61 $fakeRev->setPageId( $page->getId() );
62 $fakeRev->setContent( SlotRecord
::MAIN
, new WikitextContent( 'YES!' ) );
64 $work = new PoolWorkArticleView( $page, $options, $rev->getId(), false, $fakeRev );
67 $text = $work->getParserOutput()->getText();
68 $this->assertContains( 'YES!', $text );
69 $this->assertNotContains( 'NOPE', $text );
72 public function testDoWorkWithContent() {
73 $options = ParserOptions
::newCanonical( 'canonical' );
74 $page = $this->getExistingTestPage( __METHOD__
);
76 $content = new WikitextContent( 'YES!' );
78 $work = new PoolWorkArticleView( $page, $options, $page->getLatest(), false, $content );
81 $text = $work->getParserOutput()->getText();
82 $this->assertContains( 'YES!', $text );
85 public function testDoWorkWithString() {
86 $options = ParserOptions
::newCanonical( 'canonical' );
87 $page = $this->getExistingTestPage( __METHOD__
);
89 $work = new PoolWorkArticleView( $page, $options, $page->getLatest(), false, 'YES!' );
92 $text = $work->getParserOutput()->getText();
93 $this->assertContains( 'YES!', $text );
96 public function provideMagicWords() {
98 'Test {{PAGEID}} Test',
99 function ( RevisionRecord
$rev ) {
100 return $rev->getPageId();
103 yield
'REVISIONID' => [
104 'Test {{REVISIONID}} Test',
105 function ( RevisionRecord
$rev ) {
106 return $rev->getId();
109 yield
'REVISIONUSER' => [
110 'Test {{REVISIONUSER}} Test',
111 function ( RevisionRecord
$rev ) {
112 return $rev->getUser()->getName();
115 yield
'REVISIONTIMESTAMP' => [
116 'Test {{REVISIONTIMESTAMP}} Test',
117 function ( RevisionRecord
$rev ) {
118 return $rev->getTimestamp();
124 * @dataProvider provideMagicWords
126 public function testMagicWords( $wikitext, $callback ) {
127 $options = ParserOptions
::newCanonical( 'canonical' );
128 $page = $this->getExistingTestPage( __METHOD__
);
129 $rev = $page->getRevision()->getRevisionRecord();
131 // NOTE: provide the input as a string and let the PoolWorkArticleView create a fake
132 // revision internally, to see if the magic words work with that fake. They should
133 // work if the Parser causes the actual revision to be loaded when needed.
134 $work = new PoolWorkArticleView( $page, $options, $page->getLatest(), false, $wikitext );
137 $expected = strval( $callback( $rev ) );
138 $output = $work->getParserOutput();
140 $this->assertContains( $expected, $output->getText() );
143 public function testDoWorkMissingPage() {
144 $options = ParserOptions
::newCanonical( 'canonical' );
145 $page = $this->getNonexistingTestPage();
147 $work = new PoolWorkArticleView( $page, $options, '667788', false );
148 $this->assertFalse( $work->execute() );
151 public function testDoWorkDeletedContent() {
152 $options = ParserOptions
::newCanonical( 'canonical' );
153 $page = $this->getExistingTestPage( __METHOD__
);
154 $rev1 = $page->getRevision()->getRevisionRecord();
156 // make another revision, since the latest revision cannot be deleted.
157 $rev2 = $this->makeRevision( $page, 'Next' );
159 // make a fake revision with deleted different content
160 $fakeRev = new MutableRevisionRecord( $page->getTitle() );
161 $fakeRev->setId( $rev1->getId() );
162 $fakeRev->setPageId( $page->getId() );
163 $fakeRev->setContent( SlotRecord
::MAIN
, new WikitextContent( 'SECRET' ) );
164 $fakeRev->setVisibility( RevisionRecord
::DELETED_TEXT
);
166 $work = new PoolWorkArticleView( $page, $options, $rev1->getId(), false, $fakeRev );
167 $this->assertFalse( $work->execute() );
169 $work = new PoolWorkArticleView( $page, $options, $rev1->getId(), false, $fakeRev,
170 RevisionRecord
::RAW
);
171 $this->assertNotFalse( $work->execute() );
173 // a deleted current revision should still be show
174 $fakeRev->setId( $rev2->getId() );
175 $work = new PoolWorkArticleView( $page, $options, $rev2->getId(), false, $fakeRev );
176 $this->assertNotFalse( $work->execute() );