MediaWiki  REL1_21
DatabaseTest.php
Go to the documentation of this file.
00001 <?php
00002 
00007 class DatabaseTest extends MediaWikiTestCase {
00008         var $db, $functionTest = false;
00009 
00010         protected function setUp() {
00011                 parent::setUp();
00012                 $this->db = wfGetDB( DB_MASTER );
00013         }
00014 
00015         protected function tearDown() {
00016                 parent::tearDown();
00017                 if ( $this->functionTest ) {
00018                         $this->dropFunctions();
00019                         $this->functionTest = false;
00020                 }
00021         }
00022 
00023         function testAddQuotesNull() {
00024                 $check = "NULL";
00025                 if ( $this->db->getType() === 'sqlite' || $this->db->getType() === 'oracle' ) {
00026                         $check = "''";
00027                 }
00028                 $this->assertEquals( $check, $this->db->addQuotes( null ) );
00029         }
00030 
00031         function testAddQuotesInt() {
00032                 # returning just "1234" should be ok too, though...
00033                 # maybe
00034                 $this->assertEquals(
00035                         "'1234'",
00036                         $this->db->addQuotes( 1234 ) );
00037         }
00038 
00039         function testAddQuotesFloat() {
00040                 # returning just "1234.5678" would be ok too, though
00041                 $this->assertEquals(
00042                         "'1234.5678'",
00043                         $this->db->addQuotes( 1234.5678 ) );
00044         }
00045 
00046         function testAddQuotesString() {
00047                 $this->assertEquals(
00048                         "'string'",
00049                         $this->db->addQuotes( 'string' ) );
00050         }
00051 
00052         function testAddQuotesStringQuote() {
00053                 $check = "'string''s cause trouble'";
00054                 if ( $this->db->getType() === 'mysql' ) {
00055                         $check = "'string\'s cause trouble'";
00056                 }
00057                 $this->assertEquals(
00058                         $check,
00059                         $this->db->addQuotes( "string's cause trouble" ) );
00060         }
00061 
00062         private function getSharedTableName( $table, $database, $prefix, $format = 'quoted' ) {
00063                 global $wgSharedDB, $wgSharedTables, $wgSharedPrefix;
00064 
00065                 $oldName = $wgSharedDB;
00066                 $oldTables = $wgSharedTables;
00067                 $oldPrefix = $wgSharedPrefix;
00068 
00069                 $wgSharedDB = $database;
00070                 $wgSharedTables = array( $table );
00071                 $wgSharedPrefix = $prefix;
00072 
00073                 $ret = $this->db->tableName( $table, $format );
00074 
00075                 $wgSharedDB = $oldName;
00076                 $wgSharedTables = $oldTables;
00077                 $wgSharedPrefix = $oldPrefix;
00078 
00079                 return $ret;
00080         }
00081 
00082         private function prefixAndQuote( $table, $database = null, $prefix = null, $format = 'quoted' ) {
00083                 if ( $this->db->getType() === 'sqlite' || $format !== 'quoted' ) {
00084                         $quote = '';
00085                 } elseif ( $this->db->getType() === 'mysql' ) {
00086                         $quote = '`';
00087                 } else {
00088                         $quote = '"';
00089                 }
00090 
00091                 if ( $database !== null ) {
00092                         $database = $quote . $database . $quote . '.';
00093                 }
00094 
00095                 if ( $prefix === null ) {
00096                         $prefix = $this->dbPrefix();
00097                 }
00098 
00099                 return $database . $quote . $prefix . $table . $quote;
00100         }
00101 
00102         function testTableNameLocal() {
00103                 $this->assertEquals(
00104                         $this->prefixAndQuote( 'tablename' ),
00105                         $this->db->tableName( 'tablename' )
00106                 );
00107         }
00108 
00109         function testTableNameRawLocal() {
00110                 $this->assertEquals(
00111                         $this->prefixAndQuote( 'tablename', null, null, 'raw' ),
00112                         $this->db->tableName( 'tablename', 'raw' )
00113                 );
00114         }
00115 
00116         function testTableNameShared() {
00117                 $this->assertEquals(
00118                         $this->prefixAndQuote( 'tablename', 'sharedatabase', 'sh_' ),
00119                         $this->getSharedTableName( 'tablename', 'sharedatabase', 'sh_' )
00120                 );
00121 
00122                 $this->assertEquals(
00123                         $this->prefixAndQuote( 'tablename', 'sharedatabase', null ),
00124                         $this->getSharedTableName( 'tablename', 'sharedatabase', null )
00125                 );
00126         }
00127 
00128         function testTableNameRawShared() {
00129                 $this->assertEquals(
00130                         $this->prefixAndQuote( 'tablename', 'sharedatabase', 'sh_', 'raw' ),
00131                         $this->getSharedTableName( 'tablename', 'sharedatabase', 'sh_', 'raw' )
00132                 );
00133 
00134                 $this->assertEquals(
00135                         $this->prefixAndQuote( 'tablename', 'sharedatabase', null, 'raw' ),
00136                         $this->getSharedTableName( 'tablename', 'sharedatabase', null, 'raw' )
00137                 );
00138         }
00139 
00140         function testTableNameForeign() {
00141                 $this->assertEquals(
00142                         $this->prefixAndQuote( 'tablename', 'databasename', '' ),
00143                         $this->db->tableName( 'databasename.tablename' )
00144                 );
00145         }
00146 
00147         function testTableNameRawForeign() {
00148                 $this->assertEquals(
00149                         $this->prefixAndQuote( 'tablename', 'databasename', '', 'raw' ),
00150                         $this->db->tableName( 'databasename.tablename', 'raw' )
00151                 );
00152         }
00153 
00154         function testFillPreparedEmpty() {
00155                 $sql = $this->db->fillPrepared(
00156                         'SELECT * FROM interwiki', array() );
00157                 $this->assertEquals(
00158                         "SELECT * FROM interwiki",
00159                         $sql );
00160         }
00161 
00162         function testFillPreparedQuestion() {
00163                 $sql = $this->db->fillPrepared(
00164                         'SELECT * FROM cur WHERE cur_namespace=? AND cur_title=?',
00165                         array( 4, "Snicker's_paradox" ) );
00166 
00167                 $check = "SELECT * FROM cur WHERE cur_namespace='4' AND cur_title='Snicker''s_paradox'";
00168                 if ( $this->db->getType() === 'mysql' ) {
00169                         $check = "SELECT * FROM cur WHERE cur_namespace='4' AND cur_title='Snicker\'s_paradox'";
00170                 }
00171                 $this->assertEquals( $check, $sql );
00172         }
00173 
00174         function testFillPreparedBang() {
00175                 $sql = $this->db->fillPrepared(
00176                         'SELECT user_id FROM ! WHERE user_name=?',
00177                         array( '"user"', "Slash's Dot" ) );
00178 
00179                 $check = "SELECT user_id FROM \"user\" WHERE user_name='Slash''s Dot'";
00180                 if ( $this->db->getType() === 'mysql' ) {
00181                         $check = "SELECT user_id FROM \"user\" WHERE user_name='Slash\'s Dot'";
00182                 }
00183                 $this->assertEquals( $check, $sql );
00184         }
00185 
00186         function testFillPreparedRaw() {
00187                 $sql = $this->db->fillPrepared(
00188                         "SELECT * FROM cur WHERE cur_title='This_\\&_that,_WTF\\?\\!'",
00189                         array( '"user"', "Slash's Dot" ) );
00190                 $this->assertEquals(
00191                         "SELECT * FROM cur WHERE cur_title='This_&_that,_WTF?!'",
00192                         $sql );
00193         }
00194 
00195         function testStoredFunctions() {
00196                 if ( !in_array( wfGetDB( DB_MASTER )->getType(), array( 'mysql', 'postgres' ) ) ) {
00197                         $this->markTestSkipped( 'MySQL or Postgres required' );
00198                 }
00199                 global $IP;
00200                 $this->dropFunctions();
00201                 $this->functionTest = true;
00202                 $this->assertTrue( $this->db->sourceFile( "$IP/tests/phpunit/data/db/{$this->db->getType()}/functions.sql" ) );
00203                 $res = $this->db->query( 'SELECT mw_test_function() AS test', __METHOD__ );
00204                 $this->assertEquals( 42, $res->fetchObject()->test );
00205         }
00206 
00207         private function dropFunctions() {
00208                 $this->db->query( 'DROP FUNCTION IF EXISTS mw_test_function'
00209                                 . ( $this->db->getType() == 'postgres' ? '()' : '' )
00210                 );
00211         }
00212 }