Merge "(bug 17808) (bug 21167) use real links for search suggestions"
[lhc/web/wiklou.git] / tests / phpunit / includes / EditPageTest.php
1 <?php
2
3 /**
4 * @group Editing
5 *
6 * @group Database
7 * ^--- tell jenkins this test needs the database
8 *
9 * @group medium
10 * ^--- tell phpunit that these test cases may take longer than 2 seconds.
11 */
12 class EditPageTest extends MediaWikiTestCase {
13
14 /**
15 * @dataProvider provideExtractSectionTitle
16 */
17 function testExtractSectionTitle( $section, $title ) {
18 $extracted = EditPage::extractSectionTitle( $section );
19 $this->assertEquals( $title, $extracted );
20 }
21
22 public static function provideExtractSectionTitle() {
23 return array(
24 array(
25 "== Test ==\n\nJust a test section.",
26 "Test"
27 ),
28 array(
29 "An initial section, no header.",
30 false
31 ),
32 array(
33 "An initial section with a fake heder (bug 32617)\n\n== Test == ??\nwtf",
34 false
35 ),
36 array(
37 "== Section ==\nfollowed by a fake == Non-section == ??\nnoooo",
38 "Section"
39 ),
40 array(
41 "== Section== \t\r\n followed by whitespace (bug 35051)",
42 'Section',
43 ),
44 );
45 }
46
47 protected function forceRevisionDate( WikiPage $page, $timestamp ) {
48 $dbw = wfGetDB( DB_MASTER );
49
50 $dbw->update( 'revision',
51 array( 'rev_timestamp' => $timestamp ),
52 array( 'rev_id' => $page->getLatest() ) );
53
54 $page->clear();
55 }
56
57 /**
58 * User input text is passed to rtrim() by edit page. This is a simple
59 * wrapper around assertEquals() which calls rrtrim() to normalize the
60 * expected and actual texts.
61 */
62 function assertEditedTextEquals( $expected, $actual, $msg='' ) {
63 return $this->assertEquals( rtrim($expected), rtrim($actual), $msg );
64 }
65
66 /**
67 * Performs an edit and checks the result.
68 *
69 * @param String|Title $title The title of the page to edit
70 * @param String|null $baseText Some text to create the page with before attempting the edit.
71 * @param User|String|null $user The user to perform the edit as.
72 * @param array $edit An array of request parameters used to define the edit to perform.
73 * Some well known fields are:
74 * * wpTextbox1: the text to submit
75 * * wpSummary: the edit summary
76 * * wpEditToken: the edit token (will be inserted if not provided)
77 * * wpEdittime: timestamp of the edit's base revision (will be inserted if not provided)
78 * * wpStarttime: timestamp when the edit started (will be inserted if not provided)
79 * * wpSectionTitle: the section to edit
80 * * wpMinorEdit: mark as minor edit
81 * * wpWatchthis: whether to watch the page
82 * @param int|null $expectedCode The expected result code (EditPage::AS_XXX constants).
83 * Set to null to skip the check. Defaults to EditPage::AS_OK.
84 * @param String|null $expectedText The text expected to be on the page after the edit.
85 * Set to null to skip the check.
86 * @param String|null $message An optional message to show along with any error message.
87 *
88 * @return WikiPage The page that was just edited, useful for getting the edit's rev_id, etc.
89 */
90 protected function assertEdit( $title, $baseText, $user = null, array $edit,
91 $expectedCode = EditPage::AS_OK, $expectedText = null, $message = null
92 ) {
93 if ( is_string( $title ) ) {
94 $ns = $this->getDefaultWikitextNS();
95 $title = Title::newFromText( $title, $ns );
96 }
97
98 if ( is_string( $user ) ) {
99 $user = User::newFromName( $user );
100
101 if ( $user->getId() === 0 ) {
102 $user->addToDatabase();
103 }
104 }
105
106 $page = WikiPage::factory( $title );
107
108 if ( $baseText !== null ) {
109 $content = ContentHandler::makeContent( $baseText, $title );
110 $page->doEditContent( $content, "base text for test" );
111 $this->forceRevisionDate( $page, '20120101000000' );
112
113 //sanity check
114 $page->clear();
115 $currentText = ContentHandler::getContentText( $page->getContent() );
116
117 # EditPage rtrim() the user input, so we alter our expected text
118 # to reflect that.
119 $this->assertEditedTextEquals( $baseText, $currentText );
120 }
121
122 if ( $user == null ) {
123 $user = $GLOBALS['wgUser'];
124 } else {
125 $this->setMwGlobals( 'wgUser', $user );
126 }
127
128 if ( !isset( $edit['wpEditToken'] ) ) {
129 $edit['wpEditToken'] = $user->getEditToken();
130 }
131
132 if ( !isset( $edit['wpEdittime'] ) ) {
133 $edit['wpEdittime'] = $page->exists() ? $page->getTimestamp() : '';
134 }
135
136 if ( !isset( $edit['wpStarttime'] ) ) {
137 $edit['wpStarttime'] = wfTimestampNow();
138 }
139
140 $req = new FauxRequest( $edit, true ); // session ??
141
142 $ep = new EditPage( new Article( $title ) );
143 $ep->setContextTitle( $title );
144 $ep->importFormData( $req );
145
146 $bot = isset( $edit['bot'] ) ? (bool)$edit['bot'] : false;
147
148 // this is where the edit happens!
149 // Note: don't want to use EditPage::AttemptSave, because it messes with $wgOut
150 // and throws exceptions like PermissionsError
151 $status = $ep->internalAttemptSave( $result, $bot );
152
153 if ( $expectedCode !== null ) {
154 // check edit code
155 $this->assertEquals( $expectedCode, $status->value,
156 "Expected result code mismatch. $message" );
157 }
158
159 $page = WikiPage::factory( $title );
160
161 if ( $expectedText !== null ) {
162 // check resulting page text
163 $content = $page->getContent();
164 $text = ContentHandler::getContentText( $content );
165
166 # EditPage rtrim() the user input, so we alter our expected text
167 # to reflect that.
168 $this->assertEditedTextEquals( $expectedText, $text,
169 "Expected article text mismatch. $message" );
170 }
171
172 return $page;
173 }
174
175 public function testCreatePage() {
176 $text = "Hello World!";
177 $edit = array(
178 'wpTextbox1' => $text,
179 'wpSummary' => 'just testing',
180 );
181
182 $this->assertEdit( 'EditPageTest_testCreatePafe', null, null, $edit,
183 EditPage::AS_SUCCESS_NEW_ARTICLE, $text,
184 "expected successfull creation with given text" );
185 }
186
187 public function testUpdatePage() {
188 $text = "one";
189 $edit = array(
190 'wpTextbox1' => $text,
191 'wpSummary' => 'first update',
192 );
193
194 $page = $this->assertEdit( 'EditPageTest_testUpdatePage', "zero", null, $edit,
195 EditPage::AS_SUCCESS_UPDATE, $text,
196 "expected successfull update with given text" );
197
198 $this->forceRevisionDate( $page, '20120101000000' );
199
200 $text = "two";
201 $edit = array(
202 'wpTextbox1' => $text,
203 'wpSummary' => 'second update',
204 );
205
206 $this->assertEdit( 'EditPageTest_testUpdatePage', null, null, $edit,
207 EditPage::AS_SUCCESS_UPDATE, $text,
208 "expected successfull update with given text" );
209 }
210
211 public static function provideSectionEdit() {
212 $text =
213 'Intro
214
215 == one ==
216 first section.
217
218 == two ==
219 second section.
220 ';
221
222 $sectionOne =
223 '== one ==
224 hello
225 ';
226
227 $newSection =
228 '== new section ==
229
230 hello
231 ';
232
233 $textWithNewSectionOne = preg_replace( '/== one ==.*== two ==/ms',
234 "$sectionOne\n== two ==", $text );
235
236 $textWithNewSectionAdded = "$text\n$newSection";
237
238 return array(
239 array( #0
240 $text,
241 '',
242 'hello',
243 'replace all',
244 'hello'
245 ),
246
247 array( #1
248 $text,
249 '1',
250 $sectionOne,
251 'replace first section',
252 $textWithNewSectionOne,
253 ),
254
255 array( #2
256 $text,
257 'new',
258 'hello',
259 'new section',
260 $textWithNewSectionAdded,
261 ),
262 );
263 }
264
265 /**
266 * @dataProvider provideSectionEdit
267 */
268 public function testSectionEdit( $base, $section, $text, $summary, $expected ) {
269 $edit = array(
270 'wpTextbox1' => $text,
271 'wpSummary' => $summary,
272 'wpSection' => $section,
273 );
274
275 $this->assertEdit( 'EditPageTest_testSectionEdit', $base, null, $edit,
276 EditPage::AS_SUCCESS_UPDATE, $expected,
277 "expected successfull update of section" );
278 }
279
280 public static function provideAutoMerge() {
281 $tests = array();
282
283 $tests[] = array( #0: plain conflict
284 "Elmo", # base edit user
285 "one\n\ntwo\n\nthree\n",
286 array( #adam's edit
287 'wpStarttime' => 1,
288 'wpTextbox1' => "ONE\n\ntwo\n\nthree\n",
289 ),
290 array( #berta's edit
291 'wpStarttime' => 2,
292 'wpTextbox1' => "(one)\n\ntwo\n\nthree\n",
293 ),
294 EditPage::AS_CONFLICT_DETECTED, # expected code
295 "ONE\n\ntwo\n\nthree\n", # expected text
296 'expected edit conflict', # message
297 );
298
299 $tests[] = array( #1: successful merge
300 "Elmo", # base edit user
301 "one\n\ntwo\n\nthree\n",
302 array( #adam's edit
303 'wpStarttime' => 1,
304 'wpTextbox1' => "ONE\n\ntwo\n\nthree\n",
305 ),
306 array( #berta's edit
307 'wpStarttime' => 2,
308 'wpTextbox1' => "one\n\ntwo\n\nTHREE\n",
309 ),
310 EditPage::AS_SUCCESS_UPDATE, # expected code
311 "ONE\n\ntwo\n\nTHREE\n", # expected text
312 'expected automatic merge', # message
313 );
314
315 $text = "Intro\n\n";
316 $text .= "== first section ==\n\n";
317 $text .= "one\n\ntwo\n\nthree\n\n";
318 $text .= "== second section ==\n\n";
319 $text .= "four\n\nfive\n\nsix\n\n";
320
321 // extract the first section.
322 $section = preg_replace( '/.*(== first section ==.*)== second section ==.*/sm', '$1', $text );
323
324 // generate expected text after merge
325 $expected = str_replace( 'one', 'ONE', str_replace( 'three', 'THREE', $text ) );
326
327 $tests[] = array( #2: merge in section
328 "Elmo", # base edit user
329 $text,
330 array( #adam's edit
331 'wpStarttime' => 1,
332 'wpTextbox1' => str_replace( 'one', 'ONE', $section ),
333 'wpSection' => '1'
334 ),
335 array( #berta's edit
336 'wpStarttime' => 2,
337 'wpTextbox1' => str_replace( 'three', 'THREE', $section ),
338 'wpSection' => '1'
339 ),
340 EditPage::AS_SUCCESS_UPDATE, # expected code
341 $expected, # expected text
342 'expected automatic section merge', # message
343 );
344
345 // see whether it makes a difference who did the base edit
346 $testsWithAdam = array_map( function( $test ) {
347 $test[0] = 'Adam'; // change base edit user
348 return $test;
349 }, $tests );
350
351 $testsWithBerta = array_map( function( $test ) {
352 $test[0] = 'Berta'; // change base edit user
353 return $test;
354 }, $tests );
355
356 return array_merge( $tests, $testsWithAdam, $testsWithBerta );
357 }
358
359 /**
360 * @dataProvider provideAutoMerge
361 */
362 public function testAutoMerge( $baseUser, $text, $adamsEdit, $bertasEdit,
363 $expectedCode, $expectedText, $message = null
364 ) {
365 $this->checkHasDiff3();
366
367 //create page
368 $ns = $this->getDefaultWikitextNS();
369 $title = Title::newFromText( 'EditPageTest_testAutoMerge', $ns );
370 $page = WikiPage::factory( $title );
371
372 if ( $page->exists() ) {
373 $page->doDeleteArticle( "clean slate for testing" );
374 }
375
376 $baseEdit = array(
377 'wpTextbox1' => $text,
378 );
379
380 $page = $this->assertEdit( 'EditPageTest_testAutoMerge', null,
381 $baseUser, $baseEdit, null, null, __METHOD__ );
382
383 $this->forceRevisionDate( $page, '20120101000000' );
384
385 $edittime = $page->getTimestamp();
386
387 // start timestamps for conflict detection
388 if ( !isset( $adamsEdit['wpStarttime'] ) ) {
389 $adamsEdit['wpStarttime'] = 1;
390 }
391
392 if ( !isset( $bertasEdit['wpStarttime'] ) ) {
393 $bertasEdit['wpStarttime'] = 2;
394 }
395
396 $starttime = wfTimestampNow();
397 $adamsTime = wfTimestamp( TS_MW, (int)wfTimestamp( TS_UNIX, $starttime ) + (int)$adamsEdit['wpStarttime'] );
398 $bertasTime = wfTimestamp( TS_MW, (int)wfTimestamp( TS_UNIX, $starttime ) + (int)$bertasEdit['wpStarttime'] );
399
400 $adamsEdit['wpStarttime'] = $adamsTime;
401 $bertasEdit['wpStarttime'] = $bertasTime;
402
403 $adamsEdit['wpSummary'] = 'Adam\'s edit';
404 $bertasEdit['wpSummary'] = 'Bertas\'s edit';
405
406 $adamsEdit['wpEdittime'] = $edittime;
407 $bertasEdit['wpEdittime'] = $edittime;
408
409 // first edit
410 $this->assertEdit( 'EditPageTest_testAutoMerge', null, 'Adam', $adamsEdit,
411 EditPage::AS_SUCCESS_UPDATE, null, "expected successfull update" );
412
413 // second edit
414 $this->assertEdit( 'EditPageTest_testAutoMerge', null, 'Berta', $bertasEdit,
415 $expectedCode, $expectedText, $message );
416 }
417 }