File tree Expand file tree Collapse file tree 4 files changed +32
-12
lines changed
app/code/Magento/Email/Model/Template
dev/tests/integration/testsuite/Magento/Framework/View/Element
lib/internal/Magento/Framework/View Expand file tree Collapse file tree 4 files changed +32
-12
lines changed Original file line number Diff line number Diff line change @@ -81,11 +81,6 @@ class Filter extends Template
8181 */
8282 protected $ _modifiers = ['nl2br ' => '' ];
8383
84- /**
85- * @var string
86- */
87- private const CACHE_KEY_PREFIX = "EMAIL_FILTER_ " ;
88-
8984 /**
9085 * @var bool
9186 */
@@ -414,10 +409,6 @@ public function blockDirective($construction)
414409 $ skipParams = ['class ' , 'id ' , 'output ' ];
415410 $ blockParameters = $ this ->getParameters ($ construction [2 ]);
416411
417- if (isset ($ blockParameters ['cache_key ' ])) {
418- $ blockParameters ['cache_key ' ] = self ::CACHE_KEY_PREFIX . $ blockParameters ['cache_key ' ];
419- }
420-
421412 $ block = null ;
422413
423414 if (isset ($ blockParameters ['class ' ])) {
Original file line number Diff line number Diff line change @@ -26,6 +26,9 @@ class AbstractBlockTest extends \PHPUnit\Framework\TestCase
2626 */
2727 protected $ _layout = null ;
2828
29+ /**
30+ * @var array
31+ */
2932 protected static $ _mocks = [];
3033
3134 /**
@@ -573,7 +576,7 @@ public function testGetCacheKey()
573576 $ this ->assertNotEquals ($ name , $ key );
574577
575578 $ block ->setCacheKey ('key ' );
576- $ this ->assertEquals (AbstractBlock::CACHE_KEY_PREFIX . 'key ' , $ block ->getCacheKey ());
579+ $ this ->assertEquals (AbstractBlock::CUSTOM_CACHE_KEY_PREFIX . 'key ' , $ block ->getCacheKey ());
577580 }
578581
579582 /**
Original file line number Diff line number Diff line change 1212use Magento \Framework \Cache \LockGuardedCacheLoader ;
1313use Magento \Framework \Config \ConfigOptionsListConstants ;
1414use Magento \Framework \DataObject \IdentityInterface ;
15+ use Magento \Framework \Exception \RuntimeException ;
1516
1617/**
1718 * Base class for all blocks.
@@ -41,6 +42,11 @@ abstract class AbstractBlock extends \Magento\Framework\DataObject implements Bl
4142 */
4243 public const CACHE_KEY_PREFIX = 'BLOCK_ ' ;
4344
45+ /**
46+ * Prefix for custom cache key of block
47+ */
48+ public const CUSTOM_CACHE_KEY_PREFIX = 'CUSTOM_BLOCK_ ' ;
49+
4450 /**
4551 * @var \Magento\Framework\View\DesignInterface
4652 */
@@ -1038,11 +1044,16 @@ public function getCacheKeyInfo()
10381044 * Get Key for caching block content
10391045 *
10401046 * @return string
1047+ * @throws RuntimeException
10411048 */
10421049 public function getCacheKey ()
10431050 {
10441051 if ($ this ->hasData ('cache_key ' )) {
1045- return static ::CACHE_KEY_PREFIX . $ this ->getData ('cache_key ' );
1052+ if (preg_match ('/[^a-z0-9\-\_]/i ' , $ this ->getData ('cache_key ' ))) {
1053+ throw new RuntimeException (__ ('Please enter cache key with only alphanumeric or hash string. ' ));
1054+ }
1055+
1056+ return static ::CUSTOM_CACHE_KEY_PREFIX . $ this ->getData ('cache_key ' );
10461057 }
10471058
10481059 /**
Original file line number Diff line number Diff line change 1515use Magento \Framework \Config \View ;
1616use Magento \Framework \Escaper ;
1717use Magento \Framework \Event \ManagerInterface as EventManagerInterface ;
18+ use Magento \Framework \Exception \RuntimeException ;
1819use Magento \Framework \ObjectManagerInterface ;
1920use Magento \Framework \Session \SessionManagerInterface ;
2021use Magento \Framework \Session \SidResolverInterface ;
@@ -239,7 +240,21 @@ public function testGetCacheKey()
239240 {
240241 $ cacheKey = 'testKey ' ;
241242 $ this ->block ->setData ('cache_key ' , $ cacheKey );
242- $ this ->assertEquals (AbstractBlock::CACHE_KEY_PREFIX . $ cacheKey , $ this ->block ->getCacheKey ());
243+ $ this ->assertEquals (AbstractBlock::CUSTOM_CACHE_KEY_PREFIX . $ cacheKey , $ this ->block ->getCacheKey ());
244+ }
245+
246+ /**
247+ * Test for invalid cacheKey name
248+ * @return void
249+ * @throws RuntimeException
250+ */
251+ public function testGetCacheKeyFail (): void
252+ {
253+ $ cacheKey = "test&''Key " ;
254+ $ this ->block ->setData ('cache_key ' , $ cacheKey );
255+ $ this ->expectException (RuntimeException::class);
256+ $ this ->expectExceptionMessage ((string )__ ('Please enter cache key with only alphanumeric or hash string. ' ));
257+ $ this ->block ->getCacheKey ();
243258 }
244259
245260 /**
You can’t perform that action at this time.
0 commit comments