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