summaryrefslogtreecommitdiff
path: root/tests/phpunit/includes/db/DatabaseMysqlBaseTest.php
blob: 42ea58e55136efbc8df87f3d663b9c0f8cf54f36 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
<?php
/**
 * Holds tests for DatabaseMysqlBase MediaWiki class.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 * http://www.gnu.org/copyleft/gpl.html
 *
 * @file
 * @author Antoine Musso
 * @author Bryan Davis
 * @copyright © 2013 Antoine Musso
 * @copyright © 2013 Bryan Davis
 * @copyright © 2013 Wikimedia Foundation Inc.
 */

/**
 * Fake class around abstract class so we can call concrete methods.
 */
class FakeDatabaseMysqlBase extends DatabaseMysqlBase {
	// From DatabaseBase
	function __construct() {
	}

	protected function closeConnection() {
	}

	protected function doQuery( $sql ) {
	}

	// From DatabaseMysql
	protected function mysqlConnect( $realServer ) {
	}

	protected function mysqlSetCharset( $charset ) {
	}

	protected function mysqlFreeResult( $res ) {
	}

	protected function mysqlFetchObject( $res ) {
	}

	protected function mysqlFetchArray( $res ) {
	}

	protected function mysqlNumRows( $res ) {
	}

	protected function mysqlNumFields( $res ) {
	}

	protected function mysqlFieldName( $res, $n ) {
	}

	protected function mysqlFieldType( $res, $n ) {
	}

	protected function mysqlDataSeek( $res, $row ) {
	}

	protected function mysqlError( $conn = null ) {
	}

	protected function mysqlFetchField( $res, $n ) {
	}

	protected function mysqlPing() {
	}

	// From interface DatabaseType
	function insertId() {
	}

	function lastErrno() {
	}

	function affectedRows() {
	}

	function getServerVersion() {
	}
}

class DatabaseMysqlBaseTest extends MediaWikiTestCase {
	/**
	 * @dataProvider provideDiapers
	 * @covers DatabaseMysqlBase::addIdentifierQuotes
	 */
	public function testAddIdentifierQuotes( $expected, $in ) {
		$db = new FakeDatabaseMysqlBase();
		$quoted = $db->addIdentifierQuotes( $in );
		$this->assertEquals( $expected, $quoted );
	}

	/**
	 * Feeds testAddIdentifierQuotes
	 *
	 * Named per bug 20281 convention.
	 */
	function provideDiapers() {
		return array(
			// Format: expected, input
			array( '``', '' ),

			// Yeah I really hate loosely typed PHP idiocies nowadays
			array( '``', null ),

			// Dear codereviewer, guess what addIdentifierQuotes()
			// will return with thoses:
			array( '``', false ),
			array( '`1`', true ),

			// We never know what could happen
			array( '`0`', 0 ),
			array( '`1`', 1 ),

			// Whatchout! Should probably use something more meaningful
			array( "`'`", "'" ),  # single quote
			array( '`"`', '"' ),  # double quote
			array( '````', '`' ), # backtick
			array( '`’`', '’' ),  # apostrophe (look at your encyclopedia)

			// sneaky NUL bytes are lurking everywhere
			array( '``', "\0" ),
			array( '`xyzzy`', "\0x\0y\0z\0z\0y\0" ),

			// unicode chars
			array(
				self::createUnicodeString( '`\u0001a\uFFFFb`' ),
				self::createUnicodeString( '\u0001a\uFFFFb' )
			),
			array(
				self::createUnicodeString( '`\u0001\uFFFF`' ),
				self::createUnicodeString( '\u0001\u0000\uFFFF\u0000' )
			),
			array( '`☃`', '☃' ),
			array( '`メインページ`', 'メインページ' ),
			array( '`Басты_бет`', 'Басты_бет' ),

			// Real world:
			array( '`Alix`', 'Alix' ),  # while( ! $recovered ) { sleep(); }
			array( '`Backtick: ```', 'Backtick: `' ),
			array( '`This is a test`', 'This is a test' ),
		);
	}

	private static function createUnicodeString( $str ) {
		return json_decode( '"' . $str . '"' );
	}

	function getMockForViews() {
		$db = $this->getMockBuilder( 'DatabaseMysql' )
			->disableOriginalConstructor()
			->setMethods( array( 'fetchRow', 'query' ) )
			->getMock();

		$db->expects( $this->any() )
			->method( 'query' )
			->with( $this->anything() )
			->will(
				$this->returnValue( null )
			);

		$db->expects( $this->any() )
			->method( 'fetchRow' )
			->with( $this->anything() )
			->will( $this->onConsecutiveCalls(
				array( 'Tables_in_' => 'view1' ),
				array( 'Tables_in_' => 'view2' ),
				array( 'Tables_in_' => 'myview' ),
				false  # no more rows
			) );
		return $db;
	}
	/**
	 * @covers DatabaseMysqlBase::listViews
	 */
	function testListviews() {
		$db = $this->getMockForViews();

		// The first call populate an internal cache of views
		$this->assertEquals( array( 'view1', 'view2', 'myview' ),
			$db->listViews() );
		$this->assertEquals( array( 'view1', 'view2', 'myview' ),
			$db->listViews() );

		// Prefix filtering
		$this->assertEquals( array( 'view1', 'view2' ),
			$db->listViews( 'view' ) );
		$this->assertEquals( array( 'myview' ),
			$db->listViews( 'my' ) );
		$this->assertEquals( array(),
			$db->listViews( 'UNUSED_PREFIX' ) );
		$this->assertEquals( array( 'view1', 'view2', 'myview' ),
			$db->listViews( '' ) );
	}

	/**
	 * @covers DatabaseMysqlBase::isView
	 * @dataProvider provideViewExistanceChecks
	 */
	function testIsView( $isView, $viewName ) {
		$db = $this->getMockForViews();

		switch ( $isView ) {
			case true:
				$this->assertTrue( $db->isView( $viewName ),
					"$viewName should be considered a view" );
			break;

			case false:
				$this->assertFalse( $db->isView( $viewName ),
					"$viewName has not been defined as a view" );
			break;
		}

	}

	function provideViewExistanceChecks() {
		return array(
			// format: whether it is a view, view name
			array( true, 'view1' ),
			array( true, 'view2' ),
			array( true, 'myview' ),

			array( false, 'user' ),

			array( false, 'view10' ),
			array( false, 'my' ),
			array( false, 'OH_MY_GOD' ),  # they killed kenny!
		);
	}

}