SchemaCheckTraitTest.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. namespace Drupal\KernelTests\Core\Config;
  3. use Drupal\Core\Config\Schema\SchemaCheckTrait;
  4. use Drupal\KernelTests\KernelTestBase;
  5. /**
  6. * Tests the functionality of SchemaCheckTrait.
  7. *
  8. * @group config
  9. */
  10. class SchemaCheckTraitTest extends KernelTestBase {
  11. use SchemaCheckTrait;
  12. /**
  13. * The typed config manager.
  14. *
  15. * @var \Drupal\Core\Config\TypedConfigManagerInterface
  16. */
  17. protected $typedConfig;
  18. /**
  19. * Modules to enable.
  20. *
  21. * @var array
  22. */
  23. public static $modules = ['config_test', 'config_schema_test'];
  24. /**
  25. * {@inheritdoc}
  26. */
  27. protected function setUp() {
  28. parent::setUp();
  29. $this->installConfig(['config_test', 'config_schema_test']);
  30. $this->typedConfig = \Drupal::service('config.typed');
  31. }
  32. /**
  33. * Tests \Drupal\Core\Config\Schema\SchemaCheckTrait.
  34. */
  35. public function testTrait() {
  36. // Test a non existing schema.
  37. $ret = $this->checkConfigSchema($this->typedConfig, 'config_schema_test.noschema', $this->config('config_schema_test.noschema')->get());
  38. $this->assertIdentical($ret, FALSE);
  39. // Test an existing schema with valid data.
  40. $config_data = $this->config('config_test.types')->get();
  41. $ret = $this->checkConfigSchema($this->typedConfig, 'config_test.types', $config_data);
  42. $this->assertIdentical($ret, TRUE);
  43. // Add a new key, a new array and overwrite boolean with array to test the
  44. // error messages.
  45. $config_data = ['new_key' => 'new_value', 'new_array' => []] + $config_data;
  46. $config_data['boolean'] = [];
  47. $ret = $this->checkConfigSchema($this->typedConfig, 'config_test.types', $config_data);
  48. $expected = [
  49. 'config_test.types:new_key' => 'missing schema',
  50. 'config_test.types:new_array' => 'missing schema',
  51. 'config_test.types:boolean' => 'non-scalar value but not defined as an array (such as mapping or sequence)',
  52. ];
  53. $this->assertEqual($ret, $expected);
  54. }
  55. }