function ConfigConfiguratorTest::testSelectiveStrictness

Same name and namespace in other branches
  1. 11.x core/tests/Drupal/KernelTests/Core/Recipe/ConfigConfiguratorTest.php \Drupal\KernelTests\Core\Recipe\ConfigConfiguratorTest::testSelectiveStrictness()

Tests with strict mode on part of the configuration.

File

core/tests/Drupal/KernelTests/Core/Recipe/ConfigConfiguratorTest.php, line 91

Class

ConfigConfiguratorTest
Tests Config Configurator.

Namespace

Drupal\KernelTests\Core\Recipe

Code

public function testSelectiveStrictness() : void {
  $recipe = Recipe::createFromDirectory('core/recipes/basic_block_type');
  RecipeRunner::processRecipe($recipe);
  $block_type = BlockContentType::load('basic');
  $original_description = $block_type->getDescription();
  $block_type->set('description', 'And now for something completely different.')
    ->save();
  $form_display = $this->container
    ->get(EntityDisplayRepositoryInterface::class)
    ->getFormDisplay('block_content', 'basic');
  $this->assertFalse($form_display->isNew());
  $this->assertIsArray($form_display->getComponent('body'));
  $form_display->removeComponent('body')
    ->save();
  // Delete something that the recipe provides, so we can be sure it is
  // recreated if it's not in the strict list.
  EntityViewDisplay::load('block_content.basic.default')->delete();
  // Clone the recipe into the virtual file system, and opt only the block
  // type into strict mode.
  $clone_dir = $this->cloneRecipe($recipe->path);
  $this->alterRecipe($clone_dir, function (array $data) : array {
    $data['config']['strict'] = [
      'block_content.type.basic',
    ];
    return $data;
  });
  // If we try to instantiate this recipe, we should an exception.
  try {
    Recipe::createFromDirectory($clone_dir);
    $this->fail('Expected an exception but none was thrown.');
  } catch (RecipePreExistingConfigException $e) {
    $this->assertSame("The configuration 'block_content.type.basic' exists already and does not match the recipe's configuration", $e->getMessage());
  }
  // If we restore the block type's original description, we should no longer
  // get an error if we try to instantiate the altered recipe, even though the
  // form display is still different from what's in the recipe.
  BlockContentType::load('basic')->set('description', $original_description)
    ->save();
  $recipe = Recipe::createFromDirectory($clone_dir);
  RecipeRunner::processRecipe($recipe);
  // Make certain that our change to the form display is still there.
  $component = $this->container
    ->get(EntityDisplayRepositoryInterface::class)
    ->getFormDisplay('block_content', 'basic')
    ->getComponent('body');
  $this->assertNull($component);
  // The thing we deleted should have been recreated.
  $this->assertInstanceOf(EntityViewDisplay::class, EntityViewDisplay::load('block_content.basic.default'));
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.