SchemaConfigListenerWebTest.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. namespace Drupal\Tests\config\Functional;
  3. use Drupal\Core\Config\Schema\SchemaIncompleteException;
  4. use Drupal\Tests\BrowserTestBase;
  5. /**
  6. * Tests the functionality of ConfigSchemaChecker in WebTestBase tests.
  7. *
  8. * @group config
  9. */
  10. class SchemaConfigListenerWebTest extends BrowserTestBase {
  11. /**
  12. * {@inheritdoc}
  13. */
  14. public static $modules = ['config_test'];
  15. /**
  16. * {@inheritdoc}
  17. */
  18. protected $defaultTheme = 'stark';
  19. /**
  20. * Tests \Drupal\Core\Config\Development\ConfigSchemaChecker.
  21. */
  22. public function testConfigSchemaChecker() {
  23. $this->drupalLogin($this->drupalCreateUser(['administer site configuration']));
  24. // Test a non-existing schema.
  25. try {
  26. $this->config('config_schema_test.schemaless')->set('foo', 'bar')->save();
  27. $this->fail('Expected SchemaIncompleteException thrown');
  28. }
  29. catch (SchemaIncompleteException $e) {
  30. $this->assertEquals('No schema for config_schema_test.schemaless', $e->getMessage());
  31. }
  32. // Test a valid schema.
  33. $config = $this->config('config_test.types')->set('int', 10);
  34. try {
  35. $config->save();
  36. }
  37. catch (SchemaIncompleteException $e) {
  38. $this->fail('Unexpected SchemaIncompleteException thrown');
  39. }
  40. // Test an invalid schema.
  41. $config = $this->config('config_test.types')
  42. ->set('foo', 'bar')
  43. ->set('array', 1);
  44. try {
  45. $config->save();
  46. $this->fail('Expected SchemaIncompleteException thrown');
  47. }
  48. catch (SchemaIncompleteException $e) {
  49. $this->assertEquals('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());
  50. }
  51. // Test that the config event listener is working in the child site.
  52. $this->drupalGet('config_test/schema_listener');
  53. $this->assertText('No schema for config_schema_test.schemaless');
  54. }
  55. }