API: Reset token cache on login, so API tests work
authordaniel <daniel.kinzler@wikimedia.de>
Fri, 22 Jun 2012 20:37:26 +0000 (22:37 +0200)
committerdaniel <daniel.kinzler@wikimedia.de>
Fri, 22 Jun 2012 21:34:59 +0000 (23:34 +0200)
Previously, logging in several times during a phpunit run would change the
session token, but keep the edit token, leasing to "bad token" failures for
all but the first login.

Change-Id: Iad49c990c5661d55cd907b8441addb74eb0ef694

includes/api/ApiLogin.php
includes/api/ApiQueryInfo.php

index 0bdaa1b..2ad2653 100644 (file)
@@ -79,6 +79,8 @@ class ApiLogin extends ApiBase {
                                $user->setOption( 'rememberpassword', 1 );
                                $user->setCookies( $this->getRequest() );
 
+                               ApiQueryInfo::resetTokenCache();
+
                                // Run hooks.
                                // @todo FIXME: Split back and frontend from this hook.
                                // @todo FIXME: This hook should be placed in the backend
index a6c0ed5..87fd58b 100644 (file)
@@ -99,6 +99,12 @@ class ApiQueryInfo extends ApiQueryBase {
                return $this->tokenFunctions;
        }
 
+       static $cachedTokens = array();
+
+       public static function resetTokenCache() {
+               ApiQueryInfo::$cachedTokens = array();
+       }
+
        public static function getEditToken( $pageid, $title ) {
                // We could check for $title->userCan('edit') here,
                // but that's too expensive for this purpose
@@ -108,14 +114,12 @@ class ApiQueryInfo extends ApiQueryBase {
                        return false;
                }
 
-               // The edit token is always the same, let's exploit that
-               static $cachedEditToken = null;
-               if ( !is_null( $cachedEditToken ) ) {
-                       return $cachedEditToken;
+               // The token is always the same, let's exploit that
+               if ( !isset( ApiQueryInfo::$cachedTokens[ 'edit' ] ) ) {
+                       ApiQueryInfo::$cachedTokens[ 'edit' ] = $wgUser->getEditToken();
                }
 
-               $cachedEditToken = $wgUser->getEditToken();
-               return $cachedEditToken;
+               return ApiQueryInfo::$cachedTokens[ 'edit' ];
        }
 
        public static function getDeleteToken( $pageid, $title ) {
@@ -124,13 +128,12 @@ class ApiQueryInfo extends ApiQueryBase {
                        return false;
                }
 
-               static $cachedDeleteToken = null;
-               if ( !is_null( $cachedDeleteToken ) ) {
-                       return $cachedDeleteToken;
+               // The token is always the same, let's exploit that
+               if ( !isset( ApiQueryInfo::$cachedTokens[ 'delete' ] ) ) {
+                       ApiQueryInfo::$cachedTokens[ 'delete' ] = $wgUser->getEditToken();
                }
 
-               $cachedDeleteToken = $wgUser->getEditToken();
-               return $cachedDeleteToken;
+               return ApiQueryInfo::$cachedTokens[ 'delete' ];
        }
 
        public static function getProtectToken( $pageid, $title ) {
@@ -139,13 +142,12 @@ class ApiQueryInfo extends ApiQueryBase {
                        return false;
                }
 
-               static $cachedProtectToken = null;
-               if ( !is_null( $cachedProtectToken ) ) {
-                       return $cachedProtectToken;
+               // The token is always the same, let's exploit that
+               if ( !isset( ApiQueryInfo::$cachedTokens[ 'protect' ] ) ) {
+                       ApiQueryInfo::$cachedTokens[ 'protect' ] = $wgUser->getEditToken();
                }
 
-               $cachedProtectToken = $wgUser->getEditToken();
-               return $cachedProtectToken;
+               return ApiQueryInfo::$cachedTokens[ 'protect' ];
        }
 
        public static function getMoveToken( $pageid, $title ) {
@@ -154,13 +156,12 @@ class ApiQueryInfo extends ApiQueryBase {
                        return false;
                }
 
-               static $cachedMoveToken = null;
-               if ( !is_null( $cachedMoveToken ) ) {
-                       return $cachedMoveToken;
+               // The token is always the same, let's exploit that
+               if ( !isset( ApiQueryInfo::$cachedTokens[ 'move' ] ) ) {
+                       ApiQueryInfo::$cachedTokens[ 'move' ] = $wgUser->getEditToken();
                }
 
-               $cachedMoveToken = $wgUser->getEditToken();
-               return $cachedMoveToken;
+               return ApiQueryInfo::$cachedTokens[ 'move' ];
        }
 
        public static function getBlockToken( $pageid, $title ) {
@@ -169,13 +170,12 @@ class ApiQueryInfo extends ApiQueryBase {
                        return false;
                }
 
-               static $cachedBlockToken = null;
-               if ( !is_null( $cachedBlockToken ) ) {
-                       return $cachedBlockToken;
+               // The token is always the same, let's exploit that
+               if ( !isset( ApiQueryInfo::$cachedTokens[ 'block' ] ) ) {
+                       ApiQueryInfo::$cachedTokens[ 'block' ] = $wgUser->getEditToken();
                }
 
-               $cachedBlockToken = $wgUser->getEditToken();
-               return $cachedBlockToken;
+               return ApiQueryInfo::$cachedTokens[ 'block' ];
        }
 
        public static function getUnblockToken( $pageid, $title ) {
@@ -189,13 +189,12 @@ class ApiQueryInfo extends ApiQueryBase {
                        return false;
                }
 
-               static $cachedEmailToken = null;
-               if ( !is_null( $cachedEmailToken ) ) {
-                       return $cachedEmailToken;
+               // The token is always the same, let's exploit that
+               if ( !isset( ApiQueryInfo::$cachedTokens[ 'email' ] ) ) {
+                       ApiQueryInfo::$cachedTokens[ 'email' ] = $wgUser->getEditToken();
                }
 
-               $cachedEmailToken = $wgUser->getEditToken();
-               return $cachedEmailToken;
+               return ApiQueryInfo::$cachedTokens[ 'email' ];
        }
 
        public static function getImportToken( $pageid, $title ) {
@@ -204,13 +203,12 @@ class ApiQueryInfo extends ApiQueryBase {
                        return false;
                }
 
-               static $cachedImportToken = null;
-               if ( !is_null( $cachedImportToken ) ) {
-                       return $cachedImportToken;
+               // The token is always the same, let's exploit that
+               if ( !isset( ApiQueryInfo::$cachedTokens[ 'import' ] ) ) {
+                       ApiQueryInfo::$cachedTokens[ 'import' ] = $wgUser->getEditToken();
                }
 
-               $cachedImportToken = $wgUser->getEditToken();
-               return $cachedImportToken;
+               return ApiQueryInfo::$cachedTokens[ 'import' ];
        }
 
        public static function getWatchToken( $pageid, $title ) {
@@ -219,13 +217,12 @@ class ApiQueryInfo extends ApiQueryBase {
                        return false;
                }
 
-               static $cachedWatchToken = null;
-               if ( !is_null( $cachedWatchToken ) ) {
-                       return $cachedWatchToken;
+               // The token is always the same, let's exploit that
+               if ( !isset( ApiQueryInfo::$cachedTokens[ 'watch' ] ) ) {
+                       ApiQueryInfo::$cachedTokens[ 'watch' ] = $wgUser->getEditToken( 'watch' );
                }
 
-               $cachedWatchToken = $wgUser->getEditToken( 'watch' );
-               return $cachedWatchToken;
+               return ApiQueryInfo::$cachedTokens[ 'watch' ];
        }
 
        public static function getOptionsToken( $pageid, $title ) {
@@ -234,13 +231,12 @@ class ApiQueryInfo extends ApiQueryBase {
                        return false;
                }
 
-               static $cachedOptionsToken = null;
-               if ( !is_null( $cachedOptionsToken ) ) {
-                       return $cachedOptionsToken;
+               // The token is always the same, let's exploit that
+               if ( !isset( ApiQueryInfo::$cachedTokens[ 'options' ] ) ) {
+                       ApiQueryInfo::$cachedTokens[ 'options' ] = $wgUser->getEditToken();
                }
 
-               $cachedOptionsToken = $wgUser->getEditToken();
-               return $cachedOptionsToken;
+               return ApiQueryInfo::$cachedTokens[ 'options' ];
        }
 
        public function execute() {