diff --git a/core/lib/Drupal/Core/Config/Config.php b/core/lib/Drupal/Core/Config/Config.php index e000200..09b0a43 100644 --- a/core/lib/Drupal/Core/Config/Config.php +++ b/core/lib/Drupal/Core/Config/Config.php @@ -137,7 +137,7 @@ class Config extends DependencySerialization { * @param \Drupal\Core\Language\Language $language * The language object used to override configuration data. */ - public function __construct($name, StorageInterface $storage, EventDispatcherInterface $event_dispatcher, TypedConfigManager $typed_config, Language $language = NULL) { + public function __construct($name, StorageInterface $storage, EventDispatcherInterface $event_dispatcher, TypedConfigManagerInterface $typed_config, Language $language = NULL) { $this->name = $name; $this->storage = $storage; $this->eventDispatcher = $event_dispatcher; @@ -201,13 +201,13 @@ public function setName($name) { public static function validateName($name) { // The name must be namespaced by owner. if (strpos($name, '.') === FALSE) { - throw new ConfigNameException(format_string('Missing namespace in Config object name @name.', array( + throw new ConfigNameException(String::format('Missing namespace in Config object name @name.', array( '@name' => $name, ))); } // The name must be shorter than Config::MAX_NAME_LENGTH characters. if (strlen($name) > self::MAX_NAME_LENGTH) { - throw new ConfigNameException(format_string('Config object name @name exceeds maximum allowed length of @length characters.', array( + throw new ConfigNameException(String::format('Config object name @name exceeds maximum allowed length of @length characters.', array( '@name' => $name, '@length' => self::MAX_NAME_LENGTH, ))); @@ -216,7 +216,7 @@ public static function validateName($name) { // The name must not contain any of the following characters: // : ? * < > " ' / \ if (preg_match('/[:?*<>"\'\/\\\\]/', $name)) { - throw new ConfigNameException(format_string('Invalid character in Config object name @name.', array( + throw new ConfigNameException(String::format('Invalid character in Config object name @name.', array( '@name' => $name, ))); } diff --git a/core/tests/Drupal/Tests/Core/Config/ConfigTest.php b/core/tests/Drupal/Tests/Core/Config/ConfigTest.php new file mode 100644 index 0000000..feefaaa --- /dev/null +++ b/core/tests/Drupal/Tests/Core/Config/ConfigTest.php @@ -0,0 +1,130 @@ + 'Config test', + 'description' => 'Tests Config.', + 'group' => 'Configuration' + ); + } + + /** + * Setup. + */ + public function setUp() { + $this->storage = $this->getMock('Drupal\Core\Config\StorageInterface'); + $this->event_dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface'); + $this->typed_config = $this->getMock('\Drupal\Core\Config\TypedConfigManagerInterface'); + $this->config = new Config('configTest', $this->storage, $this->event_dispatcher, $this->typed_config); + } + + /** + * Check that the config name is set correctly. + */ + public function testSetName() { + // Valid name with dot. + $testName = 'test.name'; + + // Set the name. + $this->config->setName($testName); + + // Check that the name has been set correctly. + $this->assertEquals($testName, $this->config->getName()); + + // Check that the name validates. + // Should throw \Drupal\Core\Config\ConfigNameException if invalid. + $this->config->validateName($testName); + } + + /** + * Checks that name validation exception are thrown. + * + * @expectedException \Drupal\Core\Config\ConfigNameException + * @dataProvider validateNameProvider + */ + public function testValidateNameException($name, $exception_message) { + $this->setExpectedException('\Drupal\Core\Config\ConfigNameException', $exception_message); + $this->config->validateName($name); + } + + /** + * Provides data to test name validation. + */ + public function validateNameProvider() { + $return = array( + // Name missing namespace (dot). + array( + 'MissingNamespace', + String::format('Missing namespace in Config object name MissingNamespace.', array( + '@name' => 'MissingNamespace', + )), + ), + // Exceeds length (max length plus an extra dot). + array( + str_repeat('a', Config::MAX_NAME_LENGTH) . ".", + String::format('Config object name @name exceeds maximum allowed length of @length characters.', array( + '@name' => str_repeat('a', Config::MAX_NAME_LENGTH) . ".", + '@length' => Config::MAX_NAME_LENGTH, + )), + ), + ); + // Name must not contain : ? * < > " ' / \ + foreach (array(':', '?', '*', '<', '>', '"',"'",'/','\\') as $char) { + $name = 'name.' . $char; + $return[] = array( + $name, + String::format('Invalid character in Config object name @name.', array( + '@name' => $name, + )) + ); + } + return $return; + } +}