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