You are here

trait SchemaCheckTestTrait in Drupal 8

Same name in this branch
  1. 8 core/tests/Drupal/Tests/SchemaCheckTestTrait.php \Drupal\Tests\SchemaCheckTestTrait
  2. 8 core/modules/config/src/Tests/SchemaCheckTestTrait.php \Drupal\config\Tests\SchemaCheckTestTrait
Same name and namespace in other branches
  1. 9 core/tests/Drupal/Tests/SchemaCheckTestTrait.php \Drupal\Tests\SchemaCheckTestTrait

Provides a class for checking configuration schema.

Hierarchy

40 files declare their use of SchemaCheckTestTrait
BlockConfigSchemaTest.php in core/modules/block/tests/src/Kernel/BlockConfigSchemaTest.php
ConfigImportAllTest.php in core/modules/config/tests/src/Functional/ConfigImportAllTest.php
DateTimeSchemaTest.php in core/modules/datetime/tests/src/Kernel/Views/DateTimeSchemaTest.php
DefaultConfigTest.php in core/tests/Drupal/KernelTests/Core/Config/DefaultConfigTest.php
EntityReferenceFieldDefaultValueTest.php in core/modules/field/tests/src/Functional/EntityReference/EntityReferenceFieldDefaultValueTest.php

... See full list

File

core/tests/Drupal/Tests/SchemaCheckTestTrait.php, line 12

Namespace

Drupal\Tests
View source
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://www.drupal.org/node/2260053.
      $this
        ->fail(new FormattableMarkup('No schema for @config_name', [
        '@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://www.drupal.org/node/2260053.
      $this
        ->pass(new FormattableMarkup('Schema found for @config_name and values comply with schema.', [
        '@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://www.drupal.org/node/2260053.
        $this
          ->fail(new FormattableMarkup('Schema key @key failed with: @error', [
          '@key' => $key,
          '@error' => $error,
        ]));
      }
    }
  }

  /**
   * Asserts configuration, specified by name, has a valid schema.
   *
   * @param string $config_name
   *   The configuration name.
   */
  public function assertConfigSchemaByName($config_name) {
    $config = $this
      ->config($config_name);
    $this
      ->assertConfigSchema(\Drupal::service('config.typed'), $config
      ->getName(), $config
      ->get());
  }

}

Members

Namesort descending Modifiers Type Description Overrides
SchemaCheckTestTrait::assertConfigSchema public function Asserts the TypedConfigManager has a valid schema for the configuration.
SchemaCheckTestTrait::assertConfigSchemaByName public function Asserts configuration, specified by name, has a valid schema.
SchemaCheckTrait::$configName protected property The configuration object name under test.
SchemaCheckTrait::$schema protected property The config schema wrapper object for the configuration object under test.
SchemaCheckTrait::checkConfigSchema public function Checks the TypedConfigManager has a valid schema for the configuration.
SchemaCheckTrait::checkValue protected function Helper method to check data type.