Merge "(bug 29898) Set cookie to force HTTPS from HTTP"
[lhc/web/wiklou.git] / tests / phpunit / includes / db / DatabaseTest.php
index e886fa8..dc12ba5 100644 (file)
@@ -2,17 +2,25 @@
 
 /**
  * @group Database
+ * @group DatabaseBase
  */
 class DatabaseTest extends MediaWikiTestCase {
-       var $db;
+       var $db, $functionTest = false;
 
        function setUp() {
-               $this->db = wfGetDB( DB_SLAVE );
+               $this->db = wfGetDB( DB_MASTER );
+       }
+
+       function tearDown() {
+               if ( $this->functionTest ) {
+                       $this->dropFunctions();
+                       $this->functionTest = false;
+               }
        }
 
        function testAddQuotesNull() {
                $check = "NULL";
-               if ( $this->db->getType() === 'sqlite' ) {
+               if ( $this->db->getType() === 'sqlite' || $this->db->getType() === 'oracle' ) {
                        $check = "''";
                }
                $this->assertEquals( $check, $this->db->addQuotes( null ) );
@@ -49,6 +57,98 @@ class DatabaseTest extends MediaWikiTestCase {
                        $this->db->addQuotes( "string's cause trouble" ) );
        }
 
+       private function getSharedTableName( $table, $database, $prefix, $format = 'quoted' ) {
+               global $wgSharedDB, $wgSharedTables, $wgSharedPrefix;
+
+               $oldName = $wgSharedDB;
+               $oldTables = $wgSharedTables;
+               $oldPrefix = $wgSharedPrefix;
+
+               $wgSharedDB = $database;
+               $wgSharedTables = array( $table );
+               $wgSharedPrefix = $prefix;
+
+               $ret = $this->db->tableName( $table, $format );
+
+               $wgSharedDB = $oldName;
+               $wgSharedTables = $oldTables;
+               $wgSharedPrefix = $oldPrefix;
+
+               return $ret;
+       }
+
+       private function prefixAndQuote( $table, $database = null, $prefix = null, $format = 'quoted' ) {
+               if ( $this->db->getType() === 'sqlite' || $format !== 'quoted' ) {
+                       $quote = '';
+               } elseif ( $this->db->getType() === 'mysql' ) {
+                       $quote = '`';
+               } else {
+                       $quote = '"';
+               }
+
+               if ( $database !== null ) {
+                       $database = $quote . $database . $quote . '.';
+               }
+
+               if ( $prefix === null ) {
+                       $prefix = $this->dbPrefix();
+               }
+
+               return $database . $quote . $prefix . $table . $quote;
+       }
+
+       function testTableNameLocal() {
+               $this->assertEquals(
+                       $this->prefixAndQuote( 'tablename' ),
+                       $this->db->tableName( 'tablename' )
+               );
+       }
+
+       function testTableNameRawLocal() {
+               $this->assertEquals(
+                       $this->prefixAndQuote( 'tablename', null, null, 'raw' ),
+                       $this->db->tableName( 'tablename', 'raw' )
+               );
+       }
+
+       function testTableNameShared() {
+               $this->assertEquals(
+                       $this->prefixAndQuote( 'tablename', 'sharedatabase', 'sh_' ),
+                       $this->getSharedTableName( 'tablename', 'sharedatabase', 'sh_' )
+               );
+
+               $this->assertEquals(
+                       $this->prefixAndQuote( 'tablename', 'sharedatabase', null ),
+                       $this->getSharedTableName( 'tablename', 'sharedatabase', null )
+               );
+       }
+
+       function testTableNameRawShared() {
+               $this->assertEquals(
+                       $this->prefixAndQuote( 'tablename', 'sharedatabase', 'sh_', 'raw' ),
+                       $this->getSharedTableName( 'tablename', 'sharedatabase', 'sh_', 'raw' )
+               );
+
+               $this->assertEquals(
+                       $this->prefixAndQuote( 'tablename', 'sharedatabase', null, 'raw' ),
+                       $this->getSharedTableName( 'tablename', 'sharedatabase', null, 'raw' )
+               );
+       }
+
+       function testTableNameForeign() {
+               $this->assertEquals(
+                       $this->prefixAndQuote( 'tablename', 'databasename', '' ),
+                       $this->db->tableName( 'databasename.tablename' )
+               );
+       }
+
+       function testTableNameRawForeign() {
+               $this->assertEquals(
+                       $this->prefixAndQuote( 'tablename', 'databasename', '', 'raw' ),
+                       $this->db->tableName( 'databasename.tablename', 'raw' )
+               );
+       }
+
        function testFillPreparedEmpty() {
                $sql = $this->db->fillPrepared(
                        'SELECT * FROM interwiki', array() );
@@ -90,32 +190,21 @@ class DatabaseTest extends MediaWikiTestCase {
                        $sql );
        }
 
-       function testMakeNotInList() {
-               $this->assertEquals(
-                       "field IN ('0','1')",
-                       $this->db->makeList( array(
-                               'field' => array( 0, 1 )
-                       ), LIST_AND )
-               );
-               $this->assertEquals(
-                       "field NOT IN ('0','1')",
-                       $this->db->makeList( array(
-                               'field!' => array( 0, 1 )
-                       ), LIST_AND )
-               );
+       function testStoredFunctions() {
+               if ( !in_array( wfGetDB( DB_MASTER )->getType(), array( 'mysql', 'postgres' ) ) ) {
+                       $this->markTestSkipped( 'MySQL or Postgres required' );
+               }
+               global $IP;
+               $this->dropFunctions();
+               $this->functionTest = true;
+               $this->assertTrue( $this->db->sourceFile( "$IP/tests/phpunit/data/db/{$this->db->getType()}/functions.sql" ) );
+               $res = $this->db->query( 'SELECT mw_test_function() AS test', __METHOD__ );
+               $this->assertEquals( 42, $res->fetchObject()->test );
+       }
 
-               // make sure an array with only one value use = or !=
-               $this->assertEquals(
-                       "field = '777'",
-                       $this->db->makeList( array(
-                               'field' => array( 777 )
-                       ), LIST_AND )
-               );
-               $this->assertEquals(
-                       "field != '888'",
-                       $this->db->makeList( array(
-                               'field!' => array( 888 )
-                       ), LIST_AND )
+       private function dropFunctions() {
+               $this->db->query( 'DROP FUNCTION IF EXISTS mw_test_function'
+                       . ( $this->db->getType() == 'postgres'  ? '()' : '' )
                );
        }
 }