RevisionUnitTest for various getters and setters
[lhc/web/wiklou.git] / tests / phpunit / includes / RevisionUnitTest.php
1 <?php
2
3 /**
4 * @group ContentHandler
5 */
6 class RevisionUnitTest extends MediaWikiTestCase {
7
8 public function provideConstructFromArray() {
9 yield 'with text' => [
10 [
11 'text' => 'hello world.',
12 'content_model' => CONTENT_MODEL_JAVASCRIPT
13 ],
14 ];
15 yield 'with content' => [
16 [
17 'content' => new JavaScriptContent( 'hellow world.' )
18 ],
19 ];
20 }
21
22 /**
23 * @dataProvider provideConstructFromArray
24 */
25 public function testConstructFromArray( $rowArray ) {
26 $rev = new Revision( $rowArray );
27 $this->assertNotNull( $rev->getContent(), 'no content object available' );
28 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT, $rev->getContent()->getModel() );
29 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT, $rev->getContentModel() );
30 }
31
32 public function provideConstructFromArrayThrowsExceptions() {
33 yield 'content and text_id both not empty' => [
34 [
35 'content' => new WikitextContent( 'GOAT' ),
36 'text_id' => 'someid',
37 ],
38 new MWException( "Text already stored in external store (id someid), " .
39 "can't serialize content object" )
40 ];
41 yield 'with bad content object (class)' => [
42 [ 'content' => new stdClass() ],
43 new MWException( '`content` field must contain a Content object.' )
44 ];
45 yield 'with bad content object (string)' => [
46 [ 'content' => 'ImAGoat' ],
47 new MWException( '`content` field must contain a Content object.' )
48 ];
49 yield 'bad row format' => [
50 'imastring, not a row',
51 new MWException( 'Revision constructor passed invalid row format.' )
52 ];
53 }
54
55 /**
56 * @dataProvider provideConstructFromArrayThrowsExceptions
57 */
58 public function testConstructFromArrayThrowsExceptions( $rowArray, Exception $expectedException ) {
59 $this->setExpectedException(
60 get_class( $expectedException ),
61 $expectedException->getMessage(),
62 $expectedException->getCode()
63 );
64 new Revision( $rowArray );
65 }
66
67 public function provideGetRevisionText() {
68 yield 'Generic test' => [
69 'This is a goat of revision text.',
70 [
71 'old_flags' => '',
72 'old_text' => 'This is a goat of revision text.',
73 ],
74 ];
75 }
76
77 public function provideGetId() {
78 yield [
79 [],
80 null
81 ];
82 yield [
83 [ 'id' => 998 ],
84 998
85 ];
86 }
87
88 /**
89 * @dataProvider provideGetId
90 * @covers Revision::getId
91 */
92 public function testGetId( $rowArray, $expectedId ) {
93 $rev = new Revision( $rowArray );
94 $this->assertEquals( $expectedId, $rev->getId() );
95 }
96
97 public function provideSetId() {
98 yield [ '123', 123 ];
99 yield [ 456, 456 ];
100 }
101
102 /**
103 * @dataProvider provideSetId
104 * @covers Revision::setId
105 */
106 public function testSetId( $input, $expected ) {
107 $rev = new Revision( [] );
108 $rev->setId( $input );
109 $this->assertSame( $expected, $rev->getId() );
110 }
111
112 public function provideSetUserIdAndName() {
113 yield [ '123', 123, 'GOaT' ];
114 yield [ 456, 456, 'GOaT' ];
115 }
116
117 /**
118 * @dataProvider provideSetUserIdAndName
119 * @covers Revision::setUserIdAndName
120 */
121 public function testSetUserIdAndName( $inputId, $expectedId, $name ) {
122 $rev = new Revision( [] );
123 $rev->setUserIdAndName( $inputId, $name );
124 $this->assertSame( $expectedId, $rev->getUser( Revision::RAW ) );
125 $this->assertEquals( $name, $rev->getUserText( Revision::RAW ) );
126 }
127
128 public function provideGetTextId() {
129 yield [ [], null ];
130 yield [ [ 'text_id' => '123' ], 123 ];
131 yield [ [ 'text_id' => 456 ], 456 ];
132 }
133
134 /**
135 * @dataProvider provideGetTextId
136 * @covers Revision::getTextId()
137 */
138 public function testGetTextId( $rowArray, $expected ) {
139 $rev = new Revision( $rowArray );
140 $this->assertSame( $expected, $rev->getTextId() );
141 }
142
143 public function provideGetParentId() {
144 yield [ [], null ];
145 yield [ [ 'parent_id' => '123' ], 123 ];
146 yield [ [ 'parent_id' => 456 ], 456 ];
147 }
148
149 /**
150 * @dataProvider provideGetParentId
151 * @covers Revision::getParentId()
152 */
153 public function testGetParentId( $rowArray, $expected ) {
154 $rev = new Revision( $rowArray );
155 $this->assertSame( $expected, $rev->getParentId() );
156 }
157
158 /**
159 * @covers Revision::getRevisionText
160 * @dataProvider provideGetRevisionText
161 */
162 public function testGetRevisionText( $expected, $rowData, $prefix = 'old_', $wiki = false ) {
163 $this->assertEquals(
164 $expected,
165 Revision::getRevisionText( (object)$rowData, $prefix, $wiki ) );
166 }
167
168 public function provideGetRevisionTextWithZlibExtension() {
169 yield 'Generic gzip test' => [
170 'This is a small goat of revision text.',
171 [
172 'old_flags' => 'gzip',
173 'old_text' => gzdeflate( 'This is a small goat of revision text.' ),
174 ],
175 ];
176 }
177
178 /**
179 * @covers Revision::getRevisionText
180 * @dataProvider provideGetRevisionTextWithZlibExtension
181 */
182 public function testGetRevisionWithZlibExtension( $expected, $rowData ) {
183 $this->checkPHPExtension( 'zlib' );
184 $this->testGetRevisionText( $expected, $rowData );
185 }
186
187 public function provideGetRevisionTextWithLegacyEncoding() {
188 yield 'Utf8Native' => [
189 "Wiki est l'\xc3\xa9cole superieur !",
190 'iso-8859-1',
191 [
192 'old_flags' => 'utf-8',
193 'old_text' => "Wiki est l'\xc3\xa9cole superieur !",
194 ]
195 ];
196 yield 'Utf8Legacy' => [
197 "Wiki est l'\xc3\xa9cole superieur !",
198 'iso-8859-1',
199 [
200 'old_flags' => '',
201 'old_text' => "Wiki est l'\xe9cole superieur !",
202 ]
203 ];
204 }
205
206 /**
207 * @covers Revision::getRevisionText
208 * @dataProvider provideGetRevisionTextWithLegacyEncoding
209 */
210 public function testGetRevisionWithLegacyEncoding( $expected, $encoding, $rowData ) {
211 $this->setMwGlobals( 'wgLegacyEncoding', $encoding );
212 $this->testGetRevisionText( $expected, $rowData );
213 }
214
215 public function provideGetRevisionTextWithGzipAndLegacyEncoding() {
216 /**
217 * WARNING!
218 * Do not set the external flag!
219 * Otherwise, getRevisionText will hit the live database (if ExternalStore is enabled)!
220 */
221 yield 'Utf8NativeGzip' => [
222 "Wiki est l'\xc3\xa9cole superieur !",
223 'iso-8859-1',
224 [
225 'old_flags' => 'gzip,utf-8',
226 'old_text' => gzdeflate( "Wiki est l'\xc3\xa9cole superieur !" ),
227 ]
228 ];
229 yield 'Utf8LegacyGzip' => [
230 "Wiki est l'\xc3\xa9cole superieur !",
231 'iso-8859-1',
232 [
233 'old_flags' => 'gzip',
234 'old_text' => gzdeflate( "Wiki est l'\xe9cole superieur !" ),
235 ]
236 ];
237 }
238
239 /**
240 * @covers Revision::getRevisionText
241 * @dataProvider provideGetRevisionTextWithGzipAndLegacyEncoding
242 */
243 public function testGetRevisionWithGzipAndLegacyEncoding( $expected, $encoding, $rowData ) {
244 $this->checkPHPExtension( 'zlib' );
245 $this->setMwGlobals( 'wgLegacyEncoding', $encoding );
246 $this->testGetRevisionText( $expected, $rowData );
247 }
248
249 /**
250 * @covers Revision::compressRevisionText
251 */
252 public function testCompressRevisionTextUtf8() {
253 $row = new stdClass;
254 $row->old_text = "Wiki est l'\xc3\xa9cole superieur !";
255 $row->old_flags = Revision::compressRevisionText( $row->old_text );
256 $this->assertTrue( false !== strpos( $row->old_flags, 'utf-8' ),
257 "Flags should contain 'utf-8'" );
258 $this->assertFalse( false !== strpos( $row->old_flags, 'gzip' ),
259 "Flags should not contain 'gzip'" );
260 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
261 $row->old_text, "Direct check" );
262 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
263 Revision::getRevisionText( $row ), "getRevisionText" );
264 }
265
266 /**
267 * @covers Revision::compressRevisionText
268 */
269 public function testCompressRevisionTextUtf8Gzip() {
270 $this->checkPHPExtension( 'zlib' );
271 $this->setMwGlobals( 'wgCompressRevisions', true );
272
273 $row = new stdClass;
274 $row->old_text = "Wiki est l'\xc3\xa9cole superieur !";
275 $row->old_flags = Revision::compressRevisionText( $row->old_text );
276 $this->assertTrue( false !== strpos( $row->old_flags, 'utf-8' ),
277 "Flags should contain 'utf-8'" );
278 $this->assertTrue( false !== strpos( $row->old_flags, 'gzip' ),
279 "Flags should contain 'gzip'" );
280 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
281 gzinflate( $row->old_text ), "Direct check" );
282 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
283 Revision::getRevisionText( $row ), "getRevisionText" );
284 }
285
286 /**
287 * @covers Revision::userJoinCond
288 */
289 public function testUserJoinCond() {
290 $this->assertEquals(
291 [ 'LEFT JOIN', [ 'rev_user != 0', 'user_id = rev_user' ] ],
292 Revision::userJoinCond()
293 );
294 }
295
296 /**
297 * @covers Revision::pageJoinCond
298 */
299 public function testPageJoinCond() {
300 $this->assertEquals(
301 [ 'INNER JOIN', [ 'page_id = rev_page' ] ],
302 Revision::pageJoinCond()
303 );
304 }
305
306 public function provideSelectFields() {
307 yield [
308 true,
309 [
310 'rev_id',
311 'rev_page',
312 'rev_text_id',
313 'rev_timestamp',
314 'rev_user_text',
315 'rev_user',
316 'rev_minor_edit',
317 'rev_deleted',
318 'rev_len',
319 'rev_parent_id',
320 'rev_sha1',
321 'rev_comment_text' => 'rev_comment',
322 'rev_comment_data' => 'NULL',
323 'rev_comment_cid' => 'NULL',
324 'rev_content_format',
325 'rev_content_model',
326 ]
327 ];
328 yield [
329 false,
330 [
331 'rev_id',
332 'rev_page',
333 'rev_text_id',
334 'rev_timestamp',
335 'rev_user_text',
336 'rev_user',
337 'rev_minor_edit',
338 'rev_deleted',
339 'rev_len',
340 'rev_parent_id',
341 'rev_sha1',
342 'rev_comment_text' => 'rev_comment',
343 'rev_comment_data' => 'NULL',
344 'rev_comment_cid' => 'NULL',
345 ]
346 ];
347 }
348
349 /**
350 * @dataProvider provideSelectFields
351 * @covers Revision::selectFields
352 * @todo a true unit test would mock CommentStore
353 */
354 public function testSelectFields( $contentHandlerUseDB, $expected ) {
355 $this->setMwGlobals( 'wgContentHandlerUseDB', $contentHandlerUseDB );
356 $this->assertEquals( $expected, Revision::selectFields() );
357 }
358
359 public function provideSelectArchiveFields() {
360 yield [
361 true,
362 [
363 'ar_id',
364 'ar_page_id',
365 'ar_rev_id',
366 'ar_text',
367 'ar_text_id',
368 'ar_timestamp',
369 'ar_user_text',
370 'ar_user',
371 'ar_minor_edit',
372 'ar_deleted',
373 'ar_len',
374 'ar_parent_id',
375 'ar_sha1',
376 'ar_comment_text' => 'ar_comment',
377 'ar_comment_data' => 'NULL',
378 'ar_comment_cid' => 'NULL',
379 'ar_content_format',
380 'ar_content_model',
381 ]
382 ];
383 yield [
384 false,
385 [
386 'ar_id',
387 'ar_page_id',
388 'ar_rev_id',
389 'ar_text',
390 'ar_text_id',
391 'ar_timestamp',
392 'ar_user_text',
393 'ar_user',
394 'ar_minor_edit',
395 'ar_deleted',
396 'ar_len',
397 'ar_parent_id',
398 'ar_sha1',
399 'ar_comment_text' => 'ar_comment',
400 'ar_comment_data' => 'NULL',
401 'ar_comment_cid' => 'NULL',
402 ]
403 ];
404 }
405
406 /**
407 * @dataProvider provideSelectArchiveFields
408 * @covers Revision::selectArchiveFields
409 * @todo a true unit test would mock CommentStore
410 */
411 public function testSelectArchiveFields( $contentHandlerUseDB, $expected ) {
412 $this->setMwGlobals( 'wgContentHandlerUseDB', $contentHandlerUseDB );
413 $this->assertEquals( $expected, Revision::selectArchiveFields() );
414 }
415
416 /**
417 * @covers Revision::selectTextFields
418 */
419 public function testSelectTextFields() {
420 $this->assertEquals(
421 [
422 'old_text',
423 'old_flags',
424 ],
425 Revision::selectTextFields()
426 );
427 }
428
429 /**
430 * @covers Revision::selectPageFields
431 */
432 public function testSelectPageFields() {
433 $this->assertEquals(
434 [
435 'page_namespace',
436 'page_title',
437 'page_id',
438 'page_latest',
439 'page_is_redirect',
440 'page_len',
441 ],
442 Revision::selectPageFields()
443 );
444 }
445
446 /**
447 * @covers Revision::selectUserFields
448 */
449 public function testSelectUserFields() {
450 $this->assertEquals(
451 [
452 'user_name',
453 ],
454 Revision::selectUserFields()
455 );
456 }
457
458 }