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