b399990af3f6064d4a7441b40d3068a3f9db7e36
[yaffs-website] / Core / Config / SchemaConfigListenerTestTrait.php
1 <?php
2
3 namespace Drupal\Tests\Traits\Core\Config;
4
5 use Drupal\Core\Config\Schema\SchemaIncompleteException;
6
7 /**
8  * Adds a test for the configuration schema checker use in tests.
9  */
10 trait SchemaConfigListenerTestTrait {
11
12   /**
13    * Tests \Drupal\Core\Config\Development\ConfigSchemaChecker.
14    */
15   public function testConfigSchemaChecker() {
16     // Test a non-existing schema.
17     $message = 'Expected SchemaIncompleteException thrown';
18     try {
19       $this->config('config_schema_test.schemaless')->set('foo', 'bar')->save();
20       $this->fail($message);
21     }
22     catch (SchemaIncompleteException $e) {
23       $this->pass($message);
24       $this->assertEqual('No schema for config_schema_test.schemaless', $e->getMessage());
25     }
26
27     // Test a valid schema.
28     $message = 'Unexpected SchemaIncompleteException thrown';
29     $config = $this->config('config_test.types')->set('int', 10);
30     try {
31       $config->save();
32       $this->pass($message);
33     }
34     catch (SchemaIncompleteException $e) {
35       $this->fail($message);
36     }
37
38     // Test a valid schema, where the value is accessed before saving. Ensures
39     // that overridden data is correctly reset after casting.
40     $message = 'Unexpected SchemaIncompleteException thrown';
41     $config = $this->config('config_test.types')->set('int', '10');
42     $config->get('int');
43     try {
44       $config->save();
45       $this->pass($message);
46     }
47     catch (SchemaIncompleteException $e) {
48       $this->fail($message);
49     }
50
51     // Test an invalid schema.
52     $message = 'Expected SchemaIncompleteException thrown';
53     $config = $this->config('config_test.types')
54       ->set('foo', 'bar')
55       ->set('array', 1);
56     try {
57       $config->save();
58       $this->fail($message);
59     }
60     catch (SchemaIncompleteException $e) {
61       $this->pass($message);
62       $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());
63     }
64
65   }
66
67 }