7 class DatabaseTest
extends MediaWikiTestCase
{
13 private $functionTest = false;
15 protected function setUp() {
17 $this->db
= wfGetDB( DB_MASTER
);
20 protected function tearDown() {
22 if ( $this->functionTest
) {
23 $this->dropFunctions();
24 $this->functionTest
= false;
28 * @covers DatabaseBase::dropTable
30 public function testAddQuotesNull() {
32 if ( $this->db
->getType() === 'sqlite' ||
$this->db
->getType() === 'oracle' ) {
35 $this->assertEquals( $check, $this->db
->addQuotes( null ) );
38 public function testAddQuotesInt() {
39 # returning just "1234" should be ok too, though...
43 $this->db
->addQuotes( 1234 ) );
46 public function testAddQuotesFloat() {
47 # returning just "1234.5678" would be ok too, though
50 $this->db
->addQuotes( 1234.5678 ) );
53 public function testAddQuotesString() {
56 $this->db
->addQuotes( 'string' ) );
59 public function testAddQuotesStringQuote() {
60 $check = "'string''s cause trouble'";
61 if ( $this->db
->getType() === 'mysql' ) {
62 $check = "'string\'s cause trouble'";
66 $this->db
->addQuotes( "string's cause trouble" ) );
69 private function getSharedTableName( $table, $database, $prefix, $format = 'quoted' ) {
70 global $wgSharedDB, $wgSharedTables, $wgSharedPrefix;
72 $oldName = $wgSharedDB;
73 $oldTables = $wgSharedTables;
74 $oldPrefix = $wgSharedPrefix;
76 $wgSharedDB = $database;
77 $wgSharedTables = array( $table );
78 $wgSharedPrefix = $prefix;
80 $ret = $this->db
->tableName( $table, $format );
82 $wgSharedDB = $oldName;
83 $wgSharedTables = $oldTables;
84 $wgSharedPrefix = $oldPrefix;
89 private function prefixAndQuote( $table, $database = null, $prefix = null, $format = 'quoted' ) {
90 if ( $this->db
->getType() === 'sqlite' ||
$format !== 'quoted' ) {
92 } elseif ( $this->db
->getType() === 'mysql' ) {
94 } elseif ( $this->db
->getType() === 'oracle' ) {
100 if ( $database !== null ) {
101 if ( $this->db
->getType() === 'oracle' ) {
102 $database = $quote . $database . '.';
104 $database = $quote . $database . $quote . '.';
108 if ( $prefix === null ) {
109 $prefix = $this->dbPrefix();
112 if ( $this->db
->getType() === 'oracle' ) {
113 return strtoupper( $database . $quote . $prefix . $table );
115 return $database . $quote . $prefix . $table . $quote;
119 public function testTableNameLocal() {
121 $this->prefixAndQuote( 'tablename' ),
122 $this->db
->tableName( 'tablename' )
126 public function testTableNameRawLocal() {
128 $this->prefixAndQuote( 'tablename', null, null, 'raw' ),
129 $this->db
->tableName( 'tablename', 'raw' )
133 public function testTableNameShared() {
135 $this->prefixAndQuote( 'tablename', 'sharedatabase', 'sh_' ),
136 $this->getSharedTableName( 'tablename', 'sharedatabase', 'sh_' )
140 $this->prefixAndQuote( 'tablename', 'sharedatabase', null ),
141 $this->getSharedTableName( 'tablename', 'sharedatabase', null )
145 public function testTableNameRawShared() {
147 $this->prefixAndQuote( 'tablename', 'sharedatabase', 'sh_', 'raw' ),
148 $this->getSharedTableName( 'tablename', 'sharedatabase', 'sh_', 'raw' )
152 $this->prefixAndQuote( 'tablename', 'sharedatabase', null, 'raw' ),
153 $this->getSharedTableName( 'tablename', 'sharedatabase', null, 'raw' )
157 public function testTableNameForeign() {
159 $this->prefixAndQuote( 'tablename', 'databasename', '' ),
160 $this->db
->tableName( 'databasename.tablename' )
164 public function testTableNameRawForeign() {
166 $this->prefixAndQuote( 'tablename', 'databasename', '', 'raw' ),
167 $this->db
->tableName( 'databasename.tablename', 'raw' )
171 public function testFillPreparedEmpty() {
172 $sql = $this->db
->fillPrepared(
173 'SELECT * FROM interwiki', array() );
175 "SELECT * FROM interwiki",
179 public function testFillPreparedQuestion() {
180 $sql = $this->db
->fillPrepared(
181 'SELECT * FROM cur WHERE cur_namespace=? AND cur_title=?',
182 array( 4, "Snicker's_paradox" ) );
184 $check = "SELECT * FROM cur WHERE cur_namespace='4' AND cur_title='Snicker''s_paradox'";
185 if ( $this->db
->getType() === 'mysql' ) {
186 $check = "SELECT * FROM cur WHERE cur_namespace='4' AND cur_title='Snicker\'s_paradox'";
188 $this->assertEquals( $check, $sql );
191 public function testFillPreparedBang() {
192 $sql = $this->db
->fillPrepared(
193 'SELECT user_id FROM ! WHERE user_name=?',
194 array( '"user"', "Slash's Dot" ) );
196 $check = "SELECT user_id FROM \"user\" WHERE user_name='Slash''s Dot'";
197 if ( $this->db
->getType() === 'mysql' ) {
198 $check = "SELECT user_id FROM \"user\" WHERE user_name='Slash\'s Dot'";
200 $this->assertEquals( $check, $sql );
203 public function testFillPreparedRaw() {
204 $sql = $this->db
->fillPrepared(
205 "SELECT * FROM cur WHERE cur_title='This_\\&_that,_WTF\\?\\!'",
206 array( '"user"', "Slash's Dot" ) );
208 "SELECT * FROM cur WHERE cur_title='This_&_that,_WTF?!'",
212 public function testStoredFunctions() {
213 if ( !in_array( wfGetDB( DB_MASTER
)->getType(), array( 'mysql', 'postgres' ) ) ) {
214 $this->markTestSkipped( 'MySQL or Postgres required' );
217 $this->dropFunctions();
218 $this->functionTest
= true;
220 $this->db
->sourceFile( "$IP/tests/phpunit/data/db/{$this->db->getType()}/functions.sql" )
222 $res = $this->db
->query( 'SELECT mw_test_function() AS test', __METHOD__
);
223 $this->assertEquals( 42, $res->fetchObject()->test
);
226 private function dropFunctions() {
227 $this->db
->query( 'DROP FUNCTION IF EXISTS mw_test_function'
228 . ( $this->db
->getType() == 'postgres' ?
'()' : '' )
232 public function testUnknownTableCorruptsResults() {
233 $res = $this->db
->select( 'page', '*', array( 'page_id' => 1 ) );
234 $this->assertFalse( $this->db
->tableExists( 'foobarbaz' ) );
235 $this->assertInternalType( 'int', $res->numRows() );