diff --git a/includes/EditPage.php b/includes/EditPage.php index 5b134c118c2..14c58caea3d 100644 --- a/includes/EditPage.php +++ b/includes/EditPage.php @@ -2301,7 +2301,7 @@ class EditPage implements IEditObject { if ( $this->sectiontitle !== null ) { // Insert the section title above the content. $content = $content->addSectionHeader( $this->sectiontitle ); - } elseif ( $this->summary !== '' ) { + } else { // Insert the section title above the content. $content = $content->addSectionHeader( $this->summary ); } @@ -4414,7 +4414,7 @@ class EditPage implements IEditObject { # If we're adding a comment, we need to show the # summary as the headline - if ( $this->section === "new" && $this->summary !== "" ) { + if ( $this->section === "new" ) { $content = $content->addSectionHeader( $this->summary ); } diff --git a/includes/api/ApiParse.php b/includes/api/ApiParse.php index 8c2ded9743a..58a4c63dc24 100644 --- a/includes/api/ApiParse.php +++ b/includes/api/ApiParse.php @@ -379,7 +379,7 @@ class ApiParse extends ApiBase { if ( $this->section !== false ) { if ( $this->section === 'new' ) { // Insert the section title above the content. - if ( $params['sectiontitle'] !== null && $params['sectiontitle'] !== '' ) { + if ( $params['sectiontitle'] !== null ) { $this->content = $this->content->addSectionHeader( $params['sectiontitle'] ); } } else { diff --git a/includes/content/WikitextContent.php b/includes/content/WikitextContent.php index b64f031dbdb..efa4c836cce 100644 --- a/includes/content/WikitextContent.php +++ b/includes/content/WikitextContent.php @@ -129,9 +129,8 @@ class WikitextContent extends TextContent { * @return Content */ public function addSectionHeader( $header ) { - $text = wfMessage( 'newsectionheaderdefaultlevel' ) - ->rawParams( $header )->inContentLanguage()->text(); - $text .= "\n\n"; + $text = strval( $header ) !== '' ? wfMessage( 'newsectionheaderdefaultlevel' ) + ->plaintextParams( $header )->inContentLanguage()->text() . "\n\n" : ''; $text .= $this->getText(); return new static( $text ); diff --git a/tests/phpunit/includes/api/ApiEditPageTest.php b/tests/phpunit/includes/api/ApiEditPageTest.php index 13f1b50a321..a7718a505a1 100644 --- a/tests/phpunit/includes/api/ApiEditPageTest.php +++ b/tests/phpunit/includes/api/ApiEditPageTest.php @@ -260,10 +260,7 @@ class ApiEditPageTest extends ApiTestCase { $count++; $name = 'Help:ApiEditPageTest_testEditNewSectionSummarySectiontitle' . $count; - // Create the page first to avoid autogenerated summary for page creation - $this->editPage( $name, '' ); - - // Test edit + // Test edit 1 (new page) $this->doApiRequestWithToken( [ 'action' => 'edit', 'title' => $name, @@ -276,12 +273,38 @@ class ApiEditPageTest extends ApiTestCase { $wikiPage = WikiPage::factory( Title::newFromText( $name ) ); // Check the page text is correct - $text = $wikiPage->getContent( RevisionRecord::RAW )->getText(); - $this->assertSame( $expectedText, $text, 'Correct text saved' ); + $savedText = $wikiPage->getContent( RevisionRecord::RAW )->getText(); + $this->assertSame( $expectedText, $savedText, 'Correct text saved (new page)' ); // Check that the edit summary is correct - $summary = $wikiPage->getRevisionRecord()->getComment( RevisionRecord::RAW )->text; - $this->assertSame( $expectedSummary, $summary, 'Correct summary saved' ); + // (when not provided or empty, there is an autogenerated summary for page creation) + $savedSummary = $wikiPage->getRevisionRecord()->getComment( RevisionRecord::RAW )->text; + $expectedSummaryNew = $expectedSummary ?: wfMessage( 'autosumm-new' )->rawParams( $expectedText ) + ->inContentLanguage()->text(); + $this->assertSame( $expectedSummaryNew, $savedSummary, 'Correct summary saved (new page)' ); + + // Clear the page + $this->editPage( $name, '' ); + + // Test edit 2 (existing page) + $this->doApiRequestWithToken( [ + 'action' => 'edit', + 'title' => $name, + 'section' => 'new', + 'text' => 'text', + 'sectiontitle' => $sectiontitle, + 'summary' => $summary, + ] ); + + $wikiPage = WikiPage::factory( Title::newFromText( $name ) ); + + // Check the page text is correct + $savedText = $wikiPage->getContent( RevisionRecord::RAW )->getText(); + $this->assertSame( $expectedText, $savedText, 'Correct text saved (existing page)' ); + + // Check that the edit summary is correct + $savedSummary = $wikiPage->getRevisionRecord()->getComment( RevisionRecord::RAW )->text; + $this->assertSame( $expectedSummary, $savedSummary, 'Correct summary saved (existing page)' ); } public function provideEditNewSectionSummarySectiontitle() { diff --git a/tests/phpunit/includes/content/WikitextContentTest.php b/tests/phpunit/includes/content/WikitextContentTest.php index 813de688221..fedc823951d 100644 --- a/tests/phpunit/includes/content/WikitextContentTest.php +++ b/tests/phpunit/includes/content/WikitextContentTest.php @@ -128,8 +128,11 @@ just a test" public function testAddSectionHeader() { $content = $this->newContent( 'hello world' ); $content = $content->addSectionHeader( 'test' ); - $this->assertEquals( "== test ==\n\nhello world", $content->getText() ); + + $content = $this->newContent( 'hello world' ); + $content = $content->addSectionHeader( '' ); + $this->assertEquals( "hello world", $content->getText() ); } public static function dataPreSaveTransform() {