MediaWiki
master
|
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->doEditContent( ContentHandler::makeContent( $text, $page->getTitle() ), $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 $wikitextNamespace = $this->getDefaultWikitextNS(); 00133 00134 try { 00135 $title = Title::newFromText( 'FetchTextTestPage1', $wikitextNamespace ); 00136 $page = WikiPage::factory( $title ); 00137 $this->textId1 = $this->addRevision( $page, "FetchTextTestPage1Text1", "FetchTextTestPage1Summary1" ); 00138 00139 $title = Title::newFromText( 'FetchTextTestPage2', $wikitextNamespace ); 00140 $page = WikiPage::factory( $title ); 00141 $this->textId2 = $this->addRevision( $page, "FetchTextTestPage2Text1", "FetchTextTestPage2Summary1" ); 00142 $this->textId3 = $this->addRevision( $page, "FetchTextTestPage2Text2", "FetchTextTestPage2Summary2" ); 00143 $this->textId4 = $this->addRevision( $page, "FetchTextTestPage2Text3", "FetchTextTestPage2Summary3" ); 00144 $this->textId5 = $this->addRevision( $page, "FetchTextTestPage2Text4 some additional Text ", "FetchTextTestPage2Summary4 extra " ); 00145 } catch ( Exception $e ) { 00146 // We'd love to pass $e directly. However, ... see 00147 // documentation of exceptionFromAddDBData 00148 $this->exceptionFromAddDBData = $e; 00149 } 00150 } 00151 00152 00153 protected function setUp() { 00154 parent::setUp(); 00155 00156 // Check if any Exception is stored for rethrowing from addDBData 00157 if ( $this->exceptionFromAddDBData !== null ) { 00158 throw $this->exceptionFromAddDBData; 00159 } 00160 00161 $this->fetchText = new SemiMockedFetchText(); 00162 } 00163 00164 00168 private function assertFilter( $input, $expectedOutput ) { 00169 $this->fetchText->mockStdin( $input ); 00170 $this->fetchText->execute(); 00171 $invocations = $this->fetchText->mockGetInvocations(); 00172 $this->assertEquals( 1, $invocations['getStdin'], 00173 "getStdin invocation counter" ); 00174 $this->expectOutputString( $expectedOutput ); 00175 } 00176 00177 00178 00179 // Instead of the following functions, a data provider would be great. 00180 // However, as data providers are evaluated /before/ addDBData, a data 00181 // provider would not know the required ids. 00182 00183 function testExistingSimple() { 00184 $this->assertFilter( $this->textId2, 00185 $this->textId2 . "\n23\nFetchTextTestPage2Text1" ); 00186 } 00187 00188 function testExistingSimpleWithNewline() { 00189 $this->assertFilter( $this->textId2 . "\n", 00190 $this->textId2 . "\n23\nFetchTextTestPage2Text1" ); 00191 } 00192 00193 function testExistingSeveral() { 00194 $this->assertFilter( "$this->textId1\n$this->textId5\n" 00195 . "$this->textId3\n$this->textId3", 00196 implode( "", array( 00197 $this->textId1 . "\n23\nFetchTextTestPage1Text1", 00198 $this->textId5 . "\n44\nFetchTextTestPage2Text4 " 00199 . "some additional Text", 00200 $this->textId3 . "\n23\nFetchTextTestPage2Text2", 00201 $this->textId3 . "\n23\nFetchTextTestPage2Text2" 00202 ) ) ); 00203 } 00204 00205 function testEmpty() { 00206 $this->assertFilter( "", null ); 00207 } 00208 00209 function testNonExisting() { 00210 $this->assertFilter( $this->textId5 + 10, ( $this->textId5 + 10 ) . "\n-1\n" ); 00211 } 00212 00213 function testNegativeInteger() { 00214 $this->assertFilter( "-42", "-42\n-1\n" ); 00215 } 00216 00217 function testFloatingPointNumberExisting() { 00218 // float -> int -> revision 00219 $this->assertFilter( $this->textId3 + 0.14159, 00220 $this->textId3 . "\n23\nFetchTextTestPage2Text2" ); 00221 } 00222 00223 function testFloatingPointNumberNonExisting() { 00224 $this->assertFilter( $this->textId5 + 3.14159, 00225 ( $this->textId5 + 3 ) . "\n-1\n" ); 00226 } 00227 00228 function testCharacters() { 00229 $this->assertFilter( "abc", "0\n-1\n" ); 00230 } 00231 00232 function testMix() { 00233 $this->assertFilter( "ab\n" . $this->textId4 . ".5cd\n\nefg\n" . $this->textId2 00234 . "\n" . $this->textId3, 00235 implode( "", array( 00236 "0\n-1\n", 00237 $this->textId4 . "\n23\nFetchTextTestPage2Text3", 00238 "0\n-1\n", 00239 "0\n-1\n", 00240 $this->textId2 . "\n23\nFetchTextTestPage2Text1", 00241 $this->textId3 . "\n23\nFetchTextTestPage2Text2" 00242 ) ) ); 00243 } 00244 00245 }