e886fa87c91d91255a9e62065bfca453579ed54f
[lhc/web/wiklou.git] / tests / phpunit / includes / db / DatabaseTest.php
1 <?php
2
3 /**
4 * @group Database
5 */
6 class DatabaseTest extends MediaWikiTestCase {
7 var $db;
8
9 function setUp() {
10 $this->db = wfGetDB( DB_SLAVE );
11 }
12
13 function testAddQuotesNull() {
14 $check = "NULL";
15 if ( $this->db->getType() === 'sqlite' ) {
16 $check = "''";
17 }
18 $this->assertEquals( $check, $this->db->addQuotes( null ) );
19 }
20
21 function testAddQuotesInt() {
22 # returning just "1234" should be ok too, though...
23 # maybe
24 $this->assertEquals(
25 "'1234'",
26 $this->db->addQuotes( 1234 ) );
27 }
28
29 function testAddQuotesFloat() {
30 # returning just "1234.5678" would be ok too, though
31 $this->assertEquals(
32 "'1234.5678'",
33 $this->db->addQuotes( 1234.5678 ) );
34 }
35
36 function testAddQuotesString() {
37 $this->assertEquals(
38 "'string'",
39 $this->db->addQuotes( 'string' ) );
40 }
41
42 function testAddQuotesStringQuote() {
43 $check = "'string''s cause trouble'";
44 if ( $this->db->getType() === 'mysql' ) {
45 $check = "'string\'s cause trouble'";
46 }
47 $this->assertEquals(
48 $check,
49 $this->db->addQuotes( "string's cause trouble" ) );
50 }
51
52 function testFillPreparedEmpty() {
53 $sql = $this->db->fillPrepared(
54 'SELECT * FROM interwiki', array() );
55 $this->assertEquals(
56 "SELECT * FROM interwiki",
57 $sql );
58 }
59
60 function testFillPreparedQuestion() {
61 $sql = $this->db->fillPrepared(
62 'SELECT * FROM cur WHERE cur_namespace=? AND cur_title=?',
63 array( 4, "Snicker's_paradox" ) );
64
65 $check = "SELECT * FROM cur WHERE cur_namespace='4' AND cur_title='Snicker''s_paradox'";
66 if ( $this->db->getType() === 'mysql' ) {
67 $check = "SELECT * FROM cur WHERE cur_namespace='4' AND cur_title='Snicker\'s_paradox'";
68 }
69 $this->assertEquals( $check, $sql );
70 }
71
72 function testFillPreparedBang() {
73 $sql = $this->db->fillPrepared(
74 'SELECT user_id FROM ! WHERE user_name=?',
75 array( '"user"', "Slash's Dot" ) );
76
77 $check = "SELECT user_id FROM \"user\" WHERE user_name='Slash''s Dot'";
78 if ( $this->db->getType() === 'mysql' ) {
79 $check = "SELECT user_id FROM \"user\" WHERE user_name='Slash\'s Dot'";
80 }
81 $this->assertEquals( $check, $sql );
82 }
83
84 function testFillPreparedRaw() {
85 $sql = $this->db->fillPrepared(
86 "SELECT * FROM cur WHERE cur_title='This_\\&_that,_WTF\\?\\!'",
87 array( '"user"', "Slash's Dot" ) );
88 $this->assertEquals(
89 "SELECT * FROM cur WHERE cur_title='This_&_that,_WTF?!'",
90 $sql );
91 }
92
93 function testMakeNotInList() {
94 $this->assertEquals(
95 "field IN ('0','1')",
96 $this->db->makeList( array(
97 'field' => array( 0, 1 )
98 ), LIST_AND )
99 );
100 $this->assertEquals(
101 "field NOT IN ('0','1')",
102 $this->db->makeList( array(
103 'field!' => array( 0, 1 )
104 ), LIST_AND )
105 );
106
107 // make sure an array with only one value use = or !=
108 $this->assertEquals(
109 "field = '777'",
110 $this->db->makeList( array(
111 'field' => array( 777 )
112 ), LIST_AND )
113 );
114 $this->assertEquals(
115 "field != '888'",
116 $this->db->makeList( array(
117 'field!' => array( 888 )
118 ), LIST_AND )
119 );
120 }
121 }
122
123