MediaWiki REL1_33
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 }
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}
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
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:45
newPageUpdater(User $user, RevisionSlotsUpdate $forUpdate=null)
Returns a PageUpdater for creating new revisions on this page (or creating the page).
Content object for wiki text pages.
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:855
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:1999
return true to allow those checks to and false if checking is done & $user
Definition hooks.txt:1510
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:2272
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:1779
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:37
$cache
Definition mcc.php:33
$content
$page->newPageUpdater($user) $updater