test: coverage recording now needs to be explicit
[mediawiki.git] / tests / phpunit / includes / db / DatabaseTest.php
blobc9f5f5cc17477f9fedf8cea57a8235e6b1c4e342
1 <?php
3 /**
4 * @group Database
5 * @group DatabaseBase
6 */
7 class DatabaseTest extends MediaWikiTestCase {
8 var $db, $functionTest = false;
10 protected function setUp() {
11 parent::setUp();
12 $this->db = wfGetDB( DB_MASTER );
15 protected function tearDown() {
16 parent::tearDown();
17 if ( $this->functionTest ) {
18 $this->dropFunctions();
19 $this->functionTest = false;
23 function testAddQuotesNull() {
24 $check = "NULL";
25 if ( $this->db->getType() === 'sqlite' || $this->db->getType() === 'oracle' ) {
26 $check = "''";
28 $this->assertEquals( $check, $this->db->addQuotes( null ) );
31 function testAddQuotesInt() {
32 # returning just "1234" should be ok too, though...
33 # maybe
34 $this->assertEquals(
35 "'1234'",
36 $this->db->addQuotes( 1234 ) );
39 function testAddQuotesFloat() {
40 # returning just "1234.5678" would be ok too, though
41 $this->assertEquals(
42 "'1234.5678'",
43 $this->db->addQuotes( 1234.5678 ) );
46 function testAddQuotesString() {
47 $this->assertEquals(
48 "'string'",
49 $this->db->addQuotes( 'string' ) );
52 function testAddQuotesStringQuote() {
53 $check = "'string''s cause trouble'";
54 if ( $this->db->getType() === 'mysql' ) {
55 $check = "'string\'s cause trouble'";
57 $this->assertEquals(
58 $check,
59 $this->db->addQuotes( "string's cause trouble" ) );
62 private function getSharedTableName( $table, $database, $prefix, $format = 'quoted' ) {
63 global $wgSharedDB, $wgSharedTables, $wgSharedPrefix;
65 $oldName = $wgSharedDB;
66 $oldTables = $wgSharedTables;
67 $oldPrefix = $wgSharedPrefix;
69 $wgSharedDB = $database;
70 $wgSharedTables = array( $table );
71 $wgSharedPrefix = $prefix;
73 $ret = $this->db->tableName( $table, $format );
75 $wgSharedDB = $oldName;
76 $wgSharedTables = $oldTables;
77 $wgSharedPrefix = $oldPrefix;
79 return $ret;
82 private function prefixAndQuote( $table, $database = null, $prefix = null, $format = 'quoted' ) {
83 if ( $this->db->getType() === 'sqlite' || $format !== 'quoted' ) {
84 $quote = '';
85 } elseif ( $this->db->getType() === 'mysql' ) {
86 $quote = '`';
87 } else {
88 $quote = '"';
91 if ( $database !== null ) {
92 $database = $quote . $database . $quote . '.';
95 if ( $prefix === null ) {
96 $prefix = $this->dbPrefix();
99 return $database . $quote . $prefix . $table . $quote;
102 function testTableNameLocal() {
103 $this->assertEquals(
104 $this->prefixAndQuote( 'tablename' ),
105 $this->db->tableName( 'tablename' )
109 function testTableNameRawLocal() {
110 $this->assertEquals(
111 $this->prefixAndQuote( 'tablename', null, null, 'raw' ),
112 $this->db->tableName( 'tablename', 'raw' )
116 function testTableNameShared() {
117 $this->assertEquals(
118 $this->prefixAndQuote( 'tablename', 'sharedatabase', 'sh_' ),
119 $this->getSharedTableName( 'tablename', 'sharedatabase', 'sh_' )
122 $this->assertEquals(
123 $this->prefixAndQuote( 'tablename', 'sharedatabase', null ),
124 $this->getSharedTableName( 'tablename', 'sharedatabase', null )
128 function testTableNameRawShared() {
129 $this->assertEquals(
130 $this->prefixAndQuote( 'tablename', 'sharedatabase', 'sh_', 'raw' ),
131 $this->getSharedTableName( 'tablename', 'sharedatabase', 'sh_', 'raw' )
134 $this->assertEquals(
135 $this->prefixAndQuote( 'tablename', 'sharedatabase', null, 'raw' ),
136 $this->getSharedTableName( 'tablename', 'sharedatabase', null, 'raw' )
140 function testTableNameForeign() {
141 $this->assertEquals(
142 $this->prefixAndQuote( 'tablename', 'databasename', '' ),
143 $this->db->tableName( 'databasename.tablename' )
147 function testTableNameRawForeign() {
148 $this->assertEquals(
149 $this->prefixAndQuote( 'tablename', 'databasename', '', 'raw' ),
150 $this->db->tableName( 'databasename.tablename', 'raw' )
154 function testFillPreparedEmpty() {
155 $sql = $this->db->fillPrepared(
156 'SELECT * FROM interwiki', array() );
157 $this->assertEquals(
158 "SELECT * FROM interwiki",
159 $sql );
162 function testFillPreparedQuestion() {
163 $sql = $this->db->fillPrepared(
164 'SELECT * FROM cur WHERE cur_namespace=? AND cur_title=?',
165 array( 4, "Snicker's_paradox" ) );
167 $check = "SELECT * FROM cur WHERE cur_namespace='4' AND cur_title='Snicker''s_paradox'";
168 if ( $this->db->getType() === 'mysql' ) {
169 $check = "SELECT * FROM cur WHERE cur_namespace='4' AND cur_title='Snicker\'s_paradox'";
171 $this->assertEquals( $check, $sql );
174 function testFillPreparedBang() {
175 $sql = $this->db->fillPrepared(
176 'SELECT user_id FROM ! WHERE user_name=?',
177 array( '"user"', "Slash's Dot" ) );
179 $check = "SELECT user_id FROM \"user\" WHERE user_name='Slash''s Dot'";
180 if ( $this->db->getType() === 'mysql' ) {
181 $check = "SELECT user_id FROM \"user\" WHERE user_name='Slash\'s Dot'";
183 $this->assertEquals( $check, $sql );
186 function testFillPreparedRaw() {
187 $sql = $this->db->fillPrepared(
188 "SELECT * FROM cur WHERE cur_title='This_\\&_that,_WTF\\?\\!'",
189 array( '"user"', "Slash's Dot" ) );
190 $this->assertEquals(
191 "SELECT * FROM cur WHERE cur_title='This_&_that,_WTF?!'",
192 $sql );
195 function testStoredFunctions() {
196 if ( !in_array( wfGetDB( DB_MASTER )->getType(), array( 'mysql', 'postgres' ) ) ) {
197 $this->markTestSkipped( 'MySQL or Postgres required' );
199 global $IP;
200 $this->dropFunctions();
201 $this->functionTest = true;
202 $this->assertTrue( $this->db->sourceFile( "$IP/tests/phpunit/data/db/{$this->db->getType()}/functions.sql" ) );
203 $res = $this->db->query( 'SELECT mw_test_function() AS test', __METHOD__ );
204 $this->assertEquals( 42, $res->fetchObject()->test );
207 private function dropFunctions() {
208 $this->db->query( 'DROP FUNCTION IF EXISTS mw_test_function'
209 . ( $this->db->getType() == 'postgres' ? '()' : '' )
213 function testUnknownTableCorruptsResults() {
214 $res = $this->db->select( 'page', '*', array( 'page_id' => 1 ) );
215 $this->assertFalse( $this->db->tableExists( 'foobarbaz' ) );
216 $this->assertInternalType( 'int', $res->numRows() );