Make it show email as required if you choose to email a random password.
[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' => $dbw->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 = 'Intro
213
214 == one ==
215 first section.
216
217 == two ==
218 second section.
219 ';
220
221 $sectionOne = '== one ==
222 hello
223 ';
224
225 $newSection = '== new section ==
226
227 hello
228 ';
229
230 $textWithNewSectionOne = preg_replace(
231 '/== one ==.*== two ==/ms',
232 "$sectionOne\n== two ==", $text
233 );
234
235 $textWithNewSectionAdded = "$text\n$newSection";
236
237 return array(
238 array( #0
239 $text,
240 '',
241 'hello',
242 'replace all',
243 'hello'
244 ),
245
246 array( #1
247 $text,
248 '1',
249 $sectionOne,
250 'replace first section',
251 $textWithNewSectionOne,
252 ),
253
254 array( #2
255 $text,
256 'new',
257 'hello',
258 'new section',
259 $textWithNewSectionAdded,
260 ),
261 );
262 }
263
264 /**
265 * @dataProvider provideSectionEdit
266 */
267 public function testSectionEdit( $base, $section, $text, $summary, $expected ) {
268 $edit = array(
269 'wpTextbox1' => $text,
270 'wpSummary' => $summary,
271 'wpSection' => $section,
272 );
273
274 $this->assertEdit( 'EditPageTest_testSectionEdit', $base, null, $edit,
275 EditPage::AS_SUCCESS_UPDATE, $expected,
276 "expected successfull update of section" );
277 }
278
279 public static function provideAutoMerge() {
280 $tests = array();
281
282 $tests[] = array( #0: plain conflict
283 "Elmo", # base edit user
284 "one\n\ntwo\n\nthree\n",
285 array( #adam's edit
286 'wpStarttime' => 1,
287 'wpTextbox1' => "ONE\n\ntwo\n\nthree\n",
288 ),
289 array( #berta's edit
290 'wpStarttime' => 2,
291 'wpTextbox1' => "(one)\n\ntwo\n\nthree\n",
292 ),
293 EditPage::AS_CONFLICT_DETECTED, # expected code
294 "ONE\n\ntwo\n\nthree\n", # expected text
295 'expected edit conflict', # message
296 );
297
298 $tests[] = array( #1: successful merge
299 "Elmo", # base edit user
300 "one\n\ntwo\n\nthree\n",
301 array( #adam's edit
302 'wpStarttime' => 1,
303 'wpTextbox1' => "ONE\n\ntwo\n\nthree\n",
304 ),
305 array( #berta's edit
306 'wpStarttime' => 2,
307 'wpTextbox1' => "one\n\ntwo\n\nTHREE\n",
308 ),
309 EditPage::AS_SUCCESS_UPDATE, # expected code
310 "ONE\n\ntwo\n\nTHREE\n", # expected text
311 'expected automatic merge', # message
312 );
313
314 $text = "Intro\n\n";
315 $text .= "== first section ==\n\n";
316 $text .= "one\n\ntwo\n\nthree\n\n";
317 $text .= "== second section ==\n\n";
318 $text .= "four\n\nfive\n\nsix\n\n";
319
320 // extract the first section.
321 $section = preg_replace( '/.*(== first section ==.*)== second section ==.*/sm', '$1', $text );
322
323 // generate expected text after merge
324 $expected = str_replace( 'one', 'ONE', str_replace( 'three', 'THREE', $text ) );
325
326 $tests[] = array( #2: merge in section
327 "Elmo", # base edit user
328 $text,
329 array( #adam's edit
330 'wpStarttime' => 1,
331 'wpTextbox1' => str_replace( 'one', 'ONE', $section ),
332 'wpSection' => '1'
333 ),
334 array( #berta's edit
335 'wpStarttime' => 2,
336 'wpTextbox1' => str_replace( 'three', 'THREE', $section ),
337 'wpSection' => '1'
338 ),
339 EditPage::AS_SUCCESS_UPDATE, # expected code
340 $expected, # expected text
341 'expected automatic section merge', # message
342 );
343
344 // see whether it makes a difference who did the base edit
345 $testsWithAdam = array_map( function ( $test ) {
346 $test[0] = 'Adam'; // change base edit user
347 return $test;
348 }, $tests );
349
350 $testsWithBerta = array_map( function ( $test ) {
351 $test[0] = 'Berta'; // change base edit user
352 return $test;
353 }, $tests );
354
355 return array_merge( $tests, $testsWithAdam, $testsWithBerta );
356 }
357
358 /**
359 * @dataProvider provideAutoMerge
360 */
361 public function testAutoMerge( $baseUser, $text, $adamsEdit, $bertasEdit,
362 $expectedCode, $expectedText, $message = null
363 ) {
364 $this->checkHasDiff3();
365
366 //create page
367 $ns = $this->getDefaultWikitextNS();
368 $title = Title::newFromText( 'EditPageTest_testAutoMerge', $ns );
369 $page = WikiPage::factory( $title );
370
371 if ( $page->exists() ) {
372 $page->doDeleteArticle( "clean slate for testing" );
373 }
374
375 $baseEdit = array(
376 'wpTextbox1' => $text,
377 );
378
379 $page = $this->assertEdit( 'EditPageTest_testAutoMerge', null,
380 $baseUser, $baseEdit, null, null, __METHOD__ );
381
382 $this->forceRevisionDate( $page, '20120101000000' );
383
384 $edittime = $page->getTimestamp();
385
386 // start timestamps for conflict detection
387 if ( !isset( $adamsEdit['wpStarttime'] ) ) {
388 $adamsEdit['wpStarttime'] = 1;
389 }
390
391 if ( !isset( $bertasEdit['wpStarttime'] ) ) {
392 $bertasEdit['wpStarttime'] = 2;
393 }
394
395 $starttime = wfTimestampNow();
396 $adamsTime = wfTimestamp( TS_MW, (int)wfTimestamp( TS_UNIX, $starttime ) + (int)$adamsEdit['wpStarttime'] );
397 $bertasTime = wfTimestamp( TS_MW, (int)wfTimestamp( TS_UNIX, $starttime ) + (int)$bertasEdit['wpStarttime'] );
398
399 $adamsEdit['wpStarttime'] = $adamsTime;
400 $bertasEdit['wpStarttime'] = $bertasTime;
401
402 $adamsEdit['wpSummary'] = 'Adam\'s edit';
403 $bertasEdit['wpSummary'] = 'Bertas\'s edit';
404
405 $adamsEdit['wpEdittime'] = $edittime;
406 $bertasEdit['wpEdittime'] = $edittime;
407
408 // first edit
409 $this->assertEdit( 'EditPageTest_testAutoMerge', null, 'Adam', $adamsEdit,
410 EditPage::AS_SUCCESS_UPDATE, null, "expected successfull update" );
411
412 // second edit
413 $this->assertEdit( 'EditPageTest_testAutoMerge', null, 'Berta', $bertasEdit,
414 $expectedCode, $expectedText, $message );
415 }
416 }