MediaWiki  1.28.0
fetchTextTest.php
Go to the documentation of this file.
1 <?php
2 
3 require_once __DIR__ . "/../../../maintenance/fetchText.php";
4 
12 
16  private $mockStdinText = null;
17 
21  private $mockSetUp = false;
22 
26  private $mockInvocations = [ 'getStdin' => 0 ];
27 
33  function mockStdin( $stdin ) {
34  $this->mockStdinText = $stdin;
35  $this->mockSetUp = true;
36  }
37 
44  function mockGetInvocations() {
46  }
47 
48  // -----------------------------------------------------------------
49  // Mocked functions from FetchText follow.
50 
51  function getStdin( $len = null ) {
52  $this->mockInvocations['getStdin']++;
53  if ( $len !== null ) {
54  throw new PHPUnit_Framework_ExpectationFailedException(
55  "Tried to get stdin with non null parameter" );
56  }
57 
58  if ( !$this->mockSetUp ) {
59  throw new PHPUnit_Framework_ExpectationFailedException(
60  "Tried to get stdin before setting up rerouting" );
61  }
62 
63  return fopen( 'data://text/plain,' . $this->mockStdinText, 'r' );
64  }
65 }
66 
75 
76  // We add 5 Revisions for this test. Their corresponding text id's
77  // are stored in the following 5 variables.
78  private $textId1;
79  private $textId2;
80  private $textId3;
81  private $textId4;
82  private $textId5;
83 
92 
96  private $fetchText;
97 
107  private function addRevision( $page, $text, $summary ) {
108  $status = $page->doEditContent(
109  ContentHandler::makeContent( $text, $page->getTitle() ),
110  $summary
111  );
112 
113  if ( $status->isGood() ) {
114  $value = $status->getValue();
115  $revision = $value['revision'];
116  $id = $revision->getTextId();
117 
118  if ( $id > 0 ) {
119  return $id;
120  }
121  }
122 
123  throw new MWException( "Could not determine text id" );
124  }
125 
126  function addDBData() {
127  $this->tablesUsed[] = 'page';
128  $this->tablesUsed[] = 'revision';
129  $this->tablesUsed[] = 'text';
130 
131  $wikitextNamespace = $this->getDefaultWikitextNS();
132 
133  try {
134  $title = Title::newFromText( 'FetchTextTestPage1', $wikitextNamespace );
136  $this->textId1 = $this->addRevision(
137  $page,
138  "FetchTextTestPage1Text1",
139  "FetchTextTestPage1Summary1"
140  );
141 
142  $title = Title::newFromText( 'FetchTextTestPage2', $wikitextNamespace );
144  $this->textId2 = $this->addRevision(
145  $page,
146  "FetchTextTestPage2Text1",
147  "FetchTextTestPage2Summary1"
148  );
149  $this->textId3 = $this->addRevision(
150  $page,
151  "FetchTextTestPage2Text2",
152  "FetchTextTestPage2Summary2"
153  );
154  $this->textId4 = $this->addRevision(
155  $page,
156  "FetchTextTestPage2Text3",
157  "FetchTextTestPage2Summary3"
158  );
159  $this->textId5 = $this->addRevision(
160  $page,
161  "FetchTextTestPage2Text4 some additional Text ",
162  "FetchTextTestPage2Summary4 extra "
163  );
164  } catch ( Exception $e ) {
165  // We'd love to pass $e directly. However, ... see
166  // documentation of exceptionFromAddDBData
167  $this->exceptionFromAddDBData = $e;
168  }
169  }
170 
171  protected function setUp() {
172  parent::setUp();
173 
174  // Check if any Exception is stored for rethrowing from addDBData
175  if ( $this->exceptionFromAddDBData !== null ) {
177  }
178 
179  $this->fetchText = new SemiMockedFetchText();
180  }
181 
187  private function assertFilter( $input, $expectedOutput ) {
188  $this->fetchText->mockStdin( $input );
189  $this->fetchText->execute();
190  $invocations = $this->fetchText->mockGetInvocations();
191  $this->assertEquals( 1, $invocations['getStdin'],
192  "getStdin invocation counter" );
193  $this->expectOutputString( $expectedOutput );
194  }
195 
196  // Instead of the following functions, a data provider would be great.
197  // However, as data providers are evaluated /before/ addDBData, a data
198  // provider would not know the required ids.
199 
200  function testExistingSimple() {
201  $this->assertFilter( $this->textId2,
202  $this->textId2 . "\n23\nFetchTextTestPage2Text1" );
203  }
204 
206  $this->assertFilter( $this->textId2 . "\n",
207  $this->textId2 . "\n23\nFetchTextTestPage2Text1" );
208  }
209 
210  function testExistingSeveral() {
211  $this->assertFilter( "$this->textId1\n$this->textId5\n"
212  . "$this->textId3\n$this->textId3",
213  implode( "", [
214  $this->textId1 . "\n23\nFetchTextTestPage1Text1",
215  $this->textId5 . "\n44\nFetchTextTestPage2Text4 "
216  . "some additional Text",
217  $this->textId3 . "\n23\nFetchTextTestPage2Text2",
218  $this->textId3 . "\n23\nFetchTextTestPage2Text2"
219  ] ) );
220  }
221 
222  function testEmpty() {
223  $this->assertFilter( "", null );
224  }
225 
226  function testNonExisting() {
227  $this->assertFilter( $this->textId5 + 10, ( $this->textId5 + 10 ) . "\n-1\n" );
228  }
229 
230  function testNegativeInteger() {
231  $this->assertFilter( "-42", "-42\n-1\n" );
232  }
233 
235  // float -> int -> revision
236  $this->assertFilter( $this->textId3 + 0.14159,
237  $this->textId3 . "\n23\nFetchTextTestPage2Text2" );
238  }
239 
241  $this->assertFilter( $this->textId5 + 3.14159,
242  ( $this->textId5 + 3 ) . "\n-1\n" );
243  }
244 
245  function testCharacters() {
246  $this->assertFilter( "abc", "0\n-1\n" );
247  }
248 
249  function testMix() {
250  $this->assertFilter( "ab\n" . $this->textId4 . ".5cd\n\nefg\n" . $this->textId2
251  . "\n" . $this->textId3,
252  implode( "", [
253  "0\n-1\n",
254  $this->textId4 . "\n23\nFetchTextTestPage2Text3",
255  "0\n-1\n",
256  "0\n-1\n",
257  $this->textId2 . "\n23\nFetchTextTestPage2Text1",
258  $this->textId3 . "\n23\nFetchTextTestPage2Text2"
259  ] ) );
260  }
261 }
static factory(Title $title)
Create a WikiPage object of the appropriate class for the given title.
Definition: WikiPage.php:115
FetchText $fetchText
The (mocked) FetchText that is to test.
div flags Integer display flags(NO_ACTION_LINK, NO_EXTRA_USER_LINKS) 'LogException'returning false will NOT prevent logging $e
Definition: hooks.txt:2102
Maintenance script used to fetch page text in a subprocess.
Definition: fetchText.php:32
$value
static newFromText($text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:262
testFloatingPointNumberExisting()
array $mockInvocations
Invocation counters for the mocked aspects.
getDefaultWikitextNS()
Returns the ID of a namespace that defaults to Wikitext.
mockStdin($stdin)
Data for the fake stdin.
$summary
Mock for the input/output of FetchText.
testFloatingPointNumberNonExisting()
bool $mockSetUp
Whether or not a text for stdin has been provided.
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:953
static makeContent($text, Title $title=null, $modelId=null, $format=null)
Convenience function for creating a Content object from a given textual representation.
addRevision($page, $text, $summary)
Adds a revision to a page, while returning the resuting text's id.
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
assertFilter($input, $expectedOutput)
Helper to relate FetchText's input and output.
mockGetInvocations()
Gets invocation counters for mocked methods.
TestCase for FetchText.
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist e g Watchlist removed from all revisions and log entries to which it was applied This gives extensions a chance to take it off their books as the deletion has already been partly carried out by this point or something similar the user will be unable to create the tag set $status
Definition: hooks.txt:1046
testExistingSimpleWithNewline()
string null $mockStdinText
Text to pass as stdin.
Exception null $exceptionFromAddDBData
As the current MediaWikiTestCase::run is not robust enough to recover from thrown exceptions directly...
do that in ParserLimitReportFormat instead use this to modify the parameters of the image and a DIV can begin in one section and end in another Make sure your code can handle that case gracefully See the EditSectionClearerLink extension for an example zero but section is usually empty its values are the globals values before the output is cached $page
Definition: hooks.txt:2491