MediaWiki
REL1_24
|
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 00033 function mockStdin( $stdin ) { 00034 $this->mockStdinText = $stdin; 00035 $this->mockSetUp = true; 00036 } 00037 00044 function mockGetInvocations() { 00045 return $this->mockInvocations; 00046 } 00047 00048 // ----------------------------------------------------------------- 00049 // Mocked functions from FetchText follow. 00050 00051 function getStdin( $len = null ) { 00052 $this->mockInvocations['getStdin']++; 00053 if ( $len !== null ) { 00054 throw new PHPUnit_Framework_ExpectationFailedException( 00055 "Tried to get stdin with non null parameter" ); 00056 } 00057 00058 if ( !$this->mockSetUp ) { 00059 throw new PHPUnit_Framework_ExpectationFailedException( 00060 "Tried to get stdin before setting up rerouting" ); 00061 } 00062 00063 return fopen( 'data://text/plain,' . $this->mockStdinText, 'r' ); 00064 } 00065 } 00066 00074 class FetchTextTest extends MediaWikiTestCase { 00075 00076 // We add 5 Revisions for this test. Their corresponding text id's 00077 // are stored in the following 5 variables. 00078 private $textId1; 00079 private $textId2; 00080 private $textId3; 00081 private $textId4; 00082 private $textId5; 00083 00091 private $exceptionFromAddDBData; 00092 00096 private $fetchText; 00097 00107 private function addRevision( $page, $text, $summary ) { 00108 $status = $page->doEditContent( 00109 ContentHandler::makeContent( $text, $page->getTitle() ), 00110 $summary 00111 ); 00112 00113 if ( $status->isGood() ) { 00114 $value = $status->getValue(); 00115 $revision = $value['revision']; 00116 $id = $revision->getTextId(); 00117 00118 if ( $id > 0 ) { 00119 return $id; 00120 } 00121 } 00122 00123 throw new MWException( "Could not determine text id" ); 00124 } 00125 00126 function addDBData() { 00127 $this->tablesUsed[] = 'page'; 00128 $this->tablesUsed[] = 'revision'; 00129 $this->tablesUsed[] = 'text'; 00130 00131 $wikitextNamespace = $this->getDefaultWikitextNS(); 00132 00133 try { 00134 $title = Title::newFromText( 'FetchTextTestPage1', $wikitextNamespace ); 00135 $page = WikiPage::factory( $title ); 00136 $this->textId1 = $this->addRevision( 00137 $page, 00138 "FetchTextTestPage1Text1", 00139 "FetchTextTestPage1Summary1" 00140 ); 00141 00142 $title = Title::newFromText( 'FetchTextTestPage2', $wikitextNamespace ); 00143 $page = WikiPage::factory( $title ); 00144 $this->textId2 = $this->addRevision( 00145 $page, 00146 "FetchTextTestPage2Text1", 00147 "FetchTextTestPage2Summary1" 00148 ); 00149 $this->textId3 = $this->addRevision( 00150 $page, 00151 "FetchTextTestPage2Text2", 00152 "FetchTextTestPage2Summary2" 00153 ); 00154 $this->textId4 = $this->addRevision( 00155 $page, 00156 "FetchTextTestPage2Text3", 00157 "FetchTextTestPage2Summary3" 00158 ); 00159 $this->textId5 = $this->addRevision( 00160 $page, 00161 "FetchTextTestPage2Text4 some additional Text ", 00162 "FetchTextTestPage2Summary4 extra " 00163 ); 00164 } catch ( Exception $e ) { 00165 // We'd love to pass $e directly. However, ... see 00166 // documentation of exceptionFromAddDBData 00167 $this->exceptionFromAddDBData = $e; 00168 } 00169 } 00170 00171 protected function setUp() { 00172 parent::setUp(); 00173 00174 // Check if any Exception is stored for rethrowing from addDBData 00175 if ( $this->exceptionFromAddDBData !== null ) { 00176 throw $this->exceptionFromAddDBData; 00177 } 00178 00179 $this->fetchText = new SemiMockedFetchText(); 00180 } 00181 00187 private function assertFilter( $input, $expectedOutput ) { 00188 $this->fetchText->mockStdin( $input ); 00189 $this->fetchText->execute(); 00190 $invocations = $this->fetchText->mockGetInvocations(); 00191 $this->assertEquals( 1, $invocations['getStdin'], 00192 "getStdin invocation counter" ); 00193 $this->expectOutputString( $expectedOutput ); 00194 } 00195 00196 // Instead of the following functions, a data provider would be great. 00197 // However, as data providers are evaluated /before/ addDBData, a data 00198 // provider would not know the required ids. 00199 00200 function testExistingSimple() { 00201 $this->assertFilter( $this->textId2, 00202 $this->textId2 . "\n23\nFetchTextTestPage2Text1" ); 00203 } 00204 00205 function testExistingSimpleWithNewline() { 00206 $this->assertFilter( $this->textId2 . "\n", 00207 $this->textId2 . "\n23\nFetchTextTestPage2Text1" ); 00208 } 00209 00210 function testExistingSeveral() { 00211 $this->assertFilter( "$this->textId1\n$this->textId5\n" 00212 . "$this->textId3\n$this->textId3", 00213 implode( "", array( 00214 $this->textId1 . "\n23\nFetchTextTestPage1Text1", 00215 $this->textId5 . "\n44\nFetchTextTestPage2Text4 " 00216 . "some additional Text", 00217 $this->textId3 . "\n23\nFetchTextTestPage2Text2", 00218 $this->textId3 . "\n23\nFetchTextTestPage2Text2" 00219 ) ) ); 00220 } 00221 00222 function testEmpty() { 00223 $this->assertFilter( "", null ); 00224 } 00225 00226 function testNonExisting() { 00227 $this->assertFilter( $this->textId5 + 10, ( $this->textId5 + 10 ) . "\n-1\n" ); 00228 } 00229 00230 function testNegativeInteger() { 00231 $this->assertFilter( "-42", "-42\n-1\n" ); 00232 } 00233 00234 function testFloatingPointNumberExisting() { 00235 // float -> int -> revision 00236 $this->assertFilter( $this->textId3 + 0.14159, 00237 $this->textId3 . "\n23\nFetchTextTestPage2Text2" ); 00238 } 00239 00240 function testFloatingPointNumberNonExisting() { 00241 $this->assertFilter( $this->textId5 + 3.14159, 00242 ( $this->textId5 + 3 ) . "\n-1\n" ); 00243 } 00244 00245 function testCharacters() { 00246 $this->assertFilter( "abc", "0\n-1\n" ); 00247 } 00248 00249 function testMix() { 00250 $this->assertFilter( "ab\n" . $this->textId4 . ".5cd\n\nefg\n" . $this->textId2 00251 . "\n" . $this->textId3, 00252 implode( "", array( 00253 "0\n-1\n", 00254 $this->textId4 . "\n23\nFetchTextTestPage2Text3", 00255 "0\n-1\n", 00256 "0\n-1\n", 00257 $this->textId2 . "\n23\nFetchTextTestPage2Text1", 00258 $this->textId3 . "\n23\nFetchTextTestPage2Text2" 00259 ) ) ); 00260 } 00261 }