3 require_once __DIR__
. "/../../../maintenance/fetchText.php";
6 * Mock for the input/output of FetchText
8 * FetchText internally tries to access stdin and stdout. We mock those aspects
11 class SemiMockedFetchText
extends FetchText
{
14 * @var string|null Text to pass as stdin
16 private $mockStdinText = null;
19 * @var bool Whether or not a text for stdin has been provided
21 private $mockSetUp = false;
24 * @var array Invocation counters for the mocked aspects
26 private $mockInvocations = array( 'getStdin' => 0 );
29 * Data for the fake stdin
31 * @param string $stdin The string to be used instead of stdin
33 function mockStdin( $stdin ) {
34 $this->mockStdinText
= $stdin;
35 $this->mockSetUp
= true;
39 * Gets invocation counters for mocked methods.
41 * @return array An array, whose keys are function names. The corresponding values
42 * denote the number of times the function has been invoked.
44 function mockGetInvocations() {
45 return $this->mockInvocations
;
48 // -----------------------------------------------------------------
49 // Mocked functions from FetchText follow.
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" );
58 if ( !$this->mockSetUp
) {
59 throw new PHPUnit_Framework_ExpectationFailedException(
60 "Tried to get stdin before setting up rerouting" );
63 return fopen( 'data://text/plain,' . $this->mockStdinText
, 'r' );
68 * TestCase for FetchText
74 class FetchTextTest
extends MediaWikiTestCase
{
76 // We add 5 Revisions for this test. Their corresponding text id's
77 // are stored in the following 5 variables.
85 * @var Exception|null As the current MediaWikiTestCase::run is not
86 * robust enough to recover from thrown exceptions directly, we cannot
87 * throw frow within addDBData, although it would be appropriate. Hence,
88 * we catch the exception and store it until we are in setUp and may
89 * finally rethrow the exception without crashing the test suite.
91 private $exceptionFromAddDBData;
94 * @var FetchText the (mocked) FetchText that is to test
99 * Adds a revision to a page, while returning the resuting text's id
101 * @param WikiPage $page The page to add the revision to
102 * @param string $text The revisions text
103 * @param string $text The revisions summare
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();
117 throw new MWException( "Could not determine text id" );
120 function addDBData() {
121 $this->tablesUsed
[] = 'page';
122 $this->tablesUsed
[] = 'revision';
123 $this->tablesUsed
[] = 'text';
125 $wikitextNamespace = $this->getDefaultWikitextNS();
128 $title = Title
::newFromText( 'FetchTextTestPage1', $wikitextNamespace );
129 $page = WikiPage
::factory( $title );
130 $this->textId1
= $this->addRevision( $page, "FetchTextTestPage1Text1", "FetchTextTestPage1Summary1" );
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;
145 protected function setUp() {
148 // Check if any Exception is stored for rethrowing from addDBData
149 if ( $this->exceptionFromAddDBData
!== null ) {
150 throw $this->exceptionFromAddDBData
;
153 $this->fetchText
= new SemiMockedFetchText();
157 * Helper to relate FetchText's input and output
158 * @param string $input
159 * @param string $expectedOutput
161 private function assertFilter( $input, $expectedOutput ) {
162 $this->fetchText
->mockStdin( $input );
163 $this->fetchText
->execute();
164 $invocations = $this->fetchText
->mockGetInvocations();
165 $this->assertEquals( 1, $invocations['getStdin'],
166 "getStdin invocation counter" );
167 $this->expectOutputString( $expectedOutput );
170 // Instead of the following functions, a data provider would be great.
171 // However, as data providers are evaluated /before/ addDBData, a data
172 // provider would not know the required ids.
174 function testExistingSimple() {
175 $this->assertFilter( $this->textId2
,
176 $this->textId2
. "\n23\nFetchTextTestPage2Text1" );
179 function testExistingSimpleWithNewline() {
180 $this->assertFilter( $this->textId2
. "\n",
181 $this->textId2
. "\n23\nFetchTextTestPage2Text1" );
184 function testExistingSeveral() {
185 $this->assertFilter( "$this->textId1\n$this->textId5\n"
186 . "$this->textId3\n$this->textId3",
188 $this->textId1
. "\n23\nFetchTextTestPage1Text1",
189 $this->textId5
. "\n44\nFetchTextTestPage2Text4 "
190 . "some additional Text",
191 $this->textId3
. "\n23\nFetchTextTestPage2Text2",
192 $this->textId3
. "\n23\nFetchTextTestPage2Text2"
196 function testEmpty() {
197 $this->assertFilter( "", null );
200 function testNonExisting() {
201 $this->assertFilter( $this->textId5 +
10, ( $this->textId5 +
10 ) . "\n-1\n" );
204 function testNegativeInteger() {
205 $this->assertFilter( "-42", "-42\n-1\n" );
208 function testFloatingPointNumberExisting() {
209 // float -> int -> revision
210 $this->assertFilter( $this->textId3 +
0.14159,
211 $this->textId3
. "\n23\nFetchTextTestPage2Text2" );
214 function testFloatingPointNumberNonExisting() {
215 $this->assertFilter( $this->textId5 +
3.14159,
216 ( $this->textId5 +
3 ) . "\n-1\n" );
219 function testCharacters() {
220 $this->assertFilter( "abc", "0\n-1\n" );
224 $this->assertFilter( "ab\n" . $this->textId4
. ".5cd\n\nefg\n" . $this->textId2
225 . "\n" . $this->textId3
,
228 $this->textId4
. "\n23\nFetchTextTestPage2Text3",
231 $this->textId2
. "\n23\nFetchTextTestPage2Text1",
232 $this->textId3
. "\n23\nFetchTextTestPage2Text2"