MediaWiki  1.23.2
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 = array( '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 
74 class FetchTextTest extends MediaWikiTestCase {
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( ContentHandler::makeContent( $text, $page->getTitle() ), $summary );
109  if ( $status->isGood() ) {
110  $value = $status->getValue();
111  $revision = $value['revision'];
112  $id = $revision->getTextId();
113  if ( $id > 0 ) {
114  return $id;
115  }
116  }
117  throw new MWException( "Could not determine text id" );
118  }
119 
120  function addDBData() {
121  $this->tablesUsed[] = 'page';
122  $this->tablesUsed[] = 'revision';
123  $this->tablesUsed[] = 'text';
124 
125  $wikitextNamespace = $this->getDefaultWikitextNS();
126 
127  try {
128  $title = Title::newFromText( 'FetchTextTestPage1', $wikitextNamespace );
129  $page = WikiPage::factory( $title );
130  $this->textId1 = $this->addRevision( $page, "FetchTextTestPage1Text1", "FetchTextTestPage1Summary1" );
131 
132  $title = Title::newFromText( 'FetchTextTestPage2', $wikitextNamespace );
133  $page = WikiPage::factory( $title );
134  $this->textId2 = $this->addRevision( $page, "FetchTextTestPage2Text1", "FetchTextTestPage2Summary1" );
135  $this->textId3 = $this->addRevision( $page, "FetchTextTestPage2Text2", "FetchTextTestPage2Summary2" );
136  $this->textId4 = $this->addRevision( $page, "FetchTextTestPage2Text3", "FetchTextTestPage2Summary3" );
137  $this->textId5 = $this->addRevision( $page, "FetchTextTestPage2Text4 some additional Text ", "FetchTextTestPage2Summary4 extra " );
138  } catch ( Exception $e ) {
139  // We'd love to pass $e directly. However, ... see
140  // documentation of exceptionFromAddDBData
141  $this->exceptionFromAddDBData = $e;
142  }
143  }
144 
145  protected function setUp() {
146  parent::setUp();
147 
148  // Check if any Exception is stored for rethrowing from addDBData
149  if ( $this->exceptionFromAddDBData !== null ) {
151  }
152 
153  $this->fetchText = new SemiMockedFetchText();
154  }
155 
159  private function assertFilter( $input, $expectedOutput ) {
160  $this->fetchText->mockStdin( $input );
161  $this->fetchText->execute();
162  $invocations = $this->fetchText->mockGetInvocations();
163  $this->assertEquals( 1, $invocations['getStdin'],
164  "getStdin invocation counter" );
165  $this->expectOutputString( $expectedOutput );
166  }
167 
168  // Instead of the following functions, a data provider would be great.
169  // However, as data providers are evaluated /before/ addDBData, a data
170  // provider would not know the required ids.
171 
172  function testExistingSimple() {
173  $this->assertFilter( $this->textId2,
174  $this->textId2 . "\n23\nFetchTextTestPage2Text1" );
175  }
176 
178  $this->assertFilter( $this->textId2 . "\n",
179  $this->textId2 . "\n23\nFetchTextTestPage2Text1" );
180  }
181 
182  function testExistingSeveral() {
183  $this->assertFilter( "$this->textId1\n$this->textId5\n"
184  . "$this->textId3\n$this->textId3",
185  implode( "", array(
186  $this->textId1 . "\n23\nFetchTextTestPage1Text1",
187  $this->textId5 . "\n44\nFetchTextTestPage2Text4 "
188  . "some additional Text",
189  $this->textId3 . "\n23\nFetchTextTestPage2Text2",
190  $this->textId3 . "\n23\nFetchTextTestPage2Text2"
191  ) ) );
192  }
193 
194  function testEmpty() {
195  $this->assertFilter( "", null );
196  }
197 
198  function testNonExisting() {
199  $this->assertFilter( $this->textId5 + 10, ( $this->textId5 + 10 ) . "\n-1\n" );
200  }
201 
202  function testNegativeInteger() {
203  $this->assertFilter( "-42", "-42\n-1\n" );
204  }
205 
207  // float -> int -> revision
208  $this->assertFilter( $this->textId3 + 0.14159,
209  $this->textId3 . "\n23\nFetchTextTestPage2Text2" );
210  }
211 
213  $this->assertFilter( $this->textId5 + 3.14159,
214  ( $this->textId5 + 3 ) . "\n-1\n" );
215  }
216 
217  function testCharacters() {
218  $this->assertFilter( "abc", "0\n-1\n" );
219  }
220 
221  function testMix() {
222  $this->assertFilter( "ab\n" . $this->textId4 . ".5cd\n\nefg\n" . $this->textId2
223  . "\n" . $this->textId3,
224  implode( "", array(
225  "0\n-1\n",
226  $this->textId4 . "\n23\nFetchTextTestPage2Text3",
227  "0\n-1\n",
228  "0\n-1\n",
229  $this->textId2 . "\n23\nFetchTextTestPage2Text1",
230  $this->textId3 . "\n23\nFetchTextTestPage2Text2"
231  ) ) );
232  }
233 }
FetchTextTest\setUp
setUp()
Definition: fetchTextTest.php:140
FetchText
Maintenance script used to fetch page text in a subprocess.
Definition: fetchText.php:32
Title\newFromText
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:189
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
FetchTextTest\assertFilter
assertFilter( $input, $expectedOutput)
Helper to relate FetchText's input and output.
Definition: fetchTextTest.php:154
FetchTextTest\$textId1
$textId1
Definition: fetchTextTest.php:75
FetchTextTest\$textId3
$textId3
Definition: fetchTextTest.php:77
SemiMockedFetchText\mockStdin
mockStdin( $stdin)
Data for the fake stdin.
Definition: fetchTextTest.php:30
FetchTextTest\testFloatingPointNumberNonExisting
testFloatingPointNumberNonExisting()
Definition: fetchTextTest.php:207
FetchTextTest\testExistingSeveral
testExistingSeveral()
Definition: fetchTextTest.php:177
FetchTextTest\testNegativeInteger
testNegativeInteger()
Definition: fetchTextTest.php:197
FetchTextTest\$exceptionFromAddDBData
Exception null $exceptionFromAddDBData
As the current MediaWikiTestCase::run is not robust enough to recover from thrown exceptions directly...
Definition: fetchTextTest.php:87
SemiMockedFetchText\mockGetInvocations
mockGetInvocations()
Gets invocation counters for mocked methods.
Definition: fetchTextTest.php:41
FetchTextTest\testExistingSimpleWithNewline
testExistingSimpleWithNewline()
Definition: fetchTextTest.php:172
FetchTextTest\testExistingSimple
testExistingSimple()
Definition: fetchTextTest.php:167
MWException
MediaWiki exception.
Definition: MWException.php:26
WikiPage\factory
static factory(Title $title)
Create a WikiPage object of the appropriate class for the given title.
Definition: WikiPage.php:103
FetchTextTest\$textId5
$textId5
Definition: fetchTextTest.php:79
MediaWikiTestCase
Definition: MediaWikiTestCase.php:6
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
MediaWikiTestCase\getDefaultWikitextNS
getDefaultWikitextNS()
Returns the ID of a namespace that defaults to Wikitext.
Definition: MediaWikiTestCase.php:903
FetchTextTest\testCharacters
testCharacters()
Definition: fetchTextTest.php:212
ContentHandler\makeContent
static makeContent( $text, Title $title=null, $modelId=null, $format=null)
Convenience function for creating a Content object from a given textual representation.
Definition: ContentHandler.php:144
$title
presenting them properly to the user as errors is done by the caller $title
Definition: hooks.txt:1324
$value
$value
Definition: styleTest.css.php:45
FetchTextTest\addRevision
addRevision( $page, $text, $summary)
Adds a revision to a page, while returning the resuting text's id.
Definition: fetchTextTest.php:102
FetchTextTest
TestCase for FetchText.
Definition: fetchTextTest.php:71
SemiMockedFetchText
Mock for the input/output of FetchText.
Definition: fetchTextTest.php:11
SemiMockedFetchText\$mockInvocations
Array $mockInvocations
Invocation counters for the mocked aspects.
Definition: fetchTextTest.php:23
$summary
$summary
Definition: importImages.php:120
SemiMockedFetchText\$mockStdinText
String null $mockStdinText
Text to pass as stdin.
Definition: fetchTextTest.php:15
FetchTextTest\testEmpty
testEmpty()
Definition: fetchTextTest.php:189
FetchTextTest\addDBData
addDBData()
Stub.
Definition: fetchTextTest.php:115
FetchTextTest\testFloatingPointNumberExisting
testFloatingPointNumberExisting()
Definition: fetchTextTest.php:201
FetchTextTest\$textId4
$textId4
Definition: fetchTextTest.php:78
FetchTextTest\testMix
testMix()
Definition: fetchTextTest.php:216
SemiMockedFetchText\getStdin
getStdin( $len=null)
Return input from stdin.
Definition: fetchTextTest.php:48
FetchTextTest\$fetchText
FetchText $fetchText
the (mocked) FetchText that is to test
Definition: fetchTextTest.php:91
$e
if( $useReadline) $e
Definition: eval.php:66
FetchTextTest\$textId2
$textId2
Definition: fetchTextTest.php:76
FetchTextTest\testNonExisting
testNonExisting()
Definition: fetchTextTest.php:193
SemiMockedFetchText\$mockSetUp
bool $mockSetUp
Whether or not a text for stdin has been provided.
Definition: fetchTextTest.php:19