Create redirects to titles correctly in WikitextContentHandler
[lhc/web/wiklou.git] / tests / phpunit / includes / content / WikitextContentHandlerTest.php
1 <?php
2
3 /**
4 * @group ContentHandler
5 */
6 class WikitextContentHandlerTest extends MediaWikiLangTestCase {
7
8 /**
9 * @var ContentHandler
10 */
11 var $handler;
12
13 public function setUp() {
14 parent::setUp();
15
16 $this->handler = ContentHandler::getForModelID( CONTENT_MODEL_WIKITEXT );
17 }
18
19 public function testSerializeContent() {
20 $content = new WikitextContent( 'hello world' );
21
22 $this->assertEquals( 'hello world', $this->handler->serializeContent( $content ) );
23 $this->assertEquals( 'hello world', $this->handler->serializeContent( $content, CONTENT_FORMAT_WIKITEXT ) );
24
25 try {
26 $this->handler->serializeContent( $content, 'dummy/foo' );
27 $this->fail( "serializeContent() should have failed on unknown format" );
28 } catch ( MWException $e ) {
29 // ok, as expected
30 }
31 }
32
33 public function testUnserializeContent() {
34 $content = $this->handler->unserializeContent( 'hello world' );
35 $this->assertEquals( 'hello world', $content->getNativeData() );
36
37 $content = $this->handler->unserializeContent( 'hello world', CONTENT_FORMAT_WIKITEXT );
38 $this->assertEquals( 'hello world', $content->getNativeData() );
39
40 try {
41 $this->handler->unserializeContent( 'hello world', 'dummy/foo' );
42 $this->fail( "unserializeContent() should have failed on unknown format" );
43 } catch ( MWException $e ) {
44 // ok, as expected
45 }
46 }
47
48 public function testMakeEmptyContent() {
49 $content = $this->handler->makeEmptyContent();
50
51 $this->assertTrue( $content->isEmpty() );
52 $this->assertEquals( '', $content->getNativeData() );
53 }
54
55 public static function dataIsSupportedFormat() {
56 return array(
57 array( null, true ),
58 array( CONTENT_FORMAT_WIKITEXT, true ),
59 array( 99887766, false ),
60 );
61 }
62
63 /**
64 * @dataProvider provideMakeRedirectContent
65 * @param Title|string $title Title object or string for Title::newFromText()
66 * @param string $expected Serialized form of the content object built
67 */
68 public function testMakeRedirectContent( $title, $expected ) {
69 global $wgContLang;
70 $wgContLang->resetNamespaces();
71
72 if ( is_string( $title ) ) {
73 $title = Title::newFromText( $title );
74 }
75 $content = $this->handler->makeRedirectContent( $title );
76 $this->assertEquals( $expected, $content->serialize() );
77 }
78
79 public static function provideMakeRedirectContent() {
80 return array(
81 array( 'Hello', '#REDIRECT [[Hello]]' ),
82 array( 'Template:Hello', '#REDIRECT [[Template:Hello]]' ),
83 array( 'Hello#section', '#REDIRECT [[Hello#section]]' ),
84 array( 'user:john_doe#section', '#REDIRECT [[User:John doe#section]]' ),
85 array( 'MEDIAWIKI:FOOBAR', '#REDIRECT [[MediaWiki:FOOBAR]]' ),
86 array( 'Category:Foo', '#REDIRECT [[:Category:Foo]]' ),
87 array( Title::makeTitle( NS_MAIN, 'en:Foo' ), '#REDIRECT [[en:Foo]]' ),
88 array( Title::makeTitle( NS_MAIN, 'Foo', '', 'en' ), '#REDIRECT [[:en:Foo]]' ),
89 array( Title::makeTitle( NS_MAIN, 'Bar', 'fragment', 'google' ), '#REDIRECT [[google:Bar#fragment]]' ),
90 );
91 }
92
93 /**
94 * @dataProvider dataIsSupportedFormat
95 */
96 public function testIsSupportedFormat( $format, $supported ) {
97 $this->assertEquals( $supported, $this->handler->isSupportedFormat( $format ) );
98 }
99
100 public static function dataMerge3() {
101 return array(
102 array(
103 "first paragraph
104
105 second paragraph\n",
106
107 "FIRST paragraph
108
109 second paragraph\n",
110
111 "first paragraph
112
113 SECOND paragraph\n",
114
115 "FIRST paragraph
116
117 SECOND paragraph\n",
118 ),
119
120 array( "first paragraph
121 second paragraph\n",
122
123 "Bla bla\n",
124
125 "Blubberdibla\n",
126
127 false,
128 ),
129 );
130 }
131
132 /**
133 * @dataProvider dataMerge3
134 */
135 public function testMerge3( $old, $mine, $yours, $expected ) {
136 $this->checkHasDiff3();
137
138 // test merge
139 $oldContent = new WikitextContent( $old );
140 $myContent = new WikitextContent( $mine );
141 $yourContent = new WikitextContent( $yours );
142
143 $merged = $this->handler->merge3( $oldContent, $myContent, $yourContent );
144
145 $this->assertEquals( $expected, $merged ? $merged->getNativeData() : $merged );
146 }
147
148 public static function dataGetAutosummary() {
149 return array(
150 array(
151 'Hello there, world!',
152 '#REDIRECT [[Foo]]',
153 0,
154 '/^Redirected page .*Foo/'
155 ),
156
157 array(
158 null,
159 'Hello world!',
160 EDIT_NEW,
161 '/^Created page .*Hello/'
162 ),
163
164 array(
165 'Hello there, world!',
166 '',
167 0,
168 '/^Blanked/'
169 ),
170
171 array(
172 'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut
173 labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et
174 ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.',
175 'Hello world!',
176 0,
177 '/^Replaced .*Hello/'
178 ),
179
180 array(
181 'foo',
182 'bar',
183 0,
184 '/^$/'
185 ),
186 );
187 }
188
189 /**
190 * @dataProvider dataGetAutosummary
191 */
192 public function testGetAutosummary( $old, $new, $flags, $expected ) {
193 $oldContent = is_null( $old ) ? null : new WikitextContent( $old );
194 $newContent = is_null( $new ) ? null : new WikitextContent( $new );
195
196 $summary = $this->handler->getAutosummary( $oldContent, $newContent, $flags );
197
198 $this->assertTrue( (bool)preg_match( $expected, $summary ), "Autosummary didn't match expected pattern $expected: $summary" );
199 }
200
201 /**
202 * @todo Text case requires database, should be done by a test class in the Database group
203 */
204 /*
205 public function testGetAutoDeleteReason( Title $title, &$hasHistory ) {}
206 */
207
208 /**
209 * @todo Text case requires database, should be done by a test class in the Database group
210 */
211 /*
212 public function testGetUndoContent( Revision $current, Revision $undo, Revision $undoafter = null ) {}
213 */
214 }