Added debugCall() for AJAXProxyAction
authorAlexander Ebert <ebert@woltlab.com>
Fri, 31 Aug 2012 11:47:34 +0000 (13:47 +0200)
committerAlexander Ebert <ebert@woltlab.com>
Fri, 31 Aug 2012 11:47:34 +0000 (13:47 +0200)
wcfsetup/install/files/lib/action/AJAXProxyAction.class.php

index fab13a263d38763f185b36320060e57b0bdeef2b..f70ac6abfe6256509871bbc3e74b7e090ccca70f 100644 (file)
@@ -23,6 +23,12 @@ use wcf\util\StringUtil;
  * @category   Community Framework
  */
 class AJAXProxyAction extends AbstractSecureAction {
+       /**
+        * action name
+        * @var string
+        */
+       protected $actionName = '';
+       
        /**
         * class name
         * @var string
@@ -30,10 +36,16 @@ class AJAXProxyAction extends AbstractSecureAction {
        protected $className = '';
        
        /**
-        * action name
-        * @var string
+        * debug mode
+        * @var boolean
         */
-       protected $actionName = '';
+       protected $inDebugMode = false;
+       
+       /**
+        * object action
+        * @var wcf\data\IDatabaseObjectAction
+        */
+       protected $objectAction = null;
        
        /**
         * list of object ids
@@ -47,12 +59,6 @@ class AJAXProxyAction extends AbstractSecureAction {
         */
        protected $parameters = array();
        
-       /**
-        * object action
-        * @var wcf\data\IDatabaseObjectAction
-        */
-       protected $objectAction = null;
-       
        /**
         * results of the executed action
         * @var mixed
@@ -134,9 +140,11 @@ class AJAXProxyAction extends AbstractSecureAction {
                $this->executed();
                
                // send JSON-encoded response
-               header('Content-type: application/json');
-               echo JSON::encode($this->response);
-               exit;
+               if (!$this->inDebugMode) {
+                       header('Content-type: application/json');
+                       echo JSON::encode($this->response);
+                       exit;
+               }
        }
        
        /**
@@ -145,6 +153,10 @@ class AJAXProxyAction extends AbstractSecureAction {
         * @param       \Exception      $e
         */
        protected function throwException(\Exception $e) {
+               if ($this->inDebugMode) {
+                       throw $e;
+               }
+               
                if ($e instanceof IllegalLinkException) {
                        throw new AJAXException(WCF::getLanguage()->get('wcf.global.ajax.error.sessionExpired'), AJAXException::SESSION_EXPIRED);
                }
@@ -161,4 +173,74 @@ class AJAXProxyAction extends AbstractSecureAction {
                        throw new AJAXException($e->getMessage(), AJAXException::INTERNAL_ERROR, $e->getTraceAsString());
                }
        }
+       
+       /**
+        * Returns action response.
+        * 
+        * @return      mixed
+        */
+       public function getResponse() {
+               return $this->response;
+       }
+       
+       /**
+        * Enables debug mode.
+        */
+       public function enableDebugMode() {
+               $this->inDebugMode = true;
+       }
+       
+       /**
+        * Performs a debug call to AJAXProxyAction, allowing testing without relying on JavaScript.
+        * The $data-array should be build like within WCF.Action.Proxy, look below for an example:
+        * 
+        * $data = array(
+        *      'actionName' => 'foo',
+        *      'className' => 'wcf\foo\bar\FooBarAction',
+        *      'objectIDs' => array(1, 2, 3, 4, 5), // optional
+        *      'parameters' => array( // optional
+        *              'foo' => 'bar',
+        *              'data' => array(
+        *                      'baz' => 'foobar'
+        *              )
+        *      )
+        * )
+        * 
+        * @param       array           $data
+        * @param       string          $className
+        * @param       string          $actionName
+        * @return      wcf\action\AJAXProxyAction
+        */
+       public static function debugCall(array $data) {
+               // validate $data array
+               if (!isset($data['actionName'])) {
+                       throw new SystemException("Could not execute debug call, 'actionName' is missing.");
+               }
+               else if (!isset($data['className'])) {
+                       throw new SystemException("Could not execute debug call, 'className' is missing.");
+               }
+               
+               // save $_POST variables
+               $postVars = $_POST;
+               
+               // fake request
+               $_POST['actionName'] = $data['actionName'];
+               $_POST['className'] = $data['className'];
+               if (isset($data['objectIDs'])) {
+                       $_POST['objectIDs'] = $data['objectIDs'];
+               }
+               if (isset($data['parameters'])) {
+                       $_POST['parameters'] = $data['parameters'];
+               }
+               
+               // execute request
+               $actionObject = new AJAXProxyAction();
+               $actionObject->enableDebugMode();
+               $actionObject->__run();
+               
+               // restore $_POST variables
+               $_POST = $postVars;
+               
+               return $actionObject;
+       }
 }