Compress images
[mediawiki.git] / tests / phpunit / includes / db / DatabaseTest.php
blob672e6645b8ce44d8043fa6694b64679a3f141284
1 <?php
3 /**
4 * @group Database
5 * @group DatabaseBase
6 */
7 class DatabaseTest extends MediaWikiTestCase {
8 var $db, $functionTest = false;
10 function setUp() {
11 $this->db = wfGetDB( DB_MASTER );
14 function tearDown() {
15 if ( $this->functionTest ) {
16 $this->dropFunctions();
17 $this->functionTest = false;
21 function testAddQuotesNull() {
22 $check = "NULL";
23 if ( $this->db->getType() === 'sqlite' || $this->db->getType() === 'oracle' ) {
24 $check = "''";
26 $this->assertEquals( $check, $this->db->addQuotes( null ) );
29 function testAddQuotesInt() {
30 # returning just "1234" should be ok too, though...
31 # maybe
32 $this->assertEquals(
33 "'1234'",
34 $this->db->addQuotes( 1234 ) );
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 ) );
44 function testAddQuotesString() {
45 $this->assertEquals(
46 "'string'",
47 $this->db->addQuotes( 'string' ) );
50 function testAddQuotesStringQuote() {
51 $check = "'string''s cause trouble'";
52 if ( $this->db->getType() === 'mysql' ) {
53 $check = "'string\'s cause trouble'";
55 $this->assertEquals(
56 $check,
57 $this->db->addQuotes( "string's cause trouble" ) );
60 function testFillPreparedEmpty() {
61 $sql = $this->db->fillPrepared(
62 'SELECT * FROM interwiki', array() );
63 $this->assertEquals(
64 "SELECT * FROM interwiki",
65 $sql );
68 function testFillPreparedQuestion() {
69 $sql = $this->db->fillPrepared(
70 'SELECT * FROM cur WHERE cur_namespace=? AND cur_title=?',
71 array( 4, "Snicker's_paradox" ) );
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'";
77 $this->assertEquals( $check, $sql );
80 function testFillPreparedBang() {
81 $sql = $this->db->fillPrepared(
82 'SELECT user_id FROM ! WHERE user_name=?',
83 array( '"user"', "Slash's Dot" ) );
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'";
89 $this->assertEquals( $check, $sql );
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 );
102 * @group Broken
104 function testStoredFunctions() {
105 if ( !in_array( wfGetDB( DB_MASTER )->getType(), array( 'mysql', 'postgres' ) ) ) {
106 $this->markTestSkipped( 'MySQL or Postgres required' );
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 );
116 private function dropFunctions() {
117 $this->db->query( 'DROP FUNCTION IF EXISTS mw_test_function'
118 . ( $this->db->getType() == 'postgres' ? '()' : '' )