Explorar el Código

ZF-7848: Properly match empty static routes in route chains

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@23185 44c647ce-9c0f-0410-b52a-842ac1e357ba
matthew hace 15 años
padre
commit
aa17c976af

+ 5 - 1
library/Zend/Controller/Router/Route/Chain.php

@@ -77,7 +77,11 @@ class Zend_Controller_Router_Route_Chain extends Zend_Controller_Router_Route_Ab
         $values  = array();
 
         foreach ($this->_routes as $key => $route) {
-            if ($key > 0 && $matchedPath !== null && $subPath !== '' && $subPath !== false) {
+            if ($key > 0 
+                && $matchedPath !== null 
+                && $subPath !== '' 
+                && $subPath !== false
+            ) {
                 $separator = substr($subPath, 0, strlen($this->_separators[$key]));
 
                 if ($separator !== $this->_separators[$key]) {

+ 4 - 1
library/Zend/Controller/Router/Route/Static.php

@@ -76,7 +76,10 @@ class Zend_Controller_Router_Route_Static extends Zend_Controller_Router_Route_A
     public function match($path, $partial = false)
     {
         if ($partial) {
-            if (substr($path, 0, strlen($this->_route)) === $this->_route) {
+            // if (substr($path, 0, strlen($this->_route)) === $this->_route) {
+            if ((empty($path) && empty($this->_route))
+                || (substr($path, 0, strlen($this->_route)) === $this->_route)
+            ) {
                 $this->setMatchedPath($this->_route);
                 return $this->_defaults;
             }

+ 117 - 0
tests/Zend/Controller/Router/Route/ChainTest.php

@@ -668,6 +668,123 @@ class Zend_Controller_Router_Route_ChainTest extends PHPUnit_Framework_TestCase
         $this->assertEquals('foo', $res['controller']);
         $this->assertEquals('bar', $res['action']);
     }
+
+    /**
+     * @group ZF-7848
+     */
+    public function testChainingWithEmptyStaticRoutesMatchesCorrectly()
+    {
+        $adminRoute = new Zend_Controller_Router_Route('admin', array(
+            'module'     => 'admin',
+            'controller' => 'index',
+            'action'     => 'index',
+        ));
+        $indexRoute = new Zend_Controller_Router_Route_Static('', array(
+            'module'     => 'admin',
+            'controller' => 'index',
+            'action'     => 'index',
+        ));
+        $loginRoute = new Zend_Controller_Router_Route('login', array(
+            'module'     => 'admin',
+            'controller' => 'login',
+            'action'     => 'index',
+        ));
+        $emptyRoute    = $adminRoute->chain($indexRoute);
+        $nonEmptyRoute = $adminRoute->chain($loginRoute);
+
+        $request = new Zend_Controller_Request_Http();
+        $request->setPathInfo('/admin');
+        $values = $emptyRoute->match($request);
+        $this->assertEquals(array(
+            'module'     => 'admin',
+            'controller' => 'index',
+            'action'     => 'index',
+        ), $values);
+
+        $request->setPathInfo('/admin/');
+        $values = $emptyRoute->match($request);
+        $this->assertEquals(array(
+            'module'     => 'admin',
+            'controller' => 'index',
+            'action'     => 'index',
+        ), $values);
+
+        $request->setPathInfo('/admin/login');
+        $values = $nonEmptyRoute->match($request);
+        $this->assertEquals(array(
+            'module'     => 'admin',
+            'controller' => 'login',
+            'action'     => 'index',
+        ), $values);
+    }
+
+    /**
+     * @group ZF-7848
+     */
+    public function testChainingWithConfiguredEmptyStaticRoutesMatchesCorrectly()
+    {
+        $routes = array(
+            'admin' => array(
+                'route' => 'admin',
+                'defaults' => array(
+                    'module'     => 'admin',
+                    'controller' => 'index',
+                    'action'     => 'index',
+                ),
+                'chains' => array(
+                    'index' => array(
+                        'type'  => 'Zend_Controller_Router_Route_Static',
+                        'route' => '',
+                        'defaults' => array(
+                            'module'     => 'admin',
+                            'controller' => 'index',
+                            'action'     => 'index',
+                        ),
+                    ),
+                    'login' => array(
+                        'route' => 'login',
+                        'defaults' => array(
+                            'module'     => 'admin',
+                            'controller' => 'login',
+                            'action'     => 'index',
+                        ),
+                    ),
+
+                ),
+            ),
+        );
+        $config = new Zend_Config($routes);
+        $rewrite = new Zend_Controller_Router_Rewrite();
+        $rewrite->addConfig($config);
+        $routes = $rewrite->getRoutes();
+        $indexRoute = $rewrite->getRoute('admin-index');
+        $loginRoute = $rewrite->getRoute('admin-login');
+
+        $request = new Zend_Controller_Request_Http();
+        $request->setPathInfo('/admin');
+        $values = $indexRoute->match($request);
+        $this->assertEquals(array(
+            'module'     => 'admin',
+            'controller' => 'index',
+            'action'     => 'index',
+        ), $values);
+
+        $request->setPathInfo('/admin/');
+        $values = $indexRoute->match($request);
+        $this->assertEquals(array(
+            'module'     => 'admin',
+            'controller' => 'index',
+            'action'     => 'index',
+        ), $values);
+
+        $request->setPathInfo('/admin/login');
+        $values = $loginRoute->match($request);
+        $this->assertEquals(array(
+            'module'     => 'admin',
+            'controller' => 'login',
+            'action'     => 'index',
+        ), $values);
+    }
     
     protected function _getRouter()
     {

+ 2 - 3
tests/Zend/Controller/Router/Route/StaticTest.php

@@ -20,12 +20,11 @@
  * @version    $Id$
  */
 
+require_once dirname(__FILE__) . '/../../../../TestHelper.php';
+
 /** Zend_Controller_Router_Route */
 require_once 'Zend/Controller/Router/Route/Static.php';
 
-/** PHPUnit test case */
-require_once 'PHPUnit/Framework/TestCase.php';
-
 /**
  * @category   Zend
  * @package    Zend_Controller