Skip to content
This repository has been archived by the owner on Jan 8, 2020. It is now read-only.

Commit

Permalink
Forward #3778
Browse files Browse the repository at this point in the history
Merge branch 'weierophinney-hotfix/3711' into develop
  • Loading branch information
Ralph Schindler committed Feb 19, 2013
2 parents 86a58c0 + c27ffb3 commit 36ba5ae
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 3 deletions.
22 changes: 20 additions & 2 deletions library/Zend/Mvc/Router/Http/Part.php
Expand Up @@ -136,8 +136,11 @@ public function match(Request $request, $pathOffset = null)
$uri = $request->getUri();
$pathLength = strlen($uri->getPath());

if ($this->mayTerminate && $nextOffset === $pathLength && trim($uri->getQuery()) == "") {
return $match;
if ($this->mayTerminate && $nextOffset === $pathLength) {
$query = $uri->getQuery();
if ('' == trim($query) || !$this->hasQueryChild()) {
return $match;
}
}

foreach ($this->routes as $name => $route) {
Expand Down Expand Up @@ -200,4 +203,19 @@ public function getAssembledParams()
// don't have to return anything here.
return array();
}

/**
* Is one of the child routes a query route?
*
* @return bool
*/
protected function hasQueryChild()
{
foreach ($this->routes as $route) {
if ($route instanceof Query) {
return true;
}
}
return false;
}
}
89 changes: 88 additions & 1 deletion tests/ZendTest/Mvc/Router/Http/PartTest.php
Expand Up @@ -13,9 +13,10 @@
use ArrayObject;
use PHPUnit_Framework_TestCase as TestCase;
use Zend\Http\Request as Request;
use Zend\Stdlib\Request as BaseRequest;
use Zend\Mvc\Router\RoutePluginManager;
use Zend\Mvc\Router\Http\Part;
use Zend\Stdlib\Parameters;
use Zend\Stdlib\Request as BaseRequest;
use ZendTest\Mvc\Router\FactoryTester;

class PartTest extends TestCase
Expand Down Expand Up @@ -382,4 +383,90 @@ public function testFactoryShouldAcceptTraversableChildRoutes()
$route = Part::factory($options);
$this->assertInstanceOf('Zend\Mvc\Router\Http\Part', $route);
}

/**
* @group 3711
*/
public function testPartRouteMarkedAsMayTerminateCanMatchWhenQueryStringPresent()
{
$options = array(
'route' => array(
'type' => 'Zend\Mvc\Router\Http\Literal',
'options' => array(
'route' => '/resource',
'defaults' => array(
'controller' => 'ResourceController',
'action' => 'resource',
),
),
),
'route_plugins' => new RoutePluginManager(),
'may_terminate' => true,
'child_routes' => array(
'child' => array(
'type' => 'Zend\Mvc\Router\Http\Literal',
'options' => array(
'route' => '/child',
'defaults' => array(
'action' => 'child',
),
),
),
),
);

$route = Part::factory($options);
$request = new Request();
$request->setUri('http://example.com/resource?foo=bar');
$query = new Parameters(array('foo' => 'bar'));
$request->setQuery($query);
$query = $request->getQuery();

$match = $route->match($request);
$this->assertInstanceOf('Zend\Mvc\Router\RouteMatch', $match);
$this->assertEquals('resource', $match->getParam('action'));
}

/**
* @group 3711
*/
public function testPartRouteMarkedAsMayTerminateButWithQueryRouteChildWillMatchChildRoute()
{
$options = array(
'route' => array(
'type' => 'Zend\Mvc\Router\Http\Literal',
'options' => array(
'route' => '/resource',
'defaults' => array(
'controller' => 'ResourceController',
'action' => 'resource',
),
),
),
'route_plugins' => new RoutePluginManager(),
'may_terminate' => true,
'child_routes' => array(
'query' => array(
'type' => 'Zend\Mvc\Router\Http\Query',
'options' => array(
'defaults' => array(
'query' => 'string',
),
),
),
),
);

$route = Part::factory($options);
$request = new Request();
$request->setUri('http://example.com/resource?foo=bar');
$query = new Parameters(array('foo' => 'bar'));
$request->setQuery($query);
$query = $request->getQuery();

$match = $route->match($request);
$this->assertInstanceOf('Zend\Mvc\Router\RouteMatch', $match);
$this->assertEquals('string', $match->getParam('query'));
$this->assertEquals('bar', $match->getParam('foo'));
}
}

0 comments on commit 36ba5ae

Please sign in to comment.