c9f5f5cc17477f9fedf8cea57a8235e6b1c4e342
[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 } else {
88 $quote = '"';
89 }
90
91 if ( $database !== null ) {
92 $database = $quote . $database . $quote . '.';
93 }
94
95 if ( $prefix === null ) {
96 $prefix = $this->dbPrefix();
97 }
98
99 return $database . $quote . $prefix . $table . $quote;
100 }
101
102 function testTableNameLocal() {
103 $this->assertEquals(
104 $this->prefixAndQuote( 'tablename' ),
105 $this->db->tableName( 'tablename' )
106 );
107 }
108
109 function testTableNameRawLocal() {
110 $this->assertEquals(
111 $this->prefixAndQuote( 'tablename', null, null, 'raw' ),
112 $this->db->tableName( 'tablename', 'raw' )
113 );
114 }
115
116 function testTableNameShared() {
117 $this->assertEquals(
118 $this->prefixAndQuote( 'tablename', 'sharedatabase', 'sh_' ),
119 $this->getSharedTableName( 'tablename', 'sharedatabase', 'sh_' )
120 );
121
122 $this->assertEquals(
123 $this->prefixAndQuote( 'tablename', 'sharedatabase', null ),
124 $this->getSharedTableName( 'tablename', 'sharedatabase', null )
125 );
126 }
127
128 function testTableNameRawShared() {
129 $this->assertEquals(
130 $this->prefixAndQuote( 'tablename', 'sharedatabase', 'sh_', 'raw' ),
131 $this->getSharedTableName( 'tablename', 'sharedatabase', 'sh_', 'raw' )
132 );
133
134 $this->assertEquals(
135 $this->prefixAndQuote( 'tablename', 'sharedatabase', null, 'raw' ),
136 $this->getSharedTableName( 'tablename', 'sharedatabase', null, 'raw' )
137 );
138 }
139
140 function testTableNameForeign() {
141 $this->assertEquals(
142 $this->prefixAndQuote( 'tablename', 'databasename', '' ),
143 $this->db->tableName( 'databasename.tablename' )
144 );
145 }
146
147 function testTableNameRawForeign() {
148 $this->assertEquals(
149 $this->prefixAndQuote( 'tablename', 'databasename', '', 'raw' ),
150 $this->db->tableName( 'databasename.tablename', 'raw' )
151 );
152 }
153
154 function testFillPreparedEmpty() {
155 $sql = $this->db->fillPrepared(
156 'SELECT * FROM interwiki', array() );
157 $this->assertEquals(
158 "SELECT * FROM interwiki",
159 $sql );
160 }
161
162 function testFillPreparedQuestion() {
163 $sql = $this->db->fillPrepared(
164 'SELECT * FROM cur WHERE cur_namespace=? AND cur_title=?',
165 array( 4, "Snicker's_paradox" ) );
166
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'";
170 }
171 $this->assertEquals( $check, $sql );
172 }
173
174 function testFillPreparedBang() {
175 $sql = $this->db->fillPrepared(
176 'SELECT user_id FROM ! WHERE user_name=?',
177 array( '"user"', "Slash's Dot" ) );
178
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'";
182 }
183 $this->assertEquals( $check, $sql );
184 }
185
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 );
193 }
194
195 function testStoredFunctions() {
196 if ( !in_array( wfGetDB( DB_MASTER )->getType(), array( 'mysql', 'postgres' ) ) ) {
197 $this->markTestSkipped( 'MySQL or Postgres required' );
198 }
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 );
205 }
206
207 private function dropFunctions() {
208 $this->db->query( 'DROP FUNCTION IF EXISTS mw_test_function'
209 . ( $this->db->getType() == 'postgres' ? '()' : '' )
210 );
211 }
212
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() );
217 }
218 }