Browse Source

Merge branch '3.next' into 4.x

Mark Story 7 years ago
parent
commit
f0b2ff9b45

+ 7 - 0
src/Core/Plugin.php

@@ -337,9 +337,16 @@ class Plugin
      * @param string|null $name name of the plugin, if null will operate on all
      *   plugins having enabled the loading of routes files.
      * @return bool
+     * @deprecated 3.6.5 This method is no longer needed when using HttpApplicationInterface based applications.
+     *   This method will be removed in 4.0.0
      */
     public static function routes($name = null)
     {
+        deprecationWarning(
+            'You no longer need to call `Plugin::routes()` after upgrading to use Http\Server. ' .
+            'See https://book.cakephp.org/3.0/en/development/application.html#adding-the-new-http-stack-to-an-existing-application ' .
+            'for upgrade information.'
+        );
         if ($name === null) {
             foreach (static::loaded() as $p) {
                 static::routes($p);

+ 14 - 0
src/Http/Response.php

@@ -1365,6 +1365,10 @@ class Response implements ResponseInterface
      */
     public function sharable($public = null, $time = null)
     {
+        deprecationWarning(
+            'Response::sharable() is deprecated. ' .
+            'Use withSharable() instead.'
+        );
         if ($public === null) {
             $public = array_key_exists('public', $this->_cacheDirectives);
             $private = array_key_exists('private', $this->_cacheDirectives);
@@ -1422,11 +1426,16 @@ class Response implements ResponseInterface
      * a good candidate to be fetched from a shared cache (like in a proxy server).
      * If called with no parameters, this function will return the current max-age value if any
      *
+     * @deprecated 3.6.5 Use withSharedMaxAge() instead.
      * @param int|null $seconds if null, the method will return the current s-maxage value
      * @return int|null
      */
     public function sharedMaxAge($seconds = null)
     {
+        deprecationWarning(
+            'Response::sharedMaxAge() is deprecated. ' .
+            'Use withSharedMaxAge() instead.'
+        );
         if ($seconds !== null) {
             $this->_cacheDirectives['s-maxage'] = $seconds;
             $this->_setCacheControl();
@@ -1462,11 +1471,16 @@ class Response implements ResponseInterface
      * a good candidate to be fetched from the local (client) cache.
      * If called with no parameters, this function will return the current max-age value if any
      *
+     * @deprecated 3.6.5 Use withMaxAge() instead.
      * @param int|null $seconds if null, the method will return the current max-age value
      * @return int|null
      */
     public function maxAge($seconds = null)
     {
+        deprecationWarning(
+            'Response::maxAge() is deprecated. ' .
+            'Use withMaxAge() instead.'
+        );
         if ($seconds !== null) {
             $this->_cacheDirectives['max-age'] = $seconds;
             $this->_setCacheControl();

+ 12 - 1
src/View/Helper.php

@@ -176,7 +176,7 @@ class Helper implements EventListenerInterface
      */
     protected function _confirm($message, $okCode, $cancelCode = '', $options = [])
     {
-        $message = str_replace('\\\n', '\n', json_encode($message));
+        $message = $this->_cleanConfirmMessage($message);
         $confirm = "if (confirm({$message})) { {$okCode} } {$cancelCode}";
         // We cannot change the key here in 3.x, but the behavior is inverted in this case
         $escape = isset($options['escape']) && $options['escape'] === false;
@@ -189,6 +189,17 @@ class Helper implements EventListenerInterface
     }
 
     /**
+     * Returns a string read to be used in confirm()
+     *
+     * @param string $message The message to clean
+     * @return mixed
+     */
+    protected function _cleanConfirmMessage($message)
+    {
+        return str_replace('\\\n', '\n', json_encode($message));
+    }
+
+    /**
      * Adds the given class to the element options
      *
      * @param array $options Array options/attributes to add a class to

+ 10 - 3
src/View/Helper/FormHelper.php

@@ -162,6 +162,8 @@ class FormHelper extends Helper
             'textarea' => '<textarea name="{{name}}"{{attrs}}>{{value}}</textarea>',
             // Container for submit buttons.
             'submitContainer' => '<div class="submit">{{content}}</div>',
+            //Confirm javascript template for postLink()
+            'confirmJs' => '{{confirm}}',
         ]
     ];
 
@@ -1834,11 +1836,16 @@ class FormHelper extends Helper
         $url = '#';
         $onClick = 'document.' . $formName . '.submit();';
         if ($confirmMessage) {
-            $options['onclick'] = $this->_confirm($confirmMessage, $onClick, '', $options);
+            $confirm = $this->_confirm($confirmMessage, $onClick, '', $options);
         } else {
-            $options['onclick'] = $onClick . ' ';
+            $confirm = $onClick . ' ';
         }
-        $options['onclick'] .= 'event.returnValue = false; return false;';
+        $confirm .= 'event.returnValue = false; return false;';
+        $options['onclick'] = $this->templater()->format('confirmJs', [
+            'confirmMessage' => $this->_cleanConfirmMessage($confirmMessage),
+            'formName' => $formName,
+            'confirm' => $confirm
+        ]);
 
         $out .= $this->Html->link($title, $url, $options);
 

+ 8 - 4
src/View/Helper/HtmlHelper.php

@@ -81,7 +81,8 @@ class HtmlHelper extends Helper
             'javascriptblock' => '<script{{attrs}}>{{content}}</script>',
             'javascriptstart' => '<script>',
             'javascriptlink' => '<script src="{{url}}"{{attrs}}></script>',
-            'javascriptend' => '</script>'
+            'javascriptend' => '</script>',
+            'confirmJs' => '{{confirm}}'
         ]
     ];
 
@@ -342,17 +343,20 @@ class HtmlHelper extends Helper
             $title = htmlentities($title, ENT_QUOTES, $escapeTitle);
         }
 
+        $templater = $this->templater();
         $confirmMessage = null;
         if (isset($options['confirm'])) {
             $confirmMessage = $options['confirm'];
             unset($options['confirm']);
         }
         if ($confirmMessage) {
-            $options['onclick'] = $this->_confirm($confirmMessage, 'return true;', 'return false;', $options);
+            $confirm = $this->_confirm($confirmMessage, 'return true;', 'return false;', $options);
+            $options['onclick'] = $templater->format('confirmJs', [
+                'confirmMessage' => $this->_cleanConfirmMessage($confirmMessage),
+                'confirm' => $confirm
+            ]);
         }
 
-        $templater = $this->templater();
-
         return $templater->format('link', [
             'url' => $url,
             'attrs' => $templater->formatAttributes($options),

+ 41 - 32
tests/TestCase/Core/PluginTest.php

@@ -177,16 +177,19 @@ class PluginTest extends TestCase
     /**
      * Tests loading a plugin with bootstrap file and routes file
      *
+     * @deprecated
      * @return void
      */
     public function testLoadSingleWithBootstrapAndRoutes()
     {
-        Plugin::load('TestPlugin', ['bootstrap' => true, 'routes' => true]);
-        $this->assertTrue(Plugin::loaded('TestPlugin'));
-        $this->assertEquals('loaded plugin bootstrap', Configure::read('PluginTest.test_plugin.bootstrap'));
-
-        Plugin::routes();
-        $this->assertEquals('loaded plugin routes', Configure::read('PluginTest.test_plugin.routes'));
+        $this->deprecated(function () {
+            Plugin::load('TestPlugin', ['bootstrap' => true, 'routes' => true]);
+            $this->assertTrue(Plugin::loaded('TestPlugin'));
+            $this->assertEquals('loaded plugin bootstrap', Configure::read('PluginTest.test_plugin.bootstrap'));
+
+            Plugin::routes();
+            $this->assertEquals('loaded plugin routes', Configure::read('PluginTest.test_plugin.routes'));
+        });
     }
 
     /**
@@ -247,16 +250,19 @@ class PluginTest extends TestCase
     /**
      * Test ignoring missing bootstrap/routes file
      *
+     * @deprecated
      * @return void
      */
     public function testIgnoreMissingFiles()
     {
-        Plugin::loadAll([[
-            'bootstrap' => true,
-            'routes' => true,
-            'ignoreMissing' => true
-        ]]);
-        $this->assertTrue(Plugin::routes());
+        $this->deprecated(function () {
+            Plugin::loadAll([[
+                'bootstrap' => true,
+                'routes' => true,
+                'ignoreMissing' => true
+            ]]);
+            $this->assertTrue(Plugin::routes());
+        });
     }
 
     /**
@@ -390,29 +396,32 @@ class PluginTest extends TestCase
      * Tests that Plugin::loadAll() will load all plugins in the configured folder wit defaults
      * and overrides for a plugin
      *
+     * @deprecated
      * @return void
      */
     public function testLoadAllWithDefaultsAndOverride()
     {
-        Plugin::loadAll([
-            ['bootstrap' => true, 'ignoreMissing' => true],
-            'TestPlugin' => ['routes' => true],
-            'TestPluginFour' => ['bootstrap' => true, 'classBase' => '']
-        ]);
-        Plugin::routes();
-
-        $expected = [
-            'Company', 'ParentPlugin', 'PluginJs', 'TestPlugin',
-            'TestPluginFour', 'TestPluginTwo', 'TestTheme'
-        ];
-        $this->assertEquals($expected, Plugin::loaded());
-        $this->assertEquals('loaded js plugin bootstrap', Configure::read('PluginTest.js_plugin.bootstrap'));
-        $this->assertEquals('loaded plugin routes', Configure::read('PluginTest.test_plugin.routes'));
-        $this->assertNull(Configure::read('PluginTest.test_plugin.bootstrap'));
-        $this->assertEquals('loaded plugin two bootstrap', Configure::read('PluginTest.test_plugin_two.bootstrap'));
-        $this->assertEquals('loaded plugin four bootstrap', Configure::read('PluginTest.test_plugin_four.bootstrap'));
-
-        // TestPluginThree won't get loaded by loadAll() since it's in a sub directory.
-        $this->assertNull(Configure::read('PluginTest.test_plugin_three.bootstrap'));
+        $this->deprecated(function () {
+            Plugin::loadAll([
+                ['bootstrap' => true, 'ignoreMissing' => true],
+                'TestPlugin' => ['routes' => true],
+                'TestPluginFour' => ['bootstrap' => true, 'classBase' => '']
+            ]);
+            Plugin::routes();
+
+            $expected = [
+                'Company', 'ParentPlugin', 'PluginJs', 'TestPlugin',
+                'TestPluginFour', 'TestPluginTwo', 'TestTheme'
+            ];
+            $this->assertEquals($expected, Plugin::loaded());
+            $this->assertEquals('loaded js plugin bootstrap', Configure::read('PluginTest.js_plugin.bootstrap'));
+            $this->assertEquals('loaded plugin routes', Configure::read('PluginTest.test_plugin.routes'));
+            $this->assertNull(Configure::read('PluginTest.test_plugin.bootstrap'));
+            $this->assertEquals('loaded plugin two bootstrap', Configure::read('PluginTest.test_plugin_two.bootstrap'));
+            $this->assertEquals('loaded plugin four bootstrap', Configure::read('PluginTest.test_plugin_four.bootstrap'));
+
+            // TestPluginThree won't get loaded by loadAll() since it's in a sub directory.
+            $this->assertNull(Configure::read('PluginTest.test_plugin_three.bootstrap'));
+        });
     }
 }

+ 42 - 33
tests/TestCase/Http/ResponseTest.php

@@ -1074,28 +1074,31 @@ class ResponseTest extends TestCase
     /**
      * Tests setting of public/private Cache-Control directives
      *
+     * @deprecated
      * @return void
      */
     public function testSharable()
     {
-        $response = new Response();
-        $this->assertNull($response->sharable());
-        $response->sharable(true);
-        $this->assertTrue($response->sharable());
-        $this->assertEquals('public', $response->getHeaderLine('Cache-Control'));
+        $this->deprecated(function () {
+            $response = new Response();
+            $this->assertNull($response->sharable());
+            $response->sharable(true);
+            $this->assertTrue($response->sharable());
+            $this->assertEquals('public', $response->getHeaderLine('Cache-Control'));
 
-        $response = new Response();
-        $response->sharable(false);
-        $this->assertFalse($response->sharable());
-        $this->assertEquals('private', $response->getHeaderLine('Cache-Control'));
+            $response = new Response();
+            $response->sharable(false);
+            $this->assertFalse($response->sharable());
+            $this->assertEquals('private', $response->getHeaderLine('Cache-Control'));
 
-        $response = new Response();
-        $response->sharable(true, 3600);
-        $this->assertEquals('public, max-age=3600', $response->getHeaderLine('Cache-Control'));
+            $response = new Response();
+            $response->sharable(true, 3600);
+            $this->assertEquals('public, max-age=3600', $response->getHeaderLine('Cache-Control'));
 
-        $response = new Response();
-        $response->sharable(false, 3600);
-        $this->assertEquals('private, max-age=3600', $response->getHeaderLine('Cache-Control'));
+            $response = new Response();
+            $response->sharable(false, 3600);
+            $this->assertEquals('private, max-age=3600', $response->getHeaderLine('Cache-Control'));
+        });
     }
 
     /**
@@ -1123,20 +1126,23 @@ class ResponseTest extends TestCase
     /**
      * Tests setting of max-age Cache-Control directive
      *
+     * @deprecated
      * @return void
      */
     public function testMaxAge()
     {
-        $response = new Response();
-        $this->assertNull($response->maxAge());
-        $response->maxAge(3600);
-        $this->assertEquals(3600, $response->maxAge());
-        $this->assertEquals('max-age=3600', $response->getHeaderLine('Cache-Control'));
+        $this->deprecated(function () {
+            $response = new Response();
+            $this->assertNull($response->maxAge());
+            $response->maxAge(3600);
+            $this->assertEquals(3600, $response->maxAge());
+            $this->assertEquals('max-age=3600', $response->getHeaderLine('Cache-Control'));
 
-        $response = new Response();
-        $response->maxAge(3600);
-        $response->sharable(false);
-        $this->assertEquals('max-age=3600, private', $response->getHeaderLine('Cache-Control'));
+            $response = new Response();
+            $response->maxAge(3600);
+            $response->sharable(false);
+            $this->assertEquals('max-age=3600, private', $response->getHeaderLine('Cache-Control'));
+        });
     }
 
     /**
@@ -1160,20 +1166,23 @@ class ResponseTest extends TestCase
     /**
      * Tests setting of s-maxage Cache-Control directive
      *
+     * @deprecated
      * @return void
      */
     public function testSharedMaxAge()
     {
-        $response = new Response();
-        $this->assertNull($response->maxAge());
-        $response->sharedMaxAge(3600);
-        $this->assertEquals(3600, $response->sharedMaxAge());
-        $this->assertEquals('s-maxage=3600', $response->getHeaderLine('Cache-Control'));
+        $this->deprecated(function () {
+            $response = new Response();
+            $this->assertNull($response->maxAge());
+            $response->sharedMaxAge(3600);
+            $this->assertEquals(3600, $response->sharedMaxAge());
+            $this->assertEquals('s-maxage=3600', $response->getHeaderLine('Cache-Control'));
 
-        $response = new Response();
-        $response->sharedMaxAge(3600);
-        $response->sharable(true);
-        $this->assertEquals('s-maxage=3600, public', $response->getHeaderLine('Cache-Control'));
+            $response = new Response();
+            $response->sharedMaxAge(3600);
+            $response->sharable(true);
+            $this->assertEquals('s-maxage=3600, public', $response->getHeaderLine('Cache-Control'));
+        });
     }
 
     /**

+ 20 - 0
tests/TestCase/View/Helper/FormHelperTest.php

@@ -7654,6 +7654,26 @@ class FormHelperTest extends TestCase
             '/a'
         ];
         $this->assertHtml($expected, $result);
+
+        $this->Form->setTemplates(['confirmJs' => 'if (confirm({{confirmMessage}})) { $(\'form[name="{{formName}}"]\').submit();};']);
+        $result = $this->Form->postLink(
+            'Delete',
+            '/posts/delete/1',
+            ['escape' => false, 'confirm' => 'Confirm this deletion?']
+        );
+        $expected = [
+            'form' => [
+                'method' => 'post', 'action' => '/posts/delete/1',
+                'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
+            ],
+            'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
+            '/form',
+            'a' => ['href' => '#', 'onclick' => 'preg:/if \(confirm\("Confirm this deletion\?"\)\) \{ \$\(\'form\[name="post_\w+"\]\'\)\.submit\(\);\};/'],
+            'Delete',
+            '/a'
+        ];
+
+        $this->assertHtml($expected, $result);
     }
 
     /**

+ 11 - 0
tests/TestCase/View/Helper/HtmlHelperTest.php

@@ -152,6 +152,17 @@ class HtmlHelperTest extends TestCase
         ];
         $this->assertHtml($expected, $result);
 
+        $this->Html->setTemplates(['confirmJs' => 'if (confirm({{confirmMessage}})) { window.location="/";};']);
+        $result = $this->Html->link('Home', '/home', ['confirm' => 'Are you sure you want to do this?']);
+        $expected = [
+            'a' => ['href' => '/home', 'onclick' => 'preg:/if \(confirm\(&quot;Are you sure you want to do this\?&quot;\)\) \{ window\.location=&quot;\/&quot;;\};/'],
+            'Home',
+            '/a'
+        ];
+
+        $this->assertHtml($expected, $result);
+        $this->Html->setTemplates(['confirmJs' => '{{confirm}}']);
+
         $result = $this->Html->link('Home', '/home', ['escape' => false, 'confirm' => 'Confirm\'s "nightmares"']);
         $expected = [
             'a' => ['href' => '/home', 'onclick' => 'if (confirm(&quot;Confirm&#039;s \&quot;nightmares\&quot;&quot;)) { return true; } return false;'],