MediaWiki  1.32.5
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 
52  public function testDoWorkWithExplicitRevision() {
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 }
CommentStoreComment\newUnsavedComment
static newUnsavedComment( $comment, array $data=null)
Create a new, unsaved CommentStoreComment.
Definition: CommentStoreComment.php:66
$user
please add to it if you re going to add events to the MediaWiki code where normally authentication against an external auth plugin would be creating a account $user
Definition: hooks.txt:244
Revision\RevisionRecord
Page revision base class.
Definition: RevisionRecord.php:45
false
processing should stop and the error should be shown to the user * false
Definition: hooks.txt:187
MediaWikiTestCase\getTestUser
static getTestUser( $groups=[])
Convenience method for getting an immutable test user.
Definition: MediaWikiTestCase.php:179
PoolWorkArticleViewTest\testDoWorkWithExplicitRevision
testDoWorkWithExplicitRevision()
Definition: PoolWorkArticleViewTest.php:52
WikiPage\newPageUpdater
newPageUpdater(User $user, RevisionSlotsUpdate $forUpdate=null)
Returns a PageUpdater for creating new revisions on this page (or creating the page).
Definition: WikiPage.php:1762
PoolWorkArticleViewTest\testMagicWords
testMagicWords( $wikitext, $callback)
provideMagicWords
Definition: PoolWorkArticleViewTest.php:124
WikiPage
Class representing a MediaWiki article and history.
Definition: WikiPage.php:44
PoolWorkArticleView
Definition: PoolWorkArticleView.php:28
Revision\MutableRevisionRecord\setId
setId( $id)
Set the revision ID.
Definition: MutableRevisionRecord.php:257
PoolWorkArticleViewTest
PoolWorkArticleView Database.
Definition: PoolWorkArticleViewTest.php:11
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
MediaWikiTestCase\getNonexistingTestPage
getNonexistingTestPage( $title=null)
Returns a WikiPage representing a non-existing page.
Definition: MediaWikiTestCase.php:255
PoolWorkArticleViewTest\testDoWorkLoadRevision
testDoWorkLoadRevision()
Definition: PoolWorkArticleViewTest.php:21
PoolCounterWork\execute
execute( $skipcache=false)
Get the result of the work (whatever it is), or the result of the error() function.
Definition: PoolCounterWork.php:104
MediaWikiTestCase
Definition: MediaWikiTestCase.php:16
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
$output
$output
Definition: SyntaxHighlight.php:334
WikitextContent
Content object for wiki text pages.
Definition: WikitextContent.php:35
PoolWorkArticleViewTest\testDoWorkWithString
testDoWorkWithString()
Definition: PoolWorkArticleViewTest.php:84
PoolWorkArticleViewTest\makeRevision
makeRevision(WikiPage $page, $text)
Definition: PoolWorkArticleViewTest.php:13
Revision\MutableRevisionRecord
Mutable RevisionRecord implementation, for building new revision entries programmatically.
Definition: MutableRevisionRecord.php:41
PoolWorkArticleViewTest\provideMagicWords
provideMagicWords()
Definition: PoolWorkArticleViewTest.php:95
ParserOptions\newCanonical
static newCanonical( $context=null, $userLang=null)
Creates a "canonical" ParserOptions object.
Definition: ParserOptions.php:1061
MediaWikiTestCase\getExistingTestPage
getExistingTestPage( $title=null)
Returns a WikiPage representing an existing page.
Definition: MediaWikiTestCase.php:219
PoolWorkArticleViewTest\testDoWorkDeletedContent
testDoWorkDeletedContent()
Definition: PoolWorkArticleViewTest.php:149
PoolWorkArticleViewTest\testDoWorkParserCache
testDoWorkParserCache()
Definition: PoolWorkArticleViewTest.php:36
PoolWorkArticleViewTest\testDoWorkWithContent
testDoWorkWithContent()
Definition: PoolWorkArticleViewTest.php:71
$cache
$cache
Definition: mcc.php:33
$options
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:2044
$rev
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:1808
PoolWorkArticleViewTest\testDoWorkMissingPage
testDoWorkMissingPage()
Definition: PoolWorkArticleViewTest.php:141
$updater
$page->newPageUpdater($user) $updater
Definition: pageupdater.txt:63
true
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 noclasses just before the function returns a value If you return true
Definition: hooks.txt:2044
$content
$content
Definition: pageupdater.txt:72
MediaWikiServices
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency MediaWikiServices
Definition: injection.txt:23
Revision\SlotRecord
Value object representing a content slot associated with a page revision.
Definition: SlotRecord.php:39
$out
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:813