function ConfigEntityValidationTestBase::testInvalidMachineNameCharacters

Same name in other branches
  1. 11.x core/tests/Drupal/KernelTests/Core/Config/ConfigEntityValidationTestBase.php \Drupal\KernelTests\Core\Config\ConfigEntityValidationTestBase::testInvalidMachineNameCharacters()

Tests that the entity's ID is tested for invalid characters.

@dataProvider providerInvalidMachineNameCharacters

Parameters

string $machine_name: A machine name to test.

bool $is_expected_to_be_valid: Whether this machine name is expected to be considered valid.

File

core/tests/Drupal/KernelTests/Core/Config/ConfigEntityValidationTestBase.php, line 157

Class

ConfigEntityValidationTestBase
Base class for testing validation of config entities.

Namespace

Drupal\KernelTests\Core\Config

Code

public function testInvalidMachineNameCharacters(string $machine_name, bool $is_expected_to_be_valid) : void {
    $constraints = $this->getMachineNameConstraints();
    $this->assertNotEmpty($constraints['Regex']);
    $this->assertIsArray($constraints['Regex']);
    $this->assertArrayHasKey('pattern', $constraints['Regex']);
    $this->assertIsString($constraints['Regex']['pattern']);
    $this->assertArrayHasKey('message', $constraints['Regex']);
    $this->assertIsString($constraints['Regex']['message']);
    $id_key = $this->entity
        ->getEntityType()
        ->getKey('id');
    if ($is_expected_to_be_valid) {
        $expected_errors = [];
    }
    else {
        $expected_errors = [
            $id_key => sprintf('The <em class="placeholder">&quot;%s&quot;</em> machine name is not valid.', $machine_name),
        ];
    }
    // Config entity IDs are immutable by default.
    $expected_errors[''] = "The '{$id_key}' property cannot be changed.";
    $this->entity
        ->set($id_key, $machine_name);
    $this->assertValidationErrors($expected_errors);
}

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