function ConfigConfiguratorTest::testSelectiveStrictness

Tests with strict mode on part of the configuration.

File

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

Class

ConfigConfiguratorTest
@covers \Drupal\Core\Recipe\ConfigConfigurator[[api-linebreak]] @group Recipe

Namespace

Drupal\KernelTests\Core\Recipe

Code

public function testSelectiveStrictness() : void {
  $recipe = Recipe::createFromDirectory('core/recipes/page_content_type');
  RecipeRunner::processRecipe($recipe);
  $node_type = NodeType::load('page');
  $original_description = $node_type->getDescription();
  $node_type->set('description', 'And now for something completely different.')
    ->save();
  $form_display = $this->container
    ->get(EntityDisplayRepositoryInterface::class)
    ->getFormDisplay('node', 'page');
  $this->assertFalse($form_display->isNew());
  $this->assertIsArray($form_display->getComponent('uid'));
  $form_display->removeComponent('uid')
    ->save();
  // Delete something that the recipe provides, so we can be sure it is
  // recreated if it's not in the strict list.
  BaseFieldOverride::loadByName('node', 'page', 'promote')->delete();
  // Clone the recipe into the virtual file system, and opt only the node
  // type into strict mode.
  $clone_dir = $this->cloneRecipe($recipe->path);
  $this->alterRecipe($clone_dir, function (array $data) : array {
    $data['config']['strict'] = [
      'node.type.page',
    ];
    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 'node.type.page' exists already and does not match the recipe's configuration", $e->getMessage());
  }
  // If we restore the node 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.
  NodeType::load('page')->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('node', 'page')
    ->getComponent('uid');
  $this->assertNull($component);
  // The thing we deleted should have been recreated.
  $this->assertInstanceOf(BaseFieldOverride::class, BaseFieldOverride::loadByName('node', 'page', 'promote'));
}

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