function ConfigCRUDTest::testNameValidation
Same name in other branches
- 9 core/tests/Drupal/KernelTests/Core/Config/ConfigCRUDTest.php \Drupal\KernelTests\Core\Config\ConfigCRUDTest::testNameValidation()
- 8.9.x core/tests/Drupal/KernelTests/Core/Config/ConfigCRUDTest.php \Drupal\KernelTests\Core\Config\ConfigCRUDTest::testNameValidation()
- 10 core/tests/Drupal/KernelTests/Core/Config/ConfigCRUDTest.php \Drupal\KernelTests\Core\Config\ConfigCRUDTest::testNameValidation()
Tests the validation of configuration object names.
File
-
core/
tests/ Drupal/ KernelTests/ Core/ Config/ ConfigCRUDTest.php, line 191
Class
- ConfigCRUDTest
- Tests CRUD operations on configuration objects.
Namespace
Drupal\KernelTests\Core\ConfigCode
public function testNameValidation() : void {
// Verify that an object name without namespace causes an exception.
$name = 'no_namespace';
try {
$this->config($name)
->save();
$this->fail('Expected ConfigNameException was thrown for a name without a namespace.');
} catch (\Exception $e) {
$this->assertInstanceOf(ConfigNameException::class, $e);
}
// Verify that a name longer than the maximum length causes an exception.
$name = 'config_test.herman_melville.dick_or_the_whale.harper_1851.now_small_fowls_flew_screaming_over_the_yet_yawning_gulf_a_sullen_white_surf_beat_against_its_steep_sides_then_all_collapsed_and_the_great_shroud_of_the_sea_rolled_on_as_it_rolled_five_thousand_years_ago';
try {
$this->config($name)
->save();
$this->fail('Expected ConfigNameException was thrown for a name longer than Config::MAX_NAME_LENGTH.');
} catch (\Exception $e) {
$this->assertInstanceOf(ConfigNameException::class, $e);
}
// Verify that disallowed characters in the name cause an exception.
$characters = $test_characters = [
':',
'?',
'*',
'<',
'>',
'"',
'\'',
'/',
'\\',
];
foreach ($test_characters as $i => $c) {
try {
$name = 'namespace.object' . $c;
$config = $this->config($name);
$config->save();
} catch (ConfigNameException) {
unset($test_characters[$i]);
}
}
$this->assertEmpty($test_characters, sprintf('Expected ConfigNameException was thrown for all invalid name characters: %s', implode(' ', $characters)));
// Verify that a valid config object name can be saved.
$name = 'namespace.object';
try {
$config = $this->config($name);
$config->save();
} catch (ConfigNameException) {
$this->fail('ConfigNameException was not thrown for a valid object name.');
}
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.