For bug 32617: lift some code out to a function - EditPage::extractSectionTitle ...
authorBrion Vibber <brion@users.mediawiki.org>
Tue, 6 Dec 2011 23:35:42 +0000 (23:35 +0000)
committerBrion Vibber <brion@users.mediawiki.org>
Tue, 6 Dec 2011 23:35:42 +0000 (23:35 +0000)
Added test cases, including one that fails thus demonstrating bug 32617.

includes/EditPage.php
tests/phpunit/includes/EditPageTest.php [new file with mode: 0644]

index e28ff2d..f403119 100644 (file)
@@ -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 (file)
index 0000000..e98e970
--- /dev/null
@@ -0,0 +1,33 @@
+<?php
+
+class EditPageTest extends MediaWikiTestCase {
+
+       /**
+        * @dataProvider dataExtractSectionTitle
+        */
+       function testExtractSectionTitle( $section, $title ) {
+               $extracted = EditPage::extractSectionTitle( $section );
+               $this->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"
+                       )
+               );
+       }
+}