Changed quoting function for oracleDB.
[mediawiki.git] / tests / phpunit / includes / content / WikitextContentTest.php
blob37b01fdeafbe10589fc7ef4afa6c32de9b9fd0a5
1 <?php
3 /**
4 * @group ContentHandler
6 * @group Database
7 * ^--- needed, because we do need the database to test link updates
8 */
9 class WikitextContentTest extends TextContentTest {
10 static $sections = "Intro
12 == stuff ==
13 hello world
15 == test ==
16 just a test
18 == foo ==
19 more stuff
22 public function newContent( $text ) {
23 return new WikitextContent( $text );
26 public static function dataGetParserOutput() {
27 return array(
28 array(
29 "WikitextContentTest_testGetParserOutput",
30 CONTENT_MODEL_WIKITEXT,
31 "hello ''world''\n",
32 "<p>hello <i>world</i>\n</p>"
34 // TODO: more...?
38 public static function dataGetSecondaryDataUpdates() {
39 return array(
40 array( "WikitextContentTest_testGetSecondaryDataUpdates_1",
41 CONTENT_MODEL_WIKITEXT, "hello ''world''\n",
42 array(
43 'LinksUpdate' => array(
44 'mRecursive' => true,
45 'mLinks' => array()
49 array( "WikitextContentTest_testGetSecondaryDataUpdates_2",
50 CONTENT_MODEL_WIKITEXT, "hello [[world test 21344]]\n",
51 array(
52 'LinksUpdate' => array(
53 'mRecursive' => true,
54 'mLinks' => array(
55 array( 'World_test_21344' => 0 )
60 // TODO: more...?
64 /**
65 * @dataProvider dataGetSecondaryDataUpdates
66 * @group Database
68 public function testGetSecondaryDataUpdates( $title, $model, $text, $expectedStuff ) {
69 $ns = $this->getDefaultWikitextNS();
70 $title = Title::newFromText( $title, $ns );
72 $content = ContentHandler::makeContent( $text, $title, $model );
74 $page = WikiPage::factory( $title );
75 $page->doEditContent( $content, '' );
77 $updates = $content->getSecondaryDataUpdates( $title );
79 // make updates accessible by class name
80 foreach ( $updates as $update ) {
81 $class = get_class( $update );
82 $updates[$class] = $update;
85 foreach ( $expectedStuff as $class => $fieldValues ) {
86 $this->assertArrayHasKey( $class, $updates, "missing an update of type $class" );
88 $update = $updates[$class];
90 foreach ( $fieldValues as $field => $value ) {
91 $v = $update->$field; #if the field doesn't exist, just crash and burn
92 $this->assertEquals( $value, $v, "unexpected value for field $field in instance of $class" );
96 $page->doDeleteArticle( '' );
99 public static function dataGetSection() {
100 return array(
101 array( WikitextContentTest::$sections,
102 "0",
103 "Intro"
105 array( WikitextContentTest::$sections,
106 "2",
107 "== test ==
108 just a test"
110 array( WikitextContentTest::$sections,
111 "8",
112 false
118 * @dataProvider dataGetSection
120 public function testGetSection( $text, $sectionId, $expectedText ) {
121 $content = $this->newContent( $text );
123 $sectionContent = $content->getSection( $sectionId );
124 if ( is_object( $sectionContent ) ) {
125 $sectionText = $sectionContent->getNativeData();
126 } else {
127 $sectionText = $sectionContent;
130 $this->assertEquals( $expectedText, $sectionText );
133 public static function dataReplaceSection() {
134 return array(
135 array( WikitextContentTest::$sections,
136 "0",
137 "No more",
138 null,
139 trim( preg_replace( '/^Intro/sm', 'No more', WikitextContentTest::$sections ) )
141 array( WikitextContentTest::$sections,
143 "No more",
144 null,
145 "No more"
147 array( WikitextContentTest::$sections,
148 "2",
149 "== TEST ==\nmore fun",
150 null,
151 trim( preg_replace( '/^== test ==.*== foo ==/sm', "== TEST ==\nmore fun\n\n== foo ==", WikitextContentTest::$sections ) )
153 array( WikitextContentTest::$sections,
154 "8",
155 "No more",
156 null,
157 WikitextContentTest::$sections
159 array( WikitextContentTest::$sections,
160 "new",
161 "No more",
162 "New",
163 trim( WikitextContentTest::$sections ) . "\n\n\n== New ==\n\nNo more"
169 * @dataProvider dataReplaceSection
171 public function testReplaceSection( $text, $section, $with, $sectionTitle, $expected ) {
172 $content = $this->newContent( $text );
173 $c = $content->replaceSection( $section, $this->newContent( $with ), $sectionTitle );
175 $this->assertEquals( $expected, is_null( $c ) ? null : $c->getNativeData() );
178 public function testAddSectionHeader() {
179 $content = $this->newContent( 'hello world' );
180 $content = $content->addSectionHeader( 'test' );
182 $this->assertEquals( "== test ==\n\nhello world", $content->getNativeData() );
185 public static function dataPreSaveTransform() {
186 return array(
187 array( 'hello this is ~~~',
188 "hello this is [[Special:Contributions/127.0.0.1|127.0.0.1]]",
190 array( 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
191 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
193 array( // rtrim
194 " Foo \n ",
195 " Foo",
200 public static function dataPreloadTransform() {
201 return array(
202 array( 'hello this is ~~~',
203 "hello this is ~~~",
205 array( 'hello \'\'this\'\' is <noinclude>foo</noinclude><includeonly>bar</includeonly>',
206 'hello \'\'this\'\' is bar',
211 public static function dataGetRedirectTarget() {
212 return array(
213 array( '#REDIRECT [[Test]]',
214 'Test',
216 array( '#REDIRECT Test',
217 null,
219 array( '* #REDIRECT [[Test]]',
220 null,
225 public static function dataGetTextForSummary() {
226 return array(
227 array( "hello\nworld.",
229 'hello world.',
231 array( 'hello world.',
233 'hello...',
235 array( '[[hello world]].',
237 'hel...',
243 * @todo Test needs database! Should be done by a test class in the Database group.
246 public function getRedirectChain() {
247 $text = $this->getNativeData();
248 return Title::newFromRedirectArray( $text );
253 * @todo Test needs database! Should be done by a test class in the Database group.
256 public function getUltimateRedirectTarget() {
257 $text = $this->getNativeData();
258 return Title::newFromRedirectRecurse( $text );
262 public static function dataIsCountable() {
263 return array(
264 array( '',
265 null,
266 'any',
267 true
269 array( 'Foo',
270 null,
271 'any',
272 true
274 array( 'Foo',
275 null,
276 'comma',
277 false
279 array( 'Foo, bar',
280 null,
281 'comma',
282 true
284 array( 'Foo',
285 null,
286 'link',
287 false
289 array( 'Foo [[bar]]',
290 null,
291 'link',
292 true
294 array( 'Foo',
295 true,
296 'link',
297 true
299 array( 'Foo [[bar]]',
300 false,
301 'link',
302 false
304 array( '#REDIRECT [[bar]]',
305 true,
306 'any',
307 false
309 array( '#REDIRECT [[bar]]',
310 true,
311 'comma',
312 false
314 array( '#REDIRECT [[bar]]',
315 true,
316 'link',
317 false
322 public function testMatchMagicWord() {
323 $mw = MagicWord::get( "staticredirect" );
325 $content = $this->newContent( "#REDIRECT [[FOO]]\n__STATICREDIRECT__" );
326 $this->assertTrue( $content->matchMagicWord( $mw ), "should have matched magic word" );
328 $content = $this->newContent( "#REDIRECT [[FOO]]" );
329 $this->assertFalse( $content->matchMagicWord( $mw ), "should not have matched magic word" );
332 public function testUpdateRedirect() {
333 $target = Title::newFromText( "testUpdateRedirect_target" );
335 // test with non-redirect page
336 $content = $this->newContent( "hello world." );
337 $newContent = $content->updateRedirect( $target );
339 $this->assertTrue( $content->equals( $newContent ), "content should be unchanged" );
341 // test with actual redirect
342 $content = $this->newContent( "#REDIRECT [[Someplace]]" );
343 $newContent = $content->updateRedirect( $target );
345 $this->assertFalse( $content->equals( $newContent ), "content should have changed" );
346 $this->assertTrue( $newContent->isRedirect(), "new content should be a redirect" );
348 $this->assertEquals( $target->getFullText(), $newContent->getRedirectTarget()->getFullText() );
351 public function testGetModel() {
352 $content = $this->newContent( "hello world." );
354 $this->assertEquals( CONTENT_MODEL_WIKITEXT, $content->getModel() );
357 public function testGetContentHandler() {
358 $content = $this->newContent( "hello world." );
360 $this->assertEquals( CONTENT_MODEL_WIKITEXT, $content->getContentHandler()->getModelID() );
363 public static function dataEquals() {
364 return array(
365 array( new WikitextContent( "hallo" ), null, false ),
366 array( new WikitextContent( "hallo" ), new WikitextContent( "hallo" ), true ),
367 array( new WikitextContent( "hallo" ), new JavaScriptContent( "hallo" ), false ),
368 array( new WikitextContent( "hallo" ), new TextContent( "hallo" ), false ),
369 array( new WikitextContent( "hallo" ), new WikitextContent( "HALLO" ), false ),
373 public static function dataGetDeletionUpdates() {
374 return array(
375 array( "WikitextContentTest_testGetSecondaryDataUpdates_1",
376 CONTENT_MODEL_WIKITEXT, "hello ''world''\n",
377 array( 'LinksDeletionUpdate' => array() )
379 array( "WikitextContentTest_testGetSecondaryDataUpdates_2",
380 CONTENT_MODEL_WIKITEXT, "hello [[world test 21344]]\n",
381 array( 'LinksDeletionUpdate' => array() )
383 // @todo more...?