Skip to content
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

Set cache id prefix on installation #18641

Merged
merged 4 commits into from
Nov 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
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
25 changes: 24 additions & 1 deletion 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 @@ -77,7 +79,7 @@ public function __construct(RedisConnectionValidator $redisValidator)
}

/**
* {@inheritdoc}
* @inheritdoc
*/
public function getOptions()
{
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) . '_';
}
}
25 changes: 24 additions & 1 deletion 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 @@ -81,7 +83,7 @@ public function __construct(RedisConnectionValidator $redisValidator)
}

/**
* {@inheritdoc}
* @inheritdoc
*/
public function getOptions()
{
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 @@ -45,7 +45,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 @@ -66,6 +66,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());
}

/**
Expand All @@ -85,7 +89,8 @@ public function testCreateConfigCacheRedis()
'port' => '',
'database' => '',
'password' => ''
]
],
'id_prefix' => $this->expectedIdPrefix(),
]
]
]
Expand All @@ -111,7 +116,8 @@ public function testCreateConfigWithRedisConfig()
'port' => '1234',
'database' => '5',
'password' => ''
]
],
'id_prefix' => $this->expectedIdPrefix(),
]
]
]
Expand All @@ -128,6 +134,54 @@ public function testCreateConfigWithRedisConfig()
$this->assertEquals($expectedConfigData, $configData->getData());
}

/**
* 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
*/
Expand Down Expand Up @@ -160,4 +214,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 @@ -45,7 +45,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 @@ -70,6 +70,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());
}

/**
Expand All @@ -90,7 +94,8 @@ public function testCreateConfigWithRedis()
'database' => '',
'compress_data' => '',
'password' => ''
]
],
'id_prefix' => $this->expectedIdPrefix(),
]
]
]
Expand All @@ -117,7 +122,8 @@ public function testCreateConfigWithRedisConfiguration()
'database' => '6',
'compress_data' => '1',
'password' => ''
]
],
'id_prefix' => $this->expectedIdPrefix(),
]
]
]
Expand All @@ -136,6 +142,54 @@ public function testCreateConfigWithRedisConfiguration()
$this->assertEquals($expectedConfigData, $configData->getData());
}

/**
* 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
*/
Expand Down Expand Up @@ -169,4 +223,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) . '_';
}
}