MediaWiki
REL1_20
|
00001 <?php 00002 00003 require_once __DIR__ . "/../../../maintenance/fetchText.php"; 00004 00011 class SemiMockedFetchText extends FetchText { 00012 00016 private $mockStdinText = null; 00017 00021 private $mockSetUp = False; 00022 00026 private $mockInvocations = array( 'getStdin' => 0 ); 00027 00028 00029 00035 function mockStdin( $stdin ) 00036 { 00037 $this->mockStdinText = $stdin; 00038 $this->mockSetUp = True; 00039 } 00040 00047 function mockGetInvocations() 00048 { 00049 return $this->mockInvocations; 00050 } 00051 00052 // ----------------------------------------------------------------- 00053 // Mocked functions from FetchText follow. 00054 00055 function getStdin( $len = null ) 00056 { 00057 $this->mockInvocations['getStdin']++; 00058 if ( $len !== null ) { 00059 throw new PHPUnit_Framework_ExpectationFailedException( 00060 "Tried to get stdin with non null parameter" ); 00061 } 00062 00063 if ( ! $this->mockSetUp ) { 00064 throw new PHPUnit_Framework_ExpectationFailedException( 00065 "Tried to get stdin before setting up rerouting" ); 00066 } 00067 00068 return fopen( 'data://text/plain,' . $this->mockStdinText, 'r' ); 00069 } 00070 00071 } 00072 00079 class FetchTextTest extends MediaWikiTestCase { 00080 00081 // We add 5 Revisions for this test. Their corresponding text id's 00082 // are stored in the following 5 variables. 00083 private $textId1; 00084 private $textId2; 00085 private $textId3; 00086 private $textId4; 00087 private $textId5; 00088 00089 00097 private $exceptionFromAddDBData; 00098 00102 private $fetchText; 00103 00113 private function addRevision( $page, $text, $summary ) { 00114 $status = $page->doEdit( $text, $summary ); 00115 if ( $status->isGood() ) { 00116 $value = $status->getValue(); 00117 $revision = $value['revision']; 00118 $id = $revision->getTextId(); 00119 if ( $id > 0 ) { 00120 return $id; 00121 } 00122 } 00123 throw new MWException( "Could not determine text id" ); 00124 } 00125 00126 00127 function addDBData() { 00128 $this->tablesUsed[] = 'page'; 00129 $this->tablesUsed[] = 'revision'; 00130 $this->tablesUsed[] = 'text'; 00131 00132 try { 00133 $title = Title::newFromText( 'FetchTextTestPage1' ); 00134 $page = WikiPage::factory( $title ); 00135 $this->textId1 = $this->addRevision( $page, "FetchTextTestPage1Text1", "FetchTextTestPage1Summary1" ); 00136 00137 $title = Title::newFromText( 'FetchTextTestPage2' ); 00138 $page = WikiPage::factory( $title ); 00139 $this->textId2 = $this->addRevision( $page, "FetchTextTestPage2Text1", "FetchTextTestPage2Summary1" ); 00140 $this->textId3 = $this->addRevision( $page, "FetchTextTestPage2Text2", "FetchTextTestPage2Summary2" ); 00141 $this->textId4 = $this->addRevision( $page, "FetchTextTestPage2Text3", "FetchTextTestPage2Summary3" ); 00142 $this->textId5 = $this->addRevision( $page, "FetchTextTestPage2Text4 some additional Text ", "FetchTextTestPage2Summary4 extra " ); 00143 } catch ( Exception $e ) { 00144 // We'd love to pass $e directly. However, ... see 00145 // documentation of exceptionFromAddDBData 00146 $this->exceptionFromAddDBData = $e; 00147 } 00148 } 00149 00150 00151 protected function setUp() { 00152 parent::setUp(); 00153 00154 // Check if any Exception is stored for rethrowing from addDBData 00155 if ( $this->exceptionFromAddDBData !== null ) { 00156 throw $this->exceptionFromAddDBData; 00157 } 00158 00159 $this->fetchText = new SemiMockedFetchText(); 00160 } 00161 00162 00166 private function assertFilter( $input, $expectedOutput ) { 00167 $this->fetchText->mockStdin( $input ); 00168 $this->fetchText->execute(); 00169 $invocations = $this->fetchText->mockGetInvocations(); 00170 $this->assertEquals( 1, $invocations['getStdin'], 00171 "getStdin invocation counter" ); 00172 $this->expectOutputString( $expectedOutput ); 00173 } 00174 00175 00176 00177 // Instead of the following functions, a data provider would be great. 00178 // However, as data providers are evaluated /before/ addDBData, a data 00179 // provider would not know the required ids. 00180 00181 function testExistingSimple() { 00182 $this->assertFilter( $this->textId2, 00183 $this->textId2 . "\n23\nFetchTextTestPage2Text1" ); 00184 } 00185 00186 function testExistingSimpleWithNewline() { 00187 $this->assertFilter( $this->textId2 . "\n", 00188 $this->textId2 . "\n23\nFetchTextTestPage2Text1" ); 00189 } 00190 00191 function testExistingSeveral() { 00192 $this->assertFilter( "$this->textId1\n$this->textId5\n" 00193 . "$this->textId3\n$this->textId3", 00194 implode( "", array( 00195 $this->textId1 . "\n23\nFetchTextTestPage1Text1", 00196 $this->textId5 . "\n44\nFetchTextTestPage2Text4 " 00197 . "some additional Text", 00198 $this->textId3 . "\n23\nFetchTextTestPage2Text2", 00199 $this->textId3 . "\n23\nFetchTextTestPage2Text2" 00200 ) ) ); 00201 } 00202 00203 function testEmpty() { 00204 $this->assertFilter( "", null ); 00205 } 00206 00207 function testNonExisting() { 00208 $this->assertFilter( $this->textId5 + 10, ( $this->textId5 + 10 ) . "\n-1\n" ); 00209 } 00210 00211 function testNegativeInteger() { 00212 $this->assertFilter( "-42", "-42\n-1\n" ); 00213 } 00214 00215 function testFloatingPointNumberExisting() { 00216 // float -> int -> revision 00217 $this->assertFilter( $this->textId3 + 0.14159, 00218 $this->textId3 . "\n23\nFetchTextTestPage2Text2" ); 00219 } 00220 00221 function testFloatingPointNumberNonExisting() { 00222 $this->assertFilter( $this->textId5 + 3.14159, 00223 ( $this->textId5 + 3 ) . "\n-1\n" ); 00224 } 00225 00226 function testCharacters() { 00227 $this->assertFilter( "abc", "0\n-1\n" ); 00228 } 00229 00230 function testMix() { 00231 $this->assertFilter( "ab\n" . $this->textId4 . ".5cd\n\nefg\n" . $this->textId2 00232 . "\n" . $this->textId3, 00233 implode( "", array( 00234 "0\n-1\n", 00235 $this->textId4 . "\n23\nFetchTextTestPage2Text3", 00236 "0\n-1\n", 00237 "0\n-1\n", 00238 $this->textId2 . "\n23\nFetchTextTestPage2Text1", 00239 $this->textId3 . "\n23\nFetchTextTestPage2Text2" 00240 ) ) ); 00241 } 00242 00243 }