Import: Handle uploads with sha1 starting with 0 properly
[mediawiki.git] / tests / phpunit / maintenance / fetchTextTest.php
blob4e38418a18428acbe64d5ffa2633c6c292078e82
1 <?php
3 require_once __DIR__ . "/../../../maintenance/fetchText.php";
5 /**
6 * Mock for the input/output of FetchText
8 * FetchText internally tries to access stdin and stdout. We mock those aspects
9 * for testing.
11 class SemiMockedFetchText extends FetchText {
13 /**
14 * @var string|null Text to pass as stdin
16 private $mockStdinText = null;
18 /**
19 * @var bool Whether or not a text for stdin has been provided
21 private $mockSetUp = false;
23 /**
24 * @var array Invocation counters for the mocked aspects
26 private $mockInvocations = array( 'getStdin' => 0 );
28 /**
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;
38 /**
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' );
67 /**
68 * TestCase for FetchText
70 * @group Database
71 * @group Dump
72 * @covers 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.
78 private $textId1;
79 private $textId2;
80 private $textId3;
81 private $textId4;
82 private $textId5;
84 /**
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;
93 /**
94 * @var FetchText The (mocked) FetchText that is to test
96 private $fetchText;
98 /**
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 $summary The revisions summare
104 * @return int
105 * @throws MWException
107 private function addRevision( $page, $text, $summary ) {
108 $status = $page->doEditContent(
109 ContentHandler::makeContent( $text, $page->getTitle() ),
110 $summary
113 if ( $status->isGood() ) {
114 $value = $status->getValue();
115 $revision = $value['revision'];
116 $id = $revision->getTextId();
118 if ( $id > 0 ) {
119 return $id;
123 throw new MWException( "Could not determine text id" );
126 function addDBData() {
127 $this->tablesUsed[] = 'page';
128 $this->tablesUsed[] = 'revision';
129 $this->tablesUsed[] = 'text';
131 $wikitextNamespace = $this->getDefaultWikitextNS();
133 try {
134 $title = Title::newFromText( 'FetchTextTestPage1', $wikitextNamespace );
135 $page = WikiPage::factory( $title );
136 $this->textId1 = $this->addRevision(
137 $page,
138 "FetchTextTestPage1Text1",
139 "FetchTextTestPage1Summary1"
142 $title = Title::newFromText( 'FetchTextTestPage2', $wikitextNamespace );
143 $page = WikiPage::factory( $title );
144 $this->textId2 = $this->addRevision(
145 $page,
146 "FetchTextTestPage2Text1",
147 "FetchTextTestPage2Summary1"
149 $this->textId3 = $this->addRevision(
150 $page,
151 "FetchTextTestPage2Text2",
152 "FetchTextTestPage2Summary2"
154 $this->textId4 = $this->addRevision(
155 $page,
156 "FetchTextTestPage2Text3",
157 "FetchTextTestPage2Summary3"
159 $this->textId5 = $this->addRevision(
160 $page,
161 "FetchTextTestPage2Text4 some additional Text ",
162 "FetchTextTestPage2Summary4 extra "
164 } catch ( Exception $e ) {
165 // We'd love to pass $e directly. However, ... see
166 // documentation of exceptionFromAddDBData
167 $this->exceptionFromAddDBData = $e;
171 protected function setUp() {
172 parent::setUp();
174 // Check if any Exception is stored for rethrowing from addDBData
175 if ( $this->exceptionFromAddDBData !== null ) {
176 throw $this->exceptionFromAddDBData;
179 $this->fetchText = new SemiMockedFetchText();
183 * Helper to relate FetchText's input and output
184 * @param string $input
185 * @param string $expectedOutput
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 );
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.
200 function testExistingSimple() {
201 $this->assertFilter( $this->textId2,
202 $this->textId2 . "\n23\nFetchTextTestPage2Text1" );
205 function testExistingSimpleWithNewline() {
206 $this->assertFilter( $this->textId2 . "\n",
207 $this->textId2 . "\n23\nFetchTextTestPage2Text1" );
210 function testExistingSeveral() {
211 $this->assertFilter( "$this->textId1\n$this->textId5\n"
212 . "$this->textId3\n$this->textId3",
213 implode( "", array(
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 ) ) );
222 function testEmpty() {
223 $this->assertFilter( "", null );
226 function testNonExisting() {
227 $this->assertFilter( $this->textId5 + 10, ( $this->textId5 + 10 ) . "\n-1\n" );
230 function testNegativeInteger() {
231 $this->assertFilter( "-42", "-42\n-1\n" );
234 function testFloatingPointNumberExisting() {
235 // float -> int -> revision
236 $this->assertFilter( $this->textId3 + 0.14159,
237 $this->textId3 . "\n23\nFetchTextTestPage2Text2" );
240 function testFloatingPointNumberNonExisting() {
241 $this->assertFilter( $this->textId5 + 3.14159,
242 ( $this->textId5 + 3 ) . "\n-1\n" );
245 function testCharacters() {
246 $this->assertFilter( "abc", "0\n-1\n" );
249 function testMix() {
250 $this->assertFilter( "ab\n" . $this->textId4 . ".5cd\n\nefg\n" . $this->textId2
251 . "\n" . $this->textId3,
252 implode( "", array(
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 ) ) );