View source
<?php
namespace Drupal\Tests\config_selector\Kernel;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\KernelTests\KernelTestBase;
use Drupal\config_selector\TestLogger;
use Psr\Log\LogLevel;
class ConfigSelectorTest extends KernelTestBase {
protected static $modules = [
'system',
'config_test',
'config_selector',
];
public function register(ContainerBuilder $container) {
parent::register($container);
TestLogger::register($container);
}
public function testConfigSelector() {
$module_installer = $this->container
->get('module_installer');
$module_installer
->install([
'config_selector_test_one',
]);
$configs = \Drupal::entityTypeManager()
->getStorage('config_test')
->loadMultiple();
$this
->assertTrue($configs['feature_a_one']
->status());
$this
->assertArrayNotHasKey('feature_a_two', $configs);
$this
->assertArrayNotHasKey('feature_a_three', $configs);
$this
->assertLogMessages([
'<em class="placeholder">config_selector_test_one</em> module installed.',
]);
$this
->assertMessages([]);
$this
->clearLogger();
$module_installer
->install([
'config_selector_test_two',
]);
$configs = \Drupal::entityTypeManager()
->getStorage('config_test')
->loadMultiple();
$this
->assertFalse($configs['feature_a_one']
->status());
$this
->assertTrue($configs['feature_a_two']
->status());
$this
->assertArrayNotHasKey('feature_a_three', $configs);
$this
->assertLogMessages([
'<em class="placeholder">config_selector_test_two</em> module installed.',
'Configuration <a href="/admin/structure/config_test/manage/feature_a_one">Feature A version 1</a> has been disabled in favor of <a href="/admin/structure/config_test/manage/feature_a_two">Feature A version 2</a>.',
]);
$this
->assertMessages([
'Configuration <a href="/admin/structure/config_test/manage/feature_a_one">Feature A version 1</a> has been disabled in favor of <a href="/admin/structure/config_test/manage/feature_a_two">Feature A version 2</a>.',
]);
$this
->clearLogger();
$module_installer
->install([
'config_selector_test_three',
]);
$configs = \Drupal::entityTypeManager()
->getStorage('config_test')
->loadMultiple();
$this
->assertFalse($configs['feature_a_one']
->status());
$this
->assertTrue($configs['feature_a_two']
->status());
$this
->assertFalse($configs['feature_a_three']
->status());
$this
->assertLogMessages([
'<em class="placeholder">config_selector_test_three</em> module installed.',
'Configuration <a href="/admin/structure/config_test/manage/feature_a_three">Feature A version 3</a> has been disabled in favor of <a href="/admin/structure/config_test/manage/feature_a_two">Feature A version 2</a>.',
]);
$this
->assertMessages([
'Configuration <a href="/admin/structure/config_test/manage/feature_a_three">Feature A version 3</a> has been disabled in favor of <a href="/admin/structure/config_test/manage/feature_a_two">Feature A version 2</a>.',
]);
$this
->clearLogger();
$module_installer
->uninstall([
'config_selector_test_two',
]);
$configs = \Drupal::entityTypeManager()
->getStorage('config_test')
->loadMultiple();
$this
->assertFalse($configs['feature_a_one']
->status());
$this
->assertArrayNotHasKey('feature_a_two', $configs);
$this
->assertTrue($configs['feature_a_three']
->status());
$this
->assertLogMessages([
'<em class="placeholder">config_selector_test_two</em> module uninstalled.',
'Configuration <a href="/admin/structure/config_test/manage/feature_a_three">Feature A version 3</a> has been enabled.',
]);
$this
->assertMessages([
'Configuration <a href="/admin/structure/config_test/manage/feature_a_three">Feature A version 3</a> has been enabled.',
]);
$this
->clearLogger();
$module_installer
->install([
'config_selector_test_two',
]);
$configs = \Drupal::entityTypeManager()
->getStorage('config_test')
->loadMultiple();
$this
->assertFalse($configs['feature_a_one']
->status());
$this
->assertTrue($configs['feature_a_two']
->status());
$this
->assertFalse($configs['feature_a_three']
->status());
$this
->assertLogMessages([
'<em class="placeholder">config_selector_test_two</em> module installed.',
'Configuration <a href="/admin/structure/config_test/manage/feature_a_three">Feature A version 3</a> has been disabled in favor of <a href="/admin/structure/config_test/manage/feature_a_two">Feature A version 2</a>.',
]);
$this
->assertMessages([
'Configuration <a href="/admin/structure/config_test/manage/feature_a_three">Feature A version 3</a> has been disabled in favor of <a href="/admin/structure/config_test/manage/feature_a_two">Feature A version 2</a>.',
]);
$this
->clearLogger();
$configs['feature_a_two']
->setStatus(FALSE)
->save();
$configs['feature_a_one']
->setStatus(TRUE)
->save();
$module_installer
->uninstall([
'config_selector_test_two',
]);
$configs = \Drupal::entityTypeManager()
->getStorage('config_test')
->loadMultiple();
$this
->assertTrue($configs['feature_a_one']
->status());
$this
->assertArrayNotHasKey('feature_a_two', $configs);
$this
->assertFalse($configs['feature_a_three']
->status());
$this
->assertLogMessages([
'<em class="placeholder">config_selector_test_two</em> module uninstalled.',
]);
$this
->assertMessages([]);
$this
->clearLogger();
$module_installer
->install([
'config_selector_test_two',
]);
$configs = \Drupal::entityTypeManager()
->getStorage('config_test')
->loadMultiple();
$this
->assertFalse($configs['feature_a_one']
->status());
$this
->assertTrue($configs['feature_a_two']
->status());
$this
->assertFalse($configs['feature_a_three']
->status());
$this
->assertLogMessages([
'<em class="placeholder">config_selector_test_two</em> module installed.',
'Configuration <a href="/admin/structure/config_test/manage/feature_a_one">Feature A version 1</a> has been disabled in favor of <a href="/admin/structure/config_test/manage/feature_a_two">Feature A version 2</a>.',
]);
$this
->assertMessages([
'Configuration <a href="/admin/structure/config_test/manage/feature_a_one">Feature A version 1</a> has been disabled in favor of <a href="/admin/structure/config_test/manage/feature_a_two">Feature A version 2</a>.',
]);
$this
->clearLogger();
$module_installer
->uninstall([
'config_selector_test_three',
]);
$configs = \Drupal::entityTypeManager()
->getStorage('config_test')
->loadMultiple();
$this
->assertFalse($configs['feature_a_one']
->status());
$this
->assertTrue($configs['feature_a_two']
->status());
$this
->assertArrayNotHasKey('feature_a_three', $configs);
$this
->assertLogMessages([
'<em class="placeholder">config_selector_test_three</em> module uninstalled.',
]);
$this
->assertMessages([]);
$this
->clearLogger();
$module_installer
->uninstall([
'config_selector_test_two',
]);
$configs = \Drupal::entityTypeManager()
->getStorage('config_test')
->loadMultiple();
$this
->assertTrue($configs['feature_a_one']
->status());
$this
->assertArrayNotHasKey('feature_a_two', $configs);
$this
->assertArrayNotHasKey('feature_a_three', $configs);
$this
->assertLogMessages([
'<em class="placeholder">config_selector_test_two</em> module uninstalled.',
'Configuration <a href="/admin/structure/config_test/manage/feature_a_one">Feature A version 1</a> has been enabled.',
]);
$this
->assertMessages([
'Configuration <a href="/admin/structure/config_test/manage/feature_a_one">Feature A version 1</a> has been enabled.',
]);
$this
->clearLogger();
$module_installer
->install([
'config_selector_test_four',
]);
$configs = \Drupal::entityTypeManager()
->getStorage('config_test')
->loadMultiple();
$this
->assertTrue($configs['feature_a_one']
->status());
$this
->assertArrayNotHasKey('feature_a_two', $configs);
$this
->assertArrayNotHasKey('feature_a_three', $configs);
$this
->assertFalse($configs['feature_a_four']
->status());
$this
->assertLogMessages([
'<em class="placeholder">config_selector_test_four</em> module installed.',
]);
$this
->assertMessages([]);
$this
->clearLogger();
$module_installer
->uninstall([
'config_selector_test_one',
]);
$configs = \Drupal::entityTypeManager()
->getStorage('config_test')
->loadMultiple();
$this
->assertArrayNotHasKey('feature_a_one', $configs);
$this
->assertArrayNotHasKey('feature_a_two', $configs);
$this
->assertArrayNotHasKey('feature_a_three', $configs);
$this
->assertTrue($configs['feature_a_four']
->status());
$this
->assertLogMessages([
'<em class="placeholder">config_selector_test_one</em> module uninstalled.',
'Configuration <a href="/admin/structure/config_test/manage/feature_a_four">Feature A version 4</a> has been enabled.',
]);
$this
->assertMessages([
'Configuration <a href="/admin/structure/config_test/manage/feature_a_four">Feature A version 4</a> has been enabled.',
]);
$this
->clearLogger();
$module_installer
->install([
'config_selector_test_one',
]);
$configs = \Drupal::entityTypeManager()
->getStorage('config_test')
->loadMultiple();
$this
->assertFalse($configs['feature_a_one']
->status());
$this
->assertArrayNotHasKey('feature_a_two', $configs);
$this
->assertArrayNotHasKey('feature_a_three', $configs);
$this
->assertTrue($configs['feature_a_four']
->status());
$configs['feature_a_four']
->setStatus(FALSE)
->save();
$this
->assertLogMessages([
'<em class="placeholder">config_selector_test_one</em> module installed.',
'Configuration <a href="/admin/structure/config_test/manage/feature_a_one">Feature A version 1</a> has been disabled in favor of <a href="/admin/structure/config_test/manage/feature_a_four">Feature A version 4</a>.',
]);
$this
->assertMessages([
'Configuration <a href="/admin/structure/config_test/manage/feature_a_one">Feature A version 1</a> has been disabled in favor of <a href="/admin/structure/config_test/manage/feature_a_four">Feature A version 4</a>.',
]);
$this
->clearLogger();
$module_installer
->uninstall([
'config_selector_test_one',
]);
$configs = \Drupal::entityTypeManager()
->getStorage('config_test')
->loadMultiple();
$this
->assertArrayNotHasKey('feature_a_one', $configs);
$this
->assertArrayNotHasKey('feature_a_two', $configs);
$this
->assertArrayNotHasKey('feature_a_three', $configs);
$this
->assertFalse($configs['feature_a_four']
->status());
$this
->assertLogMessages([
'<em class="placeholder">config_selector_test_one</em> module uninstalled.',
]);
$this
->assertMessages([]);
$this
->clearLogger();
}
public function testConfigSelectorIndirectDependency() {
$module_installer = $this->container
->get('module_installer');
$module_installer
->install([
'config_selector_test_one',
'config_selector_test_depends_on_test_two',
]);
$configs = \Drupal::entityTypeManager()
->getStorage('config_test')
->loadMultiple();
$this
->assertFalse($configs['feature_a_one']
->status());
$this
->assertFalse($configs['feature_a_two']
->status());
$this
->assertTrue($configs['feature_a_depends_on_test_two']
->status());
$this
->assertArrayNotHasKey('feature_a_three', $configs);
$this
->assertLogMessages([
'<em class="placeholder">config_selector_test_two</em> module installed.',
'<em class="placeholder">config_selector_test_depends_on_test_two</em> module installed.',
'<em class="placeholder">config_selector_test_one</em> module installed.',
'Configuration <a href="/admin/structure/config_test/manage/feature_a_one">Feature A version 1</a> has been disabled in favor of <a href="/admin/structure/config_test/manage/feature_a_depends_on_test_two">Feature A indirect depending on Test Two</a>.',
'Configuration <a href="/admin/structure/config_test/manage/feature_a_two">Feature A version 2</a> has been disabled in favor of <a href="/admin/structure/config_test/manage/feature_a_depends_on_test_two">Feature A indirect depending on Test Two</a>.',
]);
$this
->assertMessages([
'Configuration <a href="/admin/structure/config_test/manage/feature_a_one">Feature A version 1</a> has been disabled in favor of <a href="/admin/structure/config_test/manage/feature_a_depends_on_test_two">Feature A indirect depending on Test Two</a>.',
'Configuration <a href="/admin/structure/config_test/manage/feature_a_two">Feature A version 2</a> has been disabled in favor of <a href="/admin/structure/config_test/manage/feature_a_depends_on_test_two">Feature A indirect depending on Test Two</a>.',
]);
$this
->clearLogger();
$module_installer
->uninstall([
'config_selector_test_two',
]);
$configs = \Drupal::entityTypeManager()
->getStorage('config_test')
->loadMultiple();
$this
->assertTrue($configs['feature_a_one']
->status(), "Configuration: Feature A version 1 - should be enabled.");
$this
->assertArrayNotHasKey('feature_a_two', $configs);
$this
->assertArrayNotHasKey('feature_a_depends_on_test_two', $configs);
$this
->assertArrayNotHasKey('feature_a_three', $configs);
$this
->assertLogMessages([
'<em class="placeholder">config_selector_test_depends_on_test_two</em> module uninstalled.',
'<em class="placeholder">config_selector_test_two</em> module uninstalled.',
'Configuration <a href="/admin/structure/config_test/manage/feature_a_one">Feature A version 1</a> has been enabled.',
]);
$this
->assertMessages([
'Configuration <a href="/admin/structure/config_test/manage/feature_a_one">Feature A version 1</a> has been enabled.',
]);
$this
->clearLogger();
}
public function testConfigSelectorMultipleFeatures() {
$module_installer = $this->container
->get('module_installer');
$module_installer
->install([
'config_selector_test_provides_multiple',
]);
$configs = \Drupal::entityTypeManager()
->getStorage('config_test')
->loadMultiple();
$this
->assertTrue($configs['feature_a_two']
->status());
$this
->assertFalse($configs['feature_a_one']
->status());
$this
->assertFalse($configs['feature_a_three']
->status());
$this
->assertFalse($configs['feature_a_four']
->status());
$this
->assertTrue($configs['feature_b_two']
->status());
$this
->assertFalse($configs['feature_b_one']
->status());
$this
->assertTrue($configs['feature_c_one']
->status());
}
public function testConfigSelectorMultipleModuleInstall() {
$module_installer = $this->container
->get('module_installer');
$module_installer
->install([
'config_selector_test_one',
]);
$configs = \Drupal::entityTypeManager()
->getStorage('config_test')
->loadMultiple();
$this
->assertTrue($configs['feature_a_one']
->status());
$this
->assertArrayNotHasKey('feature_a_two', $configs);
$this
->assertArrayNotHasKey('feature_a_three', $configs);
$this
->assertMessages([]);
$this
->clearLogger();
$module_installer
->install([
'config_selector_test_two',
'config_selector_zzzzz_test',
]);
$configs = \Drupal::entityTypeManager()
->getStorage('config_test')
->loadMultiple();
$this
->assertFalse($configs['feature_a_one']
->status());
$this
->assertTrue($configs['feature_a_two']
->status());
$this
->assertArrayNotHasKey('feature_a_three', $configs);
$this
->assertMessages([
'Configuration <a href="/admin/structure/config_test/manage/feature_a_one">Feature A version 1</a> has been disabled in favor of <a href="/admin/structure/config_test/manage/feature_a_two">Feature A version 2</a>.',
]);
$this
->clearLogger();
}
protected function assertLogMessages(array $messages = [], $level = LogLevel::INFO) {
$this
->assertEquals($messages, $this->container
->get('config_selector.test_logger')
->getLogs($level));
}
protected function assertMessages(array $messages = [], $type = 'status') {
$actual_messages = \Drupal::messenger()
->messagesByType($type);
if (!empty($actual_messages)) {
$actual_messages = array_map('strval', $actual_messages);
}
$this
->assertEquals($messages, $actual_messages);
\Drupal::messenger()
->deleteByType($type);
}
protected function clearLogger() {
$this->container
->get('config_selector.test_logger')
->clear();
}
}