function MappingTest::providerMappingInterpretation

Same name and namespace in other branches
  1. 10 core/tests/Drupal/KernelTests/Config/Schema/MappingTest.php \Drupal\KernelTests\Config\Schema\MappingTest::providerMappingInterpretation()

Provides test cases for all kinds of i) dynamic typing, ii) optional keys.

Return value

\Generator

See also

https://www.drupal.org/files/ConfigSchemaCheatSheet2.0.pdf

File

core/tests/Drupal/KernelTests/Config/Schema/MappingTest.php, line 178

Class

MappingTest
@coversDefaultClass <a href="/api/drupal/core%21lib%21Drupal%21Core%21Config%21Schema%21Mapping.php/class/Mapping/11.x" title="Defines a mapping configuration element." class="local">\Drupal\Core\Config\Schema\Mapping</a> @group Config

Namespace

Drupal\KernelTests\Config\Schema

Code

public static function providerMappingInterpretation() : \Generator {
    $available_block_settings_types = [
        'block.settings.field_block:*:*:*' => [
            'formatter',
        ],
        'block.settings.extra_field_block:*:*:*' => [
            'formatter',
        ],
        'block.settings.system_branding_block' => [
            'use_site_logo',
            'use_site_name',
            'use_site_slogan',
        ],
        'block.settings.system_menu_block:*' => [
            'level',
            'depth',
            'expand_all_items',
        ],
        'block.settings.local_tasks_block' => [
            'primary',
            'secondary',
        ],
    ];
    // A simple config often is just a single Mapping object.
    (yield 'No dynamic type: core.extension' => [
        'core.extension',
        NULL,
        [
            // Keys inherited from `type: config_object`.
            // @see core/config/schema/core.data_types.schema.yml
'_core',
            'langcode',
            // Keys defined locally, in `type: core.extension`.
            // @see core/config/schema/core.extension.schema.yml
'module',
            'theme',
            'profile',
        ],
        [
            '_core',
            'langcode',
            'profile',
        ],
        [],
    ]);
    // Special case: deprecated  is needed for deprecated config schema:
    // - deprecated keys are treated as optional
    // - if a deprecated property path is itself a mapping, then the keys inside
    //   are not optional
    (yield 'No dynamic type: config_schema_deprecated_test.settings' => [
        'config_schema_deprecated_test.settings',
        NULL,
        [
            // Keys inherited from `type: config_object`.
            // @see core/config/schema/core.data_types.schema.yml
'_core',
            'langcode',
            // Keys defined locally, in `type: config_schema_deprecated_test.settings`.
            // @see core/modules/config/tests/config_schema_deprecated_test/config/schema/config_schema_deprecated_test.schema.yml
'complex_structure_deprecated',
        ],
        [
            '_core',
            'langcode',
            'complex_structure_deprecated',
        ],
        [],
    ]);
    (yield 'No dynamic type: config_schema_deprecated_test.settings:complex_structure_deprecated' => [
        'config_schema_deprecated_test.settings',
        'complex_structure_deprecated',
        [
            // Keys defined locally, in `type: config_schema_deprecated_test.settings`.
            // @see core/modules/config/tests/config_schema_deprecated_test/config/schema/config_schema_deprecated_test.schema.yml
'type',
            'products',
        ],
        [],
        [],
    ]);
    // A config entity is always a Mapping at the top level, but most nesting is
    // also using Mappings (unless the keys are free to be chosen, then a
    // Sequence would be used).
    (yield 'No dynamic type: block.block.branding' => [
        'block.block.branding',
        NULL,
        [
            // Keys inherited from `type: config_entity`.
            // @see core/config/schema/core.data_types.schema.yml
'uuid',
            'langcode',
            'status',
            'dependencies',
            'third_party_settings',
            '_core',
            // Keys defined locally, in `type: block.block.*`.
            // @see core/modules/block/config/schema/block.schema.yml
'id',
            'theme',
            'region',
            'weight',
            'provider',
            'plugin',
            'settings',
            'visibility',
        ],
        [
            'third_party_settings',
            '_core',
        ],
        [],
    ]);
    // An example of nested Mapping objects in config entities.
    (yield 'No dynamic type: block.block.branding:dependencies' => [
        'block.block.branding',
        'dependencies',
        [
            // Keys inherited from `type: config_dependencies_base`.
            // @see core/config/schema/core.data_types.schema.yml
'config',
            'content',
            'module',
            'theme',
            // Keys defined locally, in `type: config_dependencies`.
            // @see core/config/schema/core.data_types.schema.yml
'enforced',
        ],
        // All these keys are optional!
[
            'config',
            'content',
            'module',
            'theme',
            'enforced',
        ],
        [],
    ]);
    // Three examples of `[%parent]`-based dynamic typing in config schema, and
    // the consequences on what keys are considered valid: the first 2 depend
    // on the block plugin being used using a single `%parent`, the third
    // depends on the field plugin being used using a double `%parent`.
    // See `type: block.block.*` which uses
    // `type: block.settings.[%parent.plugin]`, and `type: field_config_base`
    // which uses `type: field.value.[%parent.%parent.field_type]`.
    (yield 'Dynamic type with [%parent]: block.block.branding:settings' => [
        'block.block.branding',
        'settings',
        [
            // Keys inherited from `type: block.settings.*`, which in turn is
            // inherited from `type: block_settings`.
            // @see core/config/schema/core.data_types.schema.yml
'id',
            'label',
            'label_display',
            'provider',
            'status',
            'info',
            'view_mode',
            'context_mapping',
            // Keys defined locally, in `type: block.settings.system_branding_block`.
            // @see core/modules/block/config/schema/block.schema.yml
$available_block_settings_types['block.settings.system_branding_block'],
        ],
        [],
        $available_block_settings_types,
    ]);
    (yield 'Dynamic type with [%parent]: block.block.local_tasks:settings' => [
        'block.block.local_tasks',
        'settings',
        [
            // Keys inherited from `type: block.settings.*`, which in turn is
            // inherited from `type: block_settings`.
            // @see core/config/schema/core.data_types.schema.yml
'id',
            'label',
            'label_display',
            'provider',
            'status',
            'info',
            'view_mode',
            'context_mapping',
            // Keys defined locally, in `type: block.settings.local_tasks_block`.
            // @see core/modules/system/config/schema/system.schema.yml
$available_block_settings_types['block.settings.local_tasks_block'],
        ],
        [],
        $available_block_settings_types,
    ]);
    (yield 'Dynamic type with [%parent.%parent]: field.field.node.config_mapping_test.comment_config_mapping_test:default_value.0' => [
        'field.field.node.config_mapping_test.comment_config_mapping_test',
        'default_value.0',
        [
            // Keys defined locally, in `type: field.value.comment`.
            // @see core/modules/comment/config/schema/comment.schema.yml
'status',
            'cid',
            'last_comment_timestamp',
            'last_comment_name',
            'last_comment_uid',
            'comment_count',
        ],
        [],
        [
            'field.value.string' => [
                'value',
            ],
            'field.value.string_long' => [
                'value',
            ],
            'field.value.uri' => [
                'value',
            ],
            'field.value.created' => [
                'value',
            ],
            'field.value.changed' => [
                'value',
            ],
            'field.value.entity_reference' => [
                'target_id',
                'target_uuid',
            ],
            'field.value.boolean' => [
                'value',
            ],
            'field.value.email' => [
                'value',
            ],
            'field.value.integer' => [
                'value',
            ],
            'field.value.decimal' => [
                'value',
            ],
            'field.value.float' => [
                'value',
            ],
            'field.value.timestamp' => [
                'value',
            ],
            'field.value.comment' => [
                'status',
                'cid',
                'last_comment_timestamp',
                'last_comment_name',
                'last_comment_uid',
                'comment_count',
            ],
        ],
    ]);
    // An example of `[childkey]`-based dynamic mapping typing in config schema,
    // for a mapping inside a sequence: the `id` key-value pair in the mapping
    // determines the type of the mapping. The key in the sequence whose value
    // is the mapping is irrelevant, it can be arbitrarily chosen.
    // See `type: block.block.*` which uses `type: condition.plugin.[id]`.
    (yield 'Dynamic type with [childkey]: block.block.positively_powered:visibility.I_CAN_CHOOSE_THIS' => [
        'block.block.positively_powered',
        'visibility.I_CAN_CHOOSE_THIS',
        [
            // Keys inherited from `type: condition.plugin`.
            // @see core/config/schema/core.data_types.schema.yml
'id',
            'negate',
            'uuid',
            'context_mapping',
            // Keys defined locally, in `type: condition.plugin.response_status`.
            // @see core/modules/system/config/schema/system.schema.yml
'status_codes',
        ],
        [],
        // Note the presence of `id`, `negate`, `uuid` and `context_mapping` here.
        // That's because there is no `condition.plugin.*` type that specifies
        // defaults. Each individual condition plugin has the freedom to deviate
        // from this approach!
[
            'condition.plugin.entity_bundle:*' => [
                'id',
                'negate',
                'uuid',
                'context_mapping',
                'bundles',
            ],
            'condition.plugin.request_path' => [
                'id',
                'negate',
                'uuid',
                'context_mapping',
                'pages',
            ],
            'condition.plugin.response_status' => [
                'id',
                'negate',
                'uuid',
                'context_mapping',
                'status_codes',
            ],
            'condition.plugin.current_theme' => [
                'id',
                'negate',
                'uuid',
                'context_mapping',
                'theme',
            ],
        ],
    ]);
    // Same, but what if `type: condition.plugin.*` would have existed?
    // @see core/modules/config/tests/config_schema_add_fallback_type_test/config/schema/config_schema_add_fallback_type_test.schema.yml
    (yield 'Dynamic type with [childkey]: block.block.positively_powered___alternate_reality_with_fallback_type___:visibility' => [
        'block.block.positively_powered___alternate_reality_with_fallback_type___',
        'visibility.I_CAN_CHOOSE_THIS',
        [
            // Keys inherited from `type: condition.plugin`.
            // @see core/config/schema/core.data_types.schema.yml
'id',
            'negate',
            'uuid',
            'context_mapping',
            // Keys defined locally, in `type: condition.plugin.response_status`.
            // @see core/modules/system/config/schema/system.schema.yml
'status_codes',
        ],
        [],
        // Note the ABSENCE of `id`, `negate`, `uuid` and `context_mapping`
        // compared to the previous test case, because now the
        // `condition.plugin.*` type does exist.
[
            'condition.plugin.entity_bundle:*' => [
                'bundles',
            ],
            'condition.plugin.request_path' => [
                'pages',
            ],
            'condition.plugin.response_status' => [
                'status_codes',
            ],
            'condition.plugin.current_theme' => [
                'theme',
            ],
        ],
    ]);
    // An example of `[%key]`-based dynamic mapping typing in config schema: the
    // key in the sequence determines the type of the mapping. Unlike the above
    // `[childkey]` example, the key has meaning here.
    // See `type: editor.settings.ckeditor5`, which uses
    // `type: ckeditor5.plugin.[%key]`.
    (yield 'Dynamic type with [%key]: editor.editor.funky:settings.plugins.ckeditor5_heading' => [
        'editor.editor.funky',
        'settings.plugins.ckeditor5_heading',
        [
            // Keys defined locally, in `type: ckeditor5.plugin.ckeditor5_heading`.
            // @see core/modules/ckeditor5/config/schema/ckeditor5.schema.yml
'enabled_headings',
        ],
        [],
        [
            'ckeditor5.plugin.ckeditor5_language' => [
                'language_list',
            ],
            'ckeditor5.plugin.ckeditor5_heading' => [
                'enabled_headings',
            ],
            'ckeditor5.plugin.ckeditor5_imageResize' => [
                'allow_resize',
            ],
            'ckeditor5.plugin.ckeditor5_sourceEditing' => [
                'allowed_tags',
            ],
            'ckeditor5.plugin.ckeditor5_alignment' => [
                'enabled_alignments',
            ],
            'ckeditor5.plugin.ckeditor5_list' => [
                'properties',
                'multiBlock',
            ],
            'ckeditor5.plugin.media_media' => [
                'allow_view_mode_override',
            ],
            'ckeditor5.plugin.ckeditor5_codeBlock' => [
                'languages',
            ],
            'ckeditor5.plugin.ckeditor5_style' => [
                'styles',
            ],
        ],
    ]);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.