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