Merge "Added Tests for ListToggle"
authorjenkins-bot <jenkins-bot@gerrit.wikimedia.org>
Fri, 5 Jan 2018 00:53:45 +0000 (00:53 +0000)
committerGerrit Code Review <gerrit@wikimedia.org>
Fri, 5 Jan 2018 00:53:46 +0000 (00:53 +0000)
27 files changed:
RELEASE-NOTES-1.31
autoload.php
composer.json
includes/EditPage.php
includes/HtmlFormatter.php [deleted file]
includes/MediaWiki.php
includes/Storage/RevisionStore.php
includes/api/ApiComparePages.php
includes/api/i18n/en.json
includes/api/i18n/qqq.json
includes/editpage/TextConflictHelper.php
includes/editpage/TextboxBuilder.php
includes/libs/rdbms/loadbalancer/ILoadBalancer.php
includes/libs/rdbms/loadbalancer/LoadBalancer.php
includes/parser/Parser.php
maintenance/Maintenance.php
resources/src/mediawiki.rcfilters/mw.rcfilters.js
resources/src/mediawiki.rcfilters/ui/mw.rcfilters.ui.ChangesLimitAndDateButtonWidget.js
resources/src/mediawiki.rcfilters/ui/mw.rcfilters.ui.FilterWrapperWidget.js
tests/phpunit/includes/Storage/RevisionStoreDbTest.php
tests/phpunit/includes/api/ApiComparePagesTest.php
tests/phpunit/includes/api/format/ApiFormatBaseTest.php [new file with mode: 0644]
tests/phpunit/includes/api/format/ApiFormatRawTest.php [new file with mode: 0644]
tests/phpunit/includes/api/format/ApiFormatTestBase.php
tests/phpunit/includes/editpage/TextboxBuilderTest.php
tests/phpunit/includes/libs/rdbms/connectionmanager/ConnectionManagerTest.php
tests/phpunit/includes/libs/rdbms/connectionmanager/SessionConsistentConnectionManagerTest.php

index a496b02..d18c5cf 100644 (file)
@@ -42,6 +42,8 @@ production.
 
 ==== Upgraded external libraries ====
 * Updated jquery.chosen from v0.9.14 to v1.8.2.
+* Updated composer/spdx-licenses from 1.1.4 to
+  1.2.0 (development dependency).
 * …
 
 ==== New external libraries ====
@@ -159,6 +161,8 @@ changes to languages because of Phabricator reports.
 * The $statementsOnOwnLine parameter of JavaScriptMinifier::minify was removed.
   The corresponding configuration variable ($wgResourceLoaderMinifierStatementsOnOwnLine)
   has been deprecated since 1.27 and was removed as well.
+* The HtmlFormatter class was removed (deprecated in 1.27). The namespaced
+  HtmlFormatter\HtmlFormatter class should be used instead.
 
 == Compatibility ==
 MediaWiki 1.31 requires PHP 5.5.9 or later. There is experimental support for
index af0b200..351136d 100644 (file)
@@ -606,7 +606,6 @@ $wgAutoloadLocalClasses = [
        'Hooks' => __DIR__ . '/includes/Hooks.php',
        'Html' => __DIR__ . '/includes/Html.php',
        'HtmlArmor' => __DIR__ . '/includes/libs/HtmlArmor.php',
-       'HtmlFormatter' => __DIR__ . '/includes/HtmlFormatter.php',
        'Http' => __DIR__ . '/includes/http/Http.php',
        'HttpError' => __DIR__ . '/includes/exception/HttpError.php',
        'HttpStatus' => __DIR__ . '/includes/libs/HttpStatus.php',
index ee050d5..6b3e8f7 100644 (file)
@@ -49,7 +49,7 @@
                "zordius/lightncandy": "0.23"
        },
        "require-dev": {
-               "composer/spdx-licenses": "1.1.4",
+               "composer/spdx-licenses": "1.2.0",
                "hamcrest/hamcrest-php": "^2.0",
                "jakub-onderka/php-parallel-lint": "0.9.2",
                "jetbrains/phpstorm-stubs": "dev-master#1b9906084d6635456fcf3f3a01f0d7d5b99a578a",
index bcaab3a..3c109f6 100644 (file)
@@ -2861,7 +2861,14 @@ class EditPage {
                        // and fallback to the raw wpTextbox1 since editconflicts can't be
                        // resolved between page source edits and custom ui edits using the
                        // custom edit ui.
-                       $this->showTextbox1();
+                       $conflictTextBoxAttribs = [];
+                       if ( $this->wasDeletedSinceLastEdit() ) {
+                               $conflictTextBoxAttribs['style'] = 'display:none;';
+                       } elseif ( $this->isOldRev ) {
+                               $conflictTextBoxAttribs['class'] = 'mw-textarea-oldrev';
+                       }
+
+                       $out->addHTML( $editConflictHelper->getEditConflictMainTextBox( $conflictTextBoxAttribs ) );
                        $out->addHTML( $editConflictHelper->getEditFormHtmlAfterContent() );
                } else {
                        $this->showContentForm();
@@ -3339,22 +3346,9 @@ class EditPage {
                if ( $this->wasDeletedSinceLastEdit() && $this->formtype == 'save' ) {
                        $attribs = [ 'style' => 'display:none;' ];
                } else {
-                       $classes = []; // Textarea CSS
-                       if ( $this->mTitle->isProtected( 'edit' ) &&
-                               MWNamespace::getRestrictionLevels( $this->mTitle->getNamespace() ) !== [ '' ]
-                       ) {
-                               # Is the title semi-protected?
-                               if ( $this->mTitle->isSemiProtected() ) {
-                                       $classes[] = 'mw-textarea-sprotected';
-                               } else {
-                                       # Then it must be protected based on static groups (regular)
-                                       $classes[] = 'mw-textarea-protected';
-                               }
-                               # Is the title cascade-protected?
-                               if ( $this->mTitle->isCascadeProtected() ) {
-                                       $classes[] = 'mw-textarea-cprotected';
-                               }
-                       }
+                       $builder = new TextboxBuilder();
+                       $classes = $builder->getTextboxProtectionCSSClasses( $this->getTitle() );
+
                        # Is an old revision being edited?
                        if ( $this->isOldRev ) {
                                $classes[] = 'mw-textarea-oldrev';
@@ -3366,12 +3360,7 @@ class EditPage {
                                $attribs += $customAttribs;
                        }
 
-                       if ( count( $classes ) ) {
-                               if ( isset( $attribs['class'] ) ) {
-                                       $classes[] = $attribs['class'];
-                               }
-                               $attribs['class'] = implode( ' ', $classes );
-                       }
+                       $attribs = $builder->mergeClassesIntoAttributes( $classes, $attribs );
                }
 
                $this->showTextbox(
diff --git a/includes/HtmlFormatter.php b/includes/HtmlFormatter.php
deleted file mode 100644 (file)
index 9bae8b5..0000000
+++ /dev/null
@@ -1,25 +0,0 @@
-<?php
-/**
- * Stub for extensions that haven't switched to Composer-based version of this class
- * @todo: remove in 1.28
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- * http://www.gnu.org/copyleft/gpl.html
- *
- * @file
- * @deprecated since 1.27, use HtmlFormatter\HtmlFormatter
- */
-class HtmlFormatter extends HtmlFormatter\HtmlFormatter {
-}
index beb9de5..a217cd1 100644 (file)
@@ -727,10 +727,12 @@ class MediaWiki {
                if ( function_exists( 'register_postsend_function' ) ) {
                        // https://github.com/facebook/hhvm/issues/1230
                        register_postsend_function( $callback );
+                       /** @noinspection PhpUnusedLocalVariableInspection */
                        $blocksHttpClient = false;
                } else {
                        if ( function_exists( 'fastcgi_finish_request' ) ) {
                                fastcgi_finish_request();
+                               /** @noinspection PhpUnusedLocalVariableInspection */
                                $blocksHttpClient = false;
                        } else {
                                // Either all DB and deferred updates should happen or none.
index ce56efc..2e953fc 100644 (file)
@@ -1476,10 +1476,20 @@ class RevisionStore implements IDBAccessObject, RevisionFactory, RevisionLookup
                $storeWiki = $storeWiki ?: wfWikiID();
                $dbWiki = $dbWiki ?: wfWikiID();
 
-               if ( $dbWiki !== $storeWiki ) {
-                       throw new MWException( "RevisionStore for $storeWiki "
-                               . "cannot be used with a DB connection for $dbWiki" );
+               if ( $dbWiki === $storeWiki ) {
+                       return;
+               }
+
+               // HACK: counteract encoding imposed by DatabaseDomain
+               $storeWiki = str_replace( '?h', '-', $storeWiki );
+               $dbWiki = str_replace( '?h', '-', $dbWiki );
+
+               if ( $dbWiki === $storeWiki ) {
+                       return;
                }
+
+               throw new MWException( "RevisionStore for $storeWiki "
+                       . "cannot be used with a DB connection for $dbWiki" );
        }
 
        /**
index eb67bab..5486594 100644 (file)
@@ -94,6 +94,26 @@ class ApiComparePages extends ApiBase {
                        $this->dieWithError( 'apierror-baddiff' );
                }
 
+               // Extract sections, if told to
+               if ( isset( $params['fromsection'] ) ) {
+                       $fromContent = $fromContent->getSection( $params['fromsection'] );
+                       if ( !$fromContent ) {
+                               $this->dieWithError(
+                                       [ 'apierror-compare-nosuchfromsection', wfEscapeWikiText( $params['fromsection'] ) ],
+                                       'nosuchfromsection'
+                               );
+                       }
+               }
+               if ( isset( $params['tosection'] ) ) {
+                       $toContent = $toContent->getSection( $params['tosection'] );
+                       if ( !$toContent ) {
+                               $this->dieWithError(
+                                       [ 'apierror-compare-nosuchtosection', wfEscapeWikiText( $params['tosection'] ) ],
+                                       'nosuchtosection'
+                               );
+                       }
+               }
+
                // Get the diff
                $context = new DerivativeContext( $this->getContext() );
                if ( $relRev && $relRev->getTitle() ) {
@@ -444,6 +464,7 @@ class ApiComparePages extends ApiBase {
                        'text' => [
                                ApiBase::PARAM_TYPE => 'text'
                        ],
+                       'section' => null,
                        'pst' => false,
                        'contentformat' => [
                                ApiBase::PARAM_TYPE => ContentHandler::getAllContentFormats(),
index e1360c8..cceed01 100644 (file)
@@ -64,6 +64,7 @@
        "apihelp-compare-param-fromid": "First page ID to compare.",
        "apihelp-compare-param-fromrev": "First revision to compare.",
        "apihelp-compare-param-fromtext": "Use this text instead of the content of the revision specified by <var>fromtitle</var>, <var>fromid</var> or <var>fromrev</var>.",
+       "apihelp-compare-param-fromsection": "Only use the specified section of the specified 'from' content.",
        "apihelp-compare-param-frompst": "Do a pre-save transform on <var>fromtext</var>.",
        "apihelp-compare-param-fromcontentmodel": "Content model of <var>fromtext</var>. If not supplied, it will be guessed based on the other parameters.",
        "apihelp-compare-param-fromcontentformat": "Content serialization format of <var>fromtext</var>.",
@@ -72,6 +73,7 @@
        "apihelp-compare-param-torev": "Second revision to compare.",
        "apihelp-compare-param-torelative": "Use a revision relative to the revision determined from <var>fromtitle</var>, <var>fromid</var> or <var>fromrev</var>. All of the other 'to' options will be ignored.",
        "apihelp-compare-param-totext": "Use this text instead of the content of the revision specified by <var>totitle</var>, <var>toid</var> or <var>torev</var>.",
+       "apihelp-compare-param-tosection": "Only use the specified section of the specified 'to' content.",
        "apihelp-compare-param-topst": "Do a pre-save transform on <var>totext</var>.",
        "apihelp-compare-param-tocontentmodel": "Content model of <var>totext</var>. If not supplied, it will be guessed based on the other parameters.",
        "apihelp-compare-param-tocontentformat": "Content serialization format of <var>totext</var>.",
        "apierror-chunk-too-small": "Minimum chunk size is $1 {{PLURAL:$1|byte|bytes}} for non-final chunks.",
        "apierror-cidrtoobroad": "$1 CIDR ranges broader than /$2 are not accepted.",
        "apierror-compare-no-title": "Cannot pre-save transform without a title. Try specifying <var>fromtitle</var> or <var>totitle</var>.",
+       "apierror-compare-nosuchfromsection": "There is no section $1 in the 'from' content.",
+       "apierror-compare-nosuchtosection": "There is no section $1 in the 'to' content.",
        "apierror-compare-relative-to-nothing": "No 'from' revision for <var>torelative</var> to be relative to.",
        "apierror-contentserializationexception": "Content serialization failed: $1",
        "apierror-contenttoobig": "The content you supplied exceeds the article size limit of $1 {{PLURAL:$1|kilobyte|kilobytes}}.",
index 1724fa9..d21f29c 100644 (file)
@@ -68,6 +68,7 @@
        "apihelp-compare-param-fromid": "{{doc-apihelp-param|compare|fromid}}",
        "apihelp-compare-param-fromrev": "{{doc-apihelp-param|compare|fromrev}}",
        "apihelp-compare-param-fromtext": "{{doc-apihelp-param|compare|fromtext}}",
+       "apihelp-compare-param-fromsection": "{{doc-apihelp-param|compare|fromsection}}",
        "apihelp-compare-param-frompst": "{{doc-apihelp-param|compare|frompst}}",
        "apihelp-compare-param-fromcontentmodel": "{{doc-apihelp-param|compare|fromcontentmodel}}",
        "apihelp-compare-param-fromcontentformat": "{{doc-apihelp-param|compare|fromcontentformat}}",
@@ -76,6 +77,7 @@
        "apihelp-compare-param-torev": "{{doc-apihelp-param|compare|torev}}",
        "apihelp-compare-param-torelative": "{{doc-apihelp-param|compare|torelative}}",
        "apihelp-compare-param-totext": "{{doc-apihelp-param|compare|totext}}",
+       "apihelp-compare-param-tosection": "{{doc-apihelp-param|compare|tosection}}",
        "apihelp-compare-param-topst": "{{doc-apihelp-param|compare|topst}}",
        "apihelp-compare-param-tocontentmodel": "{{doc-apihelp-param|compare|tocontentmodel}}",
        "apihelp-compare-param-tocontentformat": "{{doc-apihelp-param|compare|tocontentformat}}",
        "apierror-chunk-too-small": "{{doc-apierror}}\n\nParameters:\n* $1 - Minimum size in bytes.",
        "apierror-cidrtoobroad": "{{doc-apierror}}\n\nParameters:\n* $1 - \"IPv4\" or \"IPv6\"\n* $2 - Minimum CIDR mask length.",
        "apierror-compare-no-title": "{{doc-apierror}}",
+       "apierror-compare-nosuchfromsection": "{{doc-apierror}}\n\nParameters:\n* $1 - Section identifier. Probably a number or \"T-\" followed by a number.",
+       "apierror-compare-nosuchtosection": "{{doc-apierror}}\n\nParameters:\n* $1 - Section identifier. Probably a number or \"T-\" followed by a number.",
        "apierror-compare-relative-to-nothing": "{{doc-apierror}}",
        "apierror-contentserializationexception": "{{doc-apierror}}\n\nParameters:\n* $1 - Exception text, may end with punctuation. Currently this is probably English, hopefully we'll fix that in the future.",
        "apierror-contenttoobig": "{{doc-apierror}}\n\nParameters:\n* $1 - Maximum article size in kilobytes.",
index 6e7e7ee..b447b18 100644 (file)
@@ -162,6 +162,33 @@ class TextConflictHelper {
                );
        }
 
+       /**
+        * HTML to build the textbox1 on edit conflicts
+        *
+        * @param mixed[]|null $customAttribs
+        * @return string HTML
+        */
+       public function getEditConflictMainTextBox( $customAttribs = [] ) {
+               $builder = new TextboxBuilder();
+               $classes = $builder->getTextboxProtectionCSSClasses( $this->title );
+
+               $attribs = [ 'tabindex' => 1 ];
+               $attribs += $customAttribs;
+
+               $attribs = $builder->mergeClassesIntoAttributes( $classes, $attribs );
+
+               $attribs = $builder->buildTextboxAttribs(
+                       'wpTextbox1',
+                       $attribs,
+                       $this->out->getUser(),
+                       $this->title
+               );
+
+               $this->out->addHTML(
+                       Html::textarea( 'wpTextbox1', $builder->addNewLineAtEnd( $this->storedversion ), $attribs )
+               );
+       }
+
        /**
         * Content to go in the edit form before textbox1
         *
index a6ae9bc..d0a2f8f 100644 (file)
@@ -24,6 +24,7 @@
 
 namespace MediaWiki\EditPage;
 
+use MWNamespace;
 use Title;
 use User;
 
@@ -50,6 +51,49 @@ class TextboxBuilder {
                return $wikitext;
        }
 
+       /**
+        * @param string[] $classes
+        * @param mixed[] $attribs
+        * @return mixed[]
+        */
+       public function mergeClassesIntoAttributes( array $classes, array $attribs ) {
+               if ( !count( $classes ) ) {
+                       return $attribs;
+               }
+
+               if ( isset( $attribs['class'] ) ) {
+                       $classes[] = $attribs['class'];
+               }
+               $attribs['class'] = implode( ' ', $classes );
+
+               return $attribs;
+       }
+
+       /**
+        * @param Title $title
+        * @return string[]
+        */
+       public function getTextboxProtectionCSSClasses( Title $title ) {
+               $classes = []; // Textarea CSS
+               if ( $title->isProtected( 'edit' ) &&
+                       MWNamespace::getRestrictionLevels( $title->getNamespace() ) !== [ '' ]
+               ) {
+                       # Is the title semi-protected?
+                       if ( $title->isSemiProtected() ) {
+                               $classes[] = 'mw-textarea-sprotected';
+                       } else {
+                               # Then it must be protected based on static groups (regular)
+                               $classes[] = 'mw-textarea-protected';
+                       }
+                       # Is the title cascade-protected?
+                       if ( $title->isCascadeProtected() ) {
+                               $classes[] = 'mw-textarea-cprotected';
+                       }
+               }
+
+               return $classes;
+       }
+
        /**
         * @param string $name
         * @param mixed[] $customAttribs
index 86c4335..e246b79 100644 (file)
@@ -309,25 +309,6 @@ interface ILoadBalancer {
         */
        public function getServerType( $i );
 
-       /**
-        * Return the server info structure for a given index, or false if the index is invalid.
-        * @param int $i
-        * @return array|bool
-        *
-        * @deprecated Since 1.30, no alternative
-        */
-       public function getServerInfo( $i );
-
-       /**
-        * Sets the server info structure for the given index. Entry at index $i
-        * is created if it doesn't exist
-        * @param int $i
-        * @param array $serverInfo
-        *
-        * @deprecated Since 1.30, construct new object
-        */
-       public function setServerInfo( $i, array $serverInfo );
-
        /**
         * Get the current master position for chronology control purposes
         * @return DBMasterPos|bool Returns false if not applicable
index 591e287..deacc42 100644 (file)
@@ -1080,26 +1080,6 @@ class LoadBalancer implements ILoadBalancer {
                return isset( $this->mServers[$i]['type'] ) ? $this->mServers[$i]['type'] : 'unknown';
        }
 
-       /**
-        * @deprecated Since 1.30, no alternative
-        */
-       public function getServerInfo( $i ) {
-               wfDeprecated( __METHOD__, '1.30' );
-               if ( isset( $this->mServers[$i] ) ) {
-                       return $this->mServers[$i];
-               } else {
-                       return false;
-               }
-       }
-
-       /**
-        * @deprecated Since 1.30, construct new object
-        */
-       public function setServerInfo( $i, array $serverInfo ) {
-               wfDeprecated( __METHOD__, '1.30' );
-               $this->mServers[$i] = $serverInfo;
-       }
-
        public function getMasterPos() {
                # If this entire request was served from a replica DB without opening a connection to the
                # master (however unlikely that may be), then we can fetch the position from the replica DB.
index b871735..f41ee01 100644 (file)
@@ -5787,9 +5787,9 @@ class Parser {
                global $wgFragmentMode;
                if ( isset( $wgFragmentMode[1] ) && $wgFragmentMode[1] === 'legacy' ) {
                        // ForAttribute() and ForLink() are the same for legacy encoding
-                       $id = Sanitizer::escapeIdForAttribute( $text, Sanitizer::ID_FALLBACK );
+                       $id = Sanitizer::escapeIdForAttribute( $sectionName, Sanitizer::ID_FALLBACK );
                } else {
-                       $id = Sanitizer::escapeIdForLink( $text );
+                       $id = Sanitizer::escapeIdForLink( $sectionName );
                }
 
                return "#$id";
index 07f547f..8bfead3 100644 (file)
@@ -381,11 +381,15 @@ abstract class Maintenance {
         * @param mixed $channel Unique identifier for the channel. See function outputChanneled.
         */
        protected function output( $out, $channel = null ) {
-               // Try to periodically flush buffered metrics to avoid OOMs
-               $stats = MediaWikiServices::getInstance()->getStatsdDataFactory();
-               if ( $stats->getDataCount() > 1000 ) {
-                       MediaWiki::emitBufferedStatsdData( $stats, $this->getConfig() );
+               // This is sometimes called very early, before Setup.php is included.
+               if ( class_exists( MediaWikiServices::class ) ) {
+                       // Try to periodically flush buffered metrics to avoid OOMs
+                       $stats = MediaWikiServices::getInstance()->getStatsdDataFactory();
+                       if ( $stats->getDataCount() > 1000 ) {
+                               MediaWiki::emitBufferedStatsdData( $stats, $this->getConfig() );
+                       }
                }
+
                if ( $this->mQuiet ) {
                        return;
                }
index 466b7c0..c62d6f2 100644 (file)
@@ -44,9 +44,6 @@
 
                                return result;
                        }
-               },
-               featureFlags: {
-                       liveUpdate: mw.config.get( 'StructuredChangeFiltersLiveUpdatePollingRate' )
                }
        };
 }( mediaWiki ) );
index 6be6968..c10011c 100644 (file)
@@ -32,7 +32,7 @@
                } );
 
                this.$element
-                       .addClass( 'mw-rcfilters-ui-changesLimitButtonWidget' );
+                       .addClass( 'mw-rcfilters-ui-changesLimitAndDateButtonWidget' );
        };
 
        /* Initialization */
index 1cd7bef..dba24fc 100644 (file)
@@ -43,7 +43,7 @@
                        this.changesListModel
                );
 
-               this.numChangesWidget = new mw.rcfilters.ui.ChangesLimitAndDateButtonWidget(
+               this.numChangesAndDateWidget = new mw.rcfilters.ui.ChangesLimitAndDateButtonWidget(
                        this.controller,
                        this.model,
                        {
                        classes: [ 'mw-rcfilters-ui-filterWrapperWidget-showNewChanges' ]
                } );
 
+               // Events
+               this.filterTagWidget.menu.connect( this, { toggle: [ 'emit', 'menuToggle' ] } );
+               this.changesListModel.connect( this, { newChangesExist: 'onNewChangesExist' } );
+               this.showNewChangesLink.connect( this, { click: 'onShowNewChangesClick' } );
+               this.showNewChangesLink.toggle( false );
+
                // Initialize
                this.$top = $( '<div>' )
                        .addClass( 'mw-rcfilters-ui-filterWrapperWidget-top' );
                        .addClass( 'mw-rcfilters-ui-filterWrapperWidget-bottom' )
                        .append(
                                this.showNewChangesLink.$element,
-                               this.numChangesWidget.$element
+                               this.numChangesAndDateWidget.$element
                        );
 
-               if ( mw.rcfilters.featureFlags.liveUpdate ) {
+               if ( mw.config.get( 'StructuredChangeFiltersLiveUpdatePollingRate' ) ) {
                        $bottom.prepend( this.liveUpdateButton.$element );
                }
 
-               // Events
-               this.filterTagWidget.menu.connect( this, { toggle: [ 'emit', 'menuToggle' ] } );
-               this.changesListModel.connect( this, { newChangesExist: 'onNewChangesExist' } );
-               this.showNewChangesLink.connect( this, { click: 'onShowNewChangesClick' } );
-               this.showNewChangesLink.toggle( false );
-
                this.$element
                        .addClass( 'mw-rcfilters-ui-filterWrapperWidget' )
                        .append(
index ee8fdc7..28a8d88 100644 (file)
@@ -4,17 +4,26 @@ namespace MediaWiki\Tests\Storage;
 
 use CommentStoreComment;
 use Exception;
+use HashBagOStuff;
 use InvalidArgumentException;
 use MediaWiki\Linker\LinkTarget;
 use MediaWiki\MediaWikiServices;
 use MediaWiki\Storage\IncompleteRevisionException;
 use MediaWiki\Storage\MutableRevisionRecord;
 use MediaWiki\Storage\RevisionRecord;
+use MediaWiki\Storage\RevisionStore;
 use MediaWiki\Storage\SlotRecord;
+use MediaWiki\Storage\SqlBlobStore;
 use MediaWikiTestCase;
 use Revision;
 use TestUserRegistry;
 use Title;
+use WANObjectCache;
+use Wikimedia\Rdbms\Database;
+use Wikimedia\Rdbms\DatabaseSqlite;
+use Wikimedia\Rdbms\FakeResultWrapper;
+use Wikimedia\Rdbms\LoadBalancer;
+use Wikimedia\Rdbms\TransactionProfiler;
 use WikiPage;
 use WikitextContent;
 
@@ -23,6 +32,107 @@ use WikitextContent;
  */
 class RevisionStoreDbTest extends MediaWikiTestCase {
 
+       /**
+        * @return LoadBalancer
+        */
+       private function getLoadBalancerMock( array $server ) {
+               $lb = $this->getMockBuilder( LoadBalancer::class )
+                       ->setMethods( [ 'reallyOpenConnection' ] )
+                       ->setConstructorArgs( [ [ 'servers' => [ $server ] ] ] )
+                       ->getMock();
+
+               $lb->method( 'reallyOpenConnection' )->willReturnCallback(
+                       function ( array $server, $dbNameOverride = false ) {
+                               return $this->getDatabaseMock( $server );
+                       }
+               );
+
+               return $lb;
+       }
+
+       /**
+        * @return Database
+        */
+       private function getDatabaseMock( array $params ) {
+               $db = $this->getMockBuilder( DatabaseSqlite::class )
+                       ->setMethods( [ 'select', 'doQuery', 'open', 'closeConnection', 'isOpen' ] )
+                       ->setConstructorArgs( [ $params ] )
+                       ->getMock();
+
+               $db->method( 'select' )->willReturn( new FakeResultWrapper( [] ) );
+               $db->method( 'isOpen' )->willReturn( true );
+
+               return $db;
+       }
+
+       public function provideDomainCheck() {
+               yield [ false, 'test', '' ];
+               yield [ 'test', 'test', '' ];
+
+               yield [ false, 'test', 'foo_' ];
+               yield [ 'test-foo_', 'test', 'foo_' ];
+
+               yield [ false, 'dash-test', '' ];
+               yield [ 'dash-test', 'dash-test', '' ];
+
+               yield [ false, 'underscore_test', 'foo_' ];
+               yield [ 'underscore_test-foo_', 'underscore_test', 'foo_' ];
+       }
+
+       /**
+        * @dataProvider provideDomainCheck
+        * @covers \MediaWiki\Storage\RevisionStore::checkDatabaseWikiId
+        */
+       public function testDomainCheck( $wikiId, $dbName, $dbPrefix ) {
+               $this->setMwGlobals(
+                       [
+                               'wgDBname' => $dbName,
+                               'wgDBprefix' => $dbPrefix,
+                       ]
+               );
+
+               $loadBalancer = $this->getLoadBalancerMock(
+                       [
+                               'host' => '*dummy*',
+                               'dbDirectory' => '*dummy*',
+                               'user' => 'test',
+                               'password' => 'test',
+                               'flags' => 0,
+                               'variables' => [],
+                               'schema' => '',
+                               'cliMode' => true,
+                               'agent' => '',
+                               'load' => 100,
+                               'profiler' => null,
+                               'trxProfiler' => new TransactionProfiler(),
+                               'connLogger' => new \Psr\Log\NullLogger(),
+                               'queryLogger' => new \Psr\Log\NullLogger(),
+                               'errorLogger' => new \Psr\Log\NullLogger(),
+                               'type' => 'test',
+                               'dbname' => $dbName,
+                               'tablePrefix' => $dbPrefix,
+                       ]
+               );
+               $db = $loadBalancer->getConnection( DB_REPLICA );
+
+               $blobStore = $this->getMockBuilder( SqlBlobStore::class )
+                       ->disableOriginalConstructor()
+                       ->getMock();
+
+               $store = new RevisionStore(
+                       $loadBalancer,
+                       $blobStore,
+                       new WANObjectCache( [ 'cache' => new HashBagOStuff() ] ),
+                       $wikiId
+               );
+
+               $count = $store->countRevisionsByPageId( $db, 0 );
+
+               // Dummy check to make PhpUnit happy. We are really only interested in
+               // countRevisionsByPageId not failing due to the DB domain check.
+               $this->assertSame( 0, $count );
+       }
+
        private function assertLinkTargetsEqual( LinkTarget $l1, LinkTarget $l2 ) {
                $this->assertEquals( $l1->getDBkey(), $l2->getDBkey() );
                $this->assertEquals( $l1->getNamespace(), $l2->getNamespace() );
index 155a08e..ea13a0d 100644 (file)
@@ -70,6 +70,9 @@ class ApiComparePagesTest extends ApiTestCase {
                        'page', [ 'page_latest' => 0 ], [ 'page_id' => self::$repl['pageE'] ]
                );
 
+               self::$repl['revF1'] = $this->addPage( 'F', "== Section 1 ==\nF 1.1\n\n== Section 2 ==\nF 1.2" );
+               self::$repl['pageF'] = Title::newFromText( 'ApiComparePagesTest F' )->getArticleId();
+
                WikiPage::factory( Title::newFromText( 'ApiComparePagesTest C' ) )
                        ->doDeleteArticleReal( 'Test for ApiComparePagesTest' );
 
@@ -372,6 +375,26 @@ class ApiComparePagesTest extends ApiTestCase {
                                ],
                                false, true
                        ],
+                       'Basic diff, test with sections' => [
+                               [
+                                       'fromtitle' => 'ApiComparePagesTest F',
+                                       'fromsection' => 1,
+                                       'totext' => "== Section 1 ==\nTo text\n\n== Section 2 ==\nTo text?",
+                                       'tosection' => 2,
+                               ],
+                               [
+                                       'compare' => [
+                                               'body' => '<tr><td colspan="2" class="diff-lineno" id="mw-diff-left-l1" >Line 1:</td>' . "\n"
+                                                       . '<td colspan="2" class="diff-lineno">Line 1:</td></tr>' . "\n"
+                                                       . '<tr><td class=\'diff-marker\'>−</td><td class=\'diff-deletedline\'><div>== Section <del class="diffchange diffchange-inline">1 </del>==</div></td><td class=\'diff-marker\'>+</td><td class=\'diff-addedline\'><div>== Section <ins class="diffchange diffchange-inline">2 </ins>==</div></td></tr>' . "\n"
+                                                       . '<tr><td class=\'diff-marker\'>−</td><td class=\'diff-deletedline\'><div><del class="diffchange diffchange-inline">F 1.1</del></div></td><td class=\'diff-marker\'>+</td><td class=\'diff-addedline\'><div><ins class="diffchange diffchange-inline">To text?</ins></div></td></tr>' . "\n",
+                                               'fromid' => '{{REPL:pageF}}',
+                                               'fromrevid' => '{{REPL:revF1}}',
+                                               'fromns' => '0',
+                                               'fromtitle' => 'ApiComparePagesTest F',
+                                       ]
+                               ],
+                       ],
                        'Diff with all props' => [
                                [
                                        'fromrev' => '{{REPL:revB1}}',
@@ -568,6 +591,26 @@ class ApiComparePagesTest extends ApiTestCase {
                                [],
                                'compare-no-title',
                        ],
+                       'Error, test with invalid from section ID' => [
+                               [
+                                       'fromtitle' => 'ApiComparePagesTest F',
+                                       'fromsection' => 5,
+                                       'totext' => "== Section 1 ==\nTo text\n\n== Section 2 ==\nTo text?",
+                                       'tosection' => 2,
+                               ],
+                               [],
+                               'nosuchfromsection',
+                       ],
+                       'Error, test with invalid to section ID' => [
+                               [
+                                       'fromtitle' => 'ApiComparePagesTest F',
+                                       'fromsection' => 1,
+                                       'totext' => "== Section 1 ==\nTo text\n\n== Section 2 ==\nTo text?",
+                                       'tosection' => 5,
+                               ],
+                               [],
+                               'nosuchtosection',
+                       ],
                        'Error, Relative diff, no from revision' => [
                                [
                                        'fromtext' => 'Foo',
diff --git a/tests/phpunit/includes/api/format/ApiFormatBaseTest.php b/tests/phpunit/includes/api/format/ApiFormatBaseTest.php
new file mode 100644 (file)
index 0000000..d6a1390
--- /dev/null
@@ -0,0 +1,345 @@
+<?php
+
+use Wikimedia\TestingAccessWrapper;
+
+/**
+ * @group API
+ * @covers ApiFormatBase
+ */
+class ApiFormatBaseTest extends ApiFormatTestBase {
+
+       protected $printerName = 'mockbase';
+
+       public function getMockFormatter( ApiMain $main = null, $format, $methods = [] ) {
+               if ( $main === null ) {
+                       $context = new RequestContext;
+                       $context->setRequest( new FauxRequest( [], true ) );
+                       $main = new ApiMain( $context );
+               }
+
+               $mock = $this->getMockBuilder( ApiFormatBase::class )
+                       ->setConstructorArgs( [ $main, $format ] )
+                       ->setMethods( array_unique( array_merge( $methods, [ 'getMimeType', 'execute' ] ) ) )
+                       ->getMock();
+               if ( !in_array( 'getMimeType', $methods, true ) ) {
+                       $mock->method( 'getMimeType' )->willReturn( 'text/x-mock' );
+               }
+               return $mock;
+       }
+
+       protected function encodeData( array $params, array $data, $options = [] ) {
+               $options += [
+                       'name' => 'mock',
+                       'class' => ApiFormatBase::class,
+                       'factory' => function ( ApiMain $main, $format ) use ( $options ) {
+                               $mock = $this->getMockFormatter( $main, $format );
+                               $mock->expects( $this->once() )->method( 'execute' )
+                                       ->willReturnCallback( function () use ( $mock ) {
+                                               $mock->printText( "Format {$mock->getFormat()}: " );
+                                               $mock->printText( "<b>ok</b>" );
+                                       } );
+
+                               if ( isset( $options['status'] ) ) {
+                                       $mock->setHttpStatus( $options['status'] );
+                               }
+
+                               return $mock;
+                       },
+                       'returnPrinter' => true,
+               ];
+
+               $this->setMwGlobals( [
+                       'wgApiFrameOptions' => 'DENY',
+               ] );
+
+               $ret = parent::encodeData( $params, $data, $options );
+               $printer = TestingAccessWrapper::newFromObject( $ret['printer'] );
+               $text = $ret['text'];
+
+               if ( $options['name'] !== 'mockfm' ) {
+                       $ct = 'text/x-mock';
+                       $file = 'api-result.mock';
+                       $status = isset( $options['status'] ) ? $options['status'] : null;
+               } elseif ( isset( $params['wrappedhtml'] ) ) {
+                       $ct = 'text/mediawiki-api-prettyprint-wrapped';
+                       $file = 'api-result-wrapped.json';
+                       $status = null;
+
+                       // Replace varying field
+                       $text = preg_replace( '/"time":\d+/', '"time":1234', $text );
+               } else {
+                       $ct = 'text/html';
+                       $file = 'api-result.html';
+                       $status = null;
+
+                       // Strip OutputPage-generated HTML
+                       if ( preg_match( '!<pre class="api-pretty-content">.*</pre>!s', $text, $m ) ) {
+                               $text = $m[0];
+                       }
+               }
+
+               $response = $printer->getMain()->getRequest()->response();
+               $this->assertSame( "$ct; charset=utf-8", strtolower( $response->getHeader( 'Content-Type' ) ) );
+               $this->assertSame( 'DENY', $response->getHeader( 'X-Frame-Options' ) );
+               $this->assertSame( $file, $printer->getFilename() );
+               $this->assertSame( "inline; filename=\"$file\"", $response->getHeader( 'Content-Disposition' ) );
+               $this->assertSame( $status, $response->getStatusCode() );
+
+               return $text;
+       }
+
+       public static function provideGeneralEncoding() {
+               return [
+                       'normal' => [
+                               [],
+                               "Format MOCK: <b>ok</b>",
+                               [],
+                               [ 'name' => 'mock' ]
+                       ],
+                       'normal ignores wrappedhtml' => [
+                               [],
+                               "Format MOCK: <b>ok</b>",
+                               [ 'wrappedhtml' => 1 ],
+                               [ 'name' => 'mock' ]
+                       ],
+                       'HTML format' => [
+                               [],
+                               '<pre class="api-pretty-content">Format MOCK: &lt;b>ok&lt;/b></pre>',
+                               [],
+                               [ 'name' => 'mockfm' ]
+                       ],
+                       'wrapped HTML format' => [
+                               [],
+                               // phpcs:ignore Generic.Files.LineLength.TooLong
+                               '{"status":200,"statustext":"OK","html":"<pre class=\"api-pretty-content\">Format MOCK: &lt;b>ok&lt;/b></pre>","modules":["mediawiki.apipretty"],"continue":null,"time":1234}',
+                               [ 'wrappedhtml' => 1 ],
+                               [ 'name' => 'mockfm' ]
+                       ],
+                       'normal, with set status' => [
+                               [],
+                               "Format MOCK: <b>ok</b>",
+                               [],
+                               [ 'name' => 'mock', 'status' => 400 ]
+                       ],
+                       'HTML format, with set status' => [
+                               [],
+                               '<pre class="api-pretty-content">Format MOCK: &lt;b>ok&lt;/b></pre>',
+                               [],
+                               [ 'name' => 'mockfm', 'status' => 400 ]
+                       ],
+                       'wrapped HTML format, with set status' => [
+                               [],
+                               // phpcs:ignore Generic.Files.LineLength.TooLong
+                               '{"status":400,"statustext":"Bad Request","html":"<pre class=\"api-pretty-content\">Format MOCK: &lt;b>ok&lt;/b></pre>","modules":["mediawiki.apipretty"],"continue":null,"time":1234}',
+                               [ 'wrappedhtml' => 1 ],
+                               [ 'name' => 'mockfm', 'status' => 400 ]
+                       ],
+                       'wrapped HTML format, cross-domain-policy' => [
+                               [ 'continue' => '< CrOsS-DoMaIn-PoLiCy >' ],
+                               // phpcs:ignore Generic.Files.LineLength.TooLong
+                               '{"status":200,"statustext":"OK","html":"<pre class=\"api-pretty-content\">Format MOCK: &lt;b>ok&lt;/b></pre>","modules":["mediawiki.apipretty"],"continue":"\u003C CrOsS-DoMaIn-PoLiCy \u003E","time":1234}',
+                               [ 'wrappedhtml' => 1 ],
+                               [ 'name' => 'mockfm' ]
+                       ],
+               ];
+       }
+
+       public function testBasics() {
+               $printer = $this->getMockFormatter( null, 'mock' );
+               $this->assertTrue( $printer->canPrintErrors() );
+               $this->assertSame(
+                       'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Data_formats',
+                       $printer->getHelpUrls()
+               );
+       }
+
+       public function testDisable() {
+               $this->setMwGlobals( [
+                       'wgApiFrameOptions' => 'DENY',
+               ] );
+
+               $printer = $this->getMockFormatter( null, 'mock' );
+               $printer->method( 'execute' )->willReturnCallback( function () use ( $printer ) {
+                       $printer->printText( 'Foo' );
+               } );
+               $this->assertFalse( $printer->isDisabled() );
+               $printer->disable();
+               $this->assertTrue( $printer->isDisabled() );
+
+               $printer->setHttpStatus( 400 );
+               $printer->initPrinter();
+               $printer->execute();
+               ob_start();
+               $printer->closePrinter();
+               $this->assertSame( '', ob_get_clean() );
+               $response = $printer->getMain()->getRequest()->response();
+               $this->assertNull( $response->getHeader( 'Content-Type' ) );
+               $this->assertNull( $response->getHeader( 'X-Frame-Options' ) );
+               $this->assertNull( $response->getHeader( 'Content-Disposition' ) );
+               $this->assertNull( $response->getStatusCode() );
+       }
+
+       public function testNullMimeType() {
+               $this->setMwGlobals( [
+                       'wgApiFrameOptions' => 'DENY',
+               ] );
+
+               $printer = $this->getMockFormatter( null, 'mock', [ 'getMimeType' ] );
+               $printer->method( 'execute' )->willReturnCallback( function () use ( $printer ) {
+                       $printer->printText( 'Foo' );
+               } );
+               $printer->method( 'getMimeType' )->willReturn( null );
+               $this->assertNull( $printer->getMimeType(), 'sanity check' );
+
+               $printer->initPrinter();
+               $printer->execute();
+               ob_start();
+               $printer->closePrinter();
+               $this->assertSame( 'Foo', ob_get_clean() );
+               $response = $printer->getMain()->getRequest()->response();
+               $this->assertNull( $response->getHeader( 'Content-Type' ) );
+               $this->assertNull( $response->getHeader( 'X-Frame-Options' ) );
+               $this->assertNull( $response->getHeader( 'Content-Disposition' ) );
+
+               $printer = $this->getMockFormatter( null, 'mockfm', [ 'getMimeType' ] );
+               $printer->method( 'execute' )->willReturnCallback( function () use ( $printer ) {
+                       $printer->printText( 'Foo' );
+               } );
+               $printer->method( 'getMimeType' )->willReturn( null );
+               $this->assertNull( $printer->getMimeType(), 'sanity check' );
+               $this->assertTrue( $printer->getIsHtml(), 'sanity check' );
+
+               $printer->initPrinter();
+               $printer->execute();
+               ob_start();
+               $printer->closePrinter();
+               $this->assertSame( 'Foo', ob_get_clean() );
+               $response = $printer->getMain()->getRequest()->response();
+               $this->assertSame(
+                       'text/html; charset=utf-8', strtolower( $response->getHeader( 'Content-Type' ) )
+               );
+               $this->assertSame( 'DENY', $response->getHeader( 'X-Frame-Options' ) );
+               $this->assertSame(
+                       'inline; filename="api-result.html"', $response->getHeader( 'Content-Disposition' )
+               );
+       }
+
+       public function testApiFrameOptions() {
+               $this->setMwGlobals( [ 'wgApiFrameOptions' => 'DENY' ] );
+               $printer = $this->getMockFormatter( null, 'mock' );
+               $printer->initPrinter();
+               $this->assertSame(
+                       'DENY',
+                       $printer->getMain()->getRequest()->response()->getHeader( 'X-Frame-Options' )
+               );
+
+               $this->setMwGlobals( [ 'wgApiFrameOptions' => 'SAMEORIGIN' ] );
+               $printer = $this->getMockFormatter( null, 'mock' );
+               $printer->initPrinter();
+               $this->assertSame(
+                       'SAMEORIGIN',
+                       $printer->getMain()->getRequest()->response()->getHeader( 'X-Frame-Options' )
+               );
+
+               $this->setMwGlobals( [ 'wgApiFrameOptions' => false ] );
+               $printer = $this->getMockFormatter( null, 'mock' );
+               $printer->initPrinter();
+               $this->assertNull(
+                       $printer->getMain()->getRequest()->response()->getHeader( 'X-Frame-Options' )
+               );
+       }
+
+       public function testForceDefaultParams() {
+               $context = new RequestContext;
+               $context->setRequest( new FauxRequest( [ 'foo' => '1', 'bar' => '2', 'baz' => '3' ], true ) );
+               $main = new ApiMain( $context );
+               $allowedParams = [
+                       'foo' => [],
+                       'bar' => [ ApiBase::PARAM_DFLT => 'bar?' ],
+                       'baz' => 'baz!',
+               ];
+
+               $printer = $this->getMockFormatter( $main, 'mock', [ 'getAllowedParams' ] );
+               $printer->method( 'getAllowedParams' )->willReturn( $allowedParams );
+               $this->assertEquals(
+                       [ 'foo' => '1', 'bar' => '2', 'baz' => '3' ],
+                       $printer->extractRequestParams(),
+                       'sanity check'
+               );
+
+               $printer = $this->getMockFormatter( $main, 'mock', [ 'getAllowedParams' ] );
+               $printer->method( 'getAllowedParams' )->willReturn( $allowedParams );
+               $printer->forceDefaultParams();
+               $this->assertEquals(
+                       [ 'foo' => null, 'bar' => 'bar?', 'baz' => 'baz!' ],
+                       $printer->extractRequestParams()
+               );
+       }
+
+       public function testGetAllowedParams() {
+               $printer = $this->getMockFormatter( null, 'mock' );
+               $this->assertSame( [], $printer->getAllowedParams() );
+
+               $printer = $this->getMockFormatter( null, 'mockfm' );
+               $this->assertSame( [
+                       'wrappedhtml' => [
+                               ApiBase::PARAM_DFLT => false,
+                               ApiBase::PARAM_HELP_MSG => 'apihelp-format-param-wrappedhtml',
+                       ]
+               ], $printer->getAllowedParams() );
+       }
+
+       public function testGetExamplesMessages() {
+               $printer = TestingAccessWrapper::newFromObject( $this->getMockFormatter( null, 'mock' ) );
+               $this->assertSame( [
+                       'action=query&meta=siteinfo&siprop=namespaces&format=mock'
+                               => [ 'apihelp-format-example-generic', 'MOCK' ]
+               ], $printer->getExamplesMessages() );
+
+               $printer = TestingAccessWrapper::newFromObject( $this->getMockFormatter( null, 'mockfm' ) );
+               $this->assertSame( [
+                       'action=query&meta=siteinfo&siprop=namespaces&format=mockfm'
+                               => [ 'apihelp-format-example-generic', 'MOCK' ]
+               ], $printer->getExamplesMessages() );
+       }
+
+       /**
+        * @dataProvider provideHtmlHeader
+        */
+       public function testHtmlHeader( $post, $registerNonHtml, $expect ) {
+               $context = new RequestContext;
+               $request = new FauxRequest( [ 'a' => 1, 'b' => 2 ], $post );
+               $request->setRequestURL( 'http://example.org/wx/api.php' );
+               $context->setRequest( $request );
+               $context->setLanguage( 'qqx' );
+               $main = new ApiMain( $context );
+               $printer = $this->getMockFormatter( $main, 'mockfm' );
+               $mm = $printer->getMain()->getModuleManager();
+               $mm->addModule( 'mockfm', 'format', ApiFormatBase::class, function () {
+                       return $mock;
+               } );
+               if ( $registerNonHtml ) {
+                       $mm->addModule( 'mock', 'format', ApiFormatBase::class, function () {
+                               return $mock;
+                       } );
+               }
+
+               $printer->initPrinter();
+               $printer->execute();
+               ob_start();
+               $printer->closePrinter();
+               $text = ob_get_clean();
+               $this->assertContains( $expect, $text );
+       }
+
+       public static function provideHtmlHeader() {
+               return [
+                       [ false, false, '(api-format-prettyprint-header-only-html: MOCK)' ],
+                       [ true, false, '(api-format-prettyprint-header-only-html: MOCK)' ],
+                       // phpcs:ignore Generic.Files.LineLength.TooLong
+                       [ false, true, '(api-format-prettyprint-header-hyperlinked: MOCK, mock, <a rel="nofollow" class="external free" href="http://example.org/wx/api.php?a=1&amp;b=2&amp;format=mock">http://example.org/wx/api.php?a=1&amp;b=2&amp;format=mock</a>)' ],
+                       [ true, true, '(api-format-prettyprint-header: MOCK, mock)' ],
+               ];
+       }
+
+}
diff --git a/tests/phpunit/includes/api/format/ApiFormatRawTest.php b/tests/phpunit/includes/api/format/ApiFormatRawTest.php
new file mode 100644 (file)
index 0000000..0d3e63f
--- /dev/null
@@ -0,0 +1,120 @@
+<?php
+
+/**
+ * @group API
+ * @covers ApiFormatRaw
+ */
+class ApiFormatRawTest extends ApiFormatTestBase {
+
+       protected $printerName = 'raw';
+
+       /**
+        * Test basic encoding and missing mime and text exceptions
+        * @return array datasets
+        */
+       public static function provideGeneralEncoding() {
+               $options = [
+                       'class' => 'ApiFormatRaw',
+                       'factory' => function ( ApiMain $main ) {
+                               return new ApiFormatRaw( $main, new ApiFormatJson( $main, 'json' ) );
+                       }
+               ];
+
+               return [
+                       [
+                               [ 'mime' => 'text/plain', 'text' => 'foo' ],
+                               'foo',
+                               [],
+                               $options
+                       ],
+                       [
+                               [ 'mime' => 'text/plain', 'text' => 'fóo' ],
+                               'fóo',
+                               [],
+                               $options
+                       ],
+                       [
+                               [ 'text' => 'some text' ],
+                               new MWException( 'No MIME type set for raw formatter' ),
+                               [],
+                               $options
+                       ],
+                       [
+                               [ 'mime' => 'text/plain' ],
+                               new MWException( 'No text given for raw formatter' ),
+                               [],
+                               $options
+                       ],
+                       'test error fallback' => [
+                               [ 'mime' => 'text/plain', 'text' => 'some text', 'error' => 'some error' ],
+                               '{"mime":"text/plain","text":"some text","error":"some error"}',
+                               [],
+                               $options
+                       ]
+               ];
+       }
+
+       /**
+        * Test specifying filename
+        */
+       public function testFilename() {
+               $printer = new ApiFormatRaw( new ApiMain );
+               $printer->getResult()->addValue( null, 'filename', 'whatever.raw' );
+               $this->assertSame( 'whatever.raw', $printer->getFilename() );
+       }
+
+       /**
+        * Test specifying filename with error fallback printer
+        */
+       public function testErrorFallbackFilename() {
+               $apiMain = new ApiMain;
+               $printer = new ApiFormatRaw( $apiMain, new ApiFormatJson( $apiMain, 'json' ) );
+               $printer->getResult()->addValue( null, 'error', 'some error' );
+               $printer->getResult()->addValue( null, 'filename', 'whatever.raw' );
+               $this->assertSame( 'api-result.json', $printer->getFilename() );
+       }
+
+       /**
+        * Test specifying mime
+        */
+       public function testMime() {
+               $printer = new ApiFormatRaw( new ApiMain );
+               $printer->getResult()->addValue( null, 'mime', 'text/plain' );
+               $this->assertSame( 'text/plain', $printer->getMimeType() );
+       }
+
+       /**
+        * Test specifying mime with error fallback printer
+        */
+       public function testErrorFallbackMime() {
+               $apiMain = new ApiMain;
+               $printer = new ApiFormatRaw( $apiMain, new ApiFormatJson( $apiMain, 'json' ) );
+               $printer->getResult()->addValue( null, 'error', 'some error' );
+               $printer->getResult()->addValue( null, 'mime', 'text/plain' );
+               $this->assertSame( 'application/json', $printer->getMimeType() );
+       }
+
+       /**
+        * Check that setting failWithHTTPError to true will result in 400 response status code
+        */
+       public function testFailWithHTTPError() {
+               $apiMain = null;
+
+               $this->testGeneralEncoding(
+                       [ 'mime' => 'text/plain', 'text' => 'some text', 'error' => 'some error' ],
+                       '{"mime":"text/plain","text":"some text","error":"some error"}',
+                       [],
+                       [
+                               'class' => 'ApiFormatRaw',
+                               'factory' => function ( ApiMain $main ) use ( &$apiMain ) {
+                                       $apiMain = $main;
+                                       $printer = new ApiFormatRaw( $main, new ApiFormatJson( $main, 'json' ) );
+                                       $printer->setFailWithHTTPError( true );
+                                       return $printer;
+                               }
+                       ]
+               );
+               $this->assertEquals( 400, $apiMain->getRequest()->response()->getStatusCode() );
+       }
+
+}
index fb086e9..4169dab 100644 (file)
@@ -11,26 +11,40 @@ abstract class ApiFormatTestBase extends MediaWikiTestCase {
        /**
         * Return general data to be encoded for testing
         * @return array See self::testGeneralEncoding
-        * @throws Exception
+        * @throws BadMethodCallException
         */
        public static function provideGeneralEncoding() {
-               throw new Exception( 'Subclass must implement ' . __METHOD__ );
+               throw new BadMethodCallException( static::class . ' must implement ' . __METHOD__ );
        }
 
        /**
         * Get the formatter output for the given input data
         * @param array $params Query parameters
         * @param array $data Data to encode
-        * @param string $class Printer class to use instead of the normal one
-        * @return string
+        * @param array $options Options. If passed a string, the string is treated
+        *  as the 'class' option.
+        *  - name: Format name, rather than $this->printerName
+        *  - class: If set, register 'name' with this class (and 'factory', if that's set)
+        *  - factory: Used with 'class' to register at runtime
+        *  - returnPrinter: Return the printer object
+        * @param callable|null $factory Factory to use instead of the normal one
+        * @return string|array The string if $options['returnPrinter'] isn't set, or an array if it is:
+        *  - text: Output text string
+        *  - printer: ApiFormatBase
         * @throws Exception
         */
-       protected function encodeData( array $params, array $data, $class = null ) {
+       protected function encodeData( array $params, array $data, $options = [] ) {
+               if ( is_string( $options ) ) {
+                       $options = [ 'class' => $options ];
+               }
+               $printerName = isset( $options['name'] ) ? $options['name'] : $this->printerName;
+
                $context = new RequestContext;
                $context->setRequest( new FauxRequest( $params, true ) );
                $main = new ApiMain( $context );
-               if ( $class !== null ) {
-                       $main->getModuleManager()->addModule( $this->printerName, 'format', $class );
+               if ( isset( $options['class'] ) ) {
+                       $factory = isset( $options['factory'] ) ? $options['factory'] : null;
+                       $main->getModuleManager()->addModule( $printerName, 'format', $options['class'], $factory );
                }
                $result = $main->getResult();
                $result->addArrayType( null, 'default' );
@@ -38,27 +52,42 @@ abstract class ApiFormatTestBase extends MediaWikiTestCase {
                        $result->addValue( null, $k, $v );
                }
 
-               $printer = $main->createPrinterByName( $this->printerName );
+               $ret = [];
+               $printer = $main->createPrinterByName( $printerName );
                $printer->initPrinter();
                $printer->execute();
                ob_start();
                try {
                        $printer->closePrinter();
-                       return ob_get_clean();
+                       $ret['text'] = ob_get_clean();
                } catch ( Exception $ex ) {
                        ob_end_clean();
                        throw $ex;
                }
+
+               if ( !empty( $options['returnPrinter'] ) ) {
+                       $ret['printer'] = $printer;
+               }
+
+               return count( $ret ) === 1 ? $ret['text'] : $ret;
        }
 
        /**
         * @dataProvider provideGeneralEncoding
+        * @param array $data Data to be encoded
+        * @param string|Exception $expect String to expect, or exception expected to be thrown
+        * @param array $params Query parameters to set in the FauxRequest
+        * @param array $options Options to pass to self::encodeData()
         */
-       public function testGeneralEncoding( array $data, $expect, array $params = [] ) {
-               if ( isset( $params['SKIP'] ) ) {
-                       $this->markTestSkipped( $expect );
+       public function testGeneralEncoding(
+               array $data, $expect, array $params = [], array $options = []
+       ) {
+               if ( $expect instanceof Exception ) {
+                       $this->setExpectedException( get_class( $expect ), $expect->getMessage() );
+                       $this->encodeData( $params, $data, $options ); // Should throw
+               } else {
+                       $this->assertSame( $expect, $this->encodeData( $params, $data, $options ) );
                }
-               $this->assertSame( $expect, $this->encodeData( $params, $data ) );
        }
 
 }
index 668badd..b9bf5b9 100644 (file)
@@ -86,4 +86,120 @@ class TextboxBuilderTest extends MediaWikiTestCase {
                // classes ok when nothing to be merged
                $this->assertSame( 'mw-editfont-monospace', $attribs3['class'] );
        }
+
+       public function provideMergeClassesIntoAttributes() {
+               return [
+                       [
+                               [],
+                               [],
+                               [],
+                       ],
+                       [
+                               [ 'mw-new-classname' ],
+                               [],
+                               [ 'class' => 'mw-new-classname' ],
+                       ],
+                       [
+                               [],
+                               [ 'title' => 'My Title' ],
+                               [ 'title' => 'My Title' ],
+                       ],
+                       [
+                               [ 'mw-new-classname' ],
+                               [ 'title' => 'My Title' ],
+                               [ 'title' => 'My Title', 'class' => 'mw-new-classname' ],
+                       ],
+                       [
+                               [ 'mw-new-classname' ],
+                               [ 'class' => 'mw-existing-classname' ],
+                               [ 'class' => 'mw-new-classname mw-existing-classname' ],
+                       ],
+               ];
+       }
+
+       /**
+        * @dataProvider provideMergeClassesIntoAttributes
+        */
+       public function testMergeClassesIntoAttributes( $inputClasses, $inputAttributes, $expected ) {
+               $builder = new TextboxBuilder();
+               $this->assertSame(
+                       $expected,
+                       $builder->mergeClassesIntoAttributes( $inputClasses, $inputAttributes )
+               );
+       }
+
+       public function provideGetTextboxProtectionCSSClasses() {
+               return [
+                       [
+                               [ '' ],
+                               [ 'isProtected' ],
+                               [],
+                       ],
+                       [
+                               true,
+                               [],
+                               [],
+                       ],
+                       [
+                               true,
+                               [ 'isProtected' ],
+                               [ 'mw-textarea-protected' ]
+                       ],
+                       [
+                               true,
+                               [ 'isProtected', 'isSemiProtected' ],
+                               [ 'mw-textarea-sprotected' ],
+                       ],
+                       [
+                               true,
+                               [ 'isProtected', 'isCascadeProtected' ],
+                               [ 'mw-textarea-protected', 'mw-textarea-cprotected' ],
+                       ],
+                       [
+                               true,
+                               [ 'isProtected', 'isCascadeProtected', 'isSemiProtected' ],
+                               [ 'mw-textarea-sprotected', 'mw-textarea-cprotected' ],
+                       ],
+               ];
+       }
+
+       /**
+        * @dataProvider provideGetTextboxProtectionCSSClasses
+        */
+       public function testGetTextboxProtectionCSSClasses(
+               $restrictionLevels,
+               $protectionModes,
+               $expected
+       ) {
+               $this->setMwGlobals( [
+                       // set to trick MWNamespace::getRestrictionLevels
+                       'wgRestrictionLevels' => $restrictionLevels
+               ] );
+
+               $builder = new TextboxBuilder();
+               $this->assertSame( $expected, $builder->getTextboxProtectionCSSClasses(
+                       $this->mockProtectedTitle( $protectionModes )
+               ) );
+       }
+
+       /**
+        * @return Title
+        */
+       private function mockProtectedTitle( $methodsToReturnTrue ) {
+               $title = $this->getMockBuilder( Title::class )
+                       ->disableOriginalConstructor()
+                       ->getMock();
+
+               $title->expects( $this->any() )
+                       ->method( 'getNamespace' )
+                       ->will( $this->returnValue( 1 ) );
+
+               foreach ( $methodsToReturnTrue as $method ) {
+                       $title->expects( $this->any() )
+                               ->method( $method )
+                               ->will( $this->returnValue( true ) );
+               }
+
+               return $title;
+       }
 }
index a3f3981..78207ac 100644 (file)
@@ -2,7 +2,7 @@
 
 namespace Wikimedia\Tests\Rdbms;
 
-use IDatabase;
+use Wikimedia\Rdbms\IDatabase;
 use Wikimedia\Rdbms\LoadBalancer;
 use PHPUnit_Framework_MockObject_MockObject;
 use Wikimedia\Rdbms\ConnectionManager;
index 4e76f2a..3982ee7 100644 (file)
@@ -2,7 +2,7 @@
 
 namespace Wikimedia\Tests\Rdbms;
 
-use IDatabase;
+use Wikimedia\Rdbms\IDatabase;
 use Wikimedia\Rdbms\LoadBalancer;
 use PHPUnit_Framework_MockObject_MockObject;
 use Wikimedia\Rdbms\SessionConsistentConnectionManager;