MediaWiki  1.33.1
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  }
121 
125  public function testMagicWords( $wikitext, $callback ) {
126  $options = ParserOptions::newCanonical( 'canonical' );
127  $page = $this->getExistingTestPage( __METHOD__ );
128  $rev = $page->getRevision()->getRevisionRecord();
129 
130  // NOTE: provide the input as a string and let the PoolWorkArticleView create a fake
131  // revision internally, to see if the magic words work with that fake. They should
132  // work if the Parser causes the actual revision to be loaded when needed.
133  $work = new PoolWorkArticleView( $page, $options, $page->getLatest(), false, $wikitext );
134  $work->execute();
135 
136  $expected = strval( $callback( $rev ) );
137  $output = $work->getParserOutput();
138 
139  $this->assertContains( $expected, $output->getText() );
140  }
141 
142  public function testDoWorkMissingPage() {
143  $options = ParserOptions::newCanonical( 'canonical' );
144  $page = $this->getNonexistingTestPage();
145 
146  $work = new PoolWorkArticleView( $page, $options, '667788', false );
147  $this->assertFalse( $work->execute() );
148  }
149 
150  public function testDoWorkDeletedContent() {
151  $options = ParserOptions::newCanonical( 'canonical' );
152  $page = $this->getExistingTestPage( __METHOD__ );
153  $rev1 = $page->getRevision()->getRevisionRecord();
154 
155  // make another revision, since the latest revision cannot be deleted.
156  $rev2 = $this->makeRevision( $page, 'Next' );
157 
158  // make a fake revision with deleted different content
159  $fakeRev = new MutableRevisionRecord( $page->getTitle() );
160  $fakeRev->setId( $rev1->getId() );
161  $fakeRev->setPageId( $page->getId() );
162  $fakeRev->setContent( SlotRecord::MAIN, new WikitextContent( 'SECRET' ) );
163  $fakeRev->setVisibility( RevisionRecord::DELETED_TEXT );
164 
165  $work = new PoolWorkArticleView( $page, $options, $rev1->getId(), false, $fakeRev );
166  $this->assertFalse( $work->execute() );
167 
168  $work = new PoolWorkArticleView( $page, $options, $rev1->getId(), false, $fakeRev,
169  RevisionRecord::RAW );
170  $this->assertNotFalse( $work->execute() );
171 
172  // a deleted current revision should still be show
173  $fakeRev->setId( $rev2->getId() );
174  $work = new PoolWorkArticleView( $page, $options, $rev2->getId(), false, $fakeRev );
175  $this->assertNotFalse( $work->execute() );
176  }
177 
178 }
CommentStoreComment\newUnsavedComment
static newUnsavedComment( $comment, array $data=null)
Create a new, unsaved CommentStoreComment.
Definition: CommentStoreComment.php:66
$user
return true to allow those checks to and false if checking is done & $user
Definition: hooks.txt:1476
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:180
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:1789
PoolWorkArticleViewTest\testMagicWords
testMagicWords( $wikitext, $callback)
provideMagicWords
Definition: PoolWorkArticleViewTest.php:125
$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 When $user is not it can be in the form of< username >< more info > e g for bot passwords intended to be added to log contexts Fields it might only if the login was with a bot password 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:780
WikiPage
Class representing a MediaWiki article and history.
Definition: WikiPage.php:45
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:256
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:17
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:36
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:1064
MediaWikiTestCase\getExistingTestPage
getExistingTestPage( $title=null)
Returns a WikiPage representing an existing page.
Definition: MediaWikiTestCase.php:220
PoolWorkArticleViewTest\testDoWorkDeletedContent
testDoWorkDeletedContent()
Definition: PoolWorkArticleViewTest.php:150
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:1993
$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:1769
PoolWorkArticleViewTest\testDoWorkMissingPage
testDoWorkMissingPage()
Definition: PoolWorkArticleViewTest.php:142
$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:1993
$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