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