Remove messages.inc, rebuildLanguage.php, writeMessagesArray.inc
[mediawiki.git] / tests / phpunit / includes / db / DatabaseTest.php
blob7e7043965640f74cd6ad4fc0900f10491fe906b0
1 <?php
3 /**
4 * @group Database
5 * @group DatabaseBase
6 */
7 class DatabaseTest extends MediaWikiTestCase {
8 /**
9 * @var DatabaseBase
11 protected $db;
13 private $functionTest = false;
15 protected function setUp() {
16 parent::setUp();
17 $this->db = wfGetDB( DB_MASTER );
20 protected function tearDown() {
21 parent::tearDown();
22 if ( $this->functionTest ) {
23 $this->dropFunctions();
24 $this->functionTest = false;
27 /**
28 * @covers DatabaseBase::dropTable
30 public function testAddQuotesNull() {
31 $check = "NULL";
32 if ( $this->db->getType() === 'sqlite' || $this->db->getType() === 'oracle' ) {
33 $check = "''";
35 $this->assertEquals( $check, $this->db->addQuotes( null ) );
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 ) );
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 ) );
53 public function testAddQuotesString() {
54 $this->assertEquals(
55 "'string'",
56 $this->db->addQuotes( 'string' ) );
59 public function testAddQuotesStringQuote() {
60 $check = "'string''s cause trouble'";
61 if ( $this->db->getType() === 'mysql' ) {
62 $check = "'string\'s cause trouble'";
64 $this->assertEquals(
65 $check,
66 $this->db->addQuotes( "string's cause trouble" ) );
69 private function getSharedTableName( $table, $database, $prefix, $format = 'quoted' ) {
70 global $wgSharedDB, $wgSharedTables, $wgSharedPrefix;
72 $oldName = $wgSharedDB;
73 $oldTables = $wgSharedTables;
74 $oldPrefix = $wgSharedPrefix;
76 $wgSharedDB = $database;
77 $wgSharedTables = array( $table );
78 $wgSharedPrefix = $prefix;
80 $ret = $this->db->tableName( $table, $format );
82 $wgSharedDB = $oldName;
83 $wgSharedTables = $oldTables;
84 $wgSharedPrefix = $oldPrefix;
86 return $ret;
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 = '"';
100 if ( $database !== null ) {
101 if ( $this->db->getType() === 'oracle' ) {
102 $database = $quote . $database . '.';
103 } else {
104 $database = $quote . $database . $quote . '.';
108 if ( $prefix === null ) {
109 $prefix = $this->dbPrefix();
112 if ( $this->db->getType() === 'oracle' ) {
113 return strtoupper( $database . $quote . $prefix . $table );
114 } else {
115 return $database . $quote . $prefix . $table . $quote;
119 public function testTableNameLocal() {
120 $this->assertEquals(
121 $this->prefixAndQuote( 'tablename' ),
122 $this->db->tableName( 'tablename' )
126 public function testTableNameRawLocal() {
127 $this->assertEquals(
128 $this->prefixAndQuote( 'tablename', null, null, 'raw' ),
129 $this->db->tableName( 'tablename', 'raw' )
133 public function testTableNameShared() {
134 $this->assertEquals(
135 $this->prefixAndQuote( 'tablename', 'sharedatabase', 'sh_' ),
136 $this->getSharedTableName( 'tablename', 'sharedatabase', 'sh_' )
139 $this->assertEquals(
140 $this->prefixAndQuote( 'tablename', 'sharedatabase', null ),
141 $this->getSharedTableName( 'tablename', 'sharedatabase', null )
145 public function testTableNameRawShared() {
146 $this->assertEquals(
147 $this->prefixAndQuote( 'tablename', 'sharedatabase', 'sh_', 'raw' ),
148 $this->getSharedTableName( 'tablename', 'sharedatabase', 'sh_', 'raw' )
151 $this->assertEquals(
152 $this->prefixAndQuote( 'tablename', 'sharedatabase', null, 'raw' ),
153 $this->getSharedTableName( 'tablename', 'sharedatabase', null, 'raw' )
157 public function testTableNameForeign() {
158 $this->assertEquals(
159 $this->prefixAndQuote( 'tablename', 'databasename', '' ),
160 $this->db->tableName( 'databasename.tablename' )
164 public function testTableNameRawForeign() {
165 $this->assertEquals(
166 $this->prefixAndQuote( 'tablename', 'databasename', '', 'raw' ),
167 $this->db->tableName( 'databasename.tablename', 'raw' )
171 public function testFillPreparedEmpty() {
172 $sql = $this->db->fillPrepared(
173 'SELECT * FROM interwiki', array() );
174 $this->assertEquals(
175 "SELECT * FROM interwiki",
176 $sql );
179 public function testFillPreparedQuestion() {
180 $sql = $this->db->fillPrepared(
181 'SELECT * FROM cur WHERE cur_namespace=? AND cur_title=?',
182 array( 4, "Snicker's_paradox" ) );
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'";
188 $this->assertEquals( $check, $sql );
191 public function testFillPreparedBang() {
192 $sql = $this->db->fillPrepared(
193 'SELECT user_id FROM ! WHERE user_name=?',
194 array( '"user"', "Slash's Dot" ) );
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'";
200 $this->assertEquals( $check, $sql );
203 public function testFillPreparedRaw() {
204 $sql = $this->db->fillPrepared(
205 "SELECT * FROM cur WHERE cur_title='This_\\&_that,_WTF\\?\\!'",
206 array( '"user"', "Slash's Dot" ) );
207 $this->assertEquals(
208 "SELECT * FROM cur WHERE cur_title='This_&_that,_WTF?!'",
209 $sql );
212 public function testStoredFunctions() {
213 if ( !in_array( wfGetDB( DB_MASTER )->getType(), array( 'mysql', 'postgres' ) ) ) {
214 $this->markTestSkipped( 'MySQL or Postgres required' );
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" )
222 $res = $this->db->query( 'SELECT mw_test_function() AS test', __METHOD__ );
223 $this->assertEquals( 42, $res->fetchObject()->test );
226 private function dropFunctions() {
227 $this->db->query( 'DROP FUNCTION IF EXISTS mw_test_function'
228 . ( $this->db->getType() == 'postgres' ? '()' : '' )
232 public function testUnknownTableCorruptsResults() {
233 $res = $this->db->select( 'page', '*', array( 'page_id' => 1 ) );
234 $this->assertFalse( $this->db->tableExists( 'foobarbaz' ) );
235 $this->assertInternalType( 'int', $res->numRows() );