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