Browse Source

ZF-9823 #resolve #comment fixes Zend_Rest_Route#assemble such that URLs routing to new|editAction() are supported. Also supports indexAction() routing with additional parameters.

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@23420 44c647ce-9c0f-0410-b52a-842ac1e357ba
wilmoore 15 years ago
parent
commit
449396cb28
2 changed files with 100 additions and 0 deletions
  1. 12 0
      library/Zend/Rest/Route.php
  2. 88 0
      tests/Zend/Rest/RouteTest.php

+ 12 - 0
library/Zend/Rest/Route.php

@@ -274,15 +274,27 @@ class Zend_Rest_Route extends Zend_Controller_Router_Route_Module
         $controller = $params[$this->_controllerKey];
         unset($params[$this->_controllerKey]);
 
+        // set $action if value given is 'new' or 'edit'
+        if (in_array($params[$this->_actionKey], array('new', 'edit'))) {
+            $action = $params[$this->_actionKey];
+        }
         unset($params[$this->_actionKey]);
 
         if (isset($params['index']) && $params['index']) {
             unset($params['index']);
             $url .= '/index';
+            if (isset($params['id'])) {
+                $url .= '/'.$params['id'];
+                unset($params['id']);
+            }
             foreach ($params as $key => $value) {
                 if ($encode) $value = urlencode($value);
                 $url .= '/' . $key . '/' . $value;
             }
+        } elseif (! empty($action) && isset($params['id'])) {
+            $url .= sprintf('/%s/%s', $params['id'], $action);
+        } elseif (! empty($action)) {
+            $url .= sprintf('/%s', $action);
         } elseif (isset($params['id'])) {
             $url .= '/' . $params['id'];
         }

+ 88 - 0
tests/Zend/Rest/RouteTest.php

@@ -582,6 +582,94 @@ class Zend_Rest_RouteTest extends PHPUnit_Framework_TestCase
         $this->assertEquals('mod/user/index/foo/bar is n!ice', $url);
     }
 
+    /**
+     * @group ZF-9823
+     */
+    public function test_assemble_edit_with_module_appends_action_after_id()
+    {
+        $route = new Zend_Rest_Route($this->_front, array(), array());
+        $params = array('module'=>'mod', 'controller'=>'users', 'action'=>'edit', 'id'=>1);
+        $url = $route->assemble($params);
+        $this->assertEquals('mod/users/1/edit', $url);
+    }
+
+    /**
+     * @group ZF-9823
+     */
+    public function test_assemble_edit_without_module_appends_action_after_id()
+    {
+        $route = new Zend_Rest_Route($this->_front, array(), array());
+        $params = array('controller'=>'users', 'action'=>'edit', 'id'=>1);
+        $url = $route->assemble($params);
+        $this->assertEquals('users/1/edit', $url);
+    }
+
+    /**
+     * @group ZF-9823
+     */
+    public function test_assemble_new_with_module_appends_action()
+    {
+        $route = new Zend_Rest_Route($this->_front, array(), array());
+        $params = array('module'=>'mod', 'controller'=>'users', 'action'=>'new');
+        $url = $route->assemble($params);
+        $this->assertEquals('mod/users/new', $url);
+    }
+
+    /**
+     * @group ZF-9823
+     */
+    public function test_assemble_new_without_module_appends_action()
+    {
+        $route = new Zend_Rest_Route($this->_front, array(), array());
+        $params = array('controller'=>'users', 'action'=>'new');
+        $url = $route->assemble($params);
+        $this->assertEquals('users/new', $url);
+    }
+
+    /**
+     * @group ZF-9823
+     */
+    public function test_assemble_random_action_with_module_removes_action()
+    {
+        $route = new Zend_Rest_Route($this->_front, array(), array());
+        $params = array('module'=>'mod', 'controller'=>'users', 'action'=>'newbar');
+        $url = $route->assemble($params);
+        $this->assertNotEquals('mod/users/newbar', $url);
+    }
+
+    /**
+     * @group ZF-9823
+     */
+    public function test_assemble_random_action_without_module_removes_action()
+    {
+        $route = new Zend_Rest_Route($this->_front, array(), array());
+        $params = array('controller'=>'users', 'action'=>'newbar');
+        $url = $route->assemble($params);
+        $this->assertNotEquals('users/newbar', $url);
+    }
+
+    /**
+     * @group ZF-9823
+     */
+    public function test_assemble_with_module_honors_index_parameter_with_resource_id_and_extra_parameters()
+    {
+        $route = new Zend_Rest_Route($this->_front, array(), array());
+        $params = array('module'=>'mod', 'controller'=>'users', 'id' => 1, 'extra'=>'parameter', 'another' => 'parameter', 'index' => true);
+        $url = $route->assemble($params, false, false);
+        $this->assertEquals('mod/users/index/1/extra/parameter/another/parameter', $url);
+    }
+
+    /**
+     * @group ZF-9823
+     */
+    public function test_assemble_without_module_honors_index_parameter_with_resource_id_and_extra_parameters()
+    {
+        $route = new Zend_Rest_Route($this->_front, array(), array());
+        $params = array('controller'=>'users', 'id' => 1, 'extra'=>'parameter', 'another' => 'parameter', 'index' => true);
+        $url = $route->assemble($params, false, false);
+        $this->assertEquals('users/index/1/extra/parameter/another/parameter', $url);
+    }
+
     private function _buildRequest($method, $uri)
     {
         $request = new Zend_Controller_Request_HttpTestCase();