MediaWiki
REL1_21
|
00001 <?php 00002 00012 class EditPageTest extends MediaWikiTestCase { 00013 00017 function testExtractSectionTitle( $section, $title ) { 00018 $extracted = EditPage::extractSectionTitle( $section ); 00019 $this->assertEquals( $title, $extracted ); 00020 } 00021 00022 public static function provideExtractSectionTitle() { 00023 return array( 00024 array( 00025 "== Test ==\n\nJust a test section.", 00026 "Test" 00027 ), 00028 array( 00029 "An initial section, no header.", 00030 false 00031 ), 00032 array( 00033 "An initial section with a fake heder (bug 32617)\n\n== Test == ??\nwtf", 00034 false 00035 ), 00036 array( 00037 "== Section ==\nfollowed by a fake == Non-section == ??\nnoooo", 00038 "Section" 00039 ), 00040 array( 00041 "== Section== \t\r\n followed by whitespace (bug 35051)", 00042 'Section', 00043 ), 00044 ); 00045 } 00046 00047 protected function forceRevisionDate( WikiPage $page, $timestamp ) { 00048 $dbw = wfGetDB( DB_MASTER ); 00049 00050 $dbw->update( 'revision', 00051 array( 'rev_timestamp' => $dbw->timestamp( $timestamp ) ), 00052 array( 'rev_id' => $page->getLatest() ) ); 00053 00054 $page->clear(); 00055 } 00056 00062 function assertEditedTextEquals( $expected, $actual, $msg = '' ) { 00063 return $this->assertEquals( rtrim( $expected ), rtrim( $actual ), $msg ); 00064 } 00065 00090 protected function assertEdit( $title, $baseText, $user = null, array $edit, 00091 $expectedCode = EditPage::AS_OK, $expectedText = null, $message = null 00092 ) { 00093 if ( is_string( $title ) ) { 00094 $ns = $this->getDefaultWikitextNS(); 00095 $title = Title::newFromText( $title, $ns ); 00096 } 00097 00098 if ( is_string( $user ) ) { 00099 $user = User::newFromName( $user ); 00100 00101 if ( $user->getId() === 0 ) { 00102 $user->addToDatabase(); 00103 } 00104 } 00105 00106 $page = WikiPage::factory( $title ); 00107 00108 if ( $baseText !== null ) { 00109 $content = ContentHandler::makeContent( $baseText, $title ); 00110 $page->doEditContent( $content, "base text for test" ); 00111 $this->forceRevisionDate( $page, '20120101000000' ); 00112 00113 //sanity check 00114 $page->clear(); 00115 $currentText = ContentHandler::getContentText( $page->getContent() ); 00116 00117 # EditPage rtrim() the user input, so we alter our expected text 00118 # to reflect that. 00119 $this->assertEditedTextEquals( $baseText, $currentText ); 00120 } 00121 00122 if ( $user == null ) { 00123 $user = $GLOBALS['wgUser']; 00124 } else { 00125 $this->setMwGlobals( 'wgUser', $user ); 00126 } 00127 00128 if ( !isset( $edit['wpEditToken'] ) ) { 00129 $edit['wpEditToken'] = $user->getEditToken(); 00130 } 00131 00132 if ( !isset( $edit['wpEdittime'] ) ) { 00133 $edit['wpEdittime'] = $page->exists() ? $page->getTimestamp() : ''; 00134 } 00135 00136 if ( !isset( $edit['wpStarttime'] ) ) { 00137 $edit['wpStarttime'] = wfTimestampNow(); 00138 } 00139 00140 $req = new FauxRequest( $edit, true ); // session ?? 00141 00142 $ep = new EditPage( new Article( $title ) ); 00143 $ep->setContextTitle( $title ); 00144 $ep->importFormData( $req ); 00145 00146 $bot = isset( $edit['bot'] ) ? (bool)$edit['bot'] : false; 00147 00148 // this is where the edit happens! 00149 // Note: don't want to use EditPage::AttemptSave, because it messes with $wgOut 00150 // and throws exceptions like PermissionsError 00151 $status = $ep->internalAttemptSave( $result, $bot ); 00152 00153 if ( $expectedCode !== null ) { 00154 // check edit code 00155 $this->assertEquals( $expectedCode, $status->value, 00156 "Expected result code mismatch. $message" ); 00157 } 00158 00159 $page = WikiPage::factory( $title ); 00160 00161 if ( $expectedText !== null ) { 00162 // check resulting page text 00163 $content = $page->getContent(); 00164 $text = ContentHandler::getContentText( $content ); 00165 00166 # EditPage rtrim() the user input, so we alter our expected text 00167 # to reflect that. 00168 $this->assertEditedTextEquals( $expectedText, $text, 00169 "Expected article text mismatch. $message" ); 00170 } 00171 00172 return $page; 00173 } 00174 00175 public function testCreatePage() { 00176 $text = "Hello World!"; 00177 $edit = array( 00178 'wpTextbox1' => $text, 00179 'wpSummary' => 'just testing', 00180 ); 00181 00182 $this->assertEdit( 'EditPageTest_testCreatePafe', null, null, $edit, 00183 EditPage::AS_SUCCESS_NEW_ARTICLE, $text, 00184 "expected successfull creation with given text" ); 00185 } 00186 00187 public function testUpdatePage() { 00188 $text = "one"; 00189 $edit = array( 00190 'wpTextbox1' => $text, 00191 'wpSummary' => 'first update', 00192 ); 00193 00194 $page = $this->assertEdit( 'EditPageTest_testUpdatePage', "zero", null, $edit, 00195 EditPage::AS_SUCCESS_UPDATE, $text, 00196 "expected successfull update with given text" ); 00197 00198 $this->forceRevisionDate( $page, '20120101000000' ); 00199 00200 $text = "two"; 00201 $edit = array( 00202 'wpTextbox1' => $text, 00203 'wpSummary' => 'second update', 00204 ); 00205 00206 $this->assertEdit( 'EditPageTest_testUpdatePage', null, null, $edit, 00207 EditPage::AS_SUCCESS_UPDATE, $text, 00208 "expected successfull update with given text" ); 00209 } 00210 00211 public static function provideSectionEdit() { 00212 $text = 'Intro 00213 00214 == one == 00215 first section. 00216 00217 == two == 00218 second section. 00219 '; 00220 00221 $sectionOne = '== one == 00222 hello 00223 '; 00224 00225 $newSection = '== new section == 00226 00227 hello 00228 '; 00229 00230 $textWithNewSectionOne = preg_replace( 00231 '/== one ==.*== two ==/ms', 00232 "$sectionOne\n== two ==", $text 00233 ); 00234 00235 $textWithNewSectionAdded = "$text\n$newSection"; 00236 00237 return array( 00238 array( #0 00239 $text, 00240 '', 00241 'hello', 00242 'replace all', 00243 'hello' 00244 ), 00245 00246 array( #1 00247 $text, 00248 '1', 00249 $sectionOne, 00250 'replace first section', 00251 $textWithNewSectionOne, 00252 ), 00253 00254 array( #2 00255 $text, 00256 'new', 00257 'hello', 00258 'new section', 00259 $textWithNewSectionAdded, 00260 ), 00261 ); 00262 } 00263 00267 public function testSectionEdit( $base, $section, $text, $summary, $expected ) { 00268 $edit = array( 00269 'wpTextbox1' => $text, 00270 'wpSummary' => $summary, 00271 'wpSection' => $section, 00272 ); 00273 00274 $this->assertEdit( 'EditPageTest_testSectionEdit', $base, null, $edit, 00275 EditPage::AS_SUCCESS_UPDATE, $expected, 00276 "expected successfull update of section" ); 00277 } 00278 00279 public static function provideAutoMerge() { 00280 $tests = array(); 00281 00282 $tests[] = array( #0: plain conflict 00283 "Elmo", # base edit user 00284 "one\n\ntwo\n\nthree\n", 00285 array( #adam's edit 00286 'wpStarttime' => 1, 00287 'wpTextbox1' => "ONE\n\ntwo\n\nthree\n", 00288 ), 00289 array( #berta's edit 00290 'wpStarttime' => 2, 00291 'wpTextbox1' => "(one)\n\ntwo\n\nthree\n", 00292 ), 00293 EditPage::AS_CONFLICT_DETECTED, # expected code 00294 "ONE\n\ntwo\n\nthree\n", # expected text 00295 'expected edit conflict', # message 00296 ); 00297 00298 $tests[] = array( #1: successful merge 00299 "Elmo", # base edit user 00300 "one\n\ntwo\n\nthree\n", 00301 array( #adam's edit 00302 'wpStarttime' => 1, 00303 'wpTextbox1' => "ONE\n\ntwo\n\nthree\n", 00304 ), 00305 array( #berta's edit 00306 'wpStarttime' => 2, 00307 'wpTextbox1' => "one\n\ntwo\n\nTHREE\n", 00308 ), 00309 EditPage::AS_SUCCESS_UPDATE, # expected code 00310 "ONE\n\ntwo\n\nTHREE\n", # expected text 00311 'expected automatic merge', # message 00312 ); 00313 00314 $text = "Intro\n\n"; 00315 $text .= "== first section ==\n\n"; 00316 $text .= "one\n\ntwo\n\nthree\n\n"; 00317 $text .= "== second section ==\n\n"; 00318 $text .= "four\n\nfive\n\nsix\n\n"; 00319 00320 // extract the first section. 00321 $section = preg_replace( '/.*(== first section ==.*)== second section ==.*/sm', '$1', $text ); 00322 00323 // generate expected text after merge 00324 $expected = str_replace( 'one', 'ONE', str_replace( 'three', 'THREE', $text ) ); 00325 00326 $tests[] = array( #2: merge in section 00327 "Elmo", # base edit user 00328 $text, 00329 array( #adam's edit 00330 'wpStarttime' => 1, 00331 'wpTextbox1' => str_replace( 'one', 'ONE', $section ), 00332 'wpSection' => '1' 00333 ), 00334 array( #berta's edit 00335 'wpStarttime' => 2, 00336 'wpTextbox1' => str_replace( 'three', 'THREE', $section ), 00337 'wpSection' => '1' 00338 ), 00339 EditPage::AS_SUCCESS_UPDATE, # expected code 00340 $expected, # expected text 00341 'expected automatic section merge', # message 00342 ); 00343 00344 // see whether it makes a difference who did the base edit 00345 $testsWithAdam = array_map( function ( $test ) { 00346 $test[0] = 'Adam'; // change base edit user 00347 return $test; 00348 }, $tests ); 00349 00350 $testsWithBerta = array_map( function ( $test ) { 00351 $test[0] = 'Berta'; // change base edit user 00352 return $test; 00353 }, $tests ); 00354 00355 return array_merge( $tests, $testsWithAdam, $testsWithBerta ); 00356 } 00357 00361 public function testAutoMerge( $baseUser, $text, $adamsEdit, $bertasEdit, 00362 $expectedCode, $expectedText, $message = null 00363 ) { 00364 $this->checkHasDiff3(); 00365 00366 //create page 00367 $ns = $this->getDefaultWikitextNS(); 00368 $title = Title::newFromText( 'EditPageTest_testAutoMerge', $ns ); 00369 $page = WikiPage::factory( $title ); 00370 00371 if ( $page->exists() ) { 00372 $page->doDeleteArticle( "clean slate for testing" ); 00373 } 00374 00375 $baseEdit = array( 00376 'wpTextbox1' => $text, 00377 ); 00378 00379 $page = $this->assertEdit( 'EditPageTest_testAutoMerge', null, 00380 $baseUser, $baseEdit, null, null, __METHOD__ ); 00381 00382 $this->forceRevisionDate( $page, '20120101000000' ); 00383 00384 $edittime = $page->getTimestamp(); 00385 00386 // start timestamps for conflict detection 00387 if ( !isset( $adamsEdit['wpStarttime'] ) ) { 00388 $adamsEdit['wpStarttime'] = 1; 00389 } 00390 00391 if ( !isset( $bertasEdit['wpStarttime'] ) ) { 00392 $bertasEdit['wpStarttime'] = 2; 00393 } 00394 00395 $starttime = wfTimestampNow(); 00396 $adamsTime = wfTimestamp( TS_MW, (int)wfTimestamp( TS_UNIX, $starttime ) + (int)$adamsEdit['wpStarttime'] ); 00397 $bertasTime = wfTimestamp( TS_MW, (int)wfTimestamp( TS_UNIX, $starttime ) + (int)$bertasEdit['wpStarttime'] ); 00398 00399 $adamsEdit['wpStarttime'] = $adamsTime; 00400 $bertasEdit['wpStarttime'] = $bertasTime; 00401 00402 $adamsEdit['wpSummary'] = 'Adam\'s edit'; 00403 $bertasEdit['wpSummary'] = 'Bertas\'s edit'; 00404 00405 $adamsEdit['wpEdittime'] = $edittime; 00406 $bertasEdit['wpEdittime'] = $edittime; 00407 00408 // first edit 00409 $this->assertEdit( 'EditPageTest_testAutoMerge', null, 'Adam', $adamsEdit, 00410 EditPage::AS_SUCCESS_UPDATE, null, "expected successfull update" ); 00411 00412 // second edit 00413 $this->assertEdit( 'EditPageTest_testAutoMerge', null, 'Berta', $bertasEdit, 00414 $expectedCode, $expectedText, $message ); 00415 } 00416 }