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