e10d7067b3b03575eee128e651a110854ff753ac
[lhc/web/wiklou.git] / tests / phpunit / includes / JavascriptContentTest.php
1 <?php
2
3 /**
4 * @group ContentHandler
5 *
6 * @group Database
7 * ^--- needed, because we do need the database to test link updates
8 */
9 class JavascriptContentTest extends TextContentTest {
10
11 public function newContent( $text ) {
12 return new JavascriptContent( $text );
13 }
14
15
16 public function dataGetParserOutput() {
17 return array(
18 array(
19 "MediaWiki:Test.js",
20 null,
21 "hello <world>\n",
22 "<pre class=\"mw-code mw-js\" dir=\"ltr\">\nhello &lt;world&gt;\n\n</pre>" ),
23
24 array(
25 "MediaWiki:Test.js",
26 null,
27 "hello(); // [[world]]\n",
28 "<pre class=\"mw-code mw-js\" dir=\"ltr\">\nhello(); // [[world]]\n\n</pre>",
29 array( 'Links' => array( // NOTE: assumes default settings for $wgTextModelsToParse
30 array( 'World' => 0 ) ) ) ),
31
32 // @todo: more...?
33 );
34 }
35
36 public function dataGetSection() {
37 return array(
38 array( WikitextContentTest::$sections,
39 "0",
40 null
41 ),
42 array( WikitextContentTest::$sections,
43 "2",
44 null
45 ),
46 array( WikitextContentTest::$sections,
47 "8",
48 null
49 ),
50 );
51 }
52
53 public function dataReplaceSection() {
54 return array(
55 array( WikitextContentTest::$sections,
56 "0",
57 "No more",
58 null,
59 null
60 ),
61 array( WikitextContentTest::$sections,
62 "",
63 "No more",
64 null,
65 null
66 ),
67 array( WikitextContentTest::$sections,
68 "2",
69 "== TEST ==\nmore fun",
70 null,
71 null
72 ),
73 array( WikitextContentTest::$sections,
74 "8",
75 "No more",
76 null,
77 null
78 ),
79 array( WikitextContentTest::$sections,
80 "new",
81 "No more",
82 "New",
83 null
84 ),
85 );
86 }
87
88 public function testAddSectionHeader( ) {
89 $content = $this->newContent( 'hello world' );
90 $c = $content->addSectionHeader( 'test' );
91
92 $this->assertTrue( $content->equals( $c ) );
93 }
94
95 // XXX: currently, preSaveTransform is applied to scripts. this may change or become optional.
96 public function dataPreSaveTransform() {
97 return array(
98 array( 'hello this is ~~~',
99 "hello this is [[Special:Contributions/127.0.0.1|127.0.0.1]]",
100 ),
101 array( 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
102 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
103 ),
104 );
105 }
106
107 public function dataPreloadTransform() {
108 return array(
109 array( 'hello this is ~~~',
110 "hello this is ~~~",
111 ),
112 array( 'hello \'\'this\'\' is <noinclude>foo</noinclude><includeonly>bar</includeonly>',
113 'hello \'\'this\'\' is <noinclude>foo</noinclude><includeonly>bar</includeonly>',
114 ),
115 );
116 }
117
118 public function dataGetRedirectTarget() {
119 return array(
120 array( '#REDIRECT [[Test]]',
121 null,
122 ),
123 array( '#REDIRECT Test',
124 null,
125 ),
126 array( '* #REDIRECT [[Test]]',
127 null,
128 ),
129 );
130 }
131
132 /**
133 * @todo: test needs database!
134 */
135 /*
136 public function getRedirectChain() {
137 $text = $this->getNativeData();
138 return Title::newFromRedirectArray( $text );
139 }
140 */
141
142 /**
143 * @todo: test needs database!
144 */
145 /*
146 public function getUltimateRedirectTarget() {
147 $text = $this->getNativeData();
148 return Title::newFromRedirectRecurse( $text );
149 }
150 */
151
152
153 public function dataIsCountable() {
154 return array(
155 array( '',
156 null,
157 'any',
158 true
159 ),
160 array( 'Foo',
161 null,
162 'any',
163 true
164 ),
165 array( 'Foo',
166 null,
167 'comma',
168 false
169 ),
170 array( 'Foo, bar',
171 null,
172 'comma',
173 false
174 ),
175 array( 'Foo',
176 null,
177 'link',
178 false
179 ),
180 array( 'Foo [[bar]]',
181 null,
182 'link',
183 false
184 ),
185 array( 'Foo',
186 true,
187 'link',
188 false
189 ),
190 array( 'Foo [[bar]]',
191 false,
192 'link',
193 false
194 ),
195 array( '#REDIRECT [[bar]]',
196 true,
197 'any',
198 true
199 ),
200 array( '#REDIRECT [[bar]]',
201 true,
202 'comma',
203 false
204 ),
205 array( '#REDIRECT [[bar]]',
206 true,
207 'link',
208 false
209 ),
210 );
211 }
212
213 public function dataGetTextForSummary() {
214 return array(
215 array( "hello\nworld.",
216 16,
217 'hello world.',
218 ),
219 array( 'hello world.',
220 8,
221 'hello...',
222 ),
223 array( '[[hello world]].',
224 8,
225 '[[hel...',
226 ),
227 );
228 }
229
230 public function testMatchMagicWord( ) {
231 $mw = MagicWord::get( "staticredirect" );
232
233 $content = $this->newContent( "#REDIRECT [[FOO]]\n__STATICREDIRECT__" );
234 $this->assertFalse( $content->matchMagicWord( $mw ), "should not have matched magic word, since it's not wikitext" );
235 }
236
237 public function testUpdateRedirect( ) {
238 $target = Title::newFromText( "testUpdateRedirect_target" );
239
240 $content = $this->newContent( "#REDIRECT [[Someplace]]" );
241 $newContent = $content->updateRedirect( $target );
242
243 $this->assertTrue( $content->equals( $newContent ), "content should be unchanged since it's not wikitext" );
244 }
245
246 # =================================================================================================================
247
248 public function testGetModel() {
249 $content = $this->newContent( "hello world." );
250
251 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT, $content->getModel() );
252 }
253
254 public function testGetContentHandler() {
255 $content = $this->newContent( "hello world." );
256
257 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT, $content->getContentHandler()->getModelID() );
258 }
259
260 public function dataEquals( ) {
261 return array(
262 array( new JavascriptContent( "hallo" ), null, false ),
263 array( new JavascriptContent( "hallo" ), new JavascriptContent( "hallo" ), true ),
264 array( new JavascriptContent( "hallo" ), new CssContent( "hallo" ), false ),
265 array( new JavascriptContent( "hallo" ), new JavascriptContent( "HALLO" ), false ),
266 );
267 }
268
269 }