3 namespace Drupal\Tests;
5 use Drupal\Core\Config\TypedConfigManagerInterface;
6 use Drupal\Core\Config\Schema\SchemaCheckTrait;
7 use Drupal\Component\Utility\SafeMarkup;
10 * Provides a class for checking configuration schema.
12 trait SchemaCheckTestTrait {
17 * Asserts the TypedConfigManager has a valid schema for the configuration.
19 * @param \Drupal\Core\Config\TypedConfigManagerInterface $typed_config
20 * The TypedConfigManager.
21 * @param string $config_name
22 * The configuration name.
23 * @param array $config_data
24 * The configuration data.
26 public function assertConfigSchema(TypedConfigManagerInterface $typed_config, $config_name, $config_data) {
27 $errors = $this->checkConfigSchema($typed_config, $config_name, $config_data);
28 if ($errors === FALSE) {
29 // @todo Since the use of this trait is under TestBase, it works.
30 // Can be fixed as part of https://www.drupal.org/node/2260053.
31 $this->fail(SafeMarkup::format('No schema for @config_name', ['@config_name' => $config_name]));
34 elseif ($errors === TRUE) {
35 // @todo Since the use of this trait is under TestBase, it works.
36 // Can be fixed as part of https://www.drupal.org/node/2260053.
37 $this->pass(SafeMarkup::format('Schema found for @config_name and values comply with schema.', ['@config_name' => $config_name]));
40 foreach ($errors as $key => $error) {
41 // @todo Since the use of this trait is under TestBase, it works.
42 // Can be fixed as part of https://www.drupal.org/node/2260053.
43 $this->fail(SafeMarkup::format('Schema key @key failed with: @error', ['@key' => $key, '@error' => $error]));
49 * Asserts configuration, specified by name, has a valid schema.
51 * @param string $config_name
52 * The configuration name.
54 public function assertConfigSchemaByName($config_name) {
55 $config = $this->config($config_name);
56 $this->assertConfigSchema(\Drupal::service('config.typed'), $config->getName(), $config->get());