MediaWiki  master
PoolWorkArticleViewTest.php
Go to the documentation of this file.
1 <?php
2 
7 
13 
14  private function makeRevision( WikiPage $page, $text ) {
15  $user = $this->getTestUser()->getUser();
16  $updater = $page->newPageUpdater( $user );
17 
18  $updater->setContent( SlotRecord::MAIN, new WikitextContent( $text ) );
19  return $updater->saveRevision( CommentStoreComment::newUnsavedComment( 'testing' ) );
20  }
21 
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!' );
27 
28  $work = new PoolWorkArticleView( $page, $options, $rev1->getId(), false );
29  $work->execute();
30  $this->assertContains( 'First', $work->getParserOutput()->getText() );
31 
32  $work = new PoolWorkArticleView( $page, $options, $rev2->getId(), false );
33  $work->execute();
34  $this->assertContains( 'Second', $work->getParserOutput()->getText() );
35  }
36 
37  public function testDoWorkParserCache() {
38  $options = ParserOptions::newCanonical( 'canonical' );
39  $page = $this->getExistingTestPage( __METHOD__ );
40  $rev1 = $this->makeRevision( $page, 'First!' );
41 
42  $work = new PoolWorkArticleView( $page, $options, $rev1->getId(), true );
43  $work->execute();
44 
45  $cache = MediaWikiServices::getInstance()->getParserCache();
46  $out = $cache->get( $page, $options );
47 
48  $this->assertNotNull( $out );
49  $this->assertNotFalse( $out );
50  $this->assertContains( 'First', $out->getText() );
51  }
52 
53  public function testDoWorkWithExplicitRevision() {
54  $options = ParserOptions::newCanonical( 'canonical' );
55  $page = $this->getExistingTestPage( __METHOD__ );
56  $rev = $this->makeRevision( $page, 'NOPE' );
57 
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!' ) );
63 
64  $work = new PoolWorkArticleView( $page, $options, $rev->getId(), false, $fakeRev );
65  $work->execute();
66 
67  $text = $work->getParserOutput()->getText();
68  $this->assertContains( 'YES!', $text );
69  $this->assertNotContains( 'NOPE', $text );
70  }
71 
72  public function testDoWorkWithContent() {
73  $options = ParserOptions::newCanonical( 'canonical' );
74  $page = $this->getExistingTestPage( __METHOD__ );
75 
76  $content = new WikitextContent( 'YES!' );
77 
78  $work = new PoolWorkArticleView( $page, $options, $page->getLatest(), false, $content );
79  $work->execute();
80 
81  $text = $work->getParserOutput()->getText();
82  $this->assertContains( 'YES!', $text );
83  }
84 
85  public function testDoWorkWithString() {
86  $options = ParserOptions::newCanonical( 'canonical' );
87  $page = $this->getExistingTestPage( __METHOD__ );
88 
89  $work = new PoolWorkArticleView( $page, $options, $page->getLatest(), false, 'YES!' );
90  $work->execute();
91 
92  $text = $work->getParserOutput()->getText();
93  $this->assertContains( 'YES!', $text );
94  }
95 
96  public function provideMagicWords() {
97  yield 'PAGEID' => [
98  'Test {{PAGEID}} Test',
99  function ( RevisionRecord $rev ) {
100  return $rev->getPageId();
101  }
102  ];
103  yield 'REVISIONID' => [
104  'Test {{REVISIONID}} Test',
105  function ( RevisionRecord $rev ) {
106  return $rev->getId();
107  }
108  ];
109  yield 'REVISIONUSER' => [
110  'Test {{REVISIONUSER}} Test',
111  function ( RevisionRecord $rev ) {
112  return $rev->getUser()->getName();
113  }
114  ];
115  yield 'REVISIONTIMESTAMP' => [
116  'Test {{REVISIONTIMESTAMP}} Test',
117  function ( RevisionRecord $rev ) {
118  return $rev->getTimestamp();
119  }
120  ];
121  }
122 
126  public function testMagicWords( $wikitext, $callback ) {
127  $options = ParserOptions::newCanonical( 'canonical' );
128  $page = $this->getExistingTestPage( __METHOD__ );
129  $rev = $page->getRevision()->getRevisionRecord();
130 
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 );
135  $work->execute();
136 
137  $expected = strval( $callback( $rev ) );
138  $output = $work->getParserOutput();
139 
140  $this->assertContains( $expected, $output->getText() );
141  }
142 
143  public function testDoWorkMissingPage() {
144  $options = ParserOptions::newCanonical( 'canonical' );
145  $page = $this->getNonexistingTestPage();
146 
147  $work = new PoolWorkArticleView( $page, $options, '667788', false );
148  $this->assertFalse( $work->execute() );
149  }
150 
151  public function testDoWorkDeletedContent() {
152  $options = ParserOptions::newCanonical( 'canonical' );
153  $page = $this->getExistingTestPage( __METHOD__ );
154  $rev1 = $page->getRevision()->getRevisionRecord();
155 
156  // make another revision, since the latest revision cannot be deleted.
157  $rev2 = $this->makeRevision( $page, 'Next' );
158 
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 );
165 
166  $work = new PoolWorkArticleView( $page, $options, $rev1->getId(), false, $fakeRev );
167  $this->assertFalse( $work->execute() );
168 
169  $work = new PoolWorkArticleView( $page, $options, $rev1->getId(), false, $fakeRev,
170  RevisionRecord::RAW );
171  $this->assertNotFalse( $work->execute() );
172 
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() );
177  }
178 
179 }
processing should stop and the error should be shown to the user * false
Definition: hooks.txt:187
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
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
execute( $skipcache=false)
Get the result of the work (whatever it is), or the result of the error() function.
static newUnsavedComment( $comment, array $data=null)
Create a new, unsaved CommentStoreComment.
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:2205
Mutable RevisionRecord implementation, for building new revision entries programmatically.
static newCanonical( $context=null, $userLang=null)
Creates a "canonical" ParserOptions object.
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:767
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:1972
newPageUpdater(User $user, RevisionSlotsUpdate $forUpdate=null)
Returns a PageUpdater for creating new revisions on this page (or creating the page).
Definition: WikiPage.php:1797
$cache
Definition: mcc.php:33
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:1972
makeRevision(WikiPage $page, $text)
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:1748
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
setId( $id)
Set the revision ID.
$page->newPageUpdater($user) $updater
Definition: pageupdater.txt:63
PoolWorkArticleView Database.
Page revision base class.
$content
Definition: pageupdater.txt:72
return true to allow those checks to and false if checking is done & $user
Definition: hooks.txt:1454
testMagicWords( $wikitext, $callback)
provideMagicWords