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