function MappingTest::testMappingInterpretation

Same name in other branches
  1. 11.x core/tests/Drupal/KernelTests/Config/Schema/MappingTest.php \Drupal\KernelTests\Config\Schema\MappingTest::testMappingInterpretation()

@dataProvider providerMappingInterpretation

File

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

Class

MappingTest
@coversDefaultClass \Drupal\Core\Config\Schema\Mapping @group Config

Namespace

Drupal\KernelTests\Config\Schema

Code

public function testMappingInterpretation(string $config_name, ?string $property_path, array $expected_valid_keys, array $expected_optional_keys, array $expected_dynamically_valid_keys) : void {
    // Some config needs some dependencies installed.
    switch ($config_name) {
        case 'block.block.branding':
            $this->enableModules([
                'system',
                'block',
            ]);
            
            /** @var \Drupal\Core\Extension\ThemeInstallerInterface $theme_installer */
            $theme_installer = $this->container
                ->get('theme_installer');
            $theme_installer->install([
                'stark',
            ]);
            Block::create([
                'id' => 'branding',
                'plugin' => 'system_branding_block',
                'theme' => 'stark',
                'status' => TRUE,
                'settings' => [
                    'use_site_logo' => TRUE,
                    'use_site_name' => TRUE,
                    'use_site_slogan' => TRUE,
                    'label_display' => FALSE,
                    // TRICKY: these 4 are inherited from `type: block_settings`.
'status' => TRUE,
                    'info' => '',
                    'view_mode' => 'full',
                    'context_mapping' => [],
                ],
            ])->save();
            break;
        case 'block.block.local_tasks':
            $this->enableModules([
                'system',
                'block',
            ]);
            
            /** @var \Drupal\Core\Extension\ThemeInstallerInterface $theme_installer */
            $theme_installer = $this->container
                ->get('theme_installer');
            $theme_installer->install([
                'stark',
            ]);
            Block::create([
                'id' => 'local_tasks',
                'plugin' => 'local_tasks_block',
                'theme' => 'stark',
                'status' => TRUE,
                'settings' => [
                    'primary' => TRUE,
                    'secondary' => FALSE,
                    // TRICKY: these 4 are inherited from `type: block_settings`.
'status' => TRUE,
                    'info' => '',
                    'view_mode' => 'full',
                    'context_mapping' => [],
                ],
            ])->save();
            break;
        case 'block.block.positively_powered___alternate_reality_with_fallback_type___':
            $this->enableModules([
                'config_schema_add_fallback_type_test',
            ]);
            $id = 'positively_powered___alternate_reality_with_fallback_type___';
        case 'block.block.positively_powered':
            $this->enableModules([
                'system',
                'block',
            ]);
            
            /** @var \Drupal\Core\Extension\ThemeInstallerInterface $theme_installer */
            $theme_installer = $this->container
                ->get('theme_installer');
            $theme_installer->install([
                'stark',
            ]);
            Block::create([
                'id' => $id ?? 'positively_powered',
                'plugin' => 'system_powered_by_block',
                'theme' => 'stark',
                'status' => TRUE,
                'settings' => [
                    'label_display' => FALSE,
                    // TRICKY: these 4 are inherited from `type: block_settings`.
'status' => TRUE,
                    'info' => '',
                    'view_mode' => 'full',
                    'context_mapping' => [],
                ],
                // Avoid showing "Powered by Drupal" on 404 responses.
'visibility' => [
                    'I_CAN_CHOOSE_THIS' => [
                        // This is what determines the
'id' => 'response_status',
                        'negate' => FALSE,
                        'status_codes' => [
                            404,
                        ],
                    ],
                ],
            ])->save();
            break;
        case 'config_schema_deprecated_test.settings':
            $this->enableModules([
                'config_schema_deprecated_test',
            ]);
            $config = $this->config('config_schema_deprecated_test.settings');
            // @see \Drupal\KernelTests\Core\Config\ConfigSchemaDeprecationTest
            $config->set('complex_structure_deprecated.type', 'fruits')
                ->set('complex_structure_deprecated.products', [
                'apricot',
                'apple',
            ])
                ->save();
            break;
        case 'editor.editor.funky':
            $this->enableModules([
                'filter',
                'editor',
                'ckeditor5',
            ]);
            FilterFormat::create([
                'format' => 'funky',
                'name' => 'Funky',
            ])->save();
            Editor::create([
                'format' => 'funky',
                'editor' => 'ckeditor5',
                'image_upload' => [
                    'status' => FALSE,
                ],
            ])->save();
            break;
        case 'field.field.node.config_mapping_test.comment_config_mapping_test':
            $this->enableModules([
                'field',
                'node',
                'comment',
                'taxonomy',
                'config_mapping_test',
            ]);
            $this->assertNull(FieldConfig::load('node.config_mapping_test.comment_config_mapping_test'));
            // TRICKY: \Drupal\node\Entity\NodeType::$preview_mode uses
            // DRUPAL_OPTIONAL, which is defined in system.module.
            require_once 'core/modules/system/system.module';
            $this->installConfig([
                'config_mapping_test',
            ]);
            $this->assertNotNull(FieldConfig::load('node.config_mapping_test.comment_config_mapping_test'));
            break;
    }
    
    /** @var \Drupal\Core\Config\TypedConfigManagerInterface $typed_config_manager */
    $typed_config_manager = \Drupal::service('config.typed');
    $mapping = $typed_config_manager->get($config_name);
    if ($property_path) {
        $mapping = $mapping->get($property_path);
    }
    assert($mapping instanceof Mapping);
    $expected_required_keys = array_values(array_diff($expected_valid_keys, $expected_optional_keys));
    $this->assertSame($expected_valid_keys, $mapping->getValidKeys());
    $this->assertSame($expected_required_keys, $mapping->getRequiredKeys());
    $this->assertSame($expected_dynamically_valid_keys, $mapping->getDynamicallyValidKeys());
    $this->assertSame($expected_optional_keys, $mapping->getOptionalKeys());
}

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