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