Browse Source

Fix element cache key generation.

Backport fix from #14145 to 3.x
mscherer 6 years ago
parent
commit
3a223cb064

+ 4 - 3
src/View/View.php

@@ -1804,15 +1804,16 @@ class View implements EventDispatcherInterface
         $plugin = null;
         $plugin = null;
         list($plugin, $name) = $this->pluginSplit($name);
         list($plugin, $name) = $this->pluginSplit($name);
 
 
-        $underscored = null;
+        $pluginKey = null;
         if ($plugin) {
         if ($plugin) {
-            $underscored = Inflector::underscore($plugin);
+            $pluginKey = str_replace('/', '_', Inflector::underscore($plugin));
         }
         }
+        $elementKey = str_replace(['\\', '/'], '_', $name);
 
 
         $cache = $options['cache'];
         $cache = $options['cache'];
         unset($options['cache'], $options['callbacks'], $options['plugin']);
         unset($options['cache'], $options['callbacks'], $options['plugin']);
         $keys = array_merge(
         $keys = array_merge(
-            [$underscored, $name],
+            [$pluginKey, $elementKey],
             array_keys($options),
             array_keys($options),
             array_keys($data)
             array_keys($data)
         );
         );

+ 27 - 0
tests/TestCase/View/ViewTest.php

@@ -1073,6 +1073,33 @@ class ViewTest extends TestCase
     }
     }
 
 
     /**
     /**
+     * Test elementCache method with namespaces and subfolder
+     *
+     * @return void
+     */
+    public function testElementCacheSubfolder()
+    {
+        Cache::drop('test_view');
+        Cache::setConfig('test_view', [
+            'engine' => 'File',
+            'duration' => '+1 day',
+            'path' => CACHE . 'views/',
+            'prefix' => '',
+        ]);
+        Cache::clear('test_view');
+
+        $View = $this->PostsController->createView();
+        $View->setElementCache('test_view');
+
+        $result = $View->element('subfolder/test_element', [], ['cache' => true]);
+        $expected = 'this is the test element in subfolder';
+        $this->assertEquals($expected, trim($result));
+
+        $result = Cache::read('element__subfolder_test_element', 'test_view');
+        $this->assertEquals($expected, trim($result));
+    }
+
+    /**
      * Test element events
      * Test element events
      *
      *
      * @return void
      * @return void

+ 1 - 0
tests/test_app/TestApp/Template/Element/subfolder/test_element.php

@@ -0,0 +1 @@
+this is the test element in subfolder