SchemaCheckTestTrait.php 2.1 KB

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