function ConfigActionValidationTest::testConfigActionsAreValidated

Same name in other branches
  1. 10 core/tests/Drupal/KernelTests/Core/Recipe/ConfigActionValidationTest.php \Drupal\KernelTests\Core\Recipe\ConfigActionValidationTest::testConfigActionsAreValidated()

@testWith ["block_content_type"] ["node_type"] ["shortcut_set"] ["menu"]

File

core/tests/Drupal/KernelTests/Core/Recipe/ConfigActionValidationTest.php, line 56

Class

ConfigActionValidationTest
@group Recipe

Namespace

Drupal\KernelTests\Core\Recipe

Code

public function testConfigActionsAreValidated(string $entity_type_id) : void {
    
    /** @var \Drupal\Core\Config\Entity\ConfigEntityStorageInterface $storage */
    $storage = $this->container
        ->get(EntityTypeManagerInterface::class)
        ->getStorage($entity_type_id);
    
    /** @var \Drupal\Core\Config\Entity\ConfigEntityTypeInterface $entity_type */
    $entity_type = $storage->getEntityType();
    // If there is a label key, it's safe to assume that it's not allowed to be
    // empty. We don't care whether it's immutable; we just care that the value
    // the config action sets it to (an empty string) violates config schema.
    $label_key = $entity_type->getKey('label');
    $this->assertNotEmpty($label_key);
    $entity = $storage->create([
        $entity_type->getKey('id') => 'test',
        $label_key => 'Test',
    ]);
    $entity->save();
    $config_name = $entity->getConfigDependencyName();
    $recipe_data = <<<YAML
name: Config actions making bad decisions
config:
  actions:
    {<span class="php-variable">$config_name</span>}:
      simpleConfigUpdate:
        {<span class="php-variable">$label_key</span>}: ''
YAML;
    $recipe = $this->createRecipe($recipe_data);
    try {
        RecipeRunner::processRecipe($recipe);
        $this->fail('An exception should have been thrown.');
    } catch (InvalidConfigException $e) {
        $this->assertCount(1, $e->violations);
        $violation = $e->violations
            ->get(0);
        $this->assertSame($label_key, $violation->getPropertyPath());
        $this->assertSame("This value should not be blank.", (string) $violation->getMessage());
    }
}

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