Revert r108603, which was itself a revert of r107376, r107994. Before considering...
[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 function testFillPreparedEmpty() {
61 $sql = $this->db->fillPrepared(
62 'SELECT * FROM interwiki', array() );
63 $this->assertEquals(
64 "SELECT * FROM interwiki",
65 $sql );
66 }
67
68 function testFillPreparedQuestion() {
69 $sql = $this->db->fillPrepared(
70 'SELECT * FROM cur WHERE cur_namespace=? AND cur_title=?',
71 array( 4, "Snicker's_paradox" ) );
72
73 $check = "SELECT * FROM cur WHERE cur_namespace='4' AND cur_title='Snicker''s_paradox'";
74 if ( $this->db->getType() === 'mysql' ) {
75 $check = "SELECT * FROM cur WHERE cur_namespace='4' AND cur_title='Snicker\'s_paradox'";
76 }
77 $this->assertEquals( $check, $sql );
78 }
79
80 function testFillPreparedBang() {
81 $sql = $this->db->fillPrepared(
82 'SELECT user_id FROM ! WHERE user_name=?',
83 array( '"user"', "Slash's Dot" ) );
84
85 $check = "SELECT user_id FROM \"user\" WHERE user_name='Slash''s Dot'";
86 if ( $this->db->getType() === 'mysql' ) {
87 $check = "SELECT user_id FROM \"user\" WHERE user_name='Slash\'s Dot'";
88 }
89 $this->assertEquals( $check, $sql );
90 }
91
92 function testFillPreparedRaw() {
93 $sql = $this->db->fillPrepared(
94 "SELECT * FROM cur WHERE cur_title='This_\\&_that,_WTF\\?\\!'",
95 array( '"user"', "Slash's Dot" ) );
96 $this->assertEquals(
97 "SELECT * FROM cur WHERE cur_title='This_&_that,_WTF?!'",
98 $sql );
99 }
100
101 /**
102 * @group Broken
103 */
104 function testStoredFunctions() {
105 if ( !in_array( wfGetDB( DB_MASTER )->getType(), array( 'mysql', 'postgres' ) ) ) {
106 $this->markTestSkipped( 'MySQL or Postgres required' );
107 }
108 global $IP;
109 $this->dropFunctions();
110 $this->functionTest = true;
111 $this->assertTrue( $this->db->sourceFile( "$IP/tests/phpunit/data/db/{$this->db->getType()}/functions.sql" ) );
112 $res = $this->db->query( 'SELECT mw_test_function() AS test', __METHOD__ );
113 $this->assertEquals( 42, $res->fetchObject()->test );
114 }
115
116 private function dropFunctions() {
117 $this->db->query( 'DROP FUNCTION IF EXISTS mw_test_function'
118 . ( $this->db->getType() == 'postgres' ? '()' : '' )
119 );
120 }
121 }
122
123