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
81
81
*/
82
82
protected $ _modifiers = ['nl2br ' => '' ];
83
83
84
- /**
85
- * @var string
86
- */
87
- private const CACHE_KEY_PREFIX = "EMAIL_FILTER_ " ;
88
-
89
84
/**
90
85
* @var bool
91
86
*/
@@ -414,10 +409,6 @@ public function blockDirective($construction)
414
409
$ skipParams = ['class ' , 'id ' , 'output ' ];
415
410
$ blockParameters = $ this ->getParameters ($ construction [2 ]);
416
411
417
- if (isset ($ blockParameters ['cache_key ' ])) {
418
- $ blockParameters ['cache_key ' ] = self ::CACHE_KEY_PREFIX . $ blockParameters ['cache_key ' ];
419
- }
420
-
421
412
$ block = null ;
422
413
423
414
if (isset ($ blockParameters ['class ' ])) {
Original file line number Diff line number Diff line change @@ -26,6 +26,9 @@ class AbstractBlockTest extends \PHPUnit\Framework\TestCase
26
26
*/
27
27
protected $ _layout = null ;
28
28
29
+ /**
30
+ * @var array
31
+ */
29
32
protected static $ _mocks = [];
30
33
31
34
/**
@@ -573,7 +576,7 @@ public function testGetCacheKey()
573
576
$ this ->assertNotEquals ($ name , $ key );
574
577
575
578
$ block ->setCacheKey ('key ' );
576
- $ this ->assertEquals (AbstractBlock::CACHE_KEY_PREFIX . 'key ' , $ block ->getCacheKey ());
579
+ $ this ->assertEquals (AbstractBlock::CUSTOM_CACHE_KEY_PREFIX . 'key ' , $ block ->getCacheKey ());
577
580
}
578
581
579
582
/**
Original file line number Diff line number Diff line change 12
12
use Magento \Framework \Cache \LockGuardedCacheLoader ;
13
13
use Magento \Framework \Config \ConfigOptionsListConstants ;
14
14
use Magento \Framework \DataObject \IdentityInterface ;
15
+ use Magento \Framework \Exception \RuntimeException ;
15
16
16
17
/**
17
18
* Base class for all blocks.
@@ -41,6 +42,11 @@ abstract class AbstractBlock extends \Magento\Framework\DataObject implements Bl
41
42
*/
42
43
public const CACHE_KEY_PREFIX = 'BLOCK_ ' ;
43
44
45
+ /**
46
+ * Prefix for custom cache key of block
47
+ */
48
+ public const CUSTOM_CACHE_KEY_PREFIX = 'CUSTOM_BLOCK_ ' ;
49
+
44
50
/**
45
51
* @var \Magento\Framework\View\DesignInterface
46
52
*/
@@ -1038,11 +1044,16 @@ public function getCacheKeyInfo()
1038
1044
* Get Key for caching block content
1039
1045
*
1040
1046
* @return string
1047
+ * @throws RuntimeException
1041
1048
*/
1042
1049
public function getCacheKey ()
1043
1050
{
1044
1051
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 ' );
1046
1057
}
1047
1058
1048
1059
/**
Original file line number Diff line number Diff line change 15
15
use Magento \Framework \Config \View ;
16
16
use Magento \Framework \Escaper ;
17
17
use Magento \Framework \Event \ManagerInterface as EventManagerInterface ;
18
+ use Magento \Framework \Exception \RuntimeException ;
18
19
use Magento \Framework \ObjectManagerInterface ;
19
20
use Magento \Framework \Session \SessionManagerInterface ;
20
21
use Magento \Framework \Session \SidResolverInterface ;
@@ -239,7 +240,21 @@ public function testGetCacheKey()
239
240
{
240
241
$ cacheKey = 'testKey ' ;
241
242
$ 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 ();
243
258
}
244
259
245
260
/**
You can’t perform that action at this time.
0 commit comments