You are here

public function FeaturesAssignTest::testAssignExclude in Features 8.3

Same name and namespace in other branches
  1. 8.4 tests/src/Kernel/FeaturesAssignTest.php \Drupal\Tests\features\Kernel\FeaturesAssignTest::testAssignExclude()

@covers Drupal\features\Plugin\FeaturesAssignment\FeaturesAssignmentExclude

File

tests/src/Kernel/FeaturesAssignTest.php, line 318

Class

FeaturesAssignTest
The Feature Assign test.

Namespace

Drupal\Tests\features\Kernel

Code

public function testAssignExclude() {
  $method_id = 'exclude';

  // Enable the method.
  $this
    ->enableAssignmentMethod($method_id);

  // Also enable Packages and Core plugins.
  $this
    ->enableAssignmentMethod('packages', FALSE);
  $this
    ->enableAssignmentMethod('core', FALSE);

  // Apply the bundle.
  $this->bundle = $this->assigner
    ->loadBundle('test_mybundle');
  $this->assigner
    ->applyAssignmentMethod('packages');
  $packages = $this->featuresManager
    ->getPackages();
  $this
    ->assertNotEmpty($packages[self::TEST_INSTALLED_PACKAGE], 'Expected package not created.');

  // 1. When Required is set to True, config should stay with the module
  // First, test with "Required" set to True.
  $packages[self::TEST_INSTALLED_PACKAGE]
    ->setRequired(TRUE);
  $this->featuresManager
    ->setPackages($packages);
  $this->assigner
    ->applyAssignmentMethod('exclude');
  $this->assigner
    ->applyAssignmentMethod('core');
  $this->assigner
    ->applyAssignmentMethod('existing');
  $packages = $this->featuresManager
    ->getPackages();
  $expected_config_items = [
    'core.date_format.long',
  ];
  $this
    ->assertEquals($expected_config_items, $packages[self::TEST_INSTALLED_PACKAGE]
    ->getConfig(), 'Expected configuration items not present in existing test_core package.');

  // 2. When Required is set to False, config still stays with module
  // Because the module is installed.
  $this
    ->reset();
  $this->bundle = $this->assigner
    ->loadBundle('test_mybundle');
  $this->assigner
    ->applyAssignmentMethod('packages');
  $packages = $this->featuresManager
    ->getPackages();
  $this
    ->assertNotEmpty($packages[self::TEST_INSTALLED_PACKAGE], 'Expected test_mybundle_core package not created.');

  // Set "Required" set to False.
  $packages[self::TEST_INSTALLED_PACKAGE]
    ->setRequired(FALSE);
  $this->featuresManager
    ->setPackages($packages);
  $this->assigner
    ->applyAssignmentMethod('exclude');
  $this->assigner
    ->applyAssignmentMethod('core');
  $this->assigner
    ->applyAssignmentMethod('existing');
  $packages = $this->featuresManager
    ->getPackages();
  $this
    ->assertFalse(array_key_exists('core', $packages), 'Core package should not be created.');
  $expected_config_items = [
    'core.date_format.long',
  ];
  $this
    ->assertEquals($expected_config_items, $packages[self::TEST_INSTALLED_PACKAGE]
    ->getConfig(), 'Expected configuration items not present in existing test_core package.');

  // 3. When Required is set to False and module is NOT installed,
  // Config stays with module if it doesn't match the current namespace
  $this
    ->reset();

  // Load a bundle different from TEST_UNINSTALLED_PACKAGE.
  $this->bundle = $this->assigner
    ->loadBundle('test_mybundle');
  $this->assigner
    ->applyAssignmentMethod('packages');
  $packages = $this->featuresManager
    ->getPackages();
  $this
    ->assertNotEmpty($packages[self::TEST_UNINSTALLED_PACKAGE], 'Expected test_feature package not created.');
  $this
    ->assertNotEmpty($packages[self::TEST_INSTALLED_PACKAGE], 'Expected test_mybundle_core package not created.');

  // Mark package as uninstalled, set "Required" set to False.
  $packages[self::TEST_UNINSTALLED_PACKAGE]
    ->setRequired(FALSE);
  $this->featuresManager
    ->setPackages($packages);
  $this->assigner
    ->applyAssignmentMethod('exclude');
  $this->assigner
    ->applyAssignmentMethod('core');
  $this->assigner
    ->applyAssignmentMethod('existing');
  $packages = $this->featuresManager
    ->getPackages();
  $this
    ->assertFalse(array_key_exists('core', $packages), 'Core package should not be created.');
  $expected_config_items = [
    'core.date_format.short',
    'system.cron',
  ];
  $this
    ->assertEquals($expected_config_items, $packages[self::TEST_UNINSTALLED_PACKAGE]
    ->getConfig(), 'Expected configuration items not present in existing test_feature package.');

  // 4. When Required is set to False and module is NOT installed,
  // Config is reassigned within modules that match the namespace.
  $this
    ->reset();

  // Load the bundle used in TEST_UNINSTALLED_PACKAGE.
  $this->bundle = $this->assigner
    ->loadBundle('test');
  if (empty($this->bundle) || $this->bundle
    ->isDefault()) {

    // Since we uninstalled the test_feature, we probably need to create
    // an empty "test" bundle.
    $this->bundle = $this->assigner
      ->createBundleFromDefault('test');
  }
  $this->assigner
    ->applyAssignmentMethod('packages');
  $packages = $this->featuresManager
    ->getPackages();
  $this
    ->assertNotEmpty($packages[self::TEST_UNINSTALLED_PACKAGE], 'Expected test_feature package not created.');

  // Set "Required" set to False.
  $packages[self::TEST_UNINSTALLED_PACKAGE]
    ->setRequired(FALSE);
  $this->featuresManager
    ->setPackages($packages);
  $this->assigner
    ->applyAssignmentMethod('exclude');
  $this->assigner
    ->applyAssignmentMethod('core');
  $this->assigner
    ->applyAssignmentMethod('existing');
  $packages = $this->featuresManager
    ->getPackages();
  $this
    ->assertNotEmpty($packages['core'], 'Expected Core package not created.');

  // Ensure "core" package is not confused with "test_core" module
  // Since we are in a bundle.
  $this
    ->assertEmpty($packages['core']
    ->getExtension(), 'Autogenerated core package should not have an extension');

  // Core config should be reassigned from TEST_UNINSTALLED_PACKAGE into Core.
  $expected_config_items = [
    'system.cron',
  ];
  $this
    ->assertEquals($expected_config_items, $packages[self::TEST_UNINSTALLED_PACKAGE]
    ->getConfig(), 'Expected configuration items not present in existing test_feature package.');
  $expected_config_items = [
    'core.date_format.short',
  ];
  $this
    ->assertEquals($expected_config_items, $packages['core']
    ->getConfig(), 'Expected configuration items not present in core package.');
}