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 function testFillPreparedEmpty() {
61 $sql = $this->db
->fillPrepared(
62 'SELECT * FROM interwiki', array() );
64 "SELECT * FROM interwiki",
68 function testFillPreparedQuestion() {
69 $sql = $this->db
->fillPrepared(
70 'SELECT * FROM cur WHERE cur_namespace=? AND cur_title=?',
71 array( 4, "Snicker's_paradox" ) );
73 $check = "SELECT * FROM cur WHERE cur_namespace='4' AND cur_title='Snicker''s_paradox'";
74 if ( $this->db
->getType() === 'mysql' ) {
75 $check = "SELECT * FROM cur WHERE cur_namespace='4' AND cur_title='Snicker\'s_paradox'";
77 $this->assertEquals( $check, $sql );
80 function testFillPreparedBang() {
81 $sql = $this->db
->fillPrepared(
82 'SELECT user_id FROM ! WHERE user_name=?',
83 array( '"user"', "Slash's Dot" ) );
85 $check = "SELECT user_id FROM \"user\" WHERE user_name='Slash''s Dot'";
86 if ( $this->db
->getType() === 'mysql' ) {
87 $check = "SELECT user_id FROM \"user\" WHERE user_name='Slash\'s Dot'";
89 $this->assertEquals( $check, $sql );
92 function testFillPreparedRaw() {
93 $sql = $this->db
->fillPrepared(
94 "SELECT * FROM cur WHERE cur_title='This_\\&_that,_WTF\\?\\!'",
95 array( '"user"', "Slash's Dot" ) );
97 "SELECT * FROM cur WHERE cur_title='This_&_that,_WTF?!'",
104 function testStoredFunctions() {
105 if ( !in_array( wfGetDB( DB_MASTER
)->getType(), array( 'mysql', 'postgres' ) ) ) {
106 $this->markTestSkipped( 'MySQL or Postgres required' );
109 $this->dropFunctions();
110 $this->functionTest
= true;
111 $this->assertTrue( $this->db
->sourceFile( "$IP/tests/phpunit/data/db/{$this->db->getType()}/functions.sql" ) );
112 $res = $this->db
->query( 'SELECT mw_test_function() AS test', __METHOD__
);
113 $this->assertEquals( 42, $res->fetchObject()->test
);
116 private function dropFunctions() {
117 $this->db
->query( 'DROP FUNCTION IF EXISTS mw_test_function'
118 . ( $this->db
->getType() == 'postgres' ?
'()' : '' )