Browse Source

Merge pull request #12580 from beporter/reverse-route-default-match-get

Make reverse routes match `[_method => GET]` by default.
Mark Story 7 years ago
parent
commit
d59dfbef89

+ 1 - 1
src/Routing/Route/Route.php

@@ -769,7 +769,7 @@ class Route
             $url['_method'] = $url['[method]'];
         }
         if (empty($url['_method'])) {
-            return false;
+            $url['_method'] = 'GET';
         }
         $methods = array_map('strtoupper', (array)$url['_method']);
         foreach ($methods as $value) {

+ 18 - 0
tests/TestCase/Routing/Route/RouteTest.php

@@ -1070,6 +1070,24 @@ class RouteTest extends TestCase
     }
 
     /**
+     * Test that match() matches explicit GET routes
+     *
+     * @return void
+     */
+    public function testMatchWithExplicitGet()
+    {
+        $route = new Route(
+            '/anything',
+            ['controller' => 'Articles', 'action' => 'foo', '_method' => 'GET']
+        );
+        $result = $route->match([
+            'controller' => 'Articles',
+            'action' => 'foo'
+        ]);
+        $this->assertEquals("/anything", $result);
+    }
+
+    /**
      * Test separartor.
      *
      * @return void

+ 3 - 3
tests/TestCase/TestSuite/IntegrationTestTraitTest.php

@@ -609,7 +609,7 @@ class IntegrationTestTraitTest extends IntegrationTestCase
      */
     public function testArrayUrls()
     {
-        $this->post(['controller' => 'Posts', 'action' => 'index']);
+        $this->post(['controller' => 'Posts', 'action' => 'index', '_method' => 'POST']);
         $this->assertEquals('value', $this->viewVariable('test'));
     }
 
@@ -932,10 +932,10 @@ class IntegrationTestTraitTest extends IntegrationTestCase
     public function testAssertRedirect()
     {
         $this->_response = new Response();
-        $this->_response = $this->_response->withHeader('Location', 'http://localhost/tasks/index');
+        $this->_response = $this->_response->withHeader('Location', 'http://localhost/get/tasks/index');
 
         $this->assertRedirect();
-        $this->assertRedirect('/tasks/index');
+        $this->assertRedirect('/get/tasks/index');
         $this->assertRedirect(['controller' => 'Tasks', 'action' => 'index']);
 
         $this->assertResponseEmpty();