038842f2a71206afe0995eabf26143e2ff89b5db
[lhc/web/wiklou.git] / tests / phpunit / includes / ContentHandlerTest.php
1 <?php
2
3 class ContentHandlerTest extends MediaWikiTestCase {
4
5 public function dataGetDefaultModelFor() {
6 return array(
7 array( 'Foo', CONTENT_MODEL_WIKITEXT ),
8 array( 'Foo.js', CONTENT_MODEL_WIKITEXT ),
9 array( 'Foo/bar.js', CONTENT_MODEL_WIKITEXT ),
10 array( 'User:Foo', CONTENT_MODEL_WIKITEXT ),
11 array( 'User:Foo.js', CONTENT_MODEL_WIKITEXT ),
12 array( 'User:Foo/bar.js', CONTENT_MODEL_JAVASCRIPT ),
13 array( 'User:Foo/bar.css', CONTENT_MODEL_CSS ),
14 array( 'User talk:Foo/bar.css', CONTENT_MODEL_WIKITEXT ),
15 array( 'User:Foo/bar.js.xxx', CONTENT_MODEL_WIKITEXT ),
16 array( 'User:Foo/bar.xxx', CONTENT_MODEL_WIKITEXT ),
17 array( 'MediaWiki:Foo.js', CONTENT_MODEL_JAVASCRIPT ),
18 array( 'MediaWiki:Foo.css', CONTENT_MODEL_CSS ),
19 array( 'MediaWiki:Foo.JS', CONTENT_MODEL_WIKITEXT ),
20 array( 'MediaWiki:Foo.CSS', CONTENT_MODEL_WIKITEXT ),
21 array( 'MediaWiki:Foo.css.xxx', CONTENT_MODEL_WIKITEXT ),
22 );
23 }
24
25 /**
26 * @dataProvider dataGetDefaultModelFor
27 */
28 public function testGetDefaultModelFor( $title, $expectedModelName ) {
29 $title = Title::newFromText( $title );
30 $this->assertEquals( $expectedModelName, ContentHandler::getDefaultModelFor( $title ) );
31 }
32 /**
33 * @dataProvider dataGetDefaultModelFor
34 */
35 public function testGetForTitle( $title, $expectedContentModel ) {
36 $title = Title::newFromText( $title );
37 $handler = ContentHandler::getForTitle( $title );
38 $this->assertEquals( $expectedContentModel, $handler->getModelName() );
39 }
40
41 public function testGetContentText_Null( ) {
42 global $wgContentHandlerTextFallback;
43
44 $content = null;
45
46 $wgContentHandlerTextFallback = 'fail';
47 $text = ContentHandler::getContentText( $content );
48 $this->assertEquals( '', $text );
49
50 $wgContentHandlerTextFallback = 'serialize';
51 $text = ContentHandler::getContentText( $content );
52 $this->assertEquals( '', $text );
53
54 $wgContentHandlerTextFallback = 'ignore';
55 $text = ContentHandler::getContentText( $content );
56 $this->assertEquals( '', $text );
57 }
58
59 public function testGetContentText_TextContent( ) {
60 global $wgContentHandlerTextFallback;
61
62 $content = new WikitextContent( "hello world" );
63
64 $wgContentHandlerTextFallback = 'fail';
65 $text = ContentHandler::getContentText( $content );
66 $this->assertEquals( $content->getNativeData(), $text );
67
68 $wgContentHandlerTextFallback = 'serialize';
69 $text = ContentHandler::getContentText( $content );
70 $this->assertEquals( $content->serialize(), $text );
71
72 $wgContentHandlerTextFallback = 'ignore';
73 $text = ContentHandler::getContentText( $content );
74 $this->assertEquals( $content->getNativeData(), $text );
75 }
76
77 public function testGetContentText_NonTextContent( ) {
78 global $wgContentHandlerTextFallback;
79
80 $content = new DummyContentForTesting( "hello world" );
81
82 $wgContentHandlerTextFallback = 'fail';
83
84 try {
85 $text = ContentHandler::getContentText( $content );
86
87 $this->fail( "ContentHandler::getContentText should have thrown an exception for non-text Content object" );
88 } catch (MWException $ex) {
89 // as expected
90 }
91
92 $wgContentHandlerTextFallback = 'serialize';
93 $text = ContentHandler::getContentText( $content );
94 $this->assertEquals( $content->serialize(), $text );
95
96 $wgContentHandlerTextFallback = 'ignore';
97 $text = ContentHandler::getContentText( $content );
98 $this->assertNull( $text );
99 }
100
101 #public static function makeContent( $text, Title $title, $modelName = null, $format = null )
102
103 public function dataMakeContent() {
104 return array(
105 array( 'hallo', 'Test', null, null, CONTENT_MODEL_WIKITEXT, 'hallo', false ),
106 array( 'hallo', 'MediaWiki:Test.js', null, null, CONTENT_MODEL_JAVASCRIPT, 'hallo', false ),
107 array( serialize('hallo'), 'Dummy:Test', null, null, 'DUMMY', 'hallo', false ),
108
109 array( 'hallo', 'Test', null, 'application/x-wiki', CONTENT_MODEL_WIKITEXT, 'hallo', false ),
110 array( 'hallo', 'MediaWiki:Test.js', null, 'text/javascript', CONTENT_MODEL_JAVASCRIPT, 'hallo', false ),
111 array( serialize('hallo'), 'Dummy:Test', null, 'dummy', 'DUMMY', 'hallo', false ),
112
113 array( 'hallo', 'Test', CONTENT_MODEL_CSS, null, CONTENT_MODEL_CSS, 'hallo', false ),
114 array( 'hallo', 'MediaWiki:Test.js', CONTENT_MODEL_CSS, null, CONTENT_MODEL_CSS, 'hallo', false ),
115 array( serialize('hallo'), 'Dummy:Test', CONTENT_MODEL_CSS, null, CONTENT_MODEL_CSS, serialize('hallo'), false ),
116
117 array( 'hallo', 'Test', CONTENT_MODEL_WIKITEXT, 'dummy', null, null, true ),
118 array( 'hallo', 'MediaWiki:Test.js', CONTENT_MODEL_CSS, 'dummy', null, null, true ),
119 array( 'hallo', 'Dummy:Test', CONTENT_MODEL_JAVASCRIPT, 'dummy', null, null, true ),
120 );
121 }
122
123 /**
124 * @dataProvider dataMakeContent
125 */
126 public function testMakeContent( $data, $title, $modelName, $format, $expectedModelName, $expectedNativeData, $shouldFail ) {
127 global $wgExtraNamespaces, $wgNamespaceContentModels, $wgContentHandlers;
128
129 $title = Title::newFromText( $title );
130
131 try {
132 $content = ContentHandler::makeContent( $data, $title, $modelName, $format );
133
134 if ( $shouldFail ) $this->fail( "ContentHandler::makeContent should have failed!" );
135
136 $this->assertEquals( $expectedModelName, $content->getModelName(), 'bad model name' );
137 $this->assertEquals( $expectedNativeData, $content->getNativeData(), 'bads native data' );
138 } catch ( MWException $ex ) {
139 if ( !$shouldFail ) $this->fail( "ContentHandler::makeContent failed unexpectedly!" );
140 else $this->assertTrue( true ); // dummy, so we don't get the "test did not perform any assertions" message.
141 }
142
143 }
144
145
146 public function setup() {
147 global $wgExtraNamespaces, $wgNamespaceContentModels, $wgContentHandlers;
148
149 $wgExtraNamespaces[ 12312 ] = 'Dummy';
150 $wgExtraNamespaces[ 12313 ] = 'Dummy_talk';
151
152 $wgNamespaceContentModels[ 12312 ] = 'DUMMY';
153 $wgContentHandlers[ 'DUMMY' ] = 'DummyContentHandlerForTesting';
154 }
155
156 public function teardown() {
157 global $wgExtraNamespaces, $wgNamespaceContentModels, $wgContentHandlers;
158
159 unset( $wgExtraNamespaces[ 12312 ] );
160 unset( $wgExtraNamespaces[ 12313 ] );
161
162 unset( $wgNamespaceContentModels[ 12312 ] );
163 unset( $wgContentHandlers[ 'DUMMY' ] );
164 }
165
166 }
167
168 class DummyContentHandlerForTesting extends ContentHandler {
169
170 public function __construct( $dataModel ) {
171 parent::__construct( $dataModel, array('dummy') );
172 }
173
174 /**
175 * Serializes Content object of the type supported by this ContentHandler.
176 *
177 * @param Content $content the Content object to serialize
178 * @param null $format the desired serialization format
179 * @return String serialized form of the content
180 */
181 public function serializeContent( Content $content, $format = null )
182 {
183 return $content->serialize();
184 }
185
186 /**
187 * Unserializes a Content object of the type supported by this ContentHandler.
188 *
189 * @param $blob String serialized form of the content
190 * @param null $format the format used for serialization
191 * @return Content the Content object created by deserializing $blob
192 */
193 public function unserializeContent( $blob, $format = null )
194 {
195 $d = unserialize( $blob );
196 return new DummyContentForTesting( $d );
197 }
198
199 /**
200 * Creates an empty Content object of the type supported by this ContentHandler.
201 *
202 */
203 public function makeEmptyContent()
204 {
205 return new DummyContentForTesting( '' );
206 }
207 }
208
209 class DummyContentForTesting extends Content {
210
211 public function __construct( $data ) {
212 parent::__construct( "DUMMY" );
213
214 $this->data = $data;
215 }
216
217 public function serialize() {
218 return serialize( $this->data );
219 }
220
221 /**
222 * @return String a string representing the content in a way useful for building a full text search index.
223 * If no useful representation exists, this method returns an empty string.
224 */
225 public function getTextForSearchIndex()
226 {
227 return '';
228 }
229
230 /**
231 * @return String the wikitext to include when another page includes this content, or false if the content is not
232 * includable in a wikitext page.
233 */
234 public function getWikitextForTransclusion()
235 {
236 return false;
237 }
238
239 /**
240 * Returns a textual representation of the content suitable for use in edit summaries and log messages.
241 *
242 * @param int $maxlength maximum length of the summary text
243 * @return String the summary text
244 */
245 public function getTextForSummary( $maxlength = 250 )
246 {
247 return '';
248 }
249
250 /**
251 * Returns native represenation of the data. Interpretation depends on the data model used,
252 * as given by getDataModel().
253 *
254 * @return mixed the native representation of the content. Could be a string, a nested array
255 * structure, an object, a binary blob... anything, really.
256 */
257 public function getNativeData()
258 {
259 return $this->data;
260 }
261
262 /**
263 * returns the content's nominal size in bogo-bytes.
264 *
265 * @return int
266 */
267 public function getSize()
268 {
269 return 23;
270 }
271
272 /**
273 * Return a copy of this Content object. The following must be true for the object returned
274 * if $copy = $original->copy()
275 *
276 * * get_class($original) === get_class($copy)
277 * * $original->getModelName() === $copy->getModelName()
278 * * $original->equals( $copy )
279 *
280 * If and only if the Content object is imutable, the copy() method can and should
281 * return $this. That is, $copy === $original may be true, but only for imutable content
282 * objects.
283 *
284 * @return Content. A copy of this object
285 */
286 public function copy()
287 {
288 return $this;
289 }
290
291 /**
292 * Returns true if this content is countable as a "real" wiki page, provided
293 * that it's also in a countable location (e.g. a current revision in the main namespace).
294 *
295 * @param $hasLinks Bool: if it is known whether this content contains links, provide this information here,
296 * to avoid redundant parsing to find out.
297 * @return boolean
298 */
299 public function isCountable( $hasLinks = null )
300 {
301 return false;
302 }
303
304 /**
305 * @param IContextSource $context
306 * @param null $revId
307 * @param null|ParserOptions $options
308 * @param Boolean $generateHtml whether to generate Html (default: true). If false,
309 * the result of calling getText() on the ParserOutput object returned by
310 * this method is undefined.
311 *
312 * @return ParserOutput
313 */
314 public function getParserOutput( IContextSource $context, $revId = null, ParserOptions $options = NULL, $generateHtml = true )
315 {
316 return new ParserOutput( $this->data );
317 }
318 }
319