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