MediaWiki  1.27.2
DatabaseTest.php
Go to the documentation of this file.
1 <?php
2 
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  }
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' ) {
71 
72  $oldName = $wgSharedDB;
73  $oldTables = $wgSharedTables;
74  $oldPrefix = $wgSharedPrefix;
75 
76  $wgSharedDB = $database;
77  $wgSharedTables = [ $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', [] );
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  [ 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  [ '"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  [ '"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(), [ '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 
233  $res = $this->db->select( 'page', '*', [ '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 }
testAddQuotesNull()
DatabaseBase::dropTable.
wfGetDB($db, $groups=[], $wiki=false)
Get a Database object.
$IP
Definition: WebStart.php:58
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
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:1798
clearFlag($flag)
Clear a flag for this connection.
Definition: Database.php:412
prefixAndQuote($table, $database=null, $prefix=null, $format= 'quoted')
getSharedTableName($table, $database, $prefix, $format= 'quoted')
const DBO_TRX
Definition: Defines.php:33
$wgSharedTables
onTransactionIdle($callback)
Run an anonymous function as soon as there is no transaction pending.
Definition: Database.php:2454
when a variable name is used in a it is silently declared as a new local masking the global
Definition: design.txt:93
$res
Definition: database.txt:21
testFillPreparedQuestion()
testUnknownTableCorruptsResults()
getFlag($flag)
Returns a boolean whether the flag $flag is set for this connection.
Definition: Database.php:416
$wgSharedDB
Shared database for multiple wikis.
setFlag($flag)
Set a flag for this connection.
Definition: Database.php:408
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
testAddQuotesStringQuote()
DatabaseBase $db
const DB_MASTER
Definition: Defines.php:47
$wgSharedPrefix
Database DatabaseBase.
Definition: DatabaseTest.php:7