Merge "FormatJson: minor cleanup"
[lhc/web/wiklou.git] / tests / phpunit / includes / db / DatabaseMysqlBaseTest.php
1 <?php
2 /**
3 * Holds tests for DatabaseMysqlBase MediaWiki class.
4 *
5 * @section LICENSE
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
20 *
21 * @file
22 * @author Antoine Musso
23 * @author Bryan Davis
24 * @copyright © 2013 Antoine Musso
25 * @copyright © 2013 Bryan Davis
26 * @copyright © 2013 Wikimedia Foundation Inc.
27 */
28
29 /**
30 * Fake class around abstract class so we can call concrete methods.
31 */
32 class FakeDatabaseMysqlBase extends DatabaseMysqlBase {
33 // From DatabaseBase
34 protected function closeConnection() {}
35 protected function doQuery( $sql ) {}
36
37 // From DatabaseMysql
38 protected function mysqlConnect( $realServer ) {}
39 protected function mysqlFreeResult( $res ) {}
40 protected function mysqlFetchObject( $res ) {}
41 protected function mysqlFetchArray( $res ) {}
42 protected function mysqlNumRows( $res ) {}
43 protected function mysqlNumFields( $res ) {}
44 protected function mysqlFieldName( $res, $n ) {}
45 protected function mysqlDataSeek( $res, $row ) {}
46 protected function mysqlError( $conn = null ) {}
47 protected function mysqlFetchField( $res, $n ) {}
48 protected function mysqlPing() {}
49
50 // From interface DatabaseType
51 function insertId() {}
52 function lastErrno() {}
53 function affectedRows() {}
54 function getServerVersion() {}
55 }
56
57 class DatabaseMysqlBaseTest extends MediaWikiTestCase {
58
59 /**
60 * @dataProvider provideDiapers
61 */
62 function testAddIdentifierQuotes( $expected, $in ) {
63 $db = new FakeDatabaseMysqlBase();
64 $quoted = $db->addIdentifierQuotes( $in );
65 $this->assertEquals($expected, $quoted);
66 }
67
68
69 /**
70 * Feeds testAddIdentifierQuotes
71 *
72 * Named per bug 20281 convention.
73 */
74 function provideDiapers() {
75 return array(
76 // Format: expected, input
77 array( '``', '' ),
78
79 // Yeah I really hate loosely typed PHP idiocies nowadays
80 array( '``', null ),
81
82 // Dear codereviewer, guess what addIdentifierQuotes()
83 // will return with thoses:
84 array( '``', false ),
85 array( '`1`', true ),
86
87 // We never know what could happen
88 array( '`0`', 0 ),
89 array( '`1`', 1 ),
90
91 // Whatchout! Should probably use something more meaningful
92 array( "`'`", "'" ), # single quote
93 array( '`"`', '"' ), # double quote
94 array( '````', '`' ), # backtick
95 array( '`’`', '’' ), # apostrophe (look at your encyclopedia)
96
97 // sneaky NUL bytes are lurking everywhere
98 array( '``', "\0" ),
99 array( '`xyzzy`', "\0x\0y\0z\0z\0y\0" ),
100
101 // unicode chars
102 array(
103 self::createUnicodeString( '`\u0001a\uFFFFb`' ),
104 self::createUnicodeString( '\u0001a\uFFFFb' )
105 ),
106 array(
107 self::createUnicodeString( '`\u0001\uFFFF`' ),
108 self::createUnicodeString( '\u0001\u0000\uFFFF\u0000' )
109 ),
110 array( '`☃`', '☃' ),
111 array( '`メインページ`', 'メインページ' ),
112 array( '`Басты_бет`', 'Басты_бет' ),
113
114 // Real world:
115 array( '`Alix`', 'Alix' ), # while( ! $recovered ) { sleep(); }
116 array( '`Backtick: ```', 'Backtick: `' ),
117 array( '`This is a test`', 'This is a test' ),
118 );
119 }
120
121 private static function createUnicodeString($str) {
122 return json_decode( '"' . $str . '"' );
123 }
124
125 }