Add @covers for main Database test types
[lhc/web/wiklou.git] / tests / phpunit / includes / db / DatabaseTest.php
1 <?php
2
3 /**
4 * @group Database
5 * @group DatabaseBase
6 */
7 class DatabaseTest extends MediaWikiTestCase {
8 /**
9 * @var DatabaseBase
10 */
11 var $db;
12 var $functionTest = false;
13
14 protected function setUp() {
15 parent::setUp();
16 $this->db = wfGetDB( DB_MASTER );
17 }
18
19 protected function tearDown() {
20 parent::tearDown();
21 if ( $this->functionTest ) {
22 $this->dropFunctions();
23 $this->functionTest = false;
24 }
25 }
26 /**
27 * @covers DatabaseBase::dropTable
28 */
29 function testAddQuotesNull() {
30 $check = "NULL";
31 if ( $this->db->getType() === 'sqlite' || $this->db->getType() === 'oracle' ) {
32 $check = "''";
33 }
34 $this->assertEquals( $check, $this->db->addQuotes( null ) );
35 }
36
37 function testAddQuotesInt() {
38 # returning just "1234" should be ok too, though...
39 # maybe
40 $this->assertEquals(
41 "'1234'",
42 $this->db->addQuotes( 1234 ) );
43 }
44
45 function testAddQuotesFloat() {
46 # returning just "1234.5678" would be ok too, though
47 $this->assertEquals(
48 "'1234.5678'",
49 $this->db->addQuotes( 1234.5678 ) );
50 }
51
52 function testAddQuotesString() {
53 $this->assertEquals(
54 "'string'",
55 $this->db->addQuotes( 'string' ) );
56 }
57
58 function testAddQuotesStringQuote() {
59 $check = "'string''s cause trouble'";
60 if ( $this->db->getType() === 'mysql' ) {
61 $check = "'string\'s cause trouble'";
62 }
63 $this->assertEquals(
64 $check,
65 $this->db->addQuotes( "string's cause trouble" ) );
66 }
67
68 private function getSharedTableName( $table, $database, $prefix, $format = 'quoted' ) {
69 global $wgSharedDB, $wgSharedTables, $wgSharedPrefix;
70
71 $oldName = $wgSharedDB;
72 $oldTables = $wgSharedTables;
73 $oldPrefix = $wgSharedPrefix;
74
75 $wgSharedDB = $database;
76 $wgSharedTables = array( $table );
77 $wgSharedPrefix = $prefix;
78
79 $ret = $this->db->tableName( $table, $format );
80
81 $wgSharedDB = $oldName;
82 $wgSharedTables = $oldTables;
83 $wgSharedPrefix = $oldPrefix;
84
85 return $ret;
86 }
87
88 private function prefixAndQuote( $table, $database = null, $prefix = null, $format = 'quoted' ) {
89 if ( $this->db->getType() === 'sqlite' || $format !== 'quoted' ) {
90 $quote = '';
91 } elseif ( $this->db->getType() === 'mysql' ) {
92 $quote = '`';
93 } elseif ( $this->db->getType() === 'oracle' ) {
94 $quote = '/*Q*/';
95 } else {
96 $quote = '"';
97 }
98
99 if ( $database !== null ) {
100 if ( $this->db->getType() === 'oracle' ) {
101 $database = $quote . $database . '.';
102 } else {
103 $database = $quote . $database . $quote . '.';
104 }
105 }
106
107 if ( $prefix === null ) {
108 $prefix = $this->dbPrefix();
109 }
110
111 if ( $this->db->getType() === 'oracle' ) {
112 return strtoupper($database . $quote . $prefix . $table);
113 } else {
114 return $database . $quote . $prefix . $table . $quote;
115 }
116 }
117
118 function testTableNameLocal() {
119 $this->assertEquals(
120 $this->prefixAndQuote( 'tablename' ),
121 $this->db->tableName( 'tablename' )
122 );
123 }
124
125 function testTableNameRawLocal() {
126 $this->assertEquals(
127 $this->prefixAndQuote( 'tablename', null, null, 'raw' ),
128 $this->db->tableName( 'tablename', 'raw' )
129 );
130 }
131
132 function testTableNameShared() {
133 $this->assertEquals(
134 $this->prefixAndQuote( 'tablename', 'sharedatabase', 'sh_' ),
135 $this->getSharedTableName( 'tablename', 'sharedatabase', 'sh_' )
136 );
137
138 $this->assertEquals(
139 $this->prefixAndQuote( 'tablename', 'sharedatabase', null ),
140 $this->getSharedTableName( 'tablename', 'sharedatabase', null )
141 );
142 }
143
144 function testTableNameRawShared() {
145 $this->assertEquals(
146 $this->prefixAndQuote( 'tablename', 'sharedatabase', 'sh_', 'raw' ),
147 $this->getSharedTableName( 'tablename', 'sharedatabase', 'sh_', 'raw' )
148 );
149
150 $this->assertEquals(
151 $this->prefixAndQuote( 'tablename', 'sharedatabase', null, 'raw' ),
152 $this->getSharedTableName( 'tablename', 'sharedatabase', null, 'raw' )
153 );
154 }
155
156 function testTableNameForeign() {
157 $this->assertEquals(
158 $this->prefixAndQuote( 'tablename', 'databasename', '' ),
159 $this->db->tableName( 'databasename.tablename' )
160 );
161 }
162
163 function testTableNameRawForeign() {
164 $this->assertEquals(
165 $this->prefixAndQuote( 'tablename', 'databasename', '', 'raw' ),
166 $this->db->tableName( 'databasename.tablename', 'raw' )
167 );
168 }
169
170 function testFillPreparedEmpty() {
171 $sql = $this->db->fillPrepared(
172 'SELECT * FROM interwiki', array() );
173 $this->assertEquals(
174 "SELECT * FROM interwiki",
175 $sql );
176 }
177
178 function testFillPreparedQuestion() {
179 $sql = $this->db->fillPrepared(
180 'SELECT * FROM cur WHERE cur_namespace=? AND cur_title=?',
181 array( 4, "Snicker's_paradox" ) );
182
183 $check = "SELECT * FROM cur WHERE cur_namespace='4' AND cur_title='Snicker''s_paradox'";
184 if ( $this->db->getType() === 'mysql' ) {
185 $check = "SELECT * FROM cur WHERE cur_namespace='4' AND cur_title='Snicker\'s_paradox'";
186 }
187 $this->assertEquals( $check, $sql );
188 }
189
190 function testFillPreparedBang() {
191 $sql = $this->db->fillPrepared(
192 'SELECT user_id FROM ! WHERE user_name=?',
193 array( '"user"', "Slash's Dot" ) );
194
195 $check = "SELECT user_id FROM \"user\" WHERE user_name='Slash''s Dot'";
196 if ( $this->db->getType() === 'mysql' ) {
197 $check = "SELECT user_id FROM \"user\" WHERE user_name='Slash\'s Dot'";
198 }
199 $this->assertEquals( $check, $sql );
200 }
201
202 function testFillPreparedRaw() {
203 $sql = $this->db->fillPrepared(
204 "SELECT * FROM cur WHERE cur_title='This_\\&_that,_WTF\\?\\!'",
205 array( '"user"', "Slash's Dot" ) );
206 $this->assertEquals(
207 "SELECT * FROM cur WHERE cur_title='This_&_that,_WTF?!'",
208 $sql );
209 }
210
211 function testStoredFunctions() {
212 if ( !in_array( wfGetDB( DB_MASTER )->getType(), array( 'mysql', 'postgres' ) ) ) {
213 $this->markTestSkipped( 'MySQL or Postgres required' );
214 }
215 global $IP;
216 $this->dropFunctions();
217 $this->functionTest = true;
218 $this->assertTrue( $this->db->sourceFile( "$IP/tests/phpunit/data/db/{$this->db->getType()}/functions.sql" ) );
219 $res = $this->db->query( 'SELECT mw_test_function() AS test', __METHOD__ );
220 $this->assertEquals( 42, $res->fetchObject()->test );
221 }
222
223 private function dropFunctions() {
224 $this->db->query( 'DROP FUNCTION IF EXISTS mw_test_function'
225 . ( $this->db->getType() == 'postgres' ? '()' : '' )
226 );
227 }
228
229 function testUnknownTableCorruptsResults() {
230 $res = $this->db->select( 'page', '*', array( 'page_id' => 1 ) );
231 $this->assertFalse( $this->db->tableExists( 'foobarbaz' ) );
232 $this->assertInternalType( 'int', $res->numRows() );
233 }
234 }