7 class DatabaseTest
extends MediaWikiTestCase
{
8 var $db, $functionTest = false;
11 $this->db
= wfGetDB( DB_MASTER
);
15 if ( $this->functionTest
) {
16 $this->dropFunctions();
17 $this->functionTest
= false;
21 function testAddQuotesNull() {
23 if ( $this->db
->getType() === 'sqlite' ||
$this->db
->getType() === 'oracle' ) {
26 $this->assertEquals( $check, $this->db
->addQuotes( null ) );
29 function testAddQuotesInt() {
30 # returning just "1234" should be ok too, though...
34 $this->db
->addQuotes( 1234 ) );
37 function testAddQuotesFloat() {
38 # returning just "1234.5678" would be ok too, though
41 $this->db
->addQuotes( 1234.5678 ) );
44 function testAddQuotesString() {
47 $this->db
->addQuotes( 'string' ) );
50 function testAddQuotesStringQuote() {
51 $check = "'string''s cause trouble'";
52 if ( $this->db
->getType() === 'mysql' ) {
53 $check = "'string\'s cause trouble'";
57 $this->db
->addQuotes( "string's cause trouble" ) );
60 private function getSharedTableName( $table, $database, $prefix, $format = 'quoted' ) {
61 global $wgSharedDB, $wgSharedTables, $wgSharedPrefix;
63 $oldName = $wgSharedDB;
64 $oldTables = $wgSharedTables;
65 $oldPrefix = $wgSharedPrefix;
67 $wgSharedDB = $database;
68 $wgSharedTables = array( $table );
69 $wgSharedPrefix = $prefix;
71 $ret = $this->db
->tableName( $table, $format );
73 $wgSharedDB = $oldName;
74 $wgSharedTables = $oldTables;
75 $wgSharedPrefix = $oldPrefix;
80 private function prefixAndQuote( $table, $database = null, $prefix = null, $format = 'quoted' ) {
81 if ( $this->db
->getType() === 'sqlite' ||
$format !== 'quoted' ) {
83 } elseif ( $this->db
->getType() === 'mysql' ) {
89 if ( $database !== null ) {
90 $database = $quote . $database . $quote . '.';
93 if ( $prefix === null ) {
94 $prefix = $this->dbPrefix();
97 return $database . $quote . $prefix . $table . $quote;
100 function testTableNameLocal() {
102 $this->prefixAndQuote( 'tablename' ),
103 $this->db
->tableName( 'tablename' )
107 function testTableNameRawLocal() {
109 $this->prefixAndQuote( 'tablename', null, null, 'raw' ),
110 $this->db
->tableName( 'tablename', 'raw' )
114 function testTableNameShared() {
116 $this->prefixAndQuote( 'tablename', 'sharedatabase', 'sh_' ),
117 $this->getSharedTableName( 'tablename', 'sharedatabase', 'sh_' )
121 $this->prefixAndQuote( 'tablename', 'sharedatabase', null ),
122 $this->getSharedTableName( 'tablename', 'sharedatabase', null )
126 function testTableNameRawShared() {
128 $this->prefixAndQuote( 'tablename', 'sharedatabase', 'sh_', 'raw' ),
129 $this->getSharedTableName( 'tablename', 'sharedatabase', 'sh_', 'raw' )
133 $this->prefixAndQuote( 'tablename', 'sharedatabase', null, 'raw' ),
134 $this->getSharedTableName( 'tablename', 'sharedatabase', null, 'raw' )
138 function testTableNameForeign() {
140 $this->prefixAndQuote( 'tablename', 'databasename', '' ),
141 $this->db
->tableName( 'databasename.tablename' )
145 function testTableNameRawForeign() {
147 $this->prefixAndQuote( 'tablename', 'databasename', '', 'raw' ),
148 $this->db
->tableName( 'databasename.tablename', 'raw' )
152 function testFillPreparedEmpty() {
153 $sql = $this->db
->fillPrepared(
154 'SELECT * FROM interwiki', array() );
156 "SELECT * FROM interwiki",
160 function testFillPreparedQuestion() {
161 $sql = $this->db
->fillPrepared(
162 'SELECT * FROM cur WHERE cur_namespace=? AND cur_title=?',
163 array( 4, "Snicker's_paradox" ) );
165 $check = "SELECT * FROM cur WHERE cur_namespace='4' AND cur_title='Snicker''s_paradox'";
166 if ( $this->db
->getType() === 'mysql' ) {
167 $check = "SELECT * FROM cur WHERE cur_namespace='4' AND cur_title='Snicker\'s_paradox'";
169 $this->assertEquals( $check, $sql );
172 function testFillPreparedBang() {
173 $sql = $this->db
->fillPrepared(
174 'SELECT user_id FROM ! WHERE user_name=?',
175 array( '"user"', "Slash's Dot" ) );
177 $check = "SELECT user_id FROM \"user\" WHERE user_name='Slash''s Dot'";
178 if ( $this->db
->getType() === 'mysql' ) {
179 $check = "SELECT user_id FROM \"user\" WHERE user_name='Slash\'s Dot'";
181 $this->assertEquals( $check, $sql );
184 function testFillPreparedRaw() {
185 $sql = $this->db
->fillPrepared(
186 "SELECT * FROM cur WHERE cur_title='This_\\&_that,_WTF\\?\\!'",
187 array( '"user"', "Slash's Dot" ) );
189 "SELECT * FROM cur WHERE cur_title='This_&_that,_WTF?!'",
196 function testStoredFunctions() {
197 if ( !in_array( wfGetDB( DB_MASTER
)->getType(), array( 'mysql', 'postgres' ) ) ) {
198 $this->markTestSkipped( 'MySQL or Postgres required' );
201 $this->dropFunctions();
202 $this->functionTest
= true;
203 $this->assertTrue( $this->db
->sourceFile( "$IP/tests/phpunit/data/db/{$this->db->getType()}/functions.sql" ) );
204 $res = $this->db
->query( 'SELECT mw_test_function() AS test', __METHOD__
);
205 $this->assertEquals( 42, $res->fetchObject()->test
);
208 private function dropFunctions() {
209 $this->db
->query( 'DROP FUNCTION IF EXISTS mw_test_function'
210 . ( $this->db
->getType() == 'postgres' ?
'()' : '' )