Skip to content

Commit 4b60fad

Browse files
schmenglerCtucker9233
authored andcommitted
Cherry Pick issue 18641
1 parent c52ee4a commit 4b60fad

File tree

4 files changed

+193
-6
lines changed

4 files changed

+193
-6
lines changed

setup/src/Magento/Setup/Model/ConfigOptionsList/Cache.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,14 @@ class Cache implements ConfigOptionsListInterface
2727
const INPUT_KEY_CACHE_BACKEND_REDIS_DATABASE = 'cache-backend-redis-db';
2828
const INPUT_KEY_CACHE_BACKEND_REDIS_PORT = 'cache-backend-redis-port';
2929
const INPUT_KEY_CACHE_BACKEND_REDIS_PASSWORD = 'cache-backend-redis-password';
30+
const INPUT_KEY_CACHE_ID_PREFIX = 'cache-id-prefix';
3031

3132
const CONFIG_PATH_CACHE_BACKEND = 'cache/frontend/default/backend';
3233
const CONFIG_PATH_CACHE_BACKEND_SERVER = 'cache/frontend/default/backend_options/server';
3334
const CONFIG_PATH_CACHE_BACKEND_DATABASE = 'cache/frontend/default/backend_options/database';
3435
const CONFIG_PATH_CACHE_BACKEND_PORT = 'cache/frontend/default/backend_options/port';
3536
const CONFIG_PATH_CACHE_BACKEND_PASSWORD = 'cache/frontend/default/backend_options/password';
37+
const CONFIG_PATH_CACHE_ID_PREFIX = 'cache/frontend/default/id_prefix';
3638

3739
/**
3840
* @var array
@@ -112,6 +114,12 @@ public function getOptions()
112114
TextConfigOption::FRONTEND_WIZARD_TEXT,
113115
self::CONFIG_PATH_CACHE_BACKEND_PASSWORD,
114116
'Redis server password'
117+
),
118+
new TextConfigOption(
119+
self::INPUT_KEY_CACHE_ID_PREFIX,
120+
TextConfigOption::FRONTEND_WIZARD_TEXT,
121+
self::CONFIG_PATH_CACHE_ID_PREFIX,
122+
'ID prefix for cache keys'
115123
)
116124
];
117125
}
@@ -122,6 +130,11 @@ public function getOptions()
122130
public function createConfig(array $options, DeploymentConfig $deploymentConfig)
123131
{
124132
$configData = new ConfigData(ConfigFilePool::APP_ENV);
133+
if (isset($options[self::INPUT_KEY_CACHE_ID_PREFIX])) {
134+
$configData->set(self::CONFIG_PATH_CACHE_ID_PREFIX, $options[self::INPUT_KEY_CACHE_ID_PREFIX]);
135+
} else {
136+
$configData->set(self::CONFIG_PATH_CACHE_ID_PREFIX, $this->generateCachePrefix());
137+
}
125138

126139
if (isset($options[self::INPUT_KEY_CACHE_BACKEND])) {
127140
if ($options[self::INPUT_KEY_CACHE_BACKEND] == self::INPUT_VALUE_CACHE_REDIS) {
@@ -241,4 +254,14 @@ private function getDefaultConfigValue($inputKey)
241254
return '';
242255
}
243256
}
257+
258+
/**
259+
* Generate default cache ID prefix based on installation dir
260+
*
261+
* @return string
262+
*/
263+
private function generateCachePrefix(): string
264+
{
265+
return substr(\md5(dirname(__DIR__, 6)), 0, 3) . '_';
266+
}
244267
}

setup/src/Magento/Setup/Model/ConfigOptionsList/PageCache.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,15 @@ class PageCache implements ConfigOptionsListInterface
2828
const INPUT_KEY_PAGE_CACHE_BACKEND_REDIS_PORT = 'page-cache-redis-port';
2929
const INPUT_KEY_PAGE_CACHE_BACKEND_REDIS_COMPRESS_DATA = 'page-cache-redis-compress-data';
3030
const INPUT_KEY_PAGE_CACHE_BACKEND_REDIS_PASSWORD = 'page-cache-redis-password';
31+
const INPUT_KEY_PAGE_CACHE_ID_PREFIX = 'page-cache-id-prefix';
3132

3233
const CONFIG_PATH_PAGE_CACHE_BACKEND = 'cache/frontend/page_cache/backend';
3334
const CONFIG_PATH_PAGE_CACHE_BACKEND_SERVER = 'cache/frontend/page_cache/backend_options/server';
3435
const CONFIG_PATH_PAGE_CACHE_BACKEND_DATABASE = 'cache/frontend/page_cache/backend_options/database';
3536
const CONFIG_PATH_PAGE_CACHE_BACKEND_PORT = 'cache/frontend/page_cache/backend_options/port';
3637
const CONFIG_PATH_PAGE_CACHE_BACKEND_COMPRESS_DATA = 'cache/frontend/page_cache/backend_options/compress_data';
3738
const CONFIG_PATH_PAGE_CACHE_BACKEND_PASSWORD = 'cache/frontend/page_cache/backend_options/password';
39+
const CONFIG_PATH_PAGE_CACHE_ID_PREFIX = 'cache/frontend/page_cache/id_prefix';
3840

3941
/**
4042
* @var array
@@ -122,6 +124,12 @@ public function getOptions()
122124
TextConfigOption::FRONTEND_WIZARD_TEXT,
123125
self::CONFIG_PATH_PAGE_CACHE_BACKEND_PASSWORD,
124126
'Redis server password'
127+
),
128+
new TextConfigOption(
129+
self::INPUT_KEY_PAGE_CACHE_ID_PREFIX,
130+
TextConfigOption::FRONTEND_WIZARD_TEXT,
131+
self::CONFIG_PATH_PAGE_CACHE_ID_PREFIX,
132+
'ID prefix for cache keys'
125133
)
126134
];
127135
}
@@ -132,6 +140,11 @@ public function getOptions()
132140
public function createConfig(array $options, DeploymentConfig $deploymentConfig)
133141
{
134142
$configData = new ConfigData(ConfigFilePool::APP_ENV);
143+
if (isset($options[self::INPUT_KEY_PAGE_CACHE_ID_PREFIX])) {
144+
$configData->set(self::CONFIG_PATH_PAGE_CACHE_ID_PREFIX, $options[self::INPUT_KEY_PAGE_CACHE_ID_PREFIX]);
145+
} else {
146+
$configData->set(self::CONFIG_PATH_PAGE_CACHE_ID_PREFIX, $this->generateCachePrefix());
147+
}
135148

136149
if (isset($options[self::INPUT_KEY_PAGE_CACHE_BACKEND])) {
137150
if ($options[self::INPUT_KEY_PAGE_CACHE_BACKEND] == self::INPUT_VALUE_PAGE_CACHE_REDIS) {
@@ -252,4 +265,14 @@ private function getDefaultConfigValue($inputKey)
252265
return '';
253266
}
254267
}
268+
269+
/**
270+
* Generate default cache ID prefix based on installation dir
271+
*
272+
* @return string
273+
*/
274+
private function generateCachePrefix(): string
275+
{
276+
return substr(\md5(dirname(__DIR__, 6)), 0, 3) . '_';
277+
}
255278
}

setup/src/Magento/Setup/Test/Unit/Model/ConfigOptionsList/CacheTest.php

Lines changed: 73 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ protected function setUp()
3939
public function testGetOptions()
4040
{
4141
$options = $this->configOptionsList->getOptions();
42-
$this->assertCount(5, $options);
42+
$this->assertCount(6, $options);
4343

4444
$this->assertArrayHasKey(0, $options);
4545
$this->assertInstanceOf(SelectConfigOption::class, $options[0]);
@@ -60,6 +60,10 @@ public function testGetOptions()
6060
$this->assertArrayHasKey(4, $options);
6161
$this->assertInstanceOf(TextConfigOption::class, $options[4]);
6262
$this->assertEquals('cache-backend-redis-password', $options[4]->getName());
63+
64+
$this->assertArrayHasKey(5, $options);
65+
$this->assertInstanceOf(TextConfigOption::class, $options[5]);
66+
$this->assertEquals('cache-id-prefix', $options[5]->getName());
6367
}
6468

6569
public function testCreateConfigCacheRedis()
@@ -76,7 +80,8 @@ public function testCreateConfigCacheRedis()
7680
'port' => '',
7781
'database' => '',
7882
'password' => ''
79-
]
83+
],
84+
'id_prefix' => $this->expectedIdPrefix(),
8085
]
8186
]
8287
]
@@ -99,7 +104,8 @@ public function testCreateConfigWithRedisConfig()
99104
'port' => '1234',
100105
'database' => '5',
101106
'password' => ''
102-
]
107+
],
108+
'id_prefix' => $this->expectedIdPrefix(),
103109
]
104110
]
105111
]
@@ -116,6 +122,60 @@ public function testCreateConfigWithRedisConfig()
116122
$this->assertEquals($expectedConfigData, $configData->getData());
117123
}
118124

125+
<<<<<<< HEAD
126+
=======
127+
/**
128+
* testCreateConfigCacheRedis
129+
*/
130+
public function testCreateConfigWithFileCache()
131+
{
132+
$this->deploymentConfigMock->method('get')->willReturn('');
133+
134+
$expectedConfigData = [
135+
'cache' => [
136+
'frontend' => [
137+
'default' => [
138+
'id_prefix' => $this->expectedIdPrefix(),
139+
]
140+
]
141+
]
142+
];
143+
144+
$configData = $this->configOptionsList->createConfig([], $this->deploymentConfigMock);
145+
146+
$this->assertEquals($expectedConfigData, $configData->getData());
147+
}
148+
149+
/**
150+
* testCreateConfigCacheRedis
151+
*/
152+
public function testCreateConfigWithIdPrefix()
153+
{
154+
$this->deploymentConfigMock->method('get')->willReturn('');
155+
156+
$explicitPrefix = 'XXX_';
157+
$expectedConfigData = [
158+
'cache' => [
159+
'frontend' => [
160+
'default' => [
161+
'id_prefix' => $explicitPrefix,
162+
]
163+
]
164+
]
165+
];
166+
167+
$configData = $this->configOptionsList->createConfig(
168+
['cache-id-prefix' => $explicitPrefix],
169+
$this->deploymentConfigMock
170+
);
171+
172+
$this->assertEquals($expectedConfigData, $configData->getData());
173+
}
174+
175+
/**
176+
* testValidateWithValidInput
177+
*/
178+
>>>>>>> 12b7e08c2f26... Set cache id prefix on installation
119179
public function testValidateWithValidInput()
120180
{
121181
$options = [
@@ -142,4 +202,14 @@ public function testValidateWithInvalidInput()
142202
$this->assertCount(1, $errors);
143203
$this->assertEquals("Invalid cache handler 'clay-tablet'", $errors[0]);
144204
}
205+
206+
/**
207+
* The default ID prefix, based on installation directory
208+
*
209+
* @return string
210+
*/
211+
private function expectedIdPrefix(): string
212+
{
213+
return substr(\md5(dirname(__DIR__, 8)), 0, 3) . '_';
214+
}
145215
}

setup/src/Magento/Setup/Test/Unit/Model/ConfigOptionsList/PageCacheTest.php

Lines changed: 74 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ protected function setUp()
3939
public function testGetOptions()
4040
{
4141
$options = $this->configList->getOptions();
42-
$this->assertCount(6, $options);
42+
$this->assertCount(7, $options);
4343

4444
$this->assertArrayHasKey(0, $options);
4545
$this->assertInstanceOf(SelectConfigOption::class, $options[0]);
@@ -64,6 +64,10 @@ public function testGetOptions()
6464
$this->assertArrayHasKey(5, $options);
6565
$this->assertInstanceOf(TextConfigOption::class, $options[5]);
6666
$this->assertEquals('page-cache-redis-password', $options[5]->getName());
67+
68+
$this->assertArrayHasKey(6, $options);
69+
$this->assertInstanceOf(TextConfigOption::class, $options[6]);
70+
$this->assertEquals('page-cache-id-prefix', $options[6]->getName());
6771
}
6872

6973
public function testCreateConfigWithRedis()
@@ -81,7 +85,8 @@ public function testCreateConfigWithRedis()
8185
'database' => '',
8286
'compress_data' => '',
8387
'password' => ''
84-
]
88+
],
89+
'id_prefix' => $this->expectedIdPrefix(),
8590
]
8691
]
8792
]
@@ -105,7 +110,8 @@ public function testCreateConfigWithRedisConfiguration()
105110
'database' => '6',
106111
'compress_data' => '1',
107112
'password' => ''
108-
]
113+
],
114+
'id_prefix' => $this->expectedIdPrefix(),
109115
]
110116
]
111117
]
@@ -124,6 +130,61 @@ public function testCreateConfigWithRedisConfiguration()
124130
$this->assertEquals($expectedConfigData, $configData->getData());
125131
}
126132

133+
<<<<<<< HEAD
134+
=======
135+
/**
136+
* testCreateConfigWithRedis
137+
*/
138+
public function testCreateConfigWithFileCache()
139+
{
140+
$this->deploymentConfigMock->method('get')->willReturn('');
141+
142+
$expectedConfigData = [
143+
'cache' => [
144+
'frontend' => [
145+
'page_cache' => [
146+
'id_prefix' => $this->expectedIdPrefix(),
147+
]
148+
]
149+
]
150+
];
151+
152+
$configData = $this->configList->createConfig([], $this->deploymentConfigMock);
153+
154+
$this->assertEquals($expectedConfigData, $configData->getData());
155+
}
156+
157+
158+
/**
159+
* testCreateConfigCacheRedis
160+
*/
161+
public function testCreateConfigWithIdPrefix()
162+
{
163+
$this->deploymentConfigMock->method('get')->willReturn('');
164+
165+
$explicitPrefix = 'XXX_';
166+
$expectedConfigData = [
167+
'cache' => [
168+
'frontend' => [
169+
'page_cache' => [
170+
'id_prefix' => $explicitPrefix,
171+
]
172+
]
173+
]
174+
];
175+
176+
$configData = $this->configList->createConfig(
177+
['page-cache-id-prefix' => $explicitPrefix],
178+
$this->deploymentConfigMock
179+
);
180+
181+
$this->assertEquals($expectedConfigData, $configData->getData());
182+
}
183+
184+
/**
185+
* testValidationWithValidData
186+
*/
187+
>>>>>>> 12b7e08c2f26... Set cache id prefix on installation
127188
public function testValidationWithValidData()
128189
{
129190
$this->validatorMock->expects($this->once())
@@ -151,4 +212,14 @@ public function testValidationWithInvalidData()
151212
$this->assertCount(1, $errors);
152213
$this->assertEquals('Invalid cache handler \'foobar\'', $errors[0]);
153214
}
215+
216+
/**
217+
* The default ID prefix, based on installation directory
218+
*
219+
* @return string
220+
*/
221+
private function expectedIdPrefix(): string
222+
{
223+
return substr(\md5(dirname(__DIR__, 8)), 0, 3) . '_';
224+
}
154225
}

0 commit comments

Comments
 (0)