f9a400cdd3838fe444ba254f273dbdd3deaf3dd1
[lhc/web/wiklou.git] / tests / phpunit / includes / RevisionTest.php
1 <?php
2
3 /**
4 * @group ContentHandler
5 */
6 class RevisionTest extends MediaWikiTestCase {
7 var $saveGlobals = array();
8
9 function setUp() {
10 global $wgContLang;
11 $wgContLang = Language::factory( 'en' );
12
13 $globalSet = array(
14 'wgLegacyEncoding' => false,
15 'wgCompressRevisions' => false,
16
17 'wgContentHandlerTextFallback' => $GLOBALS['wgContentHandlerTextFallback'],
18 'wgExtraNamespaces' => $GLOBALS['wgExtraNamespaces'],
19 'wgNamespaceContentModels' => $GLOBALS['wgNamespaceContentModels'],
20 'wgContentHandlers' => $GLOBALS['wgContentHandlers'],
21 );
22
23 foreach ( $globalSet as $var => $data ) {
24 $this->saveGlobals[$var] = $GLOBALS[$var];
25 $GLOBALS[$var] = $data;
26 }
27
28 global $wgExtraNamespaces, $wgNamespaceContentModels, $wgContentHandlers, $wgContLang;
29 $wgExtraNamespaces[ 12312 ] = 'Dummy';
30 $wgExtraNamespaces[ 12313 ] = 'Dummy_talk';
31
32 $wgNamespaceContentModels[ 12312 ] = "testing";
33 $wgContentHandlers[ "testing" ] = 'DummyContentHandlerForTesting';
34
35 MWNamespace::getCanonicalNamespaces( true ); # reset namespace cache
36 $wgContLang->resetNamespaces(); # reset namespace cache
37
38 global $wgContentHandlerTextFallback;
39 $wgContentHandlerTextFallback = 'ignore';
40 }
41
42 function tearDown() {
43 global $wgContLang;
44
45 foreach ( $this->saveGlobals as $var => $data ) {
46 $GLOBALS[$var] = $data;
47 }
48
49 MWNamespace::getCanonicalNamespaces( true ); # reset namespace cache
50 $wgContLang->resetNamespaces(); # reset namespace cache
51 }
52
53 function testGetRevisionText() {
54 $row = new stdClass;
55 $row->old_flags = '';
56 $row->old_text = 'This is a bunch of revision text.';
57 $this->assertEquals(
58 'This is a bunch of revision text.',
59 Revision::getRevisionText( $row ) );
60 }
61
62 function testGetRevisionTextGzip() {
63 if ( !function_exists( 'gzdeflate' ) ) {
64 $this->markTestSkipped( 'Gzip compression is not enabled (requires zlib).' );
65 } else {
66 $row = new stdClass;
67 $row->old_flags = 'gzip';
68 $row->old_text = gzdeflate( 'This is a bunch of revision text.' );
69 $this->assertEquals(
70 'This is a bunch of revision text.',
71 Revision::getRevisionText( $row ) );
72 }
73 }
74
75 function testGetRevisionTextUtf8Native() {
76 $row = new stdClass;
77 $row->old_flags = 'utf-8';
78 $row->old_text = "Wiki est l'\xc3\xa9cole superieur !";
79 $GLOBALS['wgLegacyEncoding'] = 'iso-8859-1';
80 $this->assertEquals(
81 "Wiki est l'\xc3\xa9cole superieur !",
82 Revision::getRevisionText( $row ) );
83 }
84
85 function testGetRevisionTextUtf8Legacy() {
86 $row = new stdClass;
87 $row->old_flags = '';
88 $row->old_text = "Wiki est l'\xe9cole superieur !";
89 $GLOBALS['wgLegacyEncoding'] = 'iso-8859-1';
90 $this->assertEquals(
91 "Wiki est l'\xc3\xa9cole superieur !",
92 Revision::getRevisionText( $row ) );
93 }
94
95 function testGetRevisionTextUtf8NativeGzip() {
96 if ( !function_exists( 'gzdeflate' ) ) {
97 $this->markTestSkipped( 'Gzip compression is not enabled (requires zlib).' );
98 } else {
99 $row = new stdClass;
100 $row->old_flags = 'gzip,utf-8';
101 $row->old_text = gzdeflate( "Wiki est l'\xc3\xa9cole superieur !" );
102 $GLOBALS['wgLegacyEncoding'] = 'iso-8859-1';
103 $this->assertEquals(
104 "Wiki est l'\xc3\xa9cole superieur !",
105 Revision::getRevisionText( $row ) );
106 }
107 }
108
109 function testGetRevisionTextUtf8LegacyGzip() {
110 if ( !function_exists( 'gzdeflate' ) ) {
111 $this->markTestSkipped( 'Gzip compression is not enabled (requires zlib).' );
112 } else {
113 $row = new stdClass;
114 $row->old_flags = 'gzip';
115 $row->old_text = gzdeflate( "Wiki est l'\xe9cole superieur !" );
116 $GLOBALS['wgLegacyEncoding'] = 'iso-8859-1';
117 $this->assertEquals(
118 "Wiki est l'\xc3\xa9cole superieur !",
119 Revision::getRevisionText( $row ) );
120 }
121 }
122
123 function testCompressRevisionTextUtf8() {
124 $row = new stdClass;
125 $row->old_text = "Wiki est l'\xc3\xa9cole superieur !";
126 $row->old_flags = Revision::compressRevisionText( $row->old_text );
127 $this->assertTrue( false !== strpos( $row->old_flags, 'utf-8' ),
128 "Flags should contain 'utf-8'" );
129 $this->assertFalse( false !== strpos( $row->old_flags, 'gzip' ),
130 "Flags should not contain 'gzip'" );
131 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
132 $row->old_text, "Direct check" );
133 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
134 Revision::getRevisionText( $row ), "getRevisionText" );
135 }
136
137 function testCompressRevisionTextUtf8Gzip() {
138 $GLOBALS['wgCompressRevisions'] = true;
139 $row = new stdClass;
140 $row->old_text = "Wiki est l'\xc3\xa9cole superieur !";
141 $row->old_flags = Revision::compressRevisionText( $row->old_text );
142 $this->assertTrue( false !== strpos( $row->old_flags, 'utf-8' ),
143 "Flags should contain 'utf-8'" );
144 $this->assertTrue( false !== strpos( $row->old_flags, 'gzip' ),
145 "Flags should contain 'gzip'" );
146 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
147 gzinflate( $row->old_text ), "Direct check" );
148 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
149 Revision::getRevisionText( $row ), "getRevisionText" );
150 }
151
152 # =================================================================================================================
153
154 /**
155 * @param string $text
156 * @param string $title
157 * @param string $model
158 * @return Revision
159 */
160 function newTestRevision( $text, $title = "Test", $model = CONTENT_MODEL_WIKITEXT, $format = null ) {
161 if ( is_string( $title ) ) {
162 $title = Title::newFromText( $title );
163 }
164
165 $content = ContentHandler::makeContent( $text, $title, $model, $format );
166
167 $rev = new Revision(
168 array(
169 'id' => 42,
170 'page' => 23,
171 'title' => $title,
172
173 'content' => $content,
174 'length' => $content->getSize(),
175 'comment' => "testing",
176 'minor_edit' => false,
177
178 'content_format' => $format,
179 )
180 );
181
182 return $rev;
183 }
184
185 function dataGetContentModel() {
186 return array(
187 array( 'hello world', 'Hello', null, null, CONTENT_MODEL_WIKITEXT ),
188 array( 'hello world', 'User:hello/there.css', null, null, CONTENT_MODEL_CSS ),
189 array( serialize('hello world'), 'Dummy:Hello', null, null, "testing" ),
190 );
191 }
192
193 /**
194 * @group Database
195 * @dataProvider dataGetContentModel
196 */
197 function testGetContentModel( $text, $title, $model, $format, $expectedModel ) {
198 $rev = $this->newTestRevision( $text, $title, $model, $format );
199
200 $this->assertEquals( $expectedModel, $rev->getContentModel() );
201 }
202
203 function dataGetContentFormat() {
204 return array(
205 array( 'hello world', 'Hello', null, null, CONTENT_FORMAT_WIKITEXT ),
206 array( 'hello world', 'Hello', CONTENT_MODEL_CSS, null, CONTENT_FORMAT_CSS ),
207 array( 'hello world', 'User:hello/there.css', null, null, CONTENT_FORMAT_CSS ),
208 array( serialize('hello world'), 'Dummy:Hello', null, null, "testing" ),
209 );
210 }
211
212 /**
213 * @group Database
214 * @dataProvider dataGetContentFormat
215 */
216 function testGetContentFormat( $text, $title, $model, $format, $expectedFormat ) {
217 $rev = $this->newTestRevision( $text, $title, $model, $format );
218
219 $this->assertEquals( $expectedFormat, $rev->getContentFormat() );
220 }
221
222 function dataGetContentHandler() {
223 return array(
224 array( 'hello world', 'Hello', null, null, 'WikitextContentHandler' ),
225 array( 'hello world', 'User:hello/there.css', null, null, 'CssContentHandler' ),
226 array( serialize('hello world'), 'Dummy:Hello', null, null, 'DummyContentHandlerForTesting' ),
227 );
228 }
229
230 /**
231 * @group Database
232 * @dataProvider dataGetContentHandler
233 */
234 function testGetContentHandler( $text, $title, $model, $format, $expectedClass ) {
235 $rev = $this->newTestRevision( $text, $title, $model, $format );
236
237 $this->assertEquals( $expectedClass, get_class( $rev->getContentHandler() ) );
238 }
239
240 function dataGetContent() {
241 return array(
242 array( 'hello world', 'Hello', null, null, Revision::FOR_PUBLIC, 'hello world' ),
243 array( serialize('hello world'), 'Hello', "testing", null, Revision::FOR_PUBLIC, serialize('hello world') ),
244 array( serialize('hello world'), 'Dummy:Hello', null, null, Revision::FOR_PUBLIC, serialize('hello world') ),
245 );
246 }
247
248 /**
249 * @group Database
250 * @dataProvider dataGetContent
251 */
252 function testGetContent( $text, $title, $model, $format, $audience, $expectedSerialization ) {
253 $rev = $this->newTestRevision( $text, $title, $model, $format );
254 $content = $rev->getContent( $audience );
255
256 $this->assertEquals( $expectedSerialization, is_null( $content ) ? null : $content->serialize( $format ) );
257 }
258
259 function dataGetText() {
260 return array(
261 array( 'hello world', 'Hello', null, null, Revision::FOR_PUBLIC, 'hello world' ),
262 array( serialize('hello world'), 'Hello', "testing", null, Revision::FOR_PUBLIC, null ),
263 array( serialize('hello world'), 'Dummy:Hello', null, null, Revision::FOR_PUBLIC, null ),
264 );
265 }
266
267 /**
268 * @group Database
269 * @dataProvider dataGetText
270 */
271 function testGetText( $text, $title, $model, $format, $audience, $expectedText ) {
272 $rev = $this->newTestRevision( $text, $title, $model, $format );
273
274 $this->assertEquals( $expectedText, $rev->getText( $audience ) );
275 }
276
277 /**
278 * @group Database
279 * @dataProvider dataGetText
280 */
281 function testGetRawText( $text, $title, $model, $format, $audience, $expectedText ) {
282 $rev = $this->newTestRevision( $text, $title, $model, $format );
283
284 $this->assertEquals( $expectedText, $rev->getRawText( $audience ) );
285 }
286
287
288 public function dataGetSize( ) {
289 return array(
290 array( "hello world.", null, 12 ),
291 array( serialize( "hello world." ), "testing", 12 ),
292 );
293 }
294
295 /**
296 * @covers Revision::getSize
297 * @group Database
298 * @dataProvider dataGetSize
299 */
300 public function testGetSize( $text, $model, $expected_size )
301 {
302 $rev = $this->newTestRevision( $text, 'RevisionTest_testGetSize', $model );
303 $this->assertEquals( $expected_size, $rev->getSize() );
304 }
305
306 public function dataGetSha1( ) {
307 return array(
308 array( "hello world.", null, Revision::base36Sha1( "hello world." ) ),
309 array( serialize( "hello world." ), "testing", Revision::base36Sha1( serialize( "hello world." ) ) ),
310 );
311 }
312
313 /**
314 * @covers Revision::getSha1
315 * @group Database
316 * @dataProvider dataGetSha1
317 */
318 public function testGetSha1( $text, $model, $expected_hash )
319 {
320 $rev = $this->newTestRevision( $text, 'RevisionTest_testGetSha1', $model );
321 $this->assertEquals( $expected_hash, $rev->getSha1() );
322 }
323
324 public function testConstructWithText() {
325 $rev = new Revision( array(
326 'text' => 'hello world.',
327 'content_model' => CONTENT_MODEL_JAVASCRIPT
328 ));
329
330 $this->assertNotNull( $rev->getText(), 'no content text' );
331 $this->assertNotNull( $rev->getContent(), 'no content object available' );
332 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT, $rev->getContent()->getModel() );
333 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT, $rev->getContentModel() );
334 }
335
336 public function testConstructWithContent() {
337 $title = Title::newFromText( 'RevisionTest_testConstructWithContent' );
338
339 $rev = new Revision( array(
340 'content' => ContentHandler::makeContent( 'hello world.', $title, CONTENT_MODEL_JAVASCRIPT ),
341 ));
342
343 $this->assertNotNull( $rev->getText(), 'no content text' );
344 $this->assertNotNull( $rev->getContent(), 'no content object available' );
345 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT, $rev->getContent()->getModel() );
346 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT, $rev->getContentModel() );
347 }
348
349 }
350
351