7 class DatabaseTest
extends MediaWikiTestCase
{
12 var $functionTest = false;
14 protected function setUp() {
16 $this->db
= wfGetDB( DB_MASTER
);
19 protected function tearDown() {
21 if ( $this->functionTest
) {
22 $this->dropFunctions();
23 $this->functionTest
= false;
27 * @covers DatabaseBase::dropTable
29 function testAddQuotesNull() {
31 if ( $this->db
->getType() === 'sqlite' ||
$this->db
->getType() === 'oracle' ) {
34 $this->assertEquals( $check, $this->db
->addQuotes( null ) );
37 function testAddQuotesInt() {
38 # returning just "1234" should be ok too, though...
42 $this->db
->addQuotes( 1234 ) );
45 function testAddQuotesFloat() {
46 # returning just "1234.5678" would be ok too, though
49 $this->db
->addQuotes( 1234.5678 ) );
52 function testAddQuotesString() {
55 $this->db
->addQuotes( 'string' ) );
58 function testAddQuotesStringQuote() {
59 $check = "'string''s cause trouble'";
60 if ( $this->db
->getType() === 'mysql' ) {
61 $check = "'string\'s cause trouble'";
65 $this->db
->addQuotes( "string's cause trouble" ) );
68 private function getSharedTableName( $table, $database, $prefix, $format = 'quoted' ) {
69 global $wgSharedDB, $wgSharedTables, $wgSharedPrefix;
71 $oldName = $wgSharedDB;
72 $oldTables = $wgSharedTables;
73 $oldPrefix = $wgSharedPrefix;
75 $wgSharedDB = $database;
76 $wgSharedTables = array( $table );
77 $wgSharedPrefix = $prefix;
79 $ret = $this->db
->tableName( $table, $format );
81 $wgSharedDB = $oldName;
82 $wgSharedTables = $oldTables;
83 $wgSharedPrefix = $oldPrefix;
88 private function prefixAndQuote( $table, $database = null, $prefix = null, $format = 'quoted' ) {
89 if ( $this->db
->getType() === 'sqlite' ||
$format !== 'quoted' ) {
91 } elseif ( $this->db
->getType() === 'mysql' ) {
93 } elseif ( $this->db
->getType() === 'oracle' ) {
99 if ( $database !== null ) {
100 if ( $this->db
->getType() === 'oracle' ) {
101 $database = $quote . $database . '.';
103 $database = $quote . $database . $quote . '.';
107 if ( $prefix === null ) {
108 $prefix = $this->dbPrefix();
111 if ( $this->db
->getType() === 'oracle' ) {
112 return strtoupper($database . $quote . $prefix . $table);
114 return $database . $quote . $prefix . $table . $quote;
118 function testTableNameLocal() {
120 $this->prefixAndQuote( 'tablename' ),
121 $this->db
->tableName( 'tablename' )
125 function testTableNameRawLocal() {
127 $this->prefixAndQuote( 'tablename', null, null, 'raw' ),
128 $this->db
->tableName( 'tablename', 'raw' )
132 function testTableNameShared() {
134 $this->prefixAndQuote( 'tablename', 'sharedatabase', 'sh_' ),
135 $this->getSharedTableName( 'tablename', 'sharedatabase', 'sh_' )
139 $this->prefixAndQuote( 'tablename', 'sharedatabase', null ),
140 $this->getSharedTableName( 'tablename', 'sharedatabase', null )
144 function testTableNameRawShared() {
146 $this->prefixAndQuote( 'tablename', 'sharedatabase', 'sh_', 'raw' ),
147 $this->getSharedTableName( 'tablename', 'sharedatabase', 'sh_', 'raw' )
151 $this->prefixAndQuote( 'tablename', 'sharedatabase', null, 'raw' ),
152 $this->getSharedTableName( 'tablename', 'sharedatabase', null, 'raw' )
156 function testTableNameForeign() {
158 $this->prefixAndQuote( 'tablename', 'databasename', '' ),
159 $this->db
->tableName( 'databasename.tablename' )
163 function testTableNameRawForeign() {
165 $this->prefixAndQuote( 'tablename', 'databasename', '', 'raw' ),
166 $this->db
->tableName( 'databasename.tablename', 'raw' )
170 function testFillPreparedEmpty() {
171 $sql = $this->db
->fillPrepared(
172 'SELECT * FROM interwiki', array() );
174 "SELECT * FROM interwiki",
178 function testFillPreparedQuestion() {
179 $sql = $this->db
->fillPrepared(
180 'SELECT * FROM cur WHERE cur_namespace=? AND cur_title=?',
181 array( 4, "Snicker's_paradox" ) );
183 $check = "SELECT * FROM cur WHERE cur_namespace='4' AND cur_title='Snicker''s_paradox'";
184 if ( $this->db
->getType() === 'mysql' ) {
185 $check = "SELECT * FROM cur WHERE cur_namespace='4' AND cur_title='Snicker\'s_paradox'";
187 $this->assertEquals( $check, $sql );
190 function testFillPreparedBang() {
191 $sql = $this->db
->fillPrepared(
192 'SELECT user_id FROM ! WHERE user_name=?',
193 array( '"user"', "Slash's Dot" ) );
195 $check = "SELECT user_id FROM \"user\" WHERE user_name='Slash''s Dot'";
196 if ( $this->db
->getType() === 'mysql' ) {
197 $check = "SELECT user_id FROM \"user\" WHERE user_name='Slash\'s Dot'";
199 $this->assertEquals( $check, $sql );
202 function testFillPreparedRaw() {
203 $sql = $this->db
->fillPrepared(
204 "SELECT * FROM cur WHERE cur_title='This_\\&_that,_WTF\\?\\!'",
205 array( '"user"', "Slash's Dot" ) );
207 "SELECT * FROM cur WHERE cur_title='This_&_that,_WTF?!'",
211 function testStoredFunctions() {
212 if ( !in_array( wfGetDB( DB_MASTER
)->getType(), array( 'mysql', 'postgres' ) ) ) {
213 $this->markTestSkipped( 'MySQL or Postgres required' );
216 $this->dropFunctions();
217 $this->functionTest
= true;
218 $this->assertTrue( $this->db
->sourceFile( "$IP/tests/phpunit/data/db/{$this->db->getType()}/functions.sql" ) );
219 $res = $this->db
->query( 'SELECT mw_test_function() AS test', __METHOD__
);
220 $this->assertEquals( 42, $res->fetchObject()->test
);
223 private function dropFunctions() {
224 $this->db
->query( 'DROP FUNCTION IF EXISTS mw_test_function'
225 . ( $this->db
->getType() == 'postgres' ?
'()' : '' )
229 function testUnknownTableCorruptsResults() {
230 $res = $this->db
->select( 'page', '*', array( 'page_id' => 1 ) );
231 $this->assertFalse( $this->db
->tableExists( 'foobarbaz' ) );
232 $this->assertInternalType( 'int', $res->numRows() );