7 class DatabaseTest
extends MediaWikiTestCase
{
8 var $db, $functionTest = false;
10 protected function setUp() {
12 $this->db
= wfGetDB( DB_MASTER
);
15 protected function tearDown() {
17 if ( $this->functionTest
) {
18 $this->dropFunctions();
19 $this->functionTest
= false;
23 function testAddQuotesNull() {
25 if ( $this->db
->getType() === 'sqlite' ||
$this->db
->getType() === 'oracle' ) {
28 $this->assertEquals( $check, $this->db
->addQuotes( null ) );
31 function testAddQuotesInt() {
32 # returning just "1234" should be ok too, though...
36 $this->db
->addQuotes( 1234 ) );
39 function testAddQuotesFloat() {
40 # returning just "1234.5678" would be ok too, though
43 $this->db
->addQuotes( 1234.5678 ) );
46 function testAddQuotesString() {
49 $this->db
->addQuotes( 'string' ) );
52 function testAddQuotesStringQuote() {
53 $check = "'string''s cause trouble'";
54 if ( $this->db
->getType() === 'mysql' ) {
55 $check = "'string\'s cause trouble'";
59 $this->db
->addQuotes( "string's cause trouble" ) );
62 private function getSharedTableName( $table, $database, $prefix, $format = 'quoted' ) {
63 global $wgSharedDB, $wgSharedTables, $wgSharedPrefix;
65 $oldName = $wgSharedDB;
66 $oldTables = $wgSharedTables;
67 $oldPrefix = $wgSharedPrefix;
69 $wgSharedDB = $database;
70 $wgSharedTables = array( $table );
71 $wgSharedPrefix = $prefix;
73 $ret = $this->db
->tableName( $table, $format );
75 $wgSharedDB = $oldName;
76 $wgSharedTables = $oldTables;
77 $wgSharedPrefix = $oldPrefix;
82 private function prefixAndQuote( $table, $database = null, $prefix = null, $format = 'quoted' ) {
83 if ( $this->db
->getType() === 'sqlite' ||
$format !== 'quoted' ) {
85 } elseif ( $this->db
->getType() === 'mysql' ) {
87 } elseif ( $this->db
->getType() === 'oracle' ) {
93 if ( $database !== null ) {
94 if ( $this->db
->getType() === 'oracle' ) {
95 $database = $quote . $database . '.';
97 $database = $quote . $database . $quote . '.';
101 if ( $prefix === null ) {
102 $prefix = $this->dbPrefix();
105 if ( $this->db
->getType() === 'oracle' ) {
106 return strtoupper($database . $quote . $prefix . $table);
108 return $database . $quote . $prefix . $table . $quote;
112 function testTableNameLocal() {
114 $this->prefixAndQuote( 'tablename' ),
115 $this->db
->tableName( 'tablename' )
119 function testTableNameRawLocal() {
121 $this->prefixAndQuote( 'tablename', null, null, 'raw' ),
122 $this->db
->tableName( 'tablename', 'raw' )
126 function testTableNameShared() {
128 $this->prefixAndQuote( 'tablename', 'sharedatabase', 'sh_' ),
129 $this->getSharedTableName( 'tablename', 'sharedatabase', 'sh_' )
133 $this->prefixAndQuote( 'tablename', 'sharedatabase', null ),
134 $this->getSharedTableName( 'tablename', 'sharedatabase', null )
138 function testTableNameRawShared() {
140 $this->prefixAndQuote( 'tablename', 'sharedatabase', 'sh_', 'raw' ),
141 $this->getSharedTableName( 'tablename', 'sharedatabase', 'sh_', 'raw' )
145 $this->prefixAndQuote( 'tablename', 'sharedatabase', null, 'raw' ),
146 $this->getSharedTableName( 'tablename', 'sharedatabase', null, 'raw' )
150 function testTableNameForeign() {
152 $this->prefixAndQuote( 'tablename', 'databasename', '' ),
153 $this->db
->tableName( 'databasename.tablename' )
157 function testTableNameRawForeign() {
159 $this->prefixAndQuote( 'tablename', 'databasename', '', 'raw' ),
160 $this->db
->tableName( 'databasename.tablename', 'raw' )
164 function testFillPreparedEmpty() {
165 $sql = $this->db
->fillPrepared(
166 'SELECT * FROM interwiki', array() );
168 "SELECT * FROM interwiki",
172 function testFillPreparedQuestion() {
173 $sql = $this->db
->fillPrepared(
174 'SELECT * FROM cur WHERE cur_namespace=? AND cur_title=?',
175 array( 4, "Snicker's_paradox" ) );
177 $check = "SELECT * FROM cur WHERE cur_namespace='4' AND cur_title='Snicker''s_paradox'";
178 if ( $this->db
->getType() === 'mysql' ) {
179 $check = "SELECT * FROM cur WHERE cur_namespace='4' AND cur_title='Snicker\'s_paradox'";
181 $this->assertEquals( $check, $sql );
184 function testFillPreparedBang() {
185 $sql = $this->db
->fillPrepared(
186 'SELECT user_id FROM ! WHERE user_name=?',
187 array( '"user"', "Slash's Dot" ) );
189 $check = "SELECT user_id FROM \"user\" WHERE user_name='Slash''s Dot'";
190 if ( $this->db
->getType() === 'mysql' ) {
191 $check = "SELECT user_id FROM \"user\" WHERE user_name='Slash\'s Dot'";
193 $this->assertEquals( $check, $sql );
196 function testFillPreparedRaw() {
197 $sql = $this->db
->fillPrepared(
198 "SELECT * FROM cur WHERE cur_title='This_\\&_that,_WTF\\?\\!'",
199 array( '"user"', "Slash's Dot" ) );
201 "SELECT * FROM cur WHERE cur_title='This_&_that,_WTF?!'",
205 function testStoredFunctions() {
206 if ( !in_array( wfGetDB( DB_MASTER
)->getType(), array( 'mysql', 'postgres' ) ) ) {
207 $this->markTestSkipped( 'MySQL or Postgres required' );
210 $this->dropFunctions();
211 $this->functionTest
= true;
212 $this->assertTrue( $this->db
->sourceFile( "$IP/tests/phpunit/data/db/{$this->db->getType()}/functions.sql" ) );
213 $res = $this->db
->query( 'SELECT mw_test_function() AS test', __METHOD__
);
214 $this->assertEquals( 42, $res->fetchObject()->test
);
217 private function dropFunctions() {
218 $this->db
->query( 'DROP FUNCTION IF EXISTS mw_test_function'
219 . ( $this->db
->getType() == 'postgres' ?
'()' : '' )
223 function testUnknownTableCorruptsResults() {
224 $res = $this->db
->select( 'page', '*', array( 'page_id' => 1 ) );
225 $this->assertFalse( $this->db
->tableExists( 'foobarbaz' ) );
226 $this->assertInternalType( 'int', $res->numRows() );