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