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