ConfigSchemaTest.php 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747
  1. <?php
  2. namespace Drupal\KernelTests\Core\Config;
  3. use Drupal\Core\Config\FileStorage;
  4. use Drupal\Core\Config\InstallStorage;
  5. use Drupal\Core\Config\Schema\ConfigSchemaAlterException;
  6. use Drupal\Core\Config\Schema\Ignore;
  7. use Drupal\Core\Config\Schema\Mapping;
  8. use Drupal\Core\Config\Schema\Undefined;
  9. use Drupal\Core\TypedData\Plugin\DataType\StringData;
  10. use Drupal\Core\TypedData\Type\IntegerInterface;
  11. use Drupal\Core\TypedData\Type\StringInterface;
  12. use Drupal\KernelTests\KernelTestBase;
  13. /**
  14. * Tests schema for configuration objects.
  15. *
  16. * @group config
  17. */
  18. class ConfigSchemaTest extends KernelTestBase {
  19. /**
  20. * Modules to enable.
  21. *
  22. * @var array
  23. */
  24. public static $modules = ['system', 'language', 'field', 'image', 'config_test', 'config_schema_test'];
  25. /**
  26. * {@inheritdoc}
  27. */
  28. protected function setUp() {
  29. parent::setUp();
  30. $this->installConfig(['system', 'image', 'config_schema_test']);
  31. }
  32. /**
  33. * Tests the basic metadata retrieval layer.
  34. */
  35. public function testSchemaMapping() {
  36. // Nonexistent configuration key will have Undefined as metadata.
  37. $this->assertSame(FALSE, \Drupal::service('config.typed')->hasConfigSchema('config_schema_test.no_such_key'));
  38. $definition = \Drupal::service('config.typed')->getDefinition('config_schema_test.no_such_key');
  39. $expected = [];
  40. $expected['label'] = 'Undefined';
  41. $expected['class'] = Undefined::class;
  42. $expected['type'] = 'undefined';
  43. $expected['definition_class'] = '\Drupal\Core\TypedData\DataDefinition';
  44. $expected['unwrap_for_canonical_representation'] = TRUE;
  45. $this->assertEqual($definition, $expected, 'Retrieved the right metadata for nonexistent configuration.');
  46. // Configuration file without schema will return Undefined as well.
  47. $this->assertSame(FALSE, \Drupal::service('config.typed')->hasConfigSchema('config_schema_test.noschema'));
  48. $definition = \Drupal::service('config.typed')->getDefinition('config_schema_test.noschema');
  49. $this->assertEqual($definition, $expected, 'Retrieved the right metadata for configuration with no schema.');
  50. // Configuration file with only some schema.
  51. $this->assertSame(TRUE, \Drupal::service('config.typed')->hasConfigSchema('config_schema_test.someschema'));
  52. $definition = \Drupal::service('config.typed')->getDefinition('config_schema_test.someschema');
  53. $expected = [];
  54. $expected['label'] = 'Schema test data';
  55. $expected['class'] = Mapping::class;
  56. $expected['mapping']['langcode']['type'] = 'string';
  57. $expected['mapping']['langcode']['label'] = 'Language code';
  58. $expected['mapping']['_core']['type'] = '_core_config_info';
  59. $expected['mapping']['testitem'] = ['label' => 'Test item'];
  60. $expected['mapping']['testlist'] = ['label' => 'Test list'];
  61. $expected['type'] = 'config_schema_test.someschema';
  62. $expected['definition_class'] = '\Drupal\Core\TypedData\MapDataDefinition';
  63. $expected['unwrap_for_canonical_representation'] = TRUE;
  64. $this->assertEqual($definition, $expected, 'Retrieved the right metadata for configuration with only some schema.');
  65. // Check type detection on elements with undefined types.
  66. $config = \Drupal::service('config.typed')->get('config_schema_test.someschema');
  67. $definition = $config->get('testitem')->getDataDefinition()->toArray();
  68. $expected = [];
  69. $expected['label'] = 'Test item';
  70. $expected['class'] = Undefined::class;
  71. $expected['type'] = 'undefined';
  72. $expected['definition_class'] = '\Drupal\Core\TypedData\DataDefinition';
  73. $expected['unwrap_for_canonical_representation'] = TRUE;
  74. $this->assertEqual($definition, $expected, 'Automatic type detected for a scalar is undefined.');
  75. $definition = $config->get('testlist')->getDataDefinition()->toArray();
  76. $expected = [];
  77. $expected['label'] = 'Test list';
  78. $expected['class'] = Undefined::class;
  79. $expected['type'] = 'undefined';
  80. $expected['definition_class'] = '\Drupal\Core\TypedData\DataDefinition';
  81. $expected['unwrap_for_canonical_representation'] = TRUE;
  82. $this->assertEqual($definition, $expected, 'Automatic type detected for a list is undefined.');
  83. $definition = $config->get('testnoschema')->getDataDefinition()->toArray();
  84. $expected = [];
  85. $expected['label'] = 'Undefined';
  86. $expected['class'] = Undefined::class;
  87. $expected['type'] = 'undefined';
  88. $expected['definition_class'] = '\Drupal\Core\TypedData\DataDefinition';
  89. $expected['unwrap_for_canonical_representation'] = TRUE;
  90. $this->assertEqual($definition, $expected, 'Automatic type detected for an undefined integer is undefined.');
  91. // Simple case, straight metadata.
  92. $definition = \Drupal::service('config.typed')->getDefinition('system.maintenance');
  93. $expected = [];
  94. $expected['label'] = 'Maintenance mode';
  95. $expected['class'] = Mapping::class;
  96. $expected['mapping']['message'] = [
  97. 'label' => 'Message to display when in maintenance mode',
  98. 'type' => 'text',
  99. ];
  100. $expected['mapping']['langcode'] = [
  101. 'label' => 'Language code',
  102. 'type' => 'string',
  103. ];
  104. $expected['mapping']['_core']['type'] = '_core_config_info';
  105. $expected['type'] = 'system.maintenance';
  106. $expected['definition_class'] = '\Drupal\Core\TypedData\MapDataDefinition';
  107. $expected['unwrap_for_canonical_representation'] = TRUE;
  108. $this->assertEqual($definition, $expected, 'Retrieved the right metadata for system.maintenance');
  109. // Mixed schema with ignore elements.
  110. $definition = \Drupal::service('config.typed')->getDefinition('config_schema_test.ignore');
  111. $expected = [];
  112. $expected['label'] = 'Ignore test';
  113. $expected['class'] = Mapping::class;
  114. $expected['definition_class'] = '\Drupal\Core\TypedData\MapDataDefinition';
  115. $expected['mapping']['langcode'] = [
  116. 'type' => 'string',
  117. 'label' => 'Language code',
  118. ];
  119. $expected['mapping']['_core']['type'] = '_core_config_info';
  120. $expected['mapping']['label'] = [
  121. 'label' => 'Label',
  122. 'type' => 'label',
  123. ];
  124. $expected['mapping']['irrelevant'] = [
  125. 'label' => 'Irrelevant',
  126. 'type' => 'ignore',
  127. ];
  128. $expected['mapping']['indescribable'] = [
  129. 'label' => 'Indescribable',
  130. 'type' => 'ignore',
  131. ];
  132. $expected['mapping']['weight'] = [
  133. 'label' => 'Weight',
  134. 'type' => 'integer',
  135. ];
  136. $expected['type'] = 'config_schema_test.ignore';
  137. $expected['unwrap_for_canonical_representation'] = TRUE;
  138. $this->assertEqual($definition, $expected);
  139. // The ignore elements themselves.
  140. $definition = \Drupal::service('config.typed')->get('config_schema_test.ignore')->get('irrelevant')->getDataDefinition()->toArray();
  141. $expected = [];
  142. $expected['type'] = 'ignore';
  143. $expected['label'] = 'Irrelevant';
  144. $expected['class'] = Ignore::class;
  145. $expected['definition_class'] = '\Drupal\Core\TypedData\DataDefinition';
  146. $expected['unwrap_for_canonical_representation'] = TRUE;
  147. $this->assertEqual($definition, $expected);
  148. $definition = \Drupal::service('config.typed')->get('config_schema_test.ignore')->get('indescribable')->getDataDefinition()->toArray();
  149. $expected['label'] = 'Indescribable';
  150. $this->assertEqual($definition, $expected);
  151. // More complex case, generic type. Metadata for image style.
  152. $definition = \Drupal::service('config.typed')->getDefinition('image.style.large');
  153. $expected = [];
  154. $expected['label'] = 'Image style';
  155. $expected['class'] = Mapping::class;
  156. $expected['definition_class'] = '\Drupal\Core\TypedData\MapDataDefinition';
  157. $expected['unwrap_for_canonical_representation'] = TRUE;
  158. $expected['mapping']['name']['type'] = 'string';
  159. $expected['mapping']['uuid']['type'] = 'uuid';
  160. $expected['mapping']['uuid']['label'] = 'UUID';
  161. $expected['mapping']['langcode']['type'] = 'string';
  162. $expected['mapping']['langcode']['label'] = 'Language code';
  163. $expected['mapping']['status']['type'] = 'boolean';
  164. $expected['mapping']['status']['label'] = 'Status';
  165. $expected['mapping']['dependencies']['type'] = 'config_dependencies';
  166. $expected['mapping']['dependencies']['label'] = 'Dependencies';
  167. $expected['mapping']['name']['type'] = 'string';
  168. $expected['mapping']['label']['type'] = 'label';
  169. $expected['mapping']['label']['label'] = 'Label';
  170. $expected['mapping']['effects']['type'] = 'sequence';
  171. $expected['mapping']['effects']['sequence']['type'] = 'mapping';
  172. $expected['mapping']['effects']['sequence']['mapping']['id']['type'] = 'string';
  173. $expected['mapping']['effects']['sequence']['mapping']['data']['type'] = 'image.effect.[%parent.id]';
  174. $expected['mapping']['effects']['sequence']['mapping']['weight']['type'] = 'integer';
  175. $expected['mapping']['effects']['sequence']['mapping']['uuid']['type'] = 'uuid';
  176. $expected['mapping']['third_party_settings']['type'] = 'sequence';
  177. $expected['mapping']['third_party_settings']['label'] = 'Third party settings';
  178. $expected['mapping']['third_party_settings']['sequence']['type'] = '[%parent.%parent.%type].third_party.[%key]';
  179. $expected['mapping']['_core']['type'] = '_core_config_info';
  180. $expected['type'] = 'image.style.*';
  181. $this->assertEqual($definition, $expected);
  182. // More complex, type based on a complex one.
  183. $definition = \Drupal::service('config.typed')->getDefinition('image.effect.image_scale');
  184. // This should be the schema for image.effect.image_scale.
  185. $expected = [];
  186. $expected['label'] = 'Image scale';
  187. $expected['class'] = Mapping::class;
  188. $expected['definition_class'] = '\Drupal\Core\TypedData\MapDataDefinition';
  189. $expected['unwrap_for_canonical_representation'] = TRUE;
  190. $expected['mapping']['width']['type'] = 'integer';
  191. $expected['mapping']['width']['label'] = 'Width';
  192. $expected['mapping']['height']['type'] = 'integer';
  193. $expected['mapping']['height']['label'] = 'Height';
  194. $expected['mapping']['upscale']['type'] = 'boolean';
  195. $expected['mapping']['upscale']['label'] = 'Upscale';
  196. $expected['type'] = 'image.effect.image_scale';
  197. $this->assertEqual($definition, $expected, 'Retrieved the right metadata for image.effect.image_scale');
  198. // Most complex case, get metadata for actual configuration element.
  199. $effects = \Drupal::service('config.typed')->get('image.style.medium')->get('effects');
  200. $definition = $effects->get('bddf0d06-42f9-4c75-a700-a33cafa25ea0')->get('data')->getDataDefinition()->toArray();
  201. // This should be the schema for image.effect.image_scale, reuse previous one.
  202. $expected['type'] = 'image.effect.image_scale';
  203. $this->assertEqual($definition, $expected, 'Retrieved the right metadata for the first effect of image.style.medium');
  204. $a = \Drupal::config('config_test.dynamic.third_party');
  205. $test = \Drupal::service('config.typed')->get('config_test.dynamic.third_party')->get('third_party_settings.config_schema_test');
  206. $definition = $test->getDataDefinition()->toArray();
  207. $expected = [];
  208. $expected['type'] = 'config_test.dynamic.*.third_party.config_schema_test';
  209. $expected['label'] = 'Mapping';
  210. $expected['class'] = Mapping::class;
  211. $expected['definition_class'] = '\Drupal\Core\TypedData\MapDataDefinition';
  212. $expected['unwrap_for_canonical_representation'] = TRUE;
  213. $expected['mapping'] = [
  214. 'integer' => ['type' => 'integer'],
  215. 'string' => ['type' => 'string'],
  216. ];
  217. $this->assertEqual($definition, $expected, 'Retrieved the right metadata for config_test.dynamic.third_party:third_party_settings.config_schema_test');
  218. // More complex, several level deep test.
  219. $definition = \Drupal::service('config.typed')->getDefinition('config_schema_test.someschema.somemodule.section_one.subsection');
  220. // This should be the schema of config_schema_test.someschema.somemodule.*.*.
  221. $expected = [];
  222. $expected['label'] = 'Schema multiple filesystem marker test';
  223. $expected['class'] = Mapping::class;
  224. $expected['mapping']['langcode']['type'] = 'string';
  225. $expected['mapping']['langcode']['label'] = 'Language code';
  226. $expected['mapping']['_core']['type'] = '_core_config_info';
  227. $expected['mapping']['testid']['type'] = 'string';
  228. $expected['mapping']['testid']['label'] = 'ID';
  229. $expected['mapping']['testdescription']['type'] = 'text';
  230. $expected['mapping']['testdescription']['label'] = 'Description';
  231. $expected['type'] = 'config_schema_test.someschema.somemodule.*.*';
  232. $expected['definition_class'] = '\Drupal\Core\TypedData\MapDataDefinition';
  233. $expected['unwrap_for_canonical_representation'] = TRUE;
  234. $this->assertEqual($definition, $expected, 'Retrieved the right metadata for config_schema_test.someschema.somemodule.section_one.subsection');
  235. $definition = \Drupal::service('config.typed')->getDefinition('config_schema_test.someschema.somemodule.section_two.subsection');
  236. // The other file should have the same schema.
  237. $this->assertEqual($definition, $expected, 'Retrieved the right metadata for config_schema_test.someschema.somemodule.section_two.subsection');
  238. }
  239. /**
  240. * Tests metadata retrieval with several levels of %parent indirection.
  241. */
  242. public function testSchemaMappingWithParents() {
  243. $config_data = \Drupal::service('config.typed')->get('config_schema_test.someschema.with_parents');
  244. // Test fetching parent one level up.
  245. $entry = $config_data->get('one_level');
  246. $definition = $entry->get('testitem')->getDataDefinition()->toArray();
  247. $expected = [
  248. 'type' => 'config_schema_test.someschema.with_parents.key_1',
  249. 'label' => 'Test item nested one level',
  250. 'class' => StringData::class,
  251. 'definition_class' => '\Drupal\Core\TypedData\DataDefinition',
  252. 'unwrap_for_canonical_representation' => TRUE,
  253. ];
  254. $this->assertEqual($definition, $expected);
  255. // Test fetching parent two levels up.
  256. $entry = $config_data->get('two_levels');
  257. $definition = $entry->get('wrapper')->get('testitem')->getDataDefinition()->toArray();
  258. $expected = [
  259. 'type' => 'config_schema_test.someschema.with_parents.key_2',
  260. 'label' => 'Test item nested two levels',
  261. 'class' => StringData::class,
  262. 'definition_class' => '\Drupal\Core\TypedData\DataDefinition',
  263. 'unwrap_for_canonical_representation' => TRUE,
  264. ];
  265. $this->assertEqual($definition, $expected);
  266. // Test fetching parent three levels up.
  267. $entry = $config_data->get('three_levels');
  268. $definition = $entry->get('wrapper_1')->get('wrapper_2')->get('testitem')->getDataDefinition()->toArray();
  269. $expected = [
  270. 'type' => 'config_schema_test.someschema.with_parents.key_3',
  271. 'label' => 'Test item nested three levels',
  272. 'class' => StringData::class,
  273. 'definition_class' => '\Drupal\Core\TypedData\DataDefinition',
  274. 'unwrap_for_canonical_representation' => TRUE,
  275. ];
  276. $this->assertEqual($definition, $expected);
  277. }
  278. /**
  279. * Tests metadata applied to configuration objects.
  280. */
  281. public function testSchemaData() {
  282. // Try a simple property.
  283. $meta = \Drupal::service('config.typed')->get('system.site');
  284. $property = $meta->get('page')->get('front');
  285. $this->assertTrue($property instanceof StringInterface, 'Got the right wrapper fo the page.front property.');
  286. $this->assertEqual($property->getValue(), '/user/login', 'Got the right value for page.front data.');
  287. $definition = $property->getDataDefinition();
  288. $this->assertTrue(empty($definition['translatable']), 'Got the right translatability setting for page.front data.');
  289. // Check nested array of properties.
  290. $list = $meta->get('page')->getElements();
  291. $this->assertEqual(count($list), 3, 'Got a list with the right number of properties for site page data');
  292. $this->assertTrue(isset($list['front']) && isset($list['403']) && isset($list['404']), 'Got a list with the right properties for site page data.');
  293. $this->assertEqual($list['front']->getValue(), '/user/login', 'Got the right value for page.front data from the list.');
  294. // And test some TypedConfigInterface methods.
  295. $properties = $list;
  296. $this->assertTrue(count($properties) == 3 && $properties['front'] == $list['front'], 'Got the right properties for site page.');
  297. $values = $meta->get('page')->toArray();
  298. $this->assertTrue(count($values) == 3 && $values['front'] == '/user/login', 'Got the right property values for site page.');
  299. // Now let's try something more complex, with nested objects.
  300. $wrapper = \Drupal::service('config.typed')->get('image.style.large');
  301. $effects = $wrapper->get('effects');
  302. $this->assertTrue(count($effects->toArray()) == 1, 'Got an array with effects for image.style.large data');
  303. $uuid = key($effects->getValue());
  304. $effect = $effects->get($uuid)->getElements();
  305. $this->assertTrue(!$effect['data']->isEmpty() && $effect['id']->getValue() == 'image_scale', 'Got data for the image scale effect from metadata.');
  306. $this->assertTrue($effect['data']->get('width') instanceof IntegerInterface, 'Got the right type for the scale effect width.');
  307. $this->assertEqual($effect['data']->get('width')->getValue(), 480, 'Got the right value for the scale effect width.');
  308. }
  309. /**
  310. * Test configuration value data type enforcement using schemas.
  311. */
  312. public function testConfigSaveWithSchema() {
  313. $untyped_values = [
  314. 'string' => 1,
  315. 'empty_string' => '',
  316. 'null_string' => NULL,
  317. 'integer' => '100',
  318. 'null_integer' => '',
  319. 'boolean' => 1,
  320. // If the config schema doesn't have a type it shouldn't be casted.
  321. 'no_type' => 1,
  322. 'mapping' => [
  323. 'string' => 1
  324. ],
  325. 'float' => '3.14',
  326. 'null_float' => '',
  327. 'sequence' => [1, 0, 1],
  328. 'sequence_bc' => [1, 0, 1],
  329. // Not in schema and therefore should be left untouched.
  330. 'not_present_in_schema' => TRUE,
  331. // Test a custom type.
  332. 'config_schema_test_integer' => '1',
  333. 'config_schema_test_integer_empty_string' => '',
  334. ];
  335. $untyped_to_typed = $untyped_values;
  336. $typed_values = [
  337. 'string' => '1',
  338. 'empty_string' => '',
  339. 'null_string' => NULL,
  340. 'integer' => 100,
  341. 'null_integer' => NULL,
  342. 'boolean' => TRUE,
  343. 'no_type' => 1,
  344. 'mapping' => [
  345. 'string' => '1'
  346. ],
  347. 'float' => 3.14,
  348. 'null_float' => NULL,
  349. 'sequence' => [TRUE, FALSE, TRUE],
  350. 'sequence_bc' => [TRUE, FALSE, TRUE],
  351. 'not_present_in_schema' => TRUE,
  352. 'config_schema_test_integer' => 1,
  353. 'config_schema_test_integer_empty_string' => NULL,
  354. ];
  355. // Save config which has a schema that enforces types.
  356. $this->config('config_schema_test.schema_data_types')
  357. ->setData($untyped_to_typed)
  358. ->save();
  359. $this->assertIdentical($this->config('config_schema_test.schema_data_types')->get(), $typed_values);
  360. // Save config which does not have a schema that enforces types.
  361. $this->config('config_schema_test.no_schema_data_types')
  362. ->setData($untyped_values)
  363. ->save();
  364. $this->assertIdentical($this->config('config_schema_test.no_schema_data_types')->get(), $untyped_values);
  365. // Ensure that configuration objects with keys marked as ignored are not
  366. // changed when saved. The 'config_schema_test.ignore' will have been saved
  367. // during the installation of configuration in the setUp method.
  368. $extension_path = __DIR__ . '/../../../../../modules/config/tests/config_schema_test/';
  369. $install_storage = new FileStorage($extension_path . InstallStorage::CONFIG_INSTALL_DIRECTORY);
  370. $original_data = $install_storage->read('config_schema_test.ignore');
  371. $installed_data = $this->config('config_schema_test.ignore')->get();
  372. unset($installed_data['_core']);
  373. $this->assertIdentical($installed_data, $original_data);
  374. }
  375. /**
  376. * Tests configuration sequence sorting using schemas.
  377. */
  378. public function testConfigSaveWithSequenceSorting() {
  379. $data = [
  380. 'keyed_sort' => [
  381. 'b' => '1',
  382. 'a' => '2',
  383. ],
  384. 'no_sort' => [
  385. 'b' => '2',
  386. 'a' => '1',
  387. ],
  388. ];
  389. // Save config which has a schema that enforces sorting.
  390. $this->config('config_schema_test.schema_sequence_sort')
  391. ->setData($data)
  392. ->save();
  393. $this->assertSame(['a' => '2', 'b' => '1'], $this->config('config_schema_test.schema_sequence_sort')->get('keyed_sort'));
  394. $this->assertSame(['b' => '2', 'a' => '1'], $this->config('config_schema_test.schema_sequence_sort')->get('no_sort'));
  395. $data = [
  396. 'value_sort' => ['b', 'a'],
  397. 'no_sort' => ['b', 'a'],
  398. ];
  399. // Save config which has a schema that enforces sorting.
  400. $this->config('config_schema_test.schema_sequence_sort')
  401. ->setData($data)
  402. ->save();
  403. $this->assertSame(['a', 'b'], $this->config('config_schema_test.schema_sequence_sort')->get('value_sort'));
  404. $this->assertSame(['b', 'a'], $this->config('config_schema_test.schema_sequence_sort')->get('no_sort'));
  405. // Value sort does not preserve keys - this is intentional.
  406. $data = [
  407. 'value_sort' => [1 => 'b', 2 => 'a'],
  408. 'no_sort' => [1 => 'b', 2 => 'a'],
  409. ];
  410. // Save config which has a schema that enforces sorting.
  411. $this->config('config_schema_test.schema_sequence_sort')
  412. ->setData($data)
  413. ->save();
  414. $this->assertSame(['a', 'b'], $this->config('config_schema_test.schema_sequence_sort')->get('value_sort'));
  415. $this->assertSame([1 => 'b', 2 => 'a'], $this->config('config_schema_test.schema_sequence_sort')->get('no_sort'));
  416. // Test sorts do not destroy complex values.
  417. $data = [
  418. 'complex_sort_value' => [['foo' => 'b', 'bar' => 'b'] , ['foo' => 'a', 'bar' => 'a']],
  419. 'complex_sort_key' => ['b' => ['foo' => '1', 'bar' => '1'] , 'a' => ['foo' => '2', 'bar' => '2']],
  420. ];
  421. $this->config('config_schema_test.schema_sequence_sort')
  422. ->setData($data)
  423. ->save();
  424. $this->assertSame([['foo' => 'a', 'bar' => 'a'], ['foo' => 'b', 'bar' => 'b']], $this->config('config_schema_test.schema_sequence_sort')->get('complex_sort_value'));
  425. $this->assertSame(['a' => ['foo' => '2', 'bar' => '2'], 'b' => ['foo' => '1', 'bar' => '1']], $this->config('config_schema_test.schema_sequence_sort')->get('complex_sort_key'));
  426. // Swap the previous test scenario around.
  427. $data = [
  428. 'complex_sort_value' => ['b' => ['foo' => '1', 'bar' => '1'] , 'a' => ['foo' => '2', 'bar' => '2']],
  429. 'complex_sort_key' => [['foo' => 'b', 'bar' => 'b'] , ['foo' => 'a', 'bar' => 'a']],
  430. ];
  431. $this->config('config_schema_test.schema_sequence_sort')
  432. ->setData($data)
  433. ->save();
  434. $this->assertSame([['foo' => '1', 'bar' => '1'], ['foo' => '2', 'bar' => '2']], $this->config('config_schema_test.schema_sequence_sort')->get('complex_sort_value'));
  435. $this->assertSame([['foo' => 'b', 'bar' => 'b'], ['foo' => 'a', 'bar' => 'a']], $this->config('config_schema_test.schema_sequence_sort')->get('complex_sort_key'));
  436. }
  437. /**
  438. * Tests fallback to a greedy wildcard.
  439. */
  440. public function testSchemaFallback() {
  441. $definition = \Drupal::service('config.typed')->getDefinition('config_schema_test.wildcard_fallback.something');
  442. // This should be the schema of config_schema_test.wildcard_fallback.*.
  443. $expected = [];
  444. $expected['label'] = 'Schema wildcard fallback test';
  445. $expected['class'] = Mapping::class;
  446. $expected['definition_class'] = '\Drupal\Core\TypedData\MapDataDefinition';
  447. $expected['unwrap_for_canonical_representation'] = TRUE;
  448. $expected['mapping']['langcode']['type'] = 'string';
  449. $expected['mapping']['langcode']['label'] = 'Language code';
  450. $expected['mapping']['_core']['type'] = '_core_config_info';
  451. $expected['mapping']['testid']['type'] = 'string';
  452. $expected['mapping']['testid']['label'] = 'ID';
  453. $expected['mapping']['testdescription']['type'] = 'text';
  454. $expected['mapping']['testdescription']['label'] = 'Description';
  455. $expected['type'] = 'config_schema_test.wildcard_fallback.*';
  456. $this->assertEqual($definition, $expected, 'Retrieved the right metadata for config_schema_test.wildcard_fallback.something');
  457. $definition2 = \Drupal::service('config.typed')->getDefinition('config_schema_test.wildcard_fallback.something.something');
  458. // This should be the schema of config_schema_test.wildcard_fallback.* as
  459. // well.
  460. $this->assertSame($definition, $definition2);
  461. }
  462. /**
  463. * Tests use of colons in schema type determination.
  464. *
  465. * @see \Drupal\Core\Config\TypedConfigManager::getFallbackName()
  466. */
  467. public function testColonsInSchemaTypeDetermination() {
  468. $tests = \Drupal::service('config.typed')->get('config_schema_test.plugin_types')->get('tests')->getElements();
  469. $definition = $tests[0]->getDataDefinition()->toArray();
  470. $this->assertEqual($definition['type'], 'test.plugin_types.boolean');
  471. $definition = $tests[1]->getDataDefinition()->toArray();
  472. $this->assertEqual($definition['type'], 'test.plugin_types.boolean:*');
  473. $definition = $tests[2]->getDataDefinition()->toArray();
  474. $this->assertEqual($definition['type'], 'test.plugin_types.*');
  475. $definition = $tests[3]->getDataDefinition()->toArray();
  476. $this->assertEqual($definition['type'], 'test.plugin_types.*');
  477. $tests = \Drupal::service('config.typed')->get('config_schema_test.plugin_types')->get('test_with_parents')->getElements();
  478. $definition = $tests[0]->get('settings')->getDataDefinition()->toArray();
  479. $this->assertEqual($definition['type'], 'test_with_parents.plugin_types.boolean');
  480. $definition = $tests[1]->get('settings')->getDataDefinition()->toArray();
  481. $this->assertEqual($definition['type'], 'test_with_parents.plugin_types.boolean:*');
  482. $definition = $tests[2]->get('settings')->getDataDefinition()->toArray();
  483. $this->assertEqual($definition['type'], 'test_with_parents.plugin_types.*');
  484. $definition = $tests[3]->get('settings')->getDataDefinition()->toArray();
  485. $this->assertEqual($definition['type'], 'test_with_parents.plugin_types.*');
  486. }
  487. /**
  488. * Tests hook_config_schema_info_alter().
  489. */
  490. public function testConfigSchemaInfoAlter() {
  491. /** @var \Drupal\Core\Config\TypedConfigManagerInterface $typed_config */
  492. $typed_config = \Drupal::service('config.typed');
  493. $typed_config->clearCachedDefinitions();
  494. // Ensure that keys can not be added or removed by
  495. // hook_config_schema_info_alter().
  496. \Drupal::state()->set('config_schema_test_exception_remove', TRUE);
  497. $message = 'Expected ConfigSchemaAlterException thrown.';
  498. try {
  499. $typed_config->getDefinitions();
  500. $this->fail($message);
  501. }
  502. catch (ConfigSchemaAlterException $e) {
  503. $this->pass($message);
  504. $this->assertEqual($e->getMessage(), 'Invoking hook_config_schema_info_alter() has removed (config_schema_test.hook) schema definitions');
  505. }
  506. \Drupal::state()->set('config_schema_test_exception_add', TRUE);
  507. $message = 'Expected ConfigSchemaAlterException thrown.';
  508. try {
  509. $typed_config->getDefinitions();
  510. $this->fail($message);
  511. }
  512. catch (ConfigSchemaAlterException $e) {
  513. $this->pass($message);
  514. $this->assertEqual($e->getMessage(), 'Invoking hook_config_schema_info_alter() has added (config_schema_test.hook_added_defintion) and removed (config_schema_test.hook) schema definitions');
  515. }
  516. \Drupal::state()->set('config_schema_test_exception_remove', FALSE);
  517. $message = 'Expected ConfigSchemaAlterException thrown.';
  518. try {
  519. $typed_config->getDefinitions();
  520. $this->fail($message);
  521. }
  522. catch (ConfigSchemaAlterException $e) {
  523. $this->pass($message);
  524. $this->assertEqual($e->getMessage(), 'Invoking hook_config_schema_info_alter() has added (config_schema_test.hook_added_defintion) schema definitions');
  525. }
  526. // Tests that hook_config_schema_info_alter() can add additional metadata to
  527. // existing configuration schema.
  528. \Drupal::state()->set('config_schema_test_exception_add', FALSE);
  529. $definitions = $typed_config->getDefinitions();
  530. $this->assertEqual($definitions['config_schema_test.hook']['additional_metadata'], 'new schema info');
  531. }
  532. /**
  533. * Tests saving config when the type is wrapped by a dynamic type.
  534. */
  535. public function testConfigSaveWithWrappingSchema() {
  536. $untyped_values = [
  537. 'tests' => [
  538. [
  539. 'wrapper_value' => 'foo',
  540. 'plugin_id' => 'wrapper:foo',
  541. 'internal_value' => 100,
  542. ],
  543. ],
  544. ];
  545. $typed_values = [
  546. 'tests' => [
  547. [
  548. 'wrapper_value' => 'foo',
  549. 'plugin_id' => 'wrapper:foo',
  550. 'internal_value' => '100',
  551. ],
  552. ],
  553. ];
  554. // Save config which has a schema that enforces types.
  555. \Drupal::configFactory()->getEditable('wrapping.config_schema_test.plugin_types')
  556. ->setData($untyped_values)
  557. ->save();
  558. $this->assertIdentical(\Drupal::config('wrapping.config_schema_test.plugin_types')
  559. ->get(), $typed_values);
  560. }
  561. /**
  562. * Tests dynamic config schema type with multiple sub-key references.
  563. */
  564. public function testConfigSaveWithWrappingSchemaDoubleBrackets() {
  565. $untyped_values = [
  566. 'tests' => [
  567. [
  568. 'wrapper_value' => 'foo',
  569. 'foo' => 'turtle',
  570. 'bar' => 'horse',
  571. // Converted to a string by 'test.double_brackets.turtle.horse'
  572. // schema.
  573. 'another_key' => '100',
  574. ],
  575. ],
  576. ];
  577. $typed_values = [
  578. 'tests' => [
  579. [
  580. 'wrapper_value' => 'foo',
  581. 'foo' => 'turtle',
  582. 'bar' => 'horse',
  583. 'another_key' => 100,
  584. ],
  585. ],
  586. ];
  587. // Save config which has a schema that enforces types.
  588. \Drupal::configFactory()->getEditable('wrapping.config_schema_test.double_brackets')
  589. ->setData($untyped_values)
  590. ->save();
  591. $this->assertIdentical(\Drupal::config('wrapping.config_schema_test.double_brackets')
  592. ->get(), $typed_values);
  593. $tests = \Drupal::service('config.typed')->get('wrapping.config_schema_test.double_brackets')->get('tests')->getElements();
  594. $definition = $tests[0]->getDataDefinition()->toArray();
  595. $this->assertEqual($definition['type'], 'wrapping.test.double_brackets.*||test.double_brackets.turtle.horse');
  596. $untyped_values = [
  597. 'tests' => [
  598. [
  599. 'wrapper_value' => 'foo',
  600. 'foo' => 'cat',
  601. 'bar' => 'dog',
  602. // Converted to a string by 'test.double_brackets.cat.dog' schema.
  603. 'another_key' => 100,
  604. ],
  605. ],
  606. ];
  607. $typed_values = [
  608. 'tests' => [
  609. [
  610. 'wrapper_value' => 'foo',
  611. 'foo' => 'cat',
  612. 'bar' => 'dog',
  613. 'another_key' => '100',
  614. ],
  615. ],
  616. ];
  617. // Save config which has a schema that enforces types.
  618. \Drupal::configFactory()->getEditable('wrapping.config_schema_test.double_brackets')
  619. ->setData($untyped_values)
  620. ->save();
  621. $this->assertIdentical(\Drupal::config('wrapping.config_schema_test.double_brackets')
  622. ->get(), $typed_values);
  623. $tests = \Drupal::service('config.typed')->get('wrapping.config_schema_test.double_brackets')->get('tests')->getElements();
  624. $definition = $tests[0]->getDataDefinition()->toArray();
  625. $this->assertEqual($definition['type'], 'wrapping.test.double_brackets.*||test.double_brackets.cat.dog');
  626. // Combine everything in a single save.
  627. $typed_values = [
  628. 'tests' => [
  629. [
  630. 'wrapper_value' => 'foo',
  631. 'foo' => 'cat',
  632. 'bar' => 'dog',
  633. 'another_key' => 100,
  634. ],
  635. [
  636. 'wrapper_value' => 'foo',
  637. 'foo' => 'turtle',
  638. 'bar' => 'horse',
  639. 'another_key' => '100',
  640. ],
  641. ],
  642. ];
  643. \Drupal::configFactory()->getEditable('wrapping.config_schema_test.double_brackets')
  644. ->setData($typed_values)
  645. ->save();
  646. $tests = \Drupal::service('config.typed')->get('wrapping.config_schema_test.double_brackets')->get('tests')->getElements();
  647. $definition = $tests[0]->getDataDefinition()->toArray();
  648. $this->assertEqual($definition['type'], 'wrapping.test.double_brackets.*||test.double_brackets.cat.dog');
  649. $definition = $tests[1]->getDataDefinition()->toArray();
  650. $this->assertEqual($definition['type'], 'wrapping.test.double_brackets.*||test.double_brackets.turtle.horse');
  651. $typed_values = [
  652. 'tests' => [
  653. [
  654. 'id' => 'cat:persion.dog',
  655. 'foo' => 'cat',
  656. 'bar' => 'dog',
  657. 'breed' => 'persion',
  658. ],
  659. ],
  660. ];
  661. \Drupal::configFactory()->getEditable('wrapping.config_schema_test.other_double_brackets')
  662. ->setData($typed_values)
  663. ->save();
  664. $tests = \Drupal::service('config.typed')->get('wrapping.config_schema_test.other_double_brackets')->get('tests')->getElements();
  665. $definition = $tests[0]->getDataDefinition()->toArray();
  666. // Check that definition type is a merge of the expected types.
  667. $this->assertEqual($definition['type'], 'wrapping.test.other_double_brackets.*||test.double_brackets.cat:*.*');
  668. // Check that breed was inherited from parent definition.
  669. $this->assertEqual($definition['mapping']['breed'], ['type' => 'string']);
  670. }
  671. }