SchemaConfigListenerTestTrait.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. namespace Drupal\Tests\Traits\Core\Config;
  3. use Drupal\Core\Config\Schema\SchemaIncompleteException;
  4. /**
  5. * Adds a test for the configuration schema checker use in tests.
  6. */
  7. trait SchemaConfigListenerTestTrait {
  8. /**
  9. * Tests \Drupal\Core\Config\Development\ConfigSchemaChecker.
  10. */
  11. public function testConfigSchemaChecker() {
  12. // Test a non-existing schema.
  13. $message = 'Expected SchemaIncompleteException thrown';
  14. try {
  15. $this->config('config_schema_test.schemaless')->set('foo', 'bar')->save();
  16. $this->fail($message);
  17. }
  18. catch (SchemaIncompleteException $e) {
  19. $this->pass($message);
  20. $this->assertEqual('No schema for config_schema_test.schemaless', $e->getMessage());
  21. }
  22. // Test a valid schema.
  23. $message = 'Unexpected SchemaIncompleteException thrown';
  24. $config = $this->config('config_test.types')->set('int', 10);
  25. try {
  26. $config->save();
  27. $this->pass($message);
  28. }
  29. catch (SchemaIncompleteException $e) {
  30. $this->fail($message);
  31. }
  32. // Test an invalid schema.
  33. $message = 'Expected SchemaIncompleteException thrown';
  34. $config = $this->config('config_test.types')
  35. ->set('foo', 'bar')
  36. ->set('array', 1);
  37. try {
  38. $config->save();
  39. $this->fail($message);
  40. }
  41. catch (SchemaIncompleteException $e) {
  42. $this->pass($message);
  43. $this->assertEqual('Schema errors for config_test.types with the following errors: config_test.types:array variable type is integer but applied schema class is Drupal\Core\Config\Schema\Sequence, config_test.types:foo missing schema', $e->getMessage());
  44. }
  45. }
  46. }