RevisionUnittest for select*Fields methods
[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 /**
78 * @covers Revision::getRevisionText
79 * @dataProvider provideGetRevisionText
80 */
81 public function testGetRevisionText( $expected, $rowData, $prefix = 'old_', $wiki = false ) {
82 $this->assertEquals(
83 $expected,
84 Revision::getRevisionText( (object)$rowData, $prefix, $wiki ) );
85 }
86
87 public function provideGetRevisionTextWithZlibExtension() {
88 yield 'Generic gzip test' => [
89 'This is a small goat of revision text.',
90 [
91 'old_flags' => 'gzip',
92 'old_text' => gzdeflate( 'This is a small goat of revision text.' ),
93 ],
94 ];
95 }
96
97 /**
98 * @covers Revision::getRevisionText
99 * @dataProvider provideGetRevisionTextWithZlibExtension
100 */
101 public function testGetRevisionWithZlibExtension( $expected, $rowData ) {
102 $this->checkPHPExtension( 'zlib' );
103 $this->testGetRevisionText( $expected, $rowData );
104 }
105
106 public function provideGetRevisionTextWithLegacyEncoding() {
107 yield 'Utf8Native' => [
108 "Wiki est l'\xc3\xa9cole superieur !",
109 'iso-8859-1',
110 [
111 'old_flags' => 'utf-8',
112 'old_text' => "Wiki est l'\xc3\xa9cole superieur !",
113 ]
114 ];
115 yield 'Utf8Legacy' => [
116 "Wiki est l'\xc3\xa9cole superieur !",
117 'iso-8859-1',
118 [
119 'old_flags' => '',
120 'old_text' => "Wiki est l'\xe9cole superieur !",
121 ]
122 ];
123 }
124
125 /**
126 * @covers Revision::getRevisionText
127 * @dataProvider provideGetRevisionTextWithLegacyEncoding
128 */
129 public function testGetRevisionWithLegacyEncoding( $expected, $encoding, $rowData ) {
130 $this->setMwGlobals( 'wgLegacyEncoding', $encoding );
131 $this->testGetRevisionText( $expected, $rowData );
132 }
133
134 public function provideGetRevisionTextWithGzipAndLegacyEncoding() {
135 /**
136 * WARNING!
137 * Do not set the external flag!
138 * Otherwise, getRevisionText will hit the live database (if ExternalStore is enabled)!
139 */
140 yield 'Utf8NativeGzip' => [
141 "Wiki est l'\xc3\xa9cole superieur !",
142 'iso-8859-1',
143 [
144 'old_flags' => 'gzip,utf-8',
145 'old_text' => gzdeflate( "Wiki est l'\xc3\xa9cole superieur !" ),
146 ]
147 ];
148 yield 'Utf8LegacyGzip' => [
149 "Wiki est l'\xc3\xa9cole superieur !",
150 'iso-8859-1',
151 [
152 'old_flags' => 'gzip',
153 'old_text' => gzdeflate( "Wiki est l'\xe9cole superieur !" ),
154 ]
155 ];
156 }
157
158 /**
159 * @covers Revision::getRevisionText
160 * @dataProvider provideGetRevisionTextWithGzipAndLegacyEncoding
161 */
162 public function testGetRevisionWithGzipAndLegacyEncoding( $expected, $encoding, $rowData ) {
163 $this->checkPHPExtension( 'zlib' );
164 $this->setMwGlobals( 'wgLegacyEncoding', $encoding );
165 $this->testGetRevisionText( $expected, $rowData );
166 }
167
168 /**
169 * @covers Revision::compressRevisionText
170 */
171 public function testCompressRevisionTextUtf8() {
172 $row = new stdClass;
173 $row->old_text = "Wiki est l'\xc3\xa9cole superieur !";
174 $row->old_flags = Revision::compressRevisionText( $row->old_text );
175 $this->assertTrue( false !== strpos( $row->old_flags, 'utf-8' ),
176 "Flags should contain 'utf-8'" );
177 $this->assertFalse( false !== strpos( $row->old_flags, 'gzip' ),
178 "Flags should not contain 'gzip'" );
179 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
180 $row->old_text, "Direct check" );
181 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
182 Revision::getRevisionText( $row ), "getRevisionText" );
183 }
184
185 /**
186 * @covers Revision::compressRevisionText
187 */
188 public function testCompressRevisionTextUtf8Gzip() {
189 $this->checkPHPExtension( 'zlib' );
190 $this->setMwGlobals( 'wgCompressRevisions', true );
191
192 $row = new stdClass;
193 $row->old_text = "Wiki est l'\xc3\xa9cole superieur !";
194 $row->old_flags = Revision::compressRevisionText( $row->old_text );
195 $this->assertTrue( false !== strpos( $row->old_flags, 'utf-8' ),
196 "Flags should contain 'utf-8'" );
197 $this->assertTrue( false !== strpos( $row->old_flags, 'gzip' ),
198 "Flags should contain 'gzip'" );
199 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
200 gzinflate( $row->old_text ), "Direct check" );
201 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
202 Revision::getRevisionText( $row ), "getRevisionText" );
203 }
204
205 /**
206 * @covers Revision::userJoinCond
207 */
208 public function testUserJoinCond() {
209 $this->assertEquals(
210 [ 'LEFT JOIN', [ 'rev_user != 0', 'user_id = rev_user' ] ],
211 Revision::userJoinCond()
212 );
213 }
214
215 /**
216 * @covers Revision::pageJoinCond
217 */
218 public function testPageJoinCond() {
219 $this->assertEquals(
220 [ 'INNER JOIN', [ 'page_id = rev_page' ] ],
221 Revision::pageJoinCond()
222 );
223 }
224
225 public function provideSelectFields() {
226 yield [
227 true,
228 [
229 'rev_id',
230 'rev_page',
231 'rev_text_id',
232 'rev_timestamp',
233 'rev_user_text',
234 'rev_user',
235 'rev_minor_edit',
236 'rev_deleted',
237 'rev_len',
238 'rev_parent_id',
239 'rev_sha1',
240 'rev_comment_text' => 'rev_comment',
241 'rev_comment_data' => 'NULL',
242 'rev_comment_cid' => 'NULL',
243 'rev_content_format',
244 'rev_content_model',
245 ]
246 ];
247 yield [
248 false,
249 [
250 'rev_id',
251 'rev_page',
252 'rev_text_id',
253 'rev_timestamp',
254 'rev_user_text',
255 'rev_user',
256 'rev_minor_edit',
257 'rev_deleted',
258 'rev_len',
259 'rev_parent_id',
260 'rev_sha1',
261 'rev_comment_text' => 'rev_comment',
262 'rev_comment_data' => 'NULL',
263 'rev_comment_cid' => 'NULL',
264 ]
265 ];
266 }
267
268 /**
269 * @dataProvider provideSelectFields
270 * @covers Revision::selectFields
271 * @todo a true unit test would mock CommentStore
272 */
273 public function testSelectFields( $contentHandlerUseDB, $expected ) {
274 $this->setMwGlobals( 'wgContentHandlerUseDB', $contentHandlerUseDB );
275 $this->assertEquals( $expected, Revision::selectFields() );
276 }
277
278 public function provideSelectArchiveFields() {
279 yield [
280 true,
281 [
282 'ar_id',
283 'ar_page_id',
284 'ar_rev_id',
285 'ar_text',
286 'ar_text_id',
287 'ar_timestamp',
288 'ar_user_text',
289 'ar_user',
290 'ar_minor_edit',
291 'ar_deleted',
292 'ar_len',
293 'ar_parent_id',
294 'ar_sha1',
295 'ar_comment_text' => 'ar_comment',
296 'ar_comment_data' => 'NULL',
297 'ar_comment_cid' => 'NULL',
298 'ar_content_format',
299 'ar_content_model',
300 ]
301 ];
302 yield [
303 false,
304 [
305 'ar_id',
306 'ar_page_id',
307 'ar_rev_id',
308 'ar_text',
309 'ar_text_id',
310 'ar_timestamp',
311 'ar_user_text',
312 'ar_user',
313 'ar_minor_edit',
314 'ar_deleted',
315 'ar_len',
316 'ar_parent_id',
317 'ar_sha1',
318 'ar_comment_text' => 'ar_comment',
319 'ar_comment_data' => 'NULL',
320 'ar_comment_cid' => 'NULL',
321 ]
322 ];
323 }
324
325 /**
326 * @dataProvider provideSelectArchiveFields
327 * @covers Revision::selectArchiveFields
328 * @todo a true unit test would mock CommentStore
329 */
330 public function testSelectArchiveFields( $contentHandlerUseDB, $expected ) {
331 $this->setMwGlobals( 'wgContentHandlerUseDB', $contentHandlerUseDB );
332 $this->assertEquals( $expected, Revision::selectArchiveFields() );
333 }
334
335 /**
336 * @covers Revision::selectTextFields
337 */
338 public function testSelectTextFields() {
339 $this->assertEquals(
340 [
341 'old_text',
342 'old_flags',
343 ],
344 Revision::selectTextFields()
345 );
346 }
347
348 /**
349 * @covers Revision::selectPageFields
350 */
351 public function testSelectPageFields() {
352 $this->assertEquals(
353 [
354 'page_namespace',
355 'page_title',
356 'page_id',
357 'page_latest',
358 'page_is_redirect',
359 'page_len',
360 ],
361 Revision::selectPageFields()
362 );
363 }
364
365 /**
366 * @covers Revision::selectUserFields
367 */
368 public function testSelectUserFields() {
369 $this->assertEquals(
370 [
371 'user_name',
372 ],
373 Revision::selectUserFields()
374 );
375 }
376
377 }