Merge "Add rc.unpatrolled to the recentchanges API"
[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 * @covers DatabaseMysqlBase::addIdentifierQuotes
62 */
63 public function testAddIdentifierQuotes( $expected, $in ) {
64 $db = new FakeDatabaseMysqlBase();
65 $quoted = $db->addIdentifierQuotes( $in );
66 $this->assertEquals($expected, $quoted);
67 }
68
69
70 /**
71 * Feeds testAddIdentifierQuotes
72 *
73 * Named per bug 20281 convention.
74 */
75 function provideDiapers() {
76 return array(
77 // Format: expected, input
78 array( '``', '' ),
79
80 // Yeah I really hate loosely typed PHP idiocies nowadays
81 array( '``', null ),
82
83 // Dear codereviewer, guess what addIdentifierQuotes()
84 // will return with thoses:
85 array( '``', false ),
86 array( '`1`', true ),
87
88 // We never know what could happen
89 array( '`0`', 0 ),
90 array( '`1`', 1 ),
91
92 // Whatchout! Should probably use something more meaningful
93 array( "`'`", "'" ), # single quote
94 array( '`"`', '"' ), # double quote
95 array( '````', '`' ), # backtick
96 array( '`’`', '’' ), # apostrophe (look at your encyclopedia)
97
98 // sneaky NUL bytes are lurking everywhere
99 array( '``', "\0" ),
100 array( '`xyzzy`', "\0x\0y\0z\0z\0y\0" ),
101
102 // unicode chars
103 array(
104 self::createUnicodeString( '`\u0001a\uFFFFb`' ),
105 self::createUnicodeString( '\u0001a\uFFFFb' )
106 ),
107 array(
108 self::createUnicodeString( '`\u0001\uFFFF`' ),
109 self::createUnicodeString( '\u0001\u0000\uFFFF\u0000' )
110 ),
111 array( '`☃`', '☃' ),
112 array( '`メインページ`', 'メインページ' ),
113 array( '`Басты_бет`', 'Басты_бет' ),
114
115 // Real world:
116 array( '`Alix`', 'Alix' ), # while( ! $recovered ) { sleep(); }
117 array( '`Backtick: ```', 'Backtick: `' ),
118 array( '`This is a test`', 'This is a test' ),
119 );
120 }
121
122 private static function createUnicodeString($str) {
123 return json_decode( '"' . $str . '"' );
124 }
125
126 function getMockForViews() {
127 $db = $this->getMockBuilder( 'DatabaseMysql' )
128 ->disableOriginalConstructor()
129 ->setMethods( array( 'fetchRow', 'query' ) )
130 ->getMock();
131
132 $db->expects( $this->any() )
133 ->method( 'query' )
134 ->with( $this->anything() )
135 ->will(
136 $this->returnValue( null )
137 );
138
139 $db->expects( $this->any() )
140 ->method( 'fetchRow' )
141 ->with( $this->anything() )
142 ->will( $this->onConsecutiveCalls(
143 array( 'Tables_in_' => 'view1' ),
144 array( 'Tables_in_' => 'view2' ),
145 array( 'Tables_in_' => 'myview' ),
146 false # no more rows
147 ));
148 return $db;
149 }
150 /**
151 * @covers DatabaseMysqlBase::listViews
152 */
153 function testListviews() {
154 $db = $this->getMockForViews();
155
156 // The first call populate an internal cache of views
157 $this->assertEquals( array( 'view1', 'view2', 'myview'),
158 $db->listViews() );
159 $this->assertEquals( array( 'view1', 'view2', 'myview'),
160 $db->listViews() );
161
162 // Prefix filtering
163 $this->assertEquals( array( 'view1', 'view2' ),
164 $db->listViews( 'view' ) );
165 $this->assertEquals( array( 'myview' ),
166 $db->listViews( 'my' ) );
167 $this->assertEquals( array(),
168 $db->listViews( 'UNUSED_PREFIX' ) );
169 $this->assertEquals( array( 'view1', 'view2', 'myview'),
170 $db->listViews( '' ) );
171 }
172
173 /**
174 * @covers DatabaseMysqlBase::isView
175 * @dataProvider provideViewExistanceChecks
176 */
177 function testIsView( $isView, $viewName ) {
178 $db = $this->getMockForViews();
179
180 switch( $isView ) {
181 case true:
182 $this->assertTrue( $db->isView( $viewName ),
183 "$viewName should be considered a view" );
184 break;
185
186 case false:
187 $this->assertFalse( $db->isView( $viewName ),
188 "$viewName has not been defined as a view" );
189 break;
190 }
191
192 }
193
194 function provideViewExistanceChecks() {
195 return array(
196 // format: whether it is a view, view name
197 array( true, 'view1' ),
198 array( true, 'view2' ),
199 array( true, 'myview' ),
200
201 array( false, 'user' ),
202
203 array( false, 'view10' ),
204 array( false, 'my' ),
205 array( false, 'OH_MY_GOD' ), # they killed kenny!
206 );
207 }
208
209 }