diff --git a/core/modules/config/src/Tests/ConfigSchemaTestBase.php b/core/lib/Drupal/Core/Config/Schema/SchemaCheckTrait.php similarity index 60% rename from core/modules/config/src/Tests/ConfigSchemaTestBase.php rename to core/lib/Drupal/Core/Config/Schema/SchemaCheckTrait.php index eac1a75abc0a70c236da44811822e9727905f24e..f09dd779babab6eb361e77d42e01a87d11b9c900 100644 --- a/core/modules/config/src/Tests/ConfigSchemaTestBase.php +++ b/core/lib/Drupal/Core/Config/Schema/SchemaCheckTrait.php @@ -2,26 +2,22 @@ /** * @file - * Contains Drupal\config\Tests\ConfigSchemaTestBase. + * Contains \Drupal\Core\Config\Schema\SchemaCheckTrait. */ -namespace Drupal\config\Tests; +namespace Drupal\Core\Config\Schema; -use Drupal\Core\Config\Schema\ArrayElement; use Drupal\Core\Config\TypedConfigManagerInterface; +use Drupal\Core\TypedData\PrimitiveInterface; use Drupal\Core\TypedData\Type\BooleanInterface; use Drupal\Core\TypedData\Type\StringInterface; -use Drupal\Component\Utility\String; -use Drupal\Core\Config\Schema\SchemaIncompleteException; -use Drupal\Core\TypedData\PrimitiveInterface; use Drupal\Core\TypedData\Type\FloatInterface; use Drupal\Core\TypedData\Type\IntegerInterface; -use Drupal\simpletest\WebTestBase; /** - * Provides a base class to help test configuration schema. + * Provides a trait for checking configuration schema. */ -abstract class ConfigSchemaTestBase extends WebTestBase { +trait SchemaCheckTrait { /** * The config schema wrapper object for the configuration object under test. @@ -38,14 +34,7 @@ abstract class ConfigSchemaTestBase extends WebTestBase { protected $configName; /** - * Global state for whether the config has a valid schema. - * - * @var boolean - */ - protected $configPass; - - /** - * Asserts the TypedConfigManager has a valid schema for the configuration. + * Checks the TypedConfigManager has a valid schema for the configuration. * * @param \Drupal\Core\Config\TypedConfigManagerInterface $typed_config * The TypedConfigManager. @@ -53,22 +42,25 @@ abstract class ConfigSchemaTestBase extends WebTestBase { * The configuration name. * @param array $config_data * The configuration data. + * + * @return array|bool + * FALSE if no schema found. List of errors if any found. TRUE if fully + * valid. */ - public function assertConfigSchema(TypedConfigManagerInterface $typed_config, $config_name, $config_data) { + public function checkConfigSchema(TypedConfigManagerInterface $typed_config, $config_name, $config_data) { $this->configName = $config_name; if (!$typed_config->hasConfigSchema($config_name)) { - $this->fail(String::format('No schema for !config_name', array('!config_name' => $config_name))); - return; + return FALSE; } $definition = $typed_config->getDefinition($config_name); $this->schema = $typed_config->create($definition, $config_data); - $this->configPass = TRUE; foreach ($config_data as $key => $value) { - $this->checkValue($key, $value); + $errors = $this->checkValue($key, $value); } - if ($this->configPass) { - $this->pass(String::format('Schema found for !config_name and values comply with schema.', array('!config_name' => $config_name))); + if (empty($errors)) { + return TRUE; } + return $errors; } /** @@ -79,25 +71,26 @@ public function assertConfigSchema(TypedConfigManagerInterface $typed_config, $c * @param mixed $value * Value of given key. * - * @return mixed - * Returns mixed value. + * @return array + * List of errors found while checking with the corresponding schema. */ protected function checkValue($key, $value) { + $error_key = $this->configName . ':' . $key; $element = FALSE; try { $element = $this->schema->get($key); } catch (SchemaIncompleteException $e) { if (is_scalar($value) || $value === NULL) { - $this->fail("{$this->configName}:$key has no schema."); + return array($error_key => 'Missing schema.'); } } // Do not check value if it is defined to be ignored. if ($element && $element instanceof Ignore) { - return $value; + return array(); } - if (is_scalar($value) || $value === NULL) { + if ($element && is_scalar($value) || $value === NULL) { $success = FALSE; $type = gettype($value); if ($element instanceof PrimitiveInterface) { @@ -111,12 +104,13 @@ protected function checkValue($key, $value) { } $class = get_class($element); if (!$success) { - $this->fail("{$this->configName}:$key has the wrong schema. Variable type is $type and schema class is $class."); + return array($error_key => "Variable type is $type but applied schema class is $class."); } } else { + $errors = array(); if (!$element instanceof ArrayElement) { - $this->fail("Non-scalar {$this->configName}:$key is not defined as an array type (such as mapping or sequence)."); + $errors[$error_key] = 'Non-scalar value but not defined as an array (such as mapping or sequence)'; } // Go on processing so we can get errors on all levels. Any non-scalar @@ -126,18 +120,11 @@ protected function checkValue($key, $value) { } // Recurse into any nested keys. foreach ($value as $nested_value_key => $nested_value) { - $value[$nested_value_key] = $this->checkValue($key . '.' . $nested_value_key, $nested_value); + $errors = array_merge($errors, $this->checkValue($key . '.' . $nested_value_key, $nested_value)); } + return $errors; } - return $value; - } - - /** - * {@inheritdoc} - */ - protected function fail($message = NULL, $group = 'Other') { - $this->configPass = FALSE; - return parent::fail($message, $group); + // No errors found. + return array(); } - } diff --git a/core/modules/block/src/Tests/BlockConfigSchemaTest.php b/core/modules/block/src/Tests/BlockConfigSchemaTest.php index 05f080e2e40aff7db905f800772907e9377dc8bd..b845f04ca7e801076f2f1b0e5ec3e63e7c8ff593 100644 --- a/core/modules/block/src/Tests/BlockConfigSchemaTest.php +++ b/core/modules/block/src/Tests/BlockConfigSchemaTest.php @@ -8,12 +8,15 @@ namespace Drupal\block\Tests; use Drupal\block\Entity\Block; -use Drupal\config\Tests\ConfigSchemaTestBase; +use Drupal\config\Tests\SchemaCheckTestTrait; +use Drupal\simpletest\WebTestBase; /** * Tests the block config schema. */ -class BlockConfigSchemaTest extends ConfigSchemaTestBase { +class BlockConfigSchemaTest extends WebTestBase { + + use SchemaCheckTestTrait; /** * {@inheritdoc} diff --git a/core/modules/color/src/Tests/ColorConfigSchemaTest.php b/core/modules/color/src/Tests/ColorConfigSchemaTest.php index ebaa303ffc4d69a2584af8e901c2a216e994b319..bf7010c2a72516174fbcdd14fd0dfdd15dd3728a 100644 --- a/core/modules/color/src/Tests/ColorConfigSchemaTest.php +++ b/core/modules/color/src/Tests/ColorConfigSchemaTest.php @@ -7,12 +7,15 @@ namespace Drupal\color\Tests; -use Drupal\config\Tests\ConfigSchemaTestBase; +use Drupal\config\Tests\SchemaCheckTestTrait; +use Drupal\simpletest\WebTestBase; /** * Tests the Color config schema. */ -class ColorConfigSchemaTest extends ConfigSchemaTestBase { +class ColorConfigSchemaTest extends WebTestBase { + + use SchemaCheckTestTrait; /** * Modules to enable. diff --git a/core/modules/config/src/Tests/DefaultConfigTest.php b/core/modules/config/src/Tests/DefaultConfigTest.php index e90a05889556351cae99267eb8d0d7ab21ddf34b..04b697b58df39844f776c9298f24c8145dee45ae 100644 --- a/core/modules/config/src/Tests/DefaultConfigTest.php +++ b/core/modules/config/src/Tests/DefaultConfigTest.php @@ -10,11 +10,14 @@ use Drupal\config_test\TestInstallStorage; use Drupal\Core\Config\InstallStorage; use Drupal\Core\Config\TypedConfigManager; +use Drupal\simpletest\WebTestBase; /** * Tests default configuration availability and type with configuration schema. */ -class DefaultConfigTest extends ConfigSchemaTestBase { +class DefaultConfigTest extends WebTestBase { + + use SchemaCheckTestTrait; /** * Modules to enable. diff --git a/core/modules/config/src/Tests/SchemaCheckTestTrait.php b/core/modules/config/src/Tests/SchemaCheckTestTrait.php new file mode 100644 index 0000000000000000000000000000000000000000..c5b4cba1d12ba2479f106daa2c4376697ec40a02 --- /dev/null +++ b/core/modules/config/src/Tests/SchemaCheckTestTrait.php @@ -0,0 +1,52 @@ +<?php + +/** + * @file + * Contains Drupal\config\Tests\SchemaCheckTestTrait. + */ + +namespace Drupal\config\Tests; + +use Drupal\Core\Config\TypedConfigManagerInterface; +use Drupal\Core\Config\Schema\SchemaCheckTrait; +use Drupal\Component\Utility\String; + +/** + * Provides a class for checking configuration schema. + */ +trait SchemaCheckTestTrait { + + use SchemaCheckTrait; + + /** + * Asserts the TypedConfigManager has a valid schema for the configuration. + * + * @param \Drupal\Core\Config\TypedConfigManagerInterface $typed_config + * The TypedConfigManager. + * @param string $config_name + * The configuration name. + * @param array $config_data + * The configuration data. + */ + public function assertConfigSchema(TypedConfigManagerInterface $typed_config, $config_name, $config_data) { + $errors = $this->checkConfigSchema($typed_config, $config_name, $config_data); + if ($errors === FALSE) { + // @todo Since the use of this trait is under TestBase, it works. + // Can be fixed as part of https://drupal.org/node/2260053. + $this->fail(String::format('No schema for !config_name', array('!config_name' => $config_name))); + return; + } + elseif ($errors === TRUE) { + // @todo Since the use of this trait is under TestBase, it works. + // Can be fixed as part of https://drupal.org/node/2260053. + $this->pass(String::format('Schema found for !config_name and values comply with schema.', array('!config_name' => $config_name))); + } + else { + foreach ($errors as $key => $error) { + // @todo Since the use of this trait is under TestBase, it works. + // Can be fixed as part of https://drupal.org/node/2260053. + $this->fail($key . ': ' . $error); + } + } + } +} diff --git a/core/modules/language/src/Tests/LanguageConfigSchemaTest.php b/core/modules/language/src/Tests/LanguageConfigSchemaTest.php index 57ed7845ca1ae47db665bb38cb0c40c2bc4b84a1..35e207afbf28bd91dbc3822d2a55bebf153b4c3a 100644 --- a/core/modules/language/src/Tests/LanguageConfigSchemaTest.php +++ b/core/modules/language/src/Tests/LanguageConfigSchemaTest.php @@ -7,12 +7,15 @@ namespace Drupal\language\Tests; -use Drupal\config\Tests\ConfigSchemaTestBase; +use Drupal\config\Tests\SchemaCheckTestTrait; +use Drupal\simpletest\WebTestBase; /** * Tests the language config schema. */ -class LanguageConfigSchemaTest extends ConfigSchemaTestBase { +class LanguageConfigSchemaTest extends WebTestBase { + + use SchemaCheckTestTrait; /** * Modules to enable. diff --git a/core/modules/user/src/Tests/UserActionConfigSchemaTest.php b/core/modules/user/src/Tests/UserActionConfigSchemaTest.php index 3e616d5b6004a972a580eb7d1742e876cfc42b9a..08d88a1e18109755516d27268163de326f172fd9 100644 --- a/core/modules/user/src/Tests/UserActionConfigSchemaTest.php +++ b/core/modules/user/src/Tests/UserActionConfigSchemaTest.php @@ -7,12 +7,15 @@ namespace Drupal\user\Tests; -use Drupal\config\Tests\ConfigSchemaTestBase; +use Drupal\config\Tests\SchemaCheckTestTrait; +use Drupal\simpletest\WebTestBase; /** * Tests the User action config schema. */ -class UserActionConfigSchemaTest extends ConfigSchemaTestBase { +class UserActionConfigSchemaTest extends WebTestBase { + + use SchemaCheckTestTrait; /** * Modules to enable.