4 * @group ContentHandler
6 class RevisionUnitTest
extends MediaWikiTestCase
{
8 public function provideConstructFromArray() {
11 'text' => 'hello world.',
12 'content_model' => CONTENT_MODEL_JAVASCRIPT
15 yield
'with content' => [
17 'content' => new JavaScriptContent( 'hellow world.' )
23 * @dataProvider provideConstructFromArray
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() );
32 public function provideConstructFromArrayThrowsExceptions() {
33 yield
'content and text_id both not empty' => [
35 'content' => new WikitextContent( 'GOAT' ),
36 'text_id' => 'someid',
38 new MWException( "Text already stored in external store (id someid), " .
39 "can't serialize content object" )
41 yield
'with bad content object (class)' => [
42 [ 'content' => new stdClass() ],
43 new MWException( '`content` field must contain a Content object.' )
45 yield
'with bad content object (string)' => [
46 [ 'content' => 'ImAGoat' ],
47 new MWException( '`content` field must contain a Content object.' )
49 yield
'bad row format' => [
50 'imastring, not a row',
51 new MWException( 'Revision constructor passed invalid row format.' )
56 * @dataProvider provideConstructFromArrayThrowsExceptions
58 public function testConstructFromArrayThrowsExceptions( $rowArray, Exception
$expectedException ) {
59 $this->setExpectedException(
60 get_class( $expectedException ),
61 $expectedException->getMessage(),
62 $expectedException->getCode()
64 new Revision( $rowArray );
67 public function provideGetRevisionText() {
68 yield
'Generic test' => [
69 'This is a goat of revision text.',
72 'old_text' => 'This is a goat of revision text.',
78 * @covers Revision::getRevisionText
79 * @dataProvider provideGetRevisionText
81 public function testGetRevisionText( $expected, $rowData, $prefix = 'old_', $wiki = false ) {
84 Revision
::getRevisionText( (object)$rowData, $prefix, $wiki ) );
87 public function provideGetRevisionTextWithZlibExtension() {
88 yield
'Generic gzip test' => [
89 'This is a small goat of revision text.',
91 'old_flags' => 'gzip',
92 'old_text' => gzdeflate( 'This is a small goat of revision text.' ),
98 * @covers Revision::getRevisionText
99 * @dataProvider provideGetRevisionTextWithZlibExtension
101 public function testGetRevisionWithZlibExtension( $expected, $rowData ) {
102 $this->checkPHPExtension( 'zlib' );
103 $this->testGetRevisionText( $expected, $rowData );
106 public function provideGetRevisionTextWithLegacyEncoding() {
107 yield
'Utf8Native' => [
108 "Wiki est l'\xc3\xa9cole superieur !",
111 'old_flags' => 'utf-8',
112 'old_text' => "Wiki est l'\xc3\xa9cole superieur !",
115 yield
'Utf8Legacy' => [
116 "Wiki est l'\xc3\xa9cole superieur !",
120 'old_text' => "Wiki est l'\xe9cole superieur !",
126 * @covers Revision::getRevisionText
127 * @dataProvider provideGetRevisionTextWithLegacyEncoding
129 public function testGetRevisionWithLegacyEncoding( $expected, $encoding, $rowData ) {
130 $this->setMwGlobals( 'wgLegacyEncoding', $encoding );
131 $this->testGetRevisionText( $expected, $rowData );
134 public function provideGetRevisionTextWithGzipAndLegacyEncoding() {
137 * Do not set the external flag!
138 * Otherwise, getRevisionText will hit the live database (if ExternalStore is enabled)!
140 yield
'Utf8NativeGzip' => [
141 "Wiki est l'\xc3\xa9cole superieur !",
144 'old_flags' => 'gzip,utf-8',
145 'old_text' => gzdeflate( "Wiki est l'\xc3\xa9cole superieur !" ),
148 yield
'Utf8LegacyGzip' => [
149 "Wiki est l'\xc3\xa9cole superieur !",
152 'old_flags' => 'gzip',
153 'old_text' => gzdeflate( "Wiki est l'\xe9cole superieur !" ),
159 * @covers Revision::getRevisionText
160 * @dataProvider provideGetRevisionTextWithGzipAndLegacyEncoding
162 public function testGetRevisionWithGzipAndLegacyEncoding( $expected, $encoding, $rowData ) {
163 $this->checkPHPExtension( 'zlib' );
164 $this->setMwGlobals( 'wgLegacyEncoding', $encoding );
165 $this->testGetRevisionText( $expected, $rowData );
169 * @covers Revision::compressRevisionText
171 public function testCompressRevisionTextUtf8() {
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" );
186 * @covers Revision::compressRevisionText
188 public function testCompressRevisionTextUtf8Gzip() {
189 $this->checkPHPExtension( 'zlib' );
190 $this->setMwGlobals( 'wgCompressRevisions', true );
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" );
206 * @covers Revision::userJoinCond
208 public function testUserJoinCond() {
210 [ 'LEFT JOIN', [ 'rev_user != 0', 'user_id = rev_user' ] ],
211 Revision
::userJoinCond()
216 * @covers Revision::pageJoinCond
218 public function testPageJoinCond() {
220 [ 'INNER JOIN', [ 'page_id = rev_page' ] ],
221 Revision
::pageJoinCond()
225 public function provideSelectFields() {
240 'rev_comment_text' => 'rev_comment',
241 'rev_comment_data' => 'NULL',
242 'rev_comment_cid' => 'NULL',
243 'rev_content_format',
261 'rev_comment_text' => 'rev_comment',
262 'rev_comment_data' => 'NULL',
263 'rev_comment_cid' => 'NULL',
269 * @dataProvider provideSelectFields
270 * @covers Revision::selectFields
271 * @todo a true unit test would mock CommentStore
273 public function testSelectFields( $contentHandlerUseDB, $expected ) {
274 $this->setMwGlobals( 'wgContentHandlerUseDB', $contentHandlerUseDB );
275 $this->assertEquals( $expected, Revision
::selectFields() );
278 public function provideSelectArchiveFields() {
295 'ar_comment_text' => 'ar_comment',
296 'ar_comment_data' => 'NULL',
297 'ar_comment_cid' => 'NULL',
318 'ar_comment_text' => 'ar_comment',
319 'ar_comment_data' => 'NULL',
320 'ar_comment_cid' => 'NULL',
326 * @dataProvider provideSelectArchiveFields
327 * @covers Revision::selectArchiveFields
328 * @todo a true unit test would mock CommentStore
330 public function testSelectArchiveFields( $contentHandlerUseDB, $expected ) {
331 $this->setMwGlobals( 'wgContentHandlerUseDB', $contentHandlerUseDB );
332 $this->assertEquals( $expected, Revision
::selectArchiveFields() );
336 * @covers Revision::selectTextFields
338 public function testSelectTextFields() {
344 Revision
::selectTextFields()
349 * @covers Revision::selectPageFields
351 public function testSelectPageFields() {
361 Revision
::selectPageFields()
366 * @covers Revision::selectUserFields
368 public function testSelectUserFields() {
373 Revision
::selectUserFields()