Browse Source

Rename option `_ssl` to `_https` in Router::url().

Refs #16698.
ADmad 3 years ago
parent
commit
e7944b5d3c

+ 9 - 3
src/Routing/Router.php

@@ -401,7 +401,7 @@ class Router
      * - `_port` - Set the port if you need to create links on non-standard ports.
      * - `_full` - If true output of `Router::fullBaseUrl()` will be prepended to generated URLs.
      * - `#` - Allows you to set URL hash fragments.
-     * - `_ssl` - Set to true to convert the generated URL to https, or false to force http.
+     * - `_https` - Set to true to convert the generated URL to https, or false to force http.
      * - `_name` - Name of route. If you have setup named routes you can use this key
      *   to specify it.
      *
@@ -449,7 +449,13 @@ class Router
             }
 
             if (isset($url['_ssl'])) {
-                $url['_scheme'] = $url['_ssl'] === true ? 'https' : 'http';
+                deprecationWarning('`_ssl` option is deprecated. Use `_https` instead.');
+                $url['_https'] = $url['_ssl'];
+                unset($url['_ssl']);
+            }
+
+            if (isset($url['_https'])) {
+                $url['_scheme'] = $url['_https'] === true ? 'https' : 'http';
             }
 
             if (isset($url['_full']) && $url['_full'] === true) {
@@ -458,7 +464,7 @@ class Router
             if (isset($url['#'])) {
                 $frag = '#' . $url['#'];
             }
-            unset($url['_ssl'], $url['_full'], $url['#']);
+            unset($url['_https'], $url['_full'], $url['#']);
 
             $url = static::_applyUrlFilters($url);
 

+ 2 - 2
src/TestSuite/IntegrationTestTrait.php

@@ -589,7 +589,7 @@ trait IntegrationTestTrait
             'QUERY_STRING' => $query,
             'REQUEST_URI' => $url,
         ];
-        if (!empty($hostInfo['ssl'])) {
+        if (!empty($hostInfo['https'])) {
             $env['HTTPS'] = 'on';
         }
         if (isset($hostInfo['host'])) {
@@ -728,7 +728,7 @@ trait IntegrationTestTrait
             $hostData['host'] = $uri->getHost();
         }
         if ($uri->getScheme()) {
-            $hostData['ssl'] = $uri->getScheme() === 'https';
+            $hostData['https'] = $uri->getScheme() === 'https';
         }
 
         return [$path, $query, $hostData];

+ 1 - 1
tests/TestCase/Command/RoutesCommandTest.php

@@ -271,7 +271,7 @@ class RoutesCommandTest extends TestCase
      */
     public function testGenerateBoolParams(): void
     {
-        $this->exec('routes generate controller:Articles action:index _ssl:true _host:example.com');
+        $this->exec('routes generate controller:Articles action:index _https:true _host:example.com');
         $this->assertExitCode(Command::CODE_SUCCESS);
         $this->assertOutputContains('> https://example.com/app/articles');
     }

+ 59 - 0
tests/TestCase/Routing/RouterTest.php

@@ -1869,6 +1869,8 @@ class RouterTest extends TestCase
 
     /**
      * Test that the ssl option works.
+     *
+     * @deprecated
      */
     public function testGenerationWithSslOption(): void
     {
@@ -1896,6 +1898,8 @@ class RouterTest extends TestCase
 
     /**
      * Test ssl option when the current request is ssl.
+     *
+     * @deprecated
      */
     public function testGenerateWithSslInSsl(): void
     {
@@ -1923,6 +1927,61 @@ class RouterTest extends TestCase
     }
 
     /**
+     * Test that the https option works.
+     */
+    public function testGenerationWithHttpsOption(): void
+    {
+        Router::fullBaseUrl('http://app.test');
+        Router::createRouteBuilder('/')->connect('/{controller}/{action}/*');
+        $request = new ServerRequest([
+            'url' => '/images/index',
+            'params' => [
+                'plugin' => null, 'controller' => 'Images', 'action' => 'index',
+            ],
+            'environment' => ['HTTP_HOST' => 'localhost'],
+        ]);
+        Router::setRequest($request);
+
+        $result = Router::url([
+            '_https' => true,
+        ]);
+        $this->assertSame('https://app.test/Images/index', $result);
+
+        $result = Router::url([
+            '_https' => false,
+        ]);
+        $this->assertSame('http://app.test/Images/index', $result);
+    }
+
+    /**
+     * Test https option when the current request is https.
+     */
+    public function testGenerateWithHttpsInHttps(): void
+    {
+        Router::createRouteBuilder('/')->connect('/{controller}/{action}/*');
+        $request = new ServerRequest([
+            'url' => '/images/index',
+            'environment' => ['HTTP_HOST' => 'app.test', 'HTTPS' => 'on'],
+            'params' => [
+                'plugin' => null,
+                'controller' => 'Images',
+                'action' => 'index',
+            ],
+        ]);
+        Router::setRequest($request);
+
+        $result = Router::url([
+            '_https' => false,
+        ]);
+        $this->assertSame('http://app.test/Images/index', $result);
+
+        $result = Router::url([
+            '_https' => true,
+        ]);
+        $this->assertSame('https://app.test/Images/index', $result);
+    }
+
+    /**
      * test that prefix routes persist when they are in the current request.
      */
     public function testPrefixRoutePersistence(): void

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

@@ -618,7 +618,7 @@ class IntegrationTestTraitTest extends TestCase
             'controller' => 'Posts',
             'action' => 'hostData',
             '_host' => 'app.example.org',
-            '_ssl' => true,
+            '_https' => true,
         ]);
         $this->assertResponseOk();
         $this->assertResponseContains('"isSsl":true');

+ 1 - 1
tests/test_app/TestApp/Controller/PostsController.php

@@ -164,7 +164,7 @@ class PostsController extends AppController
     {
         $data = [
             'host' => $this->request->host(),
-            'isSsl' => $this->request->is('ssl'),
+            'isSsl' => $this->request->is('https'),
         ];
 
         return $this->getResponse()->withStringBody(json_encode($data));