From 0f78700c55d6ad4a9423f0830e9f061b480c62b5 Mon Sep 17 00:00:00 2001 From: Brion Vibber Date: Tue, 6 Dec 2011 23:35:42 +0000 Subject: [PATCH] For bug 32617: lift some code out to a function - EditPage::extractSectionTitle - so it can be tested and fixed. Added test cases, including one that fails thus demonstrating bug 32617. --- includes/EditPage.php | 25 ++++++++++++++----- tests/phpunit/includes/EditPageTest.php | 33 +++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 6 deletions(-) create mode 100644 tests/phpunit/includes/EditPageTest.php diff --git a/includes/EditPage.php b/includes/EditPage.php index e28ff2d0e6..f40311900a 100644 --- a/includes/EditPage.php +++ b/includes/EditPage.php @@ -1713,6 +1713,22 @@ HTML wfProfileOut( __METHOD__ ); } + /** + * Extract the section title from current section text, if any. + * + * @param string $text + * @return Mixed|string or false + */ + public static function extractSectionTitle( $text ) { + preg_match( "/^(=+)(.+)\\1/mi", $text, $matches ); + if ( !empty( $matches[2] ) ) { + global $wgParser; + return $wgParser->stripSectionName(trim($matches[2])); + } else { + return false; + } + } + protected function showHeader() { global $wgOut, $wgUser, $wgMaxArticleSize, $wgLang; if ( $this->isConflict ) { @@ -1730,12 +1746,9 @@ HTML if ( $this->section != '' && $this->section != 'new' ) { $matches = array(); if ( !$this->summary && !$this->preview && !$this->diff ) { - preg_match( "/^(=+)(.+)\\1/mi", $this->textbox1, $matches ); - if ( !empty( $matches[2] ) ) { - global $wgParser; - $this->summary = "/* " . - $wgParser->stripSectionName(trim($matches[2])) . - " */ "; + $sectionTitle = self::extractSectionTitle( $this->textbox1 ); + if ( $sectionTitle !== false ) { + $this->summary = "/* $sectionTitle */ "; } } } diff --git a/tests/phpunit/includes/EditPageTest.php b/tests/phpunit/includes/EditPageTest.php new file mode 100644 index 0000000000..e98e97075f --- /dev/null +++ b/tests/phpunit/includes/EditPageTest.php @@ -0,0 +1,33 @@ +assertEquals( $title, $extracted ); + } + + function dataExtractSectionTitle() { + return array( + array( + "== Test ==\n\nJust a test section.", + "Test" + ), + array( + "An initial section, no header.", + false + ), + array( + "An initial section with a fake heder (bug 32617)\n\n== Test == ??\nwtf", + false + ), + array( + "== Section ==\nfollowed by a fake == Non-section == ??\nnoooo", + "Section" + ) + ); + } +} -- 2.20.1