From 24e3948120b666e2af2684c84434fab2b39632c1 Mon Sep 17 00:00:00 2001 From: Erwane Breton Date: Tue, 9 Jul 2019 18:56:25 +0200 Subject: [PATCH] force UrlComparerTrait::_normalize to use GET method --- src/View/Helper/UrlComparerTrait.php | 3 ++- .../View/Helper/UrlComparerTraitTest.php | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/View/Helper/UrlComparerTrait.php b/src/View/Helper/UrlComparerTrait.php index 22786e9..c749f77 100644 --- a/src/View/Helper/UrlComparerTrait.php +++ b/src/View/Helper/UrlComparerTrait.php @@ -120,7 +120,8 @@ protected function _normalize($url, array $parts = []) { if (!$this->_matchRelative($url)) { return null; } - $url = Router::parseRequest(new ServerRequest($this->_removeRelative($url))); + $request = new ServerRequest($this->_removeRelative($url)); + $url = Router::parseRequest($request->withMethod('GET')); $arr = []; foreach ($this->_parts as $part) { if (!isset($url[$part]) || (isset($parts[$part]) && !$parts[$part])) { diff --git a/tests/TestCase/View/Helper/UrlComparerTraitTest.php b/tests/TestCase/View/Helper/UrlComparerTraitTest.php index 7aa0bc8..3797a16 100644 --- a/tests/TestCase/View/Helper/UrlComparerTraitTest.php +++ b/tests/TestCase/View/Helper/UrlComparerTraitTest.php @@ -295,4 +295,20 @@ public function testCompareCustom() { $this->_testCompare($tests, [], ['action' => false, 'pass' => false]); } + public function testGetOnlyRoutes() { + // current request is POST + $_SERVER['REQUEST_METHOD'] = 'POST'; + + // cleanup router and add an unique get only route + Router::resetRoutes(); + $builder = new RouteBuilder(Router::getRouteCollection(), '/'); + $builder->get('/get_only', ['controller' => 'tests_apps', 'action' => 'get_method']); + // force route collection to avoid loading config/routes.php + Router::setRouteCollection(Router::getRouteCollection()); + + $url = $this->trait->normalize('/get_only'); + + $this->assertSame('/tests_apps/get_method', $url); + } + };