API: Handle "special" options in action=options
authorBrad Jorsch <bjorsch@wikimedia.org>
Thu, 10 Oct 2013 14:54:08 +0000 (10:54 -0400)
committerBrad Jorsch <bjorsch@wikimedia.org>
Thu, 10 Oct 2013 15:02:04 +0000 (11:02 -0400)
There are certain preferences, such as realname and emailaddress, that
are handled specially in Special:Preferences and are not accessible by
User::getOptions or User::setOptions. But action=options was allowing
a 'dummy' version to be set in User::setOptions.

Change-Id: I4c1b3d8e1eae9520228d7b6da9c41ada80f7c387

includes/Preferences.php
includes/User.php
includes/api/ApiOptions.php
tests/phpunit/includes/api/ApiOptionsTest.php

index c9caf4f..87118d6 100644 (file)
@@ -62,6 +62,13 @@ class Preferences {
                'emailaddress',
        );
 
+       /**
+        * @return array
+        */
+       static function getSaveBlacklist() {
+               return self::$saveBlacklist;
+       }
+
        /**
         * @throws MWException
         * @param $user User
index 12912e1..d6e0a51 100644 (file)
@@ -2443,6 +2443,8 @@ class User {
         * - 'registered-checkmatrix' - as above, using the 'checkmatrix' type.
         * - 'userjs' - preferences with names starting with 'userjs-', intended to
         *              be used by user scripts.
+        * - 'special' - "preferences" that are not accessible via User::getOptions
+        *               or User::setOptions.
         * - 'unused' - preferences about which MediaWiki doesn't know anything.
         *              These are usually legacy options, removed in newer versions.
         *
@@ -2459,6 +2461,7 @@ class User {
                        'registered-multiselect',
                        'registered-checkmatrix',
                        'userjs',
+                       'special',
                        'unused'
                );
        }
@@ -2483,6 +2486,13 @@ class User {
                $prefs = Preferences::getPreferences( $this, $context );
                $mapping = array();
 
+               // Pull out the "special" options, so they don't get converted as
+               // multiselect or checkmatrix.
+               $specialOptions = array_fill_keys( Preferences::getSaveBlacklist(), true );
+               foreach ( $specialOptions as $name => $value ) {
+                       unset( $prefs[$name] );
+               }
+
                // Multiselect and checkmatrix options are stored in the database with
                // one key per option, each having a boolean value. Extract those keys.
                $multiselectOptions = array();
@@ -2525,6 +2535,8 @@ class User {
                                $mapping[$key] = 'registered-multiselect';
                        } elseif ( isset( $checkmatrixOptions[$key] ) ) {
                                $mapping[$key] = 'registered-checkmatrix';
+                       } elseif ( isset( $specialOptions[$key] ) ) {
+                               $mapping[$key] = 'special';
                        } elseif ( substr( $key, 0, 7 ) === 'userjs-' ) {
                                $mapping[$key] = 'userjs';
                        } else {
index b5aec77..c9d6378 100644 (file)
@@ -99,6 +99,9 @@ class ApiOptions extends ApiBase {
                                                $validation = true;
                                        }
                                        break;
+                               case 'special':
+                                       $validation = "cannot be set by this module";
+                                       break;
                                case 'unused':
                                default:
                                        $validation = "not a valid preference";
index ad1e73a..e718957 100644 (file)
@@ -102,6 +102,7 @@ class ApiOptionsTest extends MediaWikiLangTestCase {
                        'testmultiselect-opt2' => 'registered-multiselect',
                        'testmultiselect-opt3' => 'registered-multiselect',
                        'testmultiselect-opt4' => 'registered-multiselect',
+                       'special' => 'special',
                );
 
                if ( $options === null ) {
@@ -375,6 +376,29 @@ class ApiOptionsTest extends MediaWikiLangTestCase {
                $this->assertEquals( self::$Success, $response );
        }
 
+       public function testSpecialOption() {
+               $this->mUserMock->expects( $this->never() )
+                       ->method( 'resetOptions' );
+
+               $this->mUserMock->expects( $this->never() )
+                       ->method( 'saveSettings' );
+
+               $request = $this->getSampleRequest( array(
+                       'change' => 'special=1'
+               ) );
+
+               $response = $this->executeQuery( $request );
+
+               $this->assertEquals( array(
+                       'options' => 'success',
+                       'warnings' => array(
+                               'options' => array(
+                                       '*' => "Validation error for 'special': cannot be set by this module"
+                               )
+                       )
+               ), $response );
+       }
+
        public function testUnknownOption() {
                $this->mUserMock->expects( $this->never() )
                        ->method( 'resetOptions' );