47dcf6591cfb82b56a5b2e4dfe00e0dc49f9d6ba
[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 }