04841161153a3e873340f84ab1082aedf69cd815
[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 public function testGetContentText_TextContent( ) {
34 global $wgContentHandlerTextFallback;
35
36 $content = new WikitextContent( "hello world" );
37
38 $wgContentHandlerTextFallback = 'fail';
39 $text = ContentHandler::getContentText( $content );
40 $this->assertEquals( $content->getNativeData(), $text );
41
42 $wgContentHandlerTextFallback = 'serialize';
43 $text = ContentHandler::getContentText( $content );
44 $this->assertEquals( $content->serialize(), $text );
45
46 $wgContentHandlerTextFallback = 'ignore';
47 $text = ContentHandler::getContentText( $content );
48 $this->assertEquals( $content->getNativeData(), $text );
49 }
50
51 public function testGetContentText_NonTextContent( ) {
52 global $wgContentHandlerTextFallback;
53
54 $content = new DummyContentForTesting( "hello world" );
55
56 $wgContentHandlerTextFallback = 'fail';
57
58 try {
59 $text = ContentHandler::getContentText( $content );
60
61 $this->fail( "ContentHandler::getContentText should have thrown an exception for non-text Content object" );
62 } catch (MWException $ex) {
63 // as expected
64 }
65
66 $wgContentHandlerTextFallback = 'serialize';
67 $text = ContentHandler::getContentText( $content );
68 $this->assertEquals( $content->serialize(), $text );
69
70 $wgContentHandlerTextFallback = 'ignore';
71 $text = ContentHandler::getContentText( $content );
72 $this->assertNull( $text );
73 }
74
75 }
76
77 class DummyContentForTesting extends Content {
78
79 public function __construct( $data ) {
80 parent::__construct( "DUMMY" );
81
82 $this->data = $data;
83 }
84
85 public function serialize() {
86 return serialize( $this->data );
87 }
88
89 /**
90 * @return String a string representing the content in a way useful for building a full text search index.
91 * If no useful representation exists, this method returns an empty string.
92 */
93 public function getTextForSearchIndex()
94 {
95 return '';
96 }
97
98 /**
99 * @return String the wikitext to include when another page includes this content, or false if the content is not
100 * includable in a wikitext page.
101 */
102 public function getWikitextForTransclusion()
103 {
104 return false;
105 }
106
107 /**
108 * Returns a textual representation of the content suitable for use in edit summaries and log messages.
109 *
110 * @param int $maxlength maximum length of the summary text
111 * @return String the summary text
112 */
113 public function getTextForSummary($maxlength = 250)
114 {
115 return '';
116 }
117
118 /**
119 * Returns native represenation of the data. Interpretation depends on the data model used,
120 * as given by getDataModel().
121 *
122 * @return mixed the native representation of the content. Could be a string, a nested array
123 * structure, an object, a binary blob... anything, really.
124 */
125 public function getNativeData()
126 {
127 return $this->data;
128 }
129
130 /**
131 * returns the content's nominal size in bogo-bytes.
132 *
133 * @return int
134 */
135 public function getSize()
136 {
137 return 23;
138 }
139
140 /**
141 * Return a copy of this Content object. The following must be true for the object returned
142 * if $copy = $original->copy()
143 *
144 * * get_class($original) === get_class($copy)
145 * * $original->getModelName() === $copy->getModelName()
146 * * $original->equals( $copy )
147 *
148 * If and only if the Content object is imutable, the copy() method can and should
149 * return $this. That is, $copy === $original may be true, but only for imutable content
150 * objects.
151 *
152 * @return Content. A copy of this object
153 */
154 public function copy()
155 {
156 return $this;
157 }
158
159 /**
160 * Returns true if this content is countable as a "real" wiki page, provided
161 * that it's also in a countable location (e.g. a current revision in the main namespace).
162 *
163 * @param $hasLinks Bool: if it is known whether this content contains links, provide this information here,
164 * to avoid redundant parsing to find out.
165 * @return boolean
166 */
167 public function isCountable($hasLinks = null)
168 {
169 return false;
170 }
171
172 /**
173 * @param IContextSource $context
174 * @param null $revId
175 * @param null|ParserOptions $options
176 * @param Boolean $generateHtml whether to generate Html (default: true). If false,
177 * the result of calling getText() on the ParserOutput object returned by
178 * this method is undefined.
179 *
180 * @return ParserOutput
181 */
182 public function getParserOutput(IContextSource $context, $revId = null, ParserOptions $options = NULL, $generateHtml = true)
183 {
184 return new ParserOutput( $this->data );
185 }
186 }
187