function ConfigImportUITest::testEntityBundleDelete

Same name and namespace in other branches
  1. 9 core/modules/config/tests/src/Functional/ConfigImportUITest.php \Drupal\Tests\config\Functional\ConfigImportUITest::testEntityBundleDelete()
  2. 10 core/modules/config/tests/src/Functional/ConfigImportUITest.php \Drupal\Tests\config\Functional\ConfigImportUITest::testEntityBundleDelete()
  3. 11.x core/modules/config/tests/src/Functional/ConfigImportUITest.php \Drupal\Tests\config\Functional\ConfigImportUITest::testEntityBundleDelete()

Tests the config importer cannot delete bundles with existing entities.

See also

\Drupal\Core\Entity\Event\BundleConfigImportValidate

File

core/modules/config/tests/src/Functional/ConfigImportUITest.php, line 456

Class

ConfigImportUITest
Tests the user interface for importing configuration.

Namespace

Drupal\Tests\config\Functional

Code

public function testEntityBundleDelete() {
    \Drupal::service('module_installer')->install([
        'node',
    ]);
    $this->copyConfig($this->container
        ->get('config.storage'), $this->container
        ->get('config.storage.sync'));
    $node_type = $this->drupalCreateContentType();
    $node = $this->drupalCreateNode([
        'type' => $node_type->id(),
    ]);
    $this->drupalGet('admin/config/development/configuration');
    // The node type, body field and entity displays will be scheduled for
    // removal.
    $this->assertText(new FormattableMarkup('node.type.@type', [
        '@type' => $node_type->id(),
    ]));
    $this->assertText(new FormattableMarkup('field.field.node.@type.body', [
        '@type' => $node_type->id(),
    ]));
    $this->assertText(new FormattableMarkup('core.entity_view_display.node.@type.teaser', [
        '@type' => $node_type->id(),
    ]));
    $this->assertText(new FormattableMarkup('core.entity_view_display.node.@type.default', [
        '@type' => $node_type->id(),
    ]));
    $this->assertText(new FormattableMarkup('core.entity_form_display.node.@type.default', [
        '@type' => $node_type->id(),
    ]));
    // Attempt to import configuration and verify that an error message appears
    // and the node type, body field and entity displays are still scheduled for
    // removal.
    $this->drupalPostForm(NULL, [], t('Import all'));
    $validation_message = t('Entities exist of type %entity_type and %bundle_label %bundle. These entities need to be deleted before importing.', [
        '%entity_type' => $node->getEntityType()
            ->getLabel(),
        '%bundle_label' => $node->getEntityType()
            ->getBundleLabel(),
        '%bundle' => $node_type->label(),
    ]);
    $this->assertRaw($validation_message);
    $this->assertText(new FormattableMarkup('node.type.@type', [
        '@type' => $node_type->id(),
    ]));
    $this->assertText(new FormattableMarkup('field.field.node.@type.body', [
        '@type' => $node_type->id(),
    ]));
    $this->assertText(new FormattableMarkup('core.entity_view_display.node.@type.teaser', [
        '@type' => $node_type->id(),
    ]));
    $this->assertText(new FormattableMarkup('core.entity_view_display.node.@type.default', [
        '@type' => $node_type->id(),
    ]));
    $this->assertText(new FormattableMarkup('core.entity_form_display.node.@type.default', [
        '@type' => $node_type->id(),
    ]));
    // Delete the node and try to import again.
    $node->delete();
    $this->drupalPostForm(NULL, [], t('Import all'));
    $this->assertNoRaw($validation_message);
    $this->assertText(t('There are no configuration changes to import.'));
    $this->assertNoText(new FormattableMarkup('node.type.@type', [
        '@type' => $node_type->id(),
    ]));
    $this->assertNoText(new FormattableMarkup('field.field.node.@type.body', [
        '@type' => $node_type->id(),
    ]));
    $this->assertNoText(new FormattableMarkup('core.entity_view_display.node.@type.teaser', [
        '@type' => $node_type->id(),
    ]));
    $this->assertNoText(new FormattableMarkup('core.entity_view_display.node.@type.default', [
        '@type' => $node_type->id(),
    ]));
    $this->assertNoText(new FormattableMarkup('core.entity_form_display.node.@type.default', [
        '@type' => $node_type->id(),
    ]));
}

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