annotate core/tests/Drupal/Tests/SchemaCheckTestTrait.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 129ea1e6d783
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\Tests;
Chris@0 4
Chris@0 5 use Drupal\Core\Config\TypedConfigManagerInterface;
Chris@0 6 use Drupal\Core\Config\Schema\SchemaCheckTrait;
Chris@17 7 use Drupal\Component\Render\FormattableMarkup;
Chris@0 8
Chris@0 9 /**
Chris@0 10 * Provides a class for checking configuration schema.
Chris@0 11 */
Chris@0 12 trait SchemaCheckTestTrait {
Chris@0 13
Chris@0 14 use SchemaCheckTrait;
Chris@0 15
Chris@0 16 /**
Chris@0 17 * Asserts the TypedConfigManager has a valid schema for the configuration.
Chris@0 18 *
Chris@0 19 * @param \Drupal\Core\Config\TypedConfigManagerInterface $typed_config
Chris@0 20 * The TypedConfigManager.
Chris@0 21 * @param string $config_name
Chris@0 22 * The configuration name.
Chris@0 23 * @param array $config_data
Chris@0 24 * The configuration data.
Chris@0 25 */
Chris@0 26 public function assertConfigSchema(TypedConfigManagerInterface $typed_config, $config_name, $config_data) {
Chris@0 27 $errors = $this->checkConfigSchema($typed_config, $config_name, $config_data);
Chris@0 28 if ($errors === FALSE) {
Chris@0 29 // @todo Since the use of this trait is under TestBase, it works.
Chris@0 30 // Can be fixed as part of https://www.drupal.org/node/2260053.
Chris@17 31 $this->fail(new FormattableMarkup('No schema for @config_name', ['@config_name' => $config_name]));
Chris@0 32 return;
Chris@0 33 }
Chris@0 34 elseif ($errors === TRUE) {
Chris@0 35 // @todo Since the use of this trait is under TestBase, it works.
Chris@0 36 // Can be fixed as part of https://www.drupal.org/node/2260053.
Chris@17 37 $this->pass(new FormattableMarkup('Schema found for @config_name and values comply with schema.', ['@config_name' => $config_name]));
Chris@0 38 }
Chris@0 39 else {
Chris@0 40 foreach ($errors as $key => $error) {
Chris@0 41 // @todo Since the use of this trait is under TestBase, it works.
Chris@0 42 // Can be fixed as part of https://www.drupal.org/node/2260053.
Chris@17 43 $this->fail(new FormattableMarkup('Schema key @key failed with: @error', ['@key' => $key, '@error' => $error]));
Chris@0 44 }
Chris@0 45 }
Chris@0 46 }
Chris@0 47
Chris@0 48 /**
Chris@0 49 * Asserts configuration, specified by name, has a valid schema.
Chris@0 50 *
Chris@0 51 * @param string $config_name
Chris@0 52 * The configuration name.
Chris@0 53 */
Chris@0 54 public function assertConfigSchemaByName($config_name) {
Chris@0 55 $config = $this->config($config_name);
Chris@0 56 $this->assertConfigSchema(\Drupal::service('config.typed'), $config->getName(), $config->get());
Chris@0 57 }
Chris@0 58
Chris@0 59 }