2010-12-14 16:26:35 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @group Database
|
2012-01-11 20:19:55 +00:00
|
|
|
* @group DatabaseBase
|
2010-12-14 16:26:35 +00:00
|
|
|
*/
|
2010-12-28 18:17:16 +00:00
|
|
|
class DatabaseTest extends MediaWikiTestCase {
|
2013-10-18 10:36:09 +00:00
|
|
|
/**
|
|
|
|
|
* @var DatabaseBase
|
|
|
|
|
*/
|
2014-04-24 16:06:21 +00:00
|
|
|
protected $db;
|
|
|
|
|
|
|
|
|
|
private $functionTest = false;
|
2010-12-14 16:26:35 +00:00
|
|
|
|
2012-10-08 10:56:20 +00:00
|
|
|
protected function setUp() {
|
2012-10-23 17:02:36 +00:00
|
|
|
parent::setUp();
|
2012-01-11 20:19:55 +00:00
|
|
|
$this->db = wfGetDB( DB_MASTER );
|
|
|
|
|
}
|
|
|
|
|
|
2012-10-08 10:56:20 +00:00
|
|
|
protected function tearDown() {
|
2012-10-23 17:02:36 +00:00
|
|
|
parent::tearDown();
|
2012-01-11 20:19:55 +00:00
|
|
|
if ( $this->functionTest ) {
|
|
|
|
|
$this->dropFunctions();
|
|
|
|
|
$this->functionTest = false;
|
|
|
|
|
}
|
2010-12-14 16:26:35 +00:00
|
|
|
}
|
2013-10-18 10:36:09 +00:00
|
|
|
/**
|
|
|
|
|
* @covers DatabaseBase::dropTable
|
|
|
|
|
*/
|
2013-10-23 22:51:31 +00:00
|
|
|
public function testAddQuotesNull() {
|
2010-12-14 16:26:35 +00:00
|
|
|
$check = "NULL";
|
2011-06-09 08:43:53 +00:00
|
|
|
if ( $this->db->getType() === 'sqlite' || $this->db->getType() === 'oracle' ) {
|
2010-12-14 16:26:35 +00:00
|
|
|
$check = "''";
|
|
|
|
|
}
|
|
|
|
|
$this->assertEquals( $check, $this->db->addQuotes( null ) );
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-23 22:51:31 +00:00
|
|
|
public function testAddQuotesInt() {
|
2010-12-14 16:26:35 +00:00
|
|
|
# returning just "1234" should be ok too, though...
|
|
|
|
|
# maybe
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
"'1234'",
|
|
|
|
|
$this->db->addQuotes( 1234 ) );
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-23 22:51:31 +00:00
|
|
|
public function testAddQuotesFloat() {
|
2010-12-14 16:26:35 +00:00
|
|
|
# returning just "1234.5678" would be ok too, though
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
"'1234.5678'",
|
|
|
|
|
$this->db->addQuotes( 1234.5678 ) );
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-23 22:51:31 +00:00
|
|
|
public function testAddQuotesString() {
|
2010-12-14 16:26:35 +00:00
|
|
|
$this->assertEquals(
|
|
|
|
|
"'string'",
|
|
|
|
|
$this->db->addQuotes( 'string' ) );
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-23 22:51:31 +00:00
|
|
|
public function testAddQuotesStringQuote() {
|
2010-12-14 16:26:35 +00:00
|
|
|
$check = "'string''s cause trouble'";
|
|
|
|
|
if ( $this->db->getType() === 'mysql' ) {
|
|
|
|
|
$check = "'string\'s cause trouble'";
|
|
|
|
|
}
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
$check,
|
|
|
|
|
$this->db->addQuotes( "string's cause trouble" ) );
|
|
|
|
|
}
|
|
|
|
|
|
2012-07-18 08:18:49 +00:00
|
|
|
private function getSharedTableName( $table, $database, $prefix, $format = 'quoted' ) {
|
|
|
|
|
global $wgSharedDB, $wgSharedTables, $wgSharedPrefix;
|
|
|
|
|
|
|
|
|
|
$oldName = $wgSharedDB;
|
|
|
|
|
$oldTables = $wgSharedTables;
|
|
|
|
|
$oldPrefix = $wgSharedPrefix;
|
|
|
|
|
|
|
|
|
|
$wgSharedDB = $database;
|
2016-02-17 09:09:32 +00:00
|
|
|
$wgSharedTables = [ $table ];
|
2012-07-18 08:18:49 +00:00
|
|
|
$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 = '`';
|
2013-05-26 14:26:41 +00:00
|
|
|
} elseif ( $this->db->getType() === 'oracle' ) {
|
|
|
|
|
$quote = '/*Q*/';
|
2012-07-18 08:18:49 +00:00
|
|
|
} else {
|
|
|
|
|
$quote = '"';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( $database !== null ) {
|
2013-05-26 14:26:41 +00:00
|
|
|
if ( $this->db->getType() === 'oracle' ) {
|
|
|
|
|
$database = $quote . $database . '.';
|
|
|
|
|
} else {
|
|
|
|
|
$database = $quote . $database . $quote . '.';
|
|
|
|
|
}
|
2012-07-18 08:18:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( $prefix === null ) {
|
|
|
|
|
$prefix = $this->dbPrefix();
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-26 14:26:41 +00:00
|
|
|
if ( $this->db->getType() === 'oracle' ) {
|
2013-11-19 18:03:54 +00:00
|
|
|
return strtoupper( $database . $quote . $prefix . $table );
|
2013-05-26 14:26:41 +00:00
|
|
|
} else {
|
|
|
|
|
return $database . $quote . $prefix . $table . $quote;
|
|
|
|
|
}
|
2012-07-18 08:18:49 +00:00
|
|
|
}
|
|
|
|
|
|
2013-10-23 22:51:31 +00:00
|
|
|
public function testTableNameLocal() {
|
2012-07-18 08:18:49 +00:00
|
|
|
$this->assertEquals(
|
|
|
|
|
$this->prefixAndQuote( 'tablename' ),
|
|
|
|
|
$this->db->tableName( 'tablename' )
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-23 22:51:31 +00:00
|
|
|
public function testTableNameRawLocal() {
|
2012-07-18 08:18:49 +00:00
|
|
|
$this->assertEquals(
|
|
|
|
|
$this->prefixAndQuote( 'tablename', null, null, 'raw' ),
|
|
|
|
|
$this->db->tableName( 'tablename', 'raw' )
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-23 22:51:31 +00:00
|
|
|
public function testTableNameShared() {
|
2012-07-18 08:18:49 +00:00
|
|
|
$this->assertEquals(
|
|
|
|
|
$this->prefixAndQuote( 'tablename', 'sharedatabase', 'sh_' ),
|
|
|
|
|
$this->getSharedTableName( 'tablename', 'sharedatabase', 'sh_' )
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
$this->prefixAndQuote( 'tablename', 'sharedatabase', null ),
|
|
|
|
|
$this->getSharedTableName( 'tablename', 'sharedatabase', null )
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-23 22:51:31 +00:00
|
|
|
public function testTableNameRawShared() {
|
2012-07-18 08:18:49 +00:00
|
|
|
$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' )
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-23 22:51:31 +00:00
|
|
|
public function testTableNameForeign() {
|
2012-07-18 08:18:49 +00:00
|
|
|
$this->assertEquals(
|
|
|
|
|
$this->prefixAndQuote( 'tablename', 'databasename', '' ),
|
|
|
|
|
$this->db->tableName( 'databasename.tablename' )
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-23 22:51:31 +00:00
|
|
|
public function testTableNameRawForeign() {
|
2012-07-18 08:18:49 +00:00
|
|
|
$this->assertEquals(
|
|
|
|
|
$this->prefixAndQuote( 'tablename', 'databasename', '', 'raw' ),
|
|
|
|
|
$this->db->tableName( 'databasename.tablename', 'raw' )
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-23 22:51:31 +00:00
|
|
|
public function testFillPreparedEmpty() {
|
2010-12-14 16:26:35 +00:00
|
|
|
$sql = $this->db->fillPrepared(
|
2016-02-17 09:09:32 +00:00
|
|
|
'SELECT * FROM interwiki', [] );
|
2010-12-14 16:26:35 +00:00
|
|
|
$this->assertEquals(
|
|
|
|
|
"SELECT * FROM interwiki",
|
|
|
|
|
$sql );
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-23 22:51:31 +00:00
|
|
|
public function testFillPreparedQuestion() {
|
2010-12-14 16:26:35 +00:00
|
|
|
$sql = $this->db->fillPrepared(
|
|
|
|
|
'SELECT * FROM cur WHERE cur_namespace=? AND cur_title=?',
|
2016-02-17 09:09:32 +00:00
|
|
|
[ 4, "Snicker's_paradox" ] );
|
2010-12-14 16:26:35 +00:00
|
|
|
|
|
|
|
|
$check = "SELECT * FROM cur WHERE cur_namespace='4' AND cur_title='Snicker''s_paradox'";
|
|
|
|
|
if ( $this->db->getType() === 'mysql' ) {
|
|
|
|
|
$check = "SELECT * FROM cur WHERE cur_namespace='4' AND cur_title='Snicker\'s_paradox'";
|
|
|
|
|
}
|
|
|
|
|
$this->assertEquals( $check, $sql );
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-23 22:51:31 +00:00
|
|
|
public function testFillPreparedBang() {
|
2010-12-14 16:26:35 +00:00
|
|
|
$sql = $this->db->fillPrepared(
|
|
|
|
|
'SELECT user_id FROM ! WHERE user_name=?',
|
2016-02-17 09:09:32 +00:00
|
|
|
[ '"user"', "Slash's Dot" ] );
|
2010-12-14 16:26:35 +00:00
|
|
|
|
|
|
|
|
$check = "SELECT user_id FROM \"user\" WHERE user_name='Slash''s Dot'";
|
|
|
|
|
if ( $this->db->getType() === 'mysql' ) {
|
|
|
|
|
$check = "SELECT user_id FROM \"user\" WHERE user_name='Slash\'s Dot'";
|
|
|
|
|
}
|
|
|
|
|
$this->assertEquals( $check, $sql );
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-23 22:51:31 +00:00
|
|
|
public function testFillPreparedRaw() {
|
2010-12-14 16:26:35 +00:00
|
|
|
$sql = $this->db->fillPrepared(
|
|
|
|
|
"SELECT * FROM cur WHERE cur_title='This_\\&_that,_WTF\\?\\!'",
|
2016-02-17 09:09:32 +00:00
|
|
|
[ '"user"', "Slash's Dot" ] );
|
2010-12-14 16:26:35 +00:00
|
|
|
$this->assertEquals(
|
|
|
|
|
"SELECT * FROM cur WHERE cur_title='This_&_that,_WTF?!'",
|
|
|
|
|
$sql );
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-23 22:51:31 +00:00
|
|
|
public function testStoredFunctions() {
|
2016-02-17 09:09:32 +00:00
|
|
|
if ( !in_array( wfGetDB( DB_MASTER )->getType(), [ 'mysql', 'postgres' ] ) ) {
|
2012-01-11 20:19:55 +00:00
|
|
|
$this->markTestSkipped( 'MySQL or Postgres required' );
|
|
|
|
|
}
|
|
|
|
|
global $IP;
|
|
|
|
|
$this->dropFunctions();
|
|
|
|
|
$this->functionTest = true;
|
2014-04-24 16:06:21 +00:00
|
|
|
$this->assertTrue(
|
|
|
|
|
$this->db->sourceFile( "$IP/tests/phpunit/data/db/{$this->db->getType()}/functions.sql" )
|
|
|
|
|
);
|
2012-01-11 20:19:55 +00:00
|
|
|
$res = $this->db->query( 'SELECT mw_test_function() AS test', __METHOD__ );
|
|
|
|
|
$this->assertEquals( 42, $res->fetchObject()->test );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function dropFunctions() {
|
|
|
|
|
$this->db->query( 'DROP FUNCTION IF EXISTS mw_test_function'
|
2013-02-14 13:10:38 +00:00
|
|
|
. ( $this->db->getType() == 'postgres' ? '()' : '' )
|
2012-01-11 20:19:55 +00:00
|
|
|
);
|
|
|
|
|
}
|
2012-11-26 13:34:20 +00:00
|
|
|
|
2013-10-23 22:51:31 +00:00
|
|
|
public function testUnknownTableCorruptsResults() {
|
2016-02-17 09:09:32 +00:00
|
|
|
$res = $this->db->select( 'page', '*', [ 'page_id' => 1 ] );
|
2012-11-26 13:34:20 +00:00
|
|
|
$this->assertFalse( $this->db->tableExists( 'foobarbaz' ) );
|
|
|
|
|
$this->assertInternalType( 'int', $res->numRows() );
|
|
|
|
|
}
|
2015-12-08 01:26:15 +00:00
|
|
|
|
|
|
|
|
public function testTransactionIdle() {
|
|
|
|
|
$db = $this->db;
|
|
|
|
|
|
|
|
|
|
$db->setFlag( DBO_TRX );
|
2016-07-04 18:02:42 +00:00
|
|
|
$called = false;
|
2015-12-08 01:26:15 +00:00
|
|
|
$flagSet = null;
|
2016-07-04 18:02:42 +00:00
|
|
|
$db->onTransactionIdle( function() use ( $db, &$flagSet, &$called ) {
|
|
|
|
|
$called = true;
|
2015-12-08 01:26:15 +00:00
|
|
|
$flagSet = $db->getFlag( DBO_TRX );
|
|
|
|
|
} );
|
|
|
|
|
$this->assertFalse( $flagSet, 'DBO_TRX off in callback' );
|
|
|
|
|
$this->assertTrue( $db->getFlag( DBO_TRX ), 'DBO_TRX restored to default' );
|
2016-07-04 18:02:42 +00:00
|
|
|
$this->assertTrue( $called, 'Callback reached' );
|
2015-12-08 01:26:15 +00:00
|
|
|
|
|
|
|
|
$db->clearFlag( DBO_TRX );
|
|
|
|
|
$flagSet = null;
|
|
|
|
|
$db->onTransactionIdle( function() use ( $db, &$flagSet ) {
|
|
|
|
|
$flagSet = $db->getFlag( DBO_TRX );
|
|
|
|
|
} );
|
|
|
|
|
$this->assertFalse( $flagSet, 'DBO_TRX off in callback' );
|
|
|
|
|
$this->assertFalse( $db->getFlag( DBO_TRX ), 'DBO_TRX restored to default' );
|
|
|
|
|
|
|
|
|
|
$db->clearFlag( DBO_TRX );
|
|
|
|
|
$db->onTransactionIdle( function() use ( $db ) {
|
|
|
|
|
$db->setFlag( DBO_TRX );
|
|
|
|
|
} );
|
|
|
|
|
$this->assertFalse( $db->getFlag( DBO_TRX ), 'DBO_TRX restored to default' );
|
|
|
|
|
}
|
2016-07-04 18:02:42 +00:00
|
|
|
|
|
|
|
|
public function testTransactionResolution() {
|
|
|
|
|
$db = $this->db;
|
|
|
|
|
|
|
|
|
|
$db->clearFlag( DBO_TRX );
|
|
|
|
|
$db->begin( __METHOD__ );
|
|
|
|
|
$called = false;
|
|
|
|
|
$db->onTransactionResolution( function() use ( $db, &$called ) {
|
|
|
|
|
$called = true;
|
|
|
|
|
$db->setFlag( DBO_TRX );
|
|
|
|
|
} );
|
|
|
|
|
$db->commit( __METHOD__ );
|
|
|
|
|
$this->assertFalse( $db->getFlag( DBO_TRX ), 'DBO_TRX restored to default' );
|
|
|
|
|
$this->assertTrue( $called, 'Callback reached' );
|
|
|
|
|
|
|
|
|
|
$db->clearFlag( DBO_TRX );
|
|
|
|
|
$db->begin( __METHOD__ );
|
|
|
|
|
$called = false;
|
|
|
|
|
$db->onTransactionResolution( function() use ( $db, &$called ) {
|
|
|
|
|
$called = true;
|
|
|
|
|
$db->setFlag( DBO_TRX );
|
|
|
|
|
} );
|
|
|
|
|
$db->rollback( __METHOD__ );
|
|
|
|
|
$this->assertFalse( $db->getFlag( DBO_TRX ), 'DBO_TRX restored to default' );
|
|
|
|
|
$this->assertTrue( $called, 'Callback reached' );
|
|
|
|
|
}
|
2010-12-14 16:26:35 +00:00
|
|
|
}
|