Only add header when description not already has it
authorMatthias Mullie <git@mullie.eu>
Tue, 13 Feb 2018 16:51:29 +0000 (17:51 +0100)
committerMatthias Mullie <mmullie@wikimedia.org>
Wed, 21 Feb 2018 17:32:48 +0000 (17:32 +0000)
Bug: T187302
Change-Id: Ic57196cc366fc53d83c624a7cd9290fb1972eab1

includes/specials/SpecialUpload.php
tests/phpunit/includes/specials/SpecialUploadTest.php [new file with mode: 0644]

index 53b7a2f..f7cb654 100644 (file)
@@ -617,7 +617,13 @@ class SpecialUpload extends SpecialPage {
                        $licenseText = '== ' . $msg['license-header'] . " ==\n{{" . $license . "}}\n";
                }
 
-               $pageText = $comment == '' ? '' : '== ' . $msg['filedesc'] . " ==\n" . $comment . "\n";
+               $pageText = $comment . "\n";
+               $headerText = '== ' . $msg['filedesc'] . ' ==';
+               if ( $comment !== '' && strpos( $comment, $headerText ) === false ) {
+                       // prepend header to page text unless it's already there (or there is no content)
+                       $pageText = $headerText . "\n" . $pageText;
+               }
+
                if ( $config->get( 'UseCopyrightUpload' ) ) {
                        $pageText .= '== ' . $msg['filestatus'] . " ==\n" . $copyStatus . "\n";
                        $pageText .= $licenseText;
diff --git a/tests/phpunit/includes/specials/SpecialUploadTest.php b/tests/phpunit/includes/specials/SpecialUploadTest.php
new file mode 100644 (file)
index 0000000..95026c1
--- /dev/null
@@ -0,0 +1,29 @@
+<?php
+
+class SpecialUploadTest extends MediaWikiTestCase {
+       /**
+        * @covers SpecialUpload::getInitialPageText
+        * @dataProvider provideGetInitialPageText
+        */
+       public function testGetInitialPageText( $expected, $inputParams ) {
+               $result = call_user_func_array( [ 'SpecialUpload', 'getInitialPageText' ], $inputParams );
+               $this->assertEquals( $expected, $result );
+       }
+
+       public function provideGetInitialPageText() {
+               return [
+                       [
+                               'expect' => "== Summary ==\nthis is a test\n",
+                               'params' => [
+                                       'this is a test'
+                               ],
+                       ],
+                       [
+                               'expect' => "== Summary ==\nthis is a test\n",
+                               'params' => [
+                                       "== Summary ==\nthis is a test",
+                               ],
+                       ],
+               ];
+       }
+}