فهرست منبع

ZF-11885: Cache action helper does not encode cache key on remove

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@24853 44c647ce-9c0f-0410-b52a-842ac1e357ba
adamlundrigan 13 سال پیش
والد
کامیت
6b766a66e6
2فایلهای تغییر یافته به همراه78 افزوده شده و 4 حذف شده
  1. 12 2
      library/Zend/Controller/Action/Helper/Cache.php
  2. 66 2
      tests/Zend/Controller/Action/Helper/CacheTest.php

+ 12 - 2
library/Zend/Controller/Action/Helper/Cache.php

@@ -130,16 +130,26 @@ class Zend_Controller_Action_Helper_Cache
     public function removePage($relativeUrl, $recursive = false)
     {
         $cache = $this->getCache(Zend_Cache_Manager::PAGECACHE);
+        $encodedCacheId = $this->_encodeCacheId($relativeUrl);
+
         if ($recursive) {
             $backend = $cache->getBackend();
             if (($backend instanceof Zend_Cache_Backend)
                 && method_exists($backend, 'removeRecursively')
             ) {
-                return $backend->removeRecursively($relativeUrl);
+                $result = $backend->removeRecursively($encodedCacheId);
+                if (is_null($result) ) {
+                    $result = $backend->removeRecursively($relativeUrl);
+                }
+                return $result;
             }
         }
 
-        return $cache->remove($relativeUrl);
+        $result = $cache->remove($encodedCacheId);
+        if (is_null($result) ) {
+            $result = $cache->remove($relativeUrl);
+        }
+        return $result;
     }
 
     /**

+ 66 - 2
tests/Zend/Controller/Action/Helper/CacheTest.php

@@ -150,6 +150,55 @@ class Zend_Controller_Action_Helper_CacheTest extends PHPUnit_Framework_TestCase
         $this->assertNotEquals('verified', $cache->res);
     }
 
+    /**
+     * @group ZF-11885
+     * @dataProvider dataprovider_testEncodedCacheIdsAreUsedConsistently
+     */
+    public function testEncodedCacheIdsAreUsedConsistently($recursive)
+    {
+        $helper = new Zend_Controller_Action_Helper_Cache();
+        $cache = new Mock_Zend_Cache_Page_TestingEncodedCacheId();
+        $helper->setCache('page', $cache);
+        $helper->direct(array('baz'));
+        $helper->preDispatch();
+        $uriKey = bin2hex($this->request->getRequestUri());
+
+        // Ensure that start method encodes key properly
+        $this->assertTrue(isset($cache->items[$uriKey]));
+
+        // Ensure that remove method encodes key properly
+        $helper->removePage($this->request->getRequestUri(), $recursive);
+        $this->assertFalse(isset($cache->items[$uriKey]));
+    }
+
+    /**
+     * @group ZF-11885
+     * @dataProvider dataprovider_testEncodedCacheIdsAreUsedConsistently
+     */
+    public function testRemovePageAcceptsPreEncodedCacheIds($recursive)
+    {
+        $helper = new Zend_Controller_Action_Helper_Cache();
+        $cache = new Mock_Zend_Cache_Page_TestingEncodedCacheId();
+        $helper->setCache('page', $cache);
+        $helper->direct(array('baz'));
+        $helper->preDispatch();
+        $uriKey = bin2hex($this->request->getRequestUri());
+
+        // Ensure that remove method will accept pre-encoded key
+        $helper->removePage($uriKey, $recursive);
+        $this->assertFalse(isset($cache->items[$uriKey]));
+    }
+
+    /**
+     * Data provider for testEncodedCacheIdsAreUsedConsistently
+     * @see ZF-11885
+     */
+    public function dataprovider_testEncodedCacheIdsAreUsedConsistently()
+    {
+        return array(array(true),array(false));
+    }
+
+
     /**public function testPostDispatchEndsOutputBufferPageCaching()
     {
         $helper = new Zend_Controller_Action_Helper_Cache;
@@ -178,14 +227,14 @@ class Mock_Zend_Cache_Page_1 extends Zend_Cache_Core
 {
     public function remove($id)
     {
-        if ($id == '/foo') {return 'verified';}
+        if ($id == bin2hex('/foo')) {return 'verified';}
     }
 }
 class Mock_Zend_Cache_Page_2 extends Zend_Cache_Backend
 {
     public function removeRecursively($id)
     {
-        if ($id == '/foo') {return 'verified';}
+        if ($id == bin2hex('/foo')) {return 'verified';}
     }
 }
 class Mock_Zend_Cache_Page_3 extends Zend_Cache_Core
@@ -221,6 +270,21 @@ class Mock_Zend_Cache_Page_6 extends Zend_Cache_Core
     }
 }
 
+class Mock_Zend_Cache_Page_TestingEncodedCacheId extends Zend_Cache_Core
+{
+    public $items;
+
+    public function start($id, array $tags = array())
+    {
+        $this->items[$id] = $tags;
+    }
+
+    public function remove($id)
+    {
+        unset($this->items[$id]);
+    }
+}
+
 /**class Mock_Zend_Cache_Page_5 extends Zend_Cache_Core
 {
     public $res;