Merge "FauxRequest: don’t override getValues()"
[lhc/web/wiklou.git] / tests / phpunit / includes / content / UnknownContentTest.php
1 <?php
2
3 /**
4 * @group ContentHandler
5 */
6 class UnknownContentTest extends MediaWikiLangTestCase {
7
8 /**
9 * @param string $data
10 * @return UnknownContent
11 */
12 public function newContent( $data, $type = 'xyzzy' ) {
13 return new UnknownContent( $data, $type );
14 }
15
16 /**
17 * @covers UnknownContent::getParserOutput
18 */
19 public function testGetParserOutput() {
20 $this->setUserLang( 'en' );
21 $this->setContentLang( 'qqx' );
22
23 $title = Title::newFromText( 'Test' );
24 $content = $this->newContent( 'Horkyporky' );
25
26 $po = $content->getParserOutput( $title );
27 $html = $po->getText();
28 $html = preg_replace( '#<!--.*?-->#sm', '', $html ); // strip comments
29
30 $this->assertNotContains( 'Horkyporky', $html );
31 $this->assertNotContains( '(unsupported-content-model)', $html );
32 }
33
34 /**
35 * @covers UnknownContent::preSaveTransform
36 */
37 public function testPreSaveTransform() {
38 $title = Title::newFromText( 'Test' );
39 $user = $this->getTestUser()->getUser();
40 $content = $this->newContent( 'Horkyporky ~~~' );
41
42 $options = new ParserOptions();
43
44 $this->assertSame( $content, $content->preSaveTransform( $title, $user, $options ) );
45 }
46
47 /**
48 * @covers UnknownContent::preloadTransform
49 */
50 public function testPreloadTransform() {
51 $title = Title::newFromText( 'Test' );
52 $content = $this->newContent( 'Horkyporky ~~~' );
53
54 $options = new ParserOptions();
55
56 $this->assertSame( $content, $content->preloadTransform( $title, $options ) );
57 }
58
59 /**
60 * @covers UnknownContent::getRedirectTarget
61 */
62 public function testGetRedirectTarget() {
63 $content = $this->newContent( '#REDIRECT [[Horkyporky]]' );
64 $this->assertNull( $content->getRedirectTarget() );
65 }
66
67 /**
68 * @covers UnknownContent::isRedirect
69 */
70 public function testIsRedirect() {
71 $content = $this->newContent( '#REDIRECT [[Horkyporky]]' );
72 $this->assertFalse( $content->isRedirect() );
73 }
74
75 /**
76 * @covers UnknownContent::isCountable
77 */
78 public function testIsCountable() {
79 $content = $this->newContent( '[[Horkyporky]]' );
80 $this->assertFalse( $content->isCountable( true ) );
81 }
82
83 /**
84 * @covers UnknownContent::getTextForSummary
85 */
86 public function testGetTextForSummary() {
87 $content = $this->newContent( 'Horkyporky' );
88 $this->assertSame( '', $content->getTextForSummary() );
89 }
90
91 /**
92 * @covers UnknownContent::getTextForSearchIndex
93 */
94 public function testGetTextForSearchIndex() {
95 $content = $this->newContent( 'Horkyporky' );
96 $this->assertSame( '', $content->getTextForSearchIndex() );
97 }
98
99 /**
100 * @covers UnknownContent::copy
101 */
102 public function testCopy() {
103 $content = $this->newContent( 'hello world.' );
104 $copy = $content->copy();
105
106 $this->assertSame( $content, $copy );
107 }
108
109 /**
110 * @covers UnknownContent::getSize
111 */
112 public function testGetSize() {
113 $content = $this->newContent( 'hello world.' );
114
115 $this->assertEquals( 12, $content->getSize() );
116 }
117
118 /**
119 * @covers UnknownContent::getData
120 */
121 public function testGetData() {
122 $content = $this->newContent( 'hello world.' );
123
124 $this->assertEquals( 'hello world.', $content->getData() );
125 }
126
127 /**
128 * @covers UnknownContent::getNativeData
129 */
130 public function testGetNativeData() {
131 $content = $this->newContent( 'hello world.' );
132
133 $this->assertEquals( 'hello world.', $content->getNativeData() );
134 }
135
136 /**
137 * @covers UnknownContent::getWikitextForTransclusion
138 */
139 public function testGetWikitextForTransclusion() {
140 $content = $this->newContent( 'hello world.' );
141
142 $this->assertFalse( $content->getWikitextForTransclusion() );
143 }
144
145 /**
146 * @covers UnknownContent::getModel
147 */
148 public function testGetModel() {
149 $content = $this->newContent( "hello world.", 'horkyporky' );
150
151 $this->assertEquals( 'horkyporky', $content->getModel() );
152 }
153
154 /**
155 * @covers UnknownContent::getContentHandler
156 */
157 public function testGetContentHandler() {
158 $this->mergeMwGlobalArrayValue(
159 'wgContentHandlers',
160 [ 'horkyporky' => 'UnknownContentHandler' ]
161 );
162
163 $content = $this->newContent( "hello world.", 'horkyporky' );
164
165 $this->assertInstanceOf( UnknownContentHandler::class, $content->getContentHandler() );
166 $this->assertEquals( 'horkyporky', $content->getContentHandler()->getModelID() );
167 }
168
169 public static function dataIsEmpty() {
170 return [
171 [ '', true ],
172 [ ' ', false ],
173 [ '0', false ],
174 [ 'hallo welt.', false ],
175 ];
176 }
177
178 /**
179 * @dataProvider dataIsEmpty
180 * @covers UnknownContent::isEmpty
181 */
182 public function testIsEmpty( $text, $empty ) {
183 $content = $this->newContent( $text );
184
185 $this->assertEquals( $empty, $content->isEmpty() );
186 }
187
188 public function provideEquals() {
189 return [
190 [ new UnknownContent( "hallo", 'horky' ), null, false ],
191 [ new UnknownContent( "hallo", 'horky' ), new UnknownContent( "hallo", 'horky' ), true ],
192 [ new UnknownContent( "hallo", 'horky' ), new UnknownContent( "hallo", 'xyzzy' ), false ],
193 [ new UnknownContent( "hallo", 'horky' ), new JavaScriptContent( "hallo" ), false ],
194 [ new UnknownContent( "hallo", 'horky' ), new WikitextContent( "hallo" ), false ],
195 ];
196 }
197
198 /**
199 * @dataProvider provideEquals
200 * @covers UnknownContent::equals
201 */
202 public function testEquals( Content $a, Content $b = null, $equal = false ) {
203 $this->assertEquals( $equal, $a->equals( $b ) );
204 }
205
206 public static function provideConvert() {
207 return [
208 [ // #0
209 'Hallo Welt',
210 CONTENT_MODEL_WIKITEXT,
211 'lossless',
212 'Hallo Welt'
213 ],
214 [ // #1
215 'Hallo Welt',
216 CONTENT_MODEL_WIKITEXT,
217 'lossless',
218 'Hallo Welt'
219 ],
220 [ // #1
221 'Hallo Welt',
222 CONTENT_MODEL_CSS,
223 'lossless',
224 'Hallo Welt'
225 ],
226 [ // #1
227 'Hallo Welt',
228 CONTENT_MODEL_JAVASCRIPT,
229 'lossless',
230 'Hallo Welt'
231 ],
232 ];
233 }
234
235 /**
236 * @covers UnknownContent::convert
237 */
238 public function testConvert() {
239 $content = $this->newContent( 'More horkyporky?' );
240
241 $this->assertFalse( $content->convert( CONTENT_MODEL_TEXT ) );
242 }
243
244 /**
245 * @covers UnknownContent::__construct
246 * @covers UnknownContentHandler::serializeContent
247 */
248 public function testSerialize() {
249 $this->mergeMwGlobalArrayValue(
250 'wgContentHandlers',
251 [ 'horkyporky' => 'UnknownContentHandler' ]
252 );
253
254 $content = $this->newContent( 'Hörkypörky', 'horkyporky' );
255
256 $this->assertSame( 'Hörkypörky', $content->serialize() );
257 }
258
259 }