MediaWiki  REL1_22
DatabaseMysqlBaseTest.php
Go to the documentation of this file.
00001 <?php
00032 class FakeDatabaseMysqlBase extends DatabaseMysqlBase {
00033     // From DatabaseBase
00034     protected function closeConnection() {}
00035     protected function doQuery( $sql ) {}
00036 
00037     // From DatabaseMysql
00038     protected function mysqlConnect( $realServer ) {}
00039     protected function mysqlFreeResult( $res ) {}
00040     protected function mysqlFetchObject( $res ) {}
00041     protected function mysqlFetchArray( $res ) {}
00042     protected function mysqlNumRows( $res ) {}
00043     protected function mysqlNumFields( $res ) {}
00044     protected function mysqlFieldName( $res, $n ) {}
00045     protected function mysqlDataSeek( $res, $row ) {}
00046     protected function mysqlError( $conn = null ) {}
00047     protected function mysqlFetchField( $res, $n ) {}
00048     protected function mysqlPing() {}
00049 
00050     // From interface DatabaseType
00051     function insertId() {}
00052     function lastErrno() {}
00053     function affectedRows() {}
00054     function getServerVersion() {}
00055 }
00056 
00057 class DatabaseMysqlBaseTest extends MediaWikiTestCase {
00058 
00063     public function testAddIdentifierQuotes( $expected, $in ) {
00064         $db = new FakeDatabaseMysqlBase();
00065         $quoted = $db->addIdentifierQuotes( $in );
00066         $this->assertEquals($expected, $quoted);
00067     }
00068 
00069 
00075     function provideDiapers() {
00076         return array(
00077             // Format: expected, input
00078             array( '``', '' ),
00079 
00080             // Yeah I really hate loosely typed PHP idiocies nowadays
00081             array( '``', null ),
00082 
00083             // Dear codereviewer, guess what addIdentifierQuotes()
00084             // will return with thoses:
00085             array( '``', false ),
00086             array( '`1`', true ),
00087 
00088             // We never know what could happen
00089             array( '`0`', 0 ),
00090             array( '`1`', 1 ),
00091 
00092             // Whatchout! Should probably use something more meaningful
00093             array( "`'`", "'" ),  # single quote
00094             array( '`"`', '"' ),  # double quote
00095             array( '````', '`' ), # backtick
00096             array( '`’`', '’' ),  # apostrophe (look at your encyclopedia)
00097 
00098             // sneaky NUL bytes are lurking everywhere
00099             array( '``', "\0" ),
00100             array( '`xyzzy`', "\0x\0y\0z\0z\0y\0" ),
00101 
00102             // unicode chars
00103             array(
00104                 self::createUnicodeString( '`\u0001a\uFFFFb`' ),
00105                 self::createUnicodeString( '\u0001a\uFFFFb' )
00106             ),
00107             array(
00108                 self::createUnicodeString( '`\u0001\uFFFF`' ),
00109                 self::createUnicodeString( '\u0001\u0000\uFFFF\u0000' )
00110             ),
00111             array( '`☃`', '☃' ),
00112             array( '`メインページ`', 'メインページ' ),
00113             array( '`Басты_бет`', 'Басты_бет' ),
00114 
00115             // Real world:
00116             array( '`Alix`', 'Alix' ),  # while( ! $recovered ) { sleep(); }
00117             array( '`Backtick: ```', 'Backtick: `' ),
00118             array( '`This is a test`', 'This is a test' ),
00119         );
00120     }
00121 
00122     private static function createUnicodeString($str) {
00123         return json_decode( '"' . $str . '"' );
00124     }
00125 
00126     function getMockForViews() {
00127         $db = $this->getMockBuilder( 'DatabaseMysql' )
00128             ->disableOriginalConstructor()
00129             ->setMethods( array( 'fetchRow', 'query' ) )
00130             ->getMock();
00131 
00132         $db->expects( $this->any() )
00133             ->method( 'query' )
00134             ->with( $this->anything() )
00135             ->will(
00136                 $this->returnValue( null )
00137             );
00138 
00139         $db->expects( $this->any() )
00140             ->method( 'fetchRow' )
00141             ->with( $this->anything() )
00142             ->will( $this->onConsecutiveCalls(
00143                 array( 'Tables_in_' => 'view1' ),
00144                 array( 'Tables_in_' => 'view2' ),
00145                 array( 'Tables_in_' => 'myview' ),
00146                 false  # no more rows
00147             ));
00148         return $db;
00149     }
00153     function testListviews() {
00154         $db = $this->getMockForViews();
00155 
00156         // The first call populate an internal cache of views
00157         $this->assertEquals( array( 'view1', 'view2', 'myview'),
00158             $db->listViews() );
00159         $this->assertEquals( array( 'view1', 'view2', 'myview'),
00160             $db->listViews() );
00161 
00162         // Prefix filtering
00163         $this->assertEquals( array( 'view1', 'view2' ),
00164             $db->listViews( 'view' ) );
00165         $this->assertEquals( array( 'myview' ),
00166             $db->listViews( 'my' ) );
00167         $this->assertEquals( array(),
00168             $db->listViews( 'UNUSED_PREFIX' ) );
00169         $this->assertEquals( array( 'view1', 'view2', 'myview'),
00170             $db->listViews( '' ) );
00171     }
00172 
00177     function testIsView( $isView, $viewName ) {
00178         $db = $this->getMockForViews();
00179 
00180         switch( $isView ) {
00181             case true:
00182                 $this->assertTrue( $db->isView( $viewName ),
00183                     "$viewName should be considered a view" );
00184             break;
00185 
00186             case false:
00187                 $this->assertFalse( $db->isView( $viewName ),
00188                     "$viewName has not been defined as a view" );
00189             break;
00190         }
00191 
00192     }
00193 
00194     function provideViewExistanceChecks() {
00195         return array(
00196             // format: whether it is a view, view name
00197             array( true, 'view1' ),
00198             array( true, 'view2' ),
00199             array( true, 'myview' ),
00200 
00201             array( false, 'user' ),
00202 
00203             array( false, 'view10' ),
00204             array( false, 'my' ),
00205             array( false, 'OH_MY_GOD' ),  # they killed kenny!
00206         );
00207     }
00208 
00209 }