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