Skip to content

Backport Magento 2.2 Set cache id prefix on installation #22439

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions setup/src/Magento/Setup/Model/ConfigOptionsList/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@ class Cache implements ConfigOptionsListInterface
const INPUT_KEY_CACHE_BACKEND_REDIS_DATABASE = 'cache-backend-redis-db';
const INPUT_KEY_CACHE_BACKEND_REDIS_PORT = 'cache-backend-redis-port';
const INPUT_KEY_CACHE_BACKEND_REDIS_PASSWORD = 'cache-backend-redis-password';
const INPUT_KEY_CACHE_ID_PREFIX = 'cache-id-prefix';

const CONFIG_PATH_CACHE_BACKEND = 'cache/frontend/default/backend';
const CONFIG_PATH_CACHE_BACKEND_SERVER = 'cache/frontend/default/backend_options/server';
const CONFIG_PATH_CACHE_BACKEND_DATABASE = 'cache/frontend/default/backend_options/database';
const CONFIG_PATH_CACHE_BACKEND_PORT = 'cache/frontend/default/backend_options/port';
const CONFIG_PATH_CACHE_BACKEND_PASSWORD = 'cache/frontend/default/backend_options/password';
const CONFIG_PATH_CACHE_ID_PREFIX = 'cache/frontend/default/id_prefix';

/**
* @var array
Expand Down Expand Up @@ -112,6 +114,12 @@ public function getOptions()
TextConfigOption::FRONTEND_WIZARD_TEXT,
self::CONFIG_PATH_CACHE_BACKEND_PASSWORD,
'Redis server password'
),
new TextConfigOption(
self::INPUT_KEY_CACHE_ID_PREFIX,
TextConfigOption::FRONTEND_WIZARD_TEXT,
self::CONFIG_PATH_CACHE_ID_PREFIX,
'ID prefix for cache keys'
)
];
}
Expand All @@ -122,6 +130,11 @@ public function getOptions()
public function createConfig(array $options, DeploymentConfig $deploymentConfig)
{
$configData = new ConfigData(ConfigFilePool::APP_ENV);
if (isset($options[self::INPUT_KEY_CACHE_ID_PREFIX])) {
$configData->set(self::CONFIG_PATH_CACHE_ID_PREFIX, $options[self::INPUT_KEY_CACHE_ID_PREFIX]);
} else {
$configData->set(self::CONFIG_PATH_CACHE_ID_PREFIX, $this->generateCachePrefix());
}

if (isset($options[self::INPUT_KEY_CACHE_BACKEND])) {
if ($options[self::INPUT_KEY_CACHE_BACKEND] == self::INPUT_VALUE_CACHE_REDIS) {
Expand Down Expand Up @@ -241,4 +254,14 @@ private function getDefaultConfigValue($inputKey)
return '';
}
}

/**
* Generate default cache ID prefix based on installation dir
*
* @return string
*/
private function generateCachePrefix(): string
{
return substr(\md5(dirname(__DIR__, 6)), 0, 3) . '_';
}
}
23 changes: 23 additions & 0 deletions setup/src/Magento/Setup/Model/ConfigOptionsList/PageCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,15 @@ class PageCache implements ConfigOptionsListInterface
const INPUT_KEY_PAGE_CACHE_BACKEND_REDIS_PORT = 'page-cache-redis-port';
const INPUT_KEY_PAGE_CACHE_BACKEND_REDIS_COMPRESS_DATA = 'page-cache-redis-compress-data';
const INPUT_KEY_PAGE_CACHE_BACKEND_REDIS_PASSWORD = 'page-cache-redis-password';
const INPUT_KEY_PAGE_CACHE_ID_PREFIX = 'page-cache-id-prefix';

const CONFIG_PATH_PAGE_CACHE_BACKEND = 'cache/frontend/page_cache/backend';
const CONFIG_PATH_PAGE_CACHE_BACKEND_SERVER = 'cache/frontend/page_cache/backend_options/server';
const CONFIG_PATH_PAGE_CACHE_BACKEND_DATABASE = 'cache/frontend/page_cache/backend_options/database';
const CONFIG_PATH_PAGE_CACHE_BACKEND_PORT = 'cache/frontend/page_cache/backend_options/port';
const CONFIG_PATH_PAGE_CACHE_BACKEND_COMPRESS_DATA = 'cache/frontend/page_cache/backend_options/compress_data';
const CONFIG_PATH_PAGE_CACHE_BACKEND_PASSWORD = 'cache/frontend/page_cache/backend_options/password';
const CONFIG_PATH_PAGE_CACHE_ID_PREFIX = 'cache/frontend/page_cache/id_prefix';

/**
* @var array
Expand Down Expand Up @@ -122,6 +124,12 @@ public function getOptions()
TextConfigOption::FRONTEND_WIZARD_TEXT,
self::CONFIG_PATH_PAGE_CACHE_BACKEND_PASSWORD,
'Redis server password'
),
new TextConfigOption(
self::INPUT_KEY_PAGE_CACHE_ID_PREFIX,
TextConfigOption::FRONTEND_WIZARD_TEXT,
self::CONFIG_PATH_PAGE_CACHE_ID_PREFIX,
'ID prefix for cache keys'
)
];
}
Expand All @@ -132,6 +140,11 @@ public function getOptions()
public function createConfig(array $options, DeploymentConfig $deploymentConfig)
{
$configData = new ConfigData(ConfigFilePool::APP_ENV);
if (isset($options[self::INPUT_KEY_PAGE_CACHE_ID_PREFIX])) {
$configData->set(self::CONFIG_PATH_PAGE_CACHE_ID_PREFIX, $options[self::INPUT_KEY_PAGE_CACHE_ID_PREFIX]);
} else {
$configData->set(self::CONFIG_PATH_PAGE_CACHE_ID_PREFIX, $this->generateCachePrefix());
}

if (isset($options[self::INPUT_KEY_PAGE_CACHE_BACKEND])) {
if ($options[self::INPUT_KEY_PAGE_CACHE_BACKEND] == self::INPUT_VALUE_PAGE_CACHE_REDIS) {
Expand Down Expand Up @@ -252,4 +265,14 @@ private function getDefaultConfigValue($inputKey)
return '';
}
}

/**
* Generate default cache ID prefix based on installation dir
*
* @return string
*/
private function generateCachePrefix(): string
{
return substr(\md5(dirname(__DIR__, 6)), 0, 3) . '_';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ protected function setUp()
public function testGetOptions()
{
$options = $this->configOptionsList->getOptions();
$this->assertCount(5, $options);
$this->assertCount(6, $options);

$this->assertArrayHasKey(0, $options);
$this->assertInstanceOf(SelectConfigOption::class, $options[0]);
Expand All @@ -60,6 +60,10 @@ public function testGetOptions()
$this->assertArrayHasKey(4, $options);
$this->assertInstanceOf(TextConfigOption::class, $options[4]);
$this->assertEquals('cache-backend-redis-password', $options[4]->getName());

$this->assertArrayHasKey(5, $options);
$this->assertInstanceOf(TextConfigOption::class, $options[5]);
$this->assertEquals('cache-id-prefix', $options[5]->getName());
}

public function testCreateConfigCacheRedis()
Expand All @@ -76,7 +80,8 @@ public function testCreateConfigCacheRedis()
'port' => '',
'database' => '',
'password' => ''
]
],
'id_prefix' => $this->expectedIdPrefix(),
]
]
]
Expand All @@ -99,7 +104,8 @@ public function testCreateConfigWithRedisConfig()
'port' => '1234',
'database' => '5',
'password' => ''
]
],
'id_prefix' => $this->expectedIdPrefix(),
]
]
]
Expand All @@ -116,6 +122,60 @@ public function testCreateConfigWithRedisConfig()
$this->assertEquals($expectedConfigData, $configData->getData());
}

<<<<<<< HEAD
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@orlangur I think this line is causing the Travis CL build to fail. Correct me if I'm wrong but this preserves the authorship so I don't think it can simply be removed.

=======
/**
* testCreateConfigCacheRedis
*/
public function testCreateConfigWithFileCache()
{
$this->deploymentConfigMock->method('get')->willReturn('');

$expectedConfigData = [
'cache' => [
'frontend' => [
'default' => [
'id_prefix' => $this->expectedIdPrefix(),
]
]
]
];

$configData = $this->configOptionsList->createConfig([], $this->deploymentConfigMock);

$this->assertEquals($expectedConfigData, $configData->getData());
}

/**
* testCreateConfigCacheRedis
*/
public function testCreateConfigWithIdPrefix()
{
$this->deploymentConfigMock->method('get')->willReturn('');

$explicitPrefix = 'XXX_';
$expectedConfigData = [
'cache' => [
'frontend' => [
'default' => [
'id_prefix' => $explicitPrefix,
]
]
]
];

$configData = $this->configOptionsList->createConfig(
['cache-id-prefix' => $explicitPrefix],
$this->deploymentConfigMock
);

$this->assertEquals($expectedConfigData, $configData->getData());
}

/**
* testValidateWithValidInput
*/
>>>>>>> 12b7e08c2f26... Set cache id prefix on installation
public function testValidateWithValidInput()
{
$options = [
Expand All @@ -142,4 +202,14 @@ public function testValidateWithInvalidInput()
$this->assertCount(1, $errors);
$this->assertEquals("Invalid cache handler 'clay-tablet'", $errors[0]);
}

/**
* The default ID prefix, based on installation directory
*
* @return string
*/
private function expectedIdPrefix(): string
{
return substr(\md5(dirname(__DIR__, 8)), 0, 3) . '_';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ protected function setUp()
public function testGetOptions()
{
$options = $this->configList->getOptions();
$this->assertCount(6, $options);
$this->assertCount(7, $options);

$this->assertArrayHasKey(0, $options);
$this->assertInstanceOf(SelectConfigOption::class, $options[0]);
Expand All @@ -64,6 +64,10 @@ public function testGetOptions()
$this->assertArrayHasKey(5, $options);
$this->assertInstanceOf(TextConfigOption::class, $options[5]);
$this->assertEquals('page-cache-redis-password', $options[5]->getName());

$this->assertArrayHasKey(6, $options);
$this->assertInstanceOf(TextConfigOption::class, $options[6]);
$this->assertEquals('page-cache-id-prefix', $options[6]->getName());
}

public function testCreateConfigWithRedis()
Expand All @@ -81,7 +85,8 @@ public function testCreateConfigWithRedis()
'database' => '',
'compress_data' => '',
'password' => ''
]
],
'id_prefix' => $this->expectedIdPrefix(),
]
]
]
Expand All @@ -105,7 +110,8 @@ public function testCreateConfigWithRedisConfiguration()
'database' => '6',
'compress_data' => '1',
'password' => ''
]
],
'id_prefix' => $this->expectedIdPrefix(),
]
]
]
Expand All @@ -124,6 +130,61 @@ public function testCreateConfigWithRedisConfiguration()
$this->assertEquals($expectedConfigData, $configData->getData());
}

<<<<<<< HEAD
=======
/**
* testCreateConfigWithRedis
*/
public function testCreateConfigWithFileCache()
{
$this->deploymentConfigMock->method('get')->willReturn('');

$expectedConfigData = [
'cache' => [
'frontend' => [
'page_cache' => [
'id_prefix' => $this->expectedIdPrefix(),
]
]
]
];

$configData = $this->configList->createConfig([], $this->deploymentConfigMock);

$this->assertEquals($expectedConfigData, $configData->getData());
}


/**
* testCreateConfigCacheRedis
*/
public function testCreateConfigWithIdPrefix()
{
$this->deploymentConfigMock->method('get')->willReturn('');

$explicitPrefix = 'XXX_';
$expectedConfigData = [
'cache' => [
'frontend' => [
'page_cache' => [
'id_prefix' => $explicitPrefix,
]
]
]
];

$configData = $this->configList->createConfig(
['page-cache-id-prefix' => $explicitPrefix],
$this->deploymentConfigMock
);

$this->assertEquals($expectedConfigData, $configData->getData());
}

/**
* testValidationWithValidData
*/
>>>>>>> 12b7e08c2f26... Set cache id prefix on installation
public function testValidationWithValidData()
{
$this->validatorMock->expects($this->once())
Expand Down Expand Up @@ -151,4 +212,14 @@ public function testValidationWithInvalidData()
$this->assertCount(1, $errors);
$this->assertEquals('Invalid cache handler \'foobar\'', $errors[0]);
}

/**
* The default ID prefix, based on installation directory
*
* @return string
*/
private function expectedIdPrefix(): string
{
return substr(\md5(dirname(__DIR__, 8)), 0, 3) . '_';
}
}