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