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