You are here

public function ConfigSelectorTest::testConfigSelector in Configuration selector 8

Same name and namespace in other branches
  1. 8.2 tests/src/Kernel/ConfigSelectorTest.php \Drupal\Tests\config_selector\Kernel\ConfigSelectorTest::testConfigSelector()

Tests \Drupal\config_selector\ConfigSelector().

File

tests/src/Kernel/ConfigSelectorTest.php, line 35

Class

ConfigSelectorTest
Tests the ConfigSelector.

Namespace

Drupal\Tests\config_selector\Kernel

Code

public function testConfigSelector() {

  /** @var \Drupal\Core\Extension\ModuleInstallerInterface $module_installer */
  $module_installer = $this->container
    ->get('module_installer');

  // Install a module that has configuration with config_selector third party
  // settings for the ConfigSelector to process..
  $module_installer
    ->install([
    'config_selector_test_one',
  ]);

  /** @var \Drupal\Core\Config\Entity\ConfigEntityInterface[] $configs */
  $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();

  // Install another module that will cause config_test.dynamic.feature_a_two
  // to be installed. This configuration has a higher priority than
  // config_test.dynamic.feature_a_one. Therefore, feature_a_one will be
  // disabled and feature_a_two will be enabled.
  $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();

  // Install another module that will cause
  // config_test.dynamic.feature_a_three to be installed. This configuration
  // has a higher priority than config_test.dynamic.feature_a_one but a lower
  // priority than config_test.dynamic.feature_a_two. Therefore,
  // feature_a_three will be disabled and feature_a_two will still be enabled.
  $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();

  // Uninstall a module causing config_test.dynamic.feature_a_two to be
  // removed. Since config_test.dynamic.feature_a_three has the next highest
  // priority it will be enabled.
  $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();

  // Install the module that will cause config_test.dynamic.feature_a_two to
  // be installed again. This configuration has a higher priority than
  // config_test.dynamic.feature_a_one and
  // config_test.dynamic.feature_a_three. Therefore, feature_a_three will be
  // disabled and feature_a_two will be enabled.
  $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();

  // Manually disable config_test.dynamic.feature_a_two and enable
  // config_test.dynamic.feature_a_one.
  $configs['feature_a_two']
    ->setStatus(FALSE)
    ->save();
  $configs['feature_a_one']
    ->setStatus(TRUE)
    ->save();

  // Uninstalling config_selector_test_two will not enable
  // config_test.dynamic.feature_a_three because
  // config_test.dynamic.feature_a_one is enabled.
  $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();

  // Install the module that will cause config_test.dynamic.feature_a_two to
  // be installed again. This configuration has a higher priority than
  // config_test.dynamic.feature_a_one and
  // config_test.dynamic.feature_a_three. Therefore, feature_a_one will be
  // disabled and feature_a_two will be enabled.
  $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();

  // Uninstalling the module that config_test.dynamic.feature_a_three depends
  // on does not change which config is enabled.
  $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();

  // Uninstalling the module that config_test.dynamic.feature_a_two depends
  // on means that as the last remaining config,
  // config_test.dynamic.feature_a_one is enabled.
  $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();

  // Install the module that will cause config_test.dynamic.feature_a_four to
  // be created. This configuration has a higher priority than
  // config_test.dynamic.feature_a_one but is disabled by default. Therefore,
  // feature_a_one will be still be enabled and feature_a_four will be
  // disabled.
  $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();

  // Uninstalling the module that will cause config_test.dynamic.feature_a_one
  // to be removed. This will cause config_test.dynamic.feature_a_four to be
  // enabled.
  $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();

  // Installing the module that will cause config_test.dynamic.feature_a_one
  // to be create. This will cause config_test.dynamic.feature_a_four to still
  // be enabled and feature_a_one will be disabled.
  $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();

  // Because both config_test.dynamic.feature_a_one and
  // config_test.dynamic.feature_a_four are disabled, uninstalling a module
  // should not enable feature_a_four even though feature_a_one is deleted.
  $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();
}