MediaWiki  1.23.8
DatabaseTest.php
Go to the documentation of this file.
1 <?php
2 
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  }
29  public 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  public 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  public 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  public function testAddQuotesString() {
53  $this->assertEquals(
54  "'string'",
55  $this->db->addQuotes( 'string' ) );
56  }
57 
58  public 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  public function testTableNameLocal() {
119  $this->assertEquals(
120  $this->prefixAndQuote( 'tablename' ),
121  $this->db->tableName( 'tablename' )
122  );
123  }
124 
125  public function testTableNameRawLocal() {
126  $this->assertEquals(
127  $this->prefixAndQuote( 'tablename', null, null, 'raw' ),
128  $this->db->tableName( 'tablename', 'raw' )
129  );
130  }
131 
132  public 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  public 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  public function testTableNameForeign() {
157  $this->assertEquals(
158  $this->prefixAndQuote( 'tablename', 'databasename', '' ),
159  $this->db->tableName( 'databasename.tablename' )
160  );
161  }
162 
163  public function testTableNameRawForeign() {
164  $this->assertEquals(
165  $this->prefixAndQuote( 'tablename', 'databasename', '', 'raw' ),
166  $this->db->tableName( 'databasename.tablename', 'raw' )
167  );
168  }
169 
170  public function testFillPreparedEmpty() {
171  $sql = $this->db->fillPrepared(
172  'SELECT * FROM interwiki', array() );
173  $this->assertEquals(
174  "SELECT * FROM interwiki",
175  $sql );
176  }
177 
178  public 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  public 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  public 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  public 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  public 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 }
DatabaseTest\testTableNameLocal
testTableNameLocal()
Definition: DatabaseTest.php:117
DatabaseTest\testFillPreparedEmpty
testFillPreparedEmpty()
Definition: DatabaseTest.php:169
DatabaseTest\dropFunctions
dropFunctions()
Definition: DatabaseTest.php:222
DatabaseTest\prefixAndQuote
prefixAndQuote( $table, $database=null, $prefix=null, $format='quoted')
Definition: DatabaseTest.php:87
DatabaseTest\testAddQuotesFloat
testAddQuotesFloat()
Definition: DatabaseTest.php:44
DB_MASTER
const DB_MASTER
Definition: Defines.php:56
DatabaseTest\testTableNameRawShared
testTableNameRawShared()
Definition: DatabaseTest.php:143
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
DatabaseTest\setUp
setUp()
Definition: DatabaseTest.php:13
DatabaseTest\testTableNameRawLocal
testTableNameRawLocal()
Definition: DatabaseTest.php:124
DatabaseTest\testFillPreparedRaw
testFillPreparedRaw()
Definition: DatabaseTest.php:201
wfGetDB
& wfGetDB( $db, $groups=array(), $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:3659
DatabaseTest\testAddQuotesNull
testAddQuotesNull()
@covers DatabaseBase::dropTable
Definition: DatabaseTest.php:28
$ret
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses & $ret
Definition: hooks.txt:1530
DatabaseTest\testFillPreparedBang
testFillPreparedBang()
Definition: DatabaseTest.php:189
DatabaseTest\testTableNameForeign
testTableNameForeign()
Definition: DatabaseTest.php:155
DatabaseTest\testUnknownTableCorruptsResults
testUnknownTableCorruptsResults()
Definition: DatabaseTest.php:228
DatabaseTest\testAddQuotesString
testAddQuotesString()
Definition: DatabaseTest.php:51
DatabaseTest\$functionTest
$functionTest
Definition: DatabaseTest.php:11
DatabaseTest\testAddQuotesInt
testAddQuotesInt()
Definition: DatabaseTest.php:36
DatabaseTest\testTableNameRawForeign
testTableNameRawForeign()
Definition: DatabaseTest.php:162
MediaWikiTestCase
Definition: MediaWikiTestCase.php:6
DatabaseTest\testStoredFunctions
testStoredFunctions()
Definition: DatabaseTest.php:210
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:93
DatabaseTest\$db
DatabaseBase $db
Definition: DatabaseTest.php:10
MediaWikiTestCase\dbPrefix
dbPrefix()
Definition: MediaWikiTestCase.php:391
DatabaseBase
Database abstraction object.
Definition: Database.php:219
DatabaseTest\tearDown
tearDown()
Definition: DatabaseTest.php:18
DatabaseTest
@group Database @group DatabaseBase
Definition: DatabaseTest.php:7
DatabaseTest\testAddQuotesStringQuote
testAddQuotesStringQuote()
Definition: DatabaseTest.php:57
$IP
$IP
Definition: WebStart.php:88
$res
$res
Definition: database.txt:21
DatabaseTest\getSharedTableName
getSharedTableName( $table, $database, $prefix, $format='quoted')
Definition: DatabaseTest.php:67
DatabaseTest\testTableNameShared
testTableNameShared()
Definition: DatabaseTest.php:131
DatabaseTest\testFillPreparedQuestion
testFillPreparedQuestion()
Definition: DatabaseTest.php:177