Merge "(bug 42337) Simplify "my talk" link logic in personal tools"
[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 dataIsSupportedFormat
65 */
66 public function testIsSupportedFormat( $format, $supported ) {
67 $this->assertEquals( $supported, $this->handler->isSupportedFormat( $format ) );
68 }
69
70 public static function dataMerge3( ) {
71 return array(
72 array( "first paragraph
73
74 second paragraph\n",
75
76 "FIRST paragraph
77
78 second paragraph\n",
79
80 "first paragraph
81
82 SECOND paragraph\n",
83
84 "FIRST paragraph
85
86 SECOND paragraph\n",
87 ),
88
89 array( "first paragraph
90 second paragraph\n",
91
92 "Bla bla\n",
93
94 "Blubberdibla\n",
95
96 false,
97 ),
98
99 );
100 }
101
102 /**
103 * @dataProvider dataMerge3
104 */
105 public function testMerge3( $old, $mine, $yours, $expected ) {
106 $this->checkHasDiff3();
107
108 // test merge
109 $oldContent = new WikitextContent( $old );
110 $myContent = new WikitextContent( $mine );
111 $yourContent = new WikitextContent( $yours );
112
113 $merged = $this->handler->merge3( $oldContent, $myContent, $yourContent );
114
115 $this->assertEquals( $expected, $merged ? $merged->getNativeData() : $merged );
116 }
117
118 public static function dataGetAutosummary( ) {
119 return array(
120 array(
121 'Hello there, world!',
122 '#REDIRECT [[Foo]]',
123 0,
124 '/^Redirected page .*Foo/'
125 ),
126
127 array(
128 null,
129 'Hello world!',
130 EDIT_NEW,
131 '/^Created page .*Hello/'
132 ),
133
134 array(
135 'Hello there, world!',
136 '',
137 0,
138 '/^Blanked/'
139 ),
140
141 array(
142 'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut
143 labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et
144 ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.',
145 'Hello world!',
146 0,
147 '/^Replaced .*Hello/'
148 ),
149
150 array(
151 'foo',
152 'bar',
153 0,
154 '/^$/'
155 ),
156 );
157 }
158
159 /**
160 * @dataProvider dataGetAutosummary
161 */
162 public function testGetAutosummary( $old, $new, $flags, $expected ) {
163 global $wgLanguageCode, $wgContLang;
164
165 $oldContent = is_null( $old ) ? null : new WikitextContent( $old );
166 $newContent = is_null( $new ) ? null : new WikitextContent( $new );
167
168 $summary = $this->handler->getAutosummary( $oldContent, $newContent, $flags );
169
170 $this->assertTrue( (bool)preg_match( $expected, $summary ), "Autosummary didn't match expected pattern $expected: $summary" );
171 }
172
173 /**
174 * @todo Text case requires database, should be done by a test class in the Database group
175 */
176 /*
177 public function testGetAutoDeleteReason( Title $title, &$hasHistory ) {}
178 */
179
180 /**
181 * @todo Text case requires database, should be done by a test class in the Database group
182 */
183 /*
184 public function testGetUndoContent( Revision $current, Revision $undo, Revision $undoafter = null ) {}
185 */
186
187 }