MediaWiki REL1_32
PoolWorkArticleViewTest.php
Go to the documentation of this file.
1<?php
6
12
13 private function makeRevision( WikiPage $page, $text ) {
14 $user = $this->getTestUser()->getUser();
15 $updater = $page->newPageUpdater( $user );
16
17 $updater->setContent( SlotRecord::MAIN, new WikitextContent( $text ) );
18 return $updater->saveRevision( CommentStoreComment::newUnsavedComment( 'testing' ) );
19 }
20
21 public function testDoWorkLoadRevision() {
22 $options = ParserOptions::newCanonical( 'canonical' );
23 $page = $this->getExistingTestPage( __METHOD__ );
24 $rev1 = $this->makeRevision( $page, 'First!' );
25 $rev2 = $this->makeRevision( $page, 'Second!' );
26
27 $work = new PoolWorkArticleView( $page, $options, $rev1->getId(), false );
28 $work->execute();
29 $this->assertContains( 'First', $work->getParserOutput()->getText() );
30
31 $work = new PoolWorkArticleView( $page, $options, $rev2->getId(), false );
32 $work->execute();
33 $this->assertContains( 'Second', $work->getParserOutput()->getText() );
34 }
35
36 public function testDoWorkParserCache() {
37 $options = ParserOptions::newCanonical( 'canonical' );
38 $page = $this->getExistingTestPage( __METHOD__ );
39 $rev1 = $this->makeRevision( $page, 'First!' );
40
41 $work = new PoolWorkArticleView( $page, $options, $rev1->getId(), true );
42 $work->execute();
43
44 $cache = MediaWikiServices::getInstance()->getParserCache();
45 $out = $cache->get( $page, $options );
46
47 $this->assertNotNull( $out );
48 $this->assertNotFalse( $out );
49 $this->assertContains( 'First', $out->getText() );
50 }
51
53 $options = ParserOptions::newCanonical( 'canonical' );
54 $page = $this->getExistingTestPage( __METHOD__ );
55 $rev = $this->makeRevision( $page, 'NOPE' );
56
57 // make a fake revision with different content, so we know it's actually being used!
58 $fakeRev = new MutableRevisionRecord( $page->getTitle() );
59 $fakeRev->setId( $rev->getId() );
60 $fakeRev->setPageId( $page->getId() );
61 $fakeRev->setContent( SlotRecord::MAIN, new WikitextContent( 'YES!' ) );
62
63 $work = new PoolWorkArticleView( $page, $options, $rev->getId(), false, $fakeRev );
64 $work->execute();
65
66 $text = $work->getParserOutput()->getText();
67 $this->assertContains( 'YES!', $text );
68 $this->assertNotContains( 'NOPE', $text );
69 }
70
71 public function testDoWorkWithContent() {
72 $options = ParserOptions::newCanonical( 'canonical' );
73 $page = $this->getExistingTestPage( __METHOD__ );
74
75 $content = new WikitextContent( 'YES!' );
76
77 $work = new PoolWorkArticleView( $page, $options, $page->getLatest(), false, $content );
78 $work->execute();
79
80 $text = $work->getParserOutput()->getText();
81 $this->assertContains( 'YES!', $text );
82 }
83
84 public function testDoWorkWithString() {
85 $options = ParserOptions::newCanonical( 'canonical' );
86 $page = $this->getExistingTestPage( __METHOD__ );
87
88 $work = new PoolWorkArticleView( $page, $options, $page->getLatest(), false, 'YES!' );
89 $work->execute();
90
91 $text = $work->getParserOutput()->getText();
92 $this->assertContains( 'YES!', $text );
93 }
94
95 public function provideMagicWords() {
96 yield 'PAGEID' => [
97 'Test {{PAGEID}} Test',
98 function ( RevisionRecord $rev ) {
99 return $rev->getPageId();
100 }
101 ];
102 yield 'REVISIONID' => [
103 'Test {{REVISIONID}} Test',
104 function ( RevisionRecord $rev ) {
105 return $rev->getId();
106 }
107 ];
108 yield 'REVISIONUSER' => [
109 'Test {{REVISIONUSER}} Test',
110 function ( RevisionRecord $rev ) {
111 return $rev->getUser()->getName();
112 }
113 ];
114 yield 'REVISIONTIMESTAMP' => [
115 'Test {{REVISIONTIMESTAMP}} Test',
116 function ( RevisionRecord $rev ) {
117 return $rev->getTimestamp();
118 }
119 ];
120 }
124 public function testMagicWords( $wikitext, $callback ) {
125 $options = ParserOptions::newCanonical( 'canonical' );
126 $page = $this->getExistingTestPage( __METHOD__ );
127 $rev = $page->getRevision()->getRevisionRecord();
128
129 // NOTE: provide the input as a string and let the PoolWorkArticleView create a fake
130 // revision internally, to see if the magic words work with that fake. They should
131 // work if the Parser causes the actual revision to be loaded when needed.
132 $work = new PoolWorkArticleView( $page, $options, $page->getLatest(), false, $wikitext );
133 $work->execute();
134
135 $expected = strval( $callback( $rev ) );
136 $output = $work->getParserOutput();
137
138 $this->assertContains( $expected, $output->getText() );
139 }
140
141 public function testDoWorkMissingPage() {
142 $options = ParserOptions::newCanonical( 'canonical' );
143 $page = $this->getNonexistingTestPage();
144
145 $work = new PoolWorkArticleView( $page, $options, '667788', false );
146 $this->assertFalse( $work->execute() );
147 }
148
149 public function testDoWorkDeletedContent() {
150 $options = ParserOptions::newCanonical( 'canonical' );
151 $page = $this->getExistingTestPage( __METHOD__ );
152 $rev1 = $page->getRevision()->getRevisionRecord();
153
154 // make another revision, since the latest revision cannot be deleted.
155 $rev2 = $this->makeRevision( $page, 'Next' );
156
157 // make a fake revision with deleted different content
158 $fakeRev = new MutableRevisionRecord( $page->getTitle() );
159 $fakeRev->setId( $rev1->getId() );
160 $fakeRev->setPageId( $page->getId() );
161 $fakeRev->setContent( SlotRecord::MAIN, new WikitextContent( 'SECRET' ) );
162 $fakeRev->setVisibility( RevisionRecord::DELETED_TEXT );
163
164 $work = new PoolWorkArticleView( $page, $options, $rev1->getId(), false, $fakeRev );
165 $this->assertFalse( $work->execute() );
166
167 $work = new PoolWorkArticleView( $page, $options, $rev1->getId(), false, $fakeRev,
168 RevisionRecord::RAW );
169 $this->assertNotFalse( $work->execute() );
170
171 // a deleted current revision should still be show
172 $fakeRev->setId( $rev2->getId() );
173 $work = new PoolWorkArticleView( $page, $options, $rev2->getId(), false, $fakeRev );
174 $this->assertNotFalse( $work->execute() );
175 }
176
177}
getNonexistingTestPage( $title=null)
Returns a WikiPage representing a non-existing page.
getExistingTestPage( $title=null)
Returns a WikiPage representing an existing page.
static getTestUser( $groups=[])
Convenience method for getting an immutable test user.
MediaWikiServices is the service locator for the application scope of MediaWiki.
Mutable RevisionRecord implementation, for building new revision entries programmatically.
Page revision base class.
Value object representing a content slot associated with a page revision.
PoolWorkArticleView Database.
makeRevision(WikiPage $page, $text)
testMagicWords( $wikitext, $callback)
provideMagicWords
Class representing a MediaWiki article and history.
Definition WikiPage.php:44
Content object for wiki text pages.
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped & $options
Definition hooks.txt:2050
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that probably a stub it is not rendered in wiki pages or galleries in category pages allow injecting custom HTML after the section Any uses of the hook need to handle escaping see BaseTemplate::getToolbox and BaseTemplate::makeListItem for details on the format of individual items inside of this array or by returning and letting standard HTTP rendering take place modifiable or by returning false and taking over the output $out
Definition hooks.txt:894
static configuration should be added through ResourceLoaderGetConfigVars instead can be used to get the real title e g db for database replication lag or jobqueue for job queue size converted to pseudo seconds It is possible to add more fields and they will be returned to the user in the API response after the basic globals have been set but before ordinary actions take place $output
Definition hooks.txt:2317
presenting them properly to the user as errors is done by the caller return true use this to change the list i e etc $rev
Definition hooks.txt:1818
$cache
Definition mcc.php:33
$page->newPageUpdater($user) $updater
$content