function ConfigImporterTest::testSecondaryUpdateDeletedParentFirst

Same name in other branches
  1. 11.x core/tests/Drupal/KernelTests/Core/Config/ConfigImporterTest.php \Drupal\KernelTests\Core\Config\ConfigImporterTest::testSecondaryUpdateDeletedParentFirst()

Tests that secondary updates for deleted files work as expected.

File

core/tests/Drupal/KernelTests/Core/Config/ConfigImporterTest.php, line 290

Class

ConfigImporterTest
Tests importing configuration from files into active configuration.

Namespace

Drupal\KernelTests\Core\Config

Code

public function testSecondaryUpdateDeletedParentFirst() : void {
    $name_dependency = 'config_test.dynamic.dependency';
    $name_dependent = 'config_test.dynamic.dependent';
    $name_other = 'config_test.dynamic.other';
    $storage = $this->container
        ->get('config.storage');
    $sync = $this->container
        ->get('config.storage.sync');
    $uuid = $this->container
        ->get('uuid');
    $values_dependency = [
        'id' => 'dependency',
        'label' => 'Dependency',
        'weight' => 0,
        'uuid' => $uuid->generate(),
    ];
    $storage->write($name_dependency, $values_dependency);
    $values_dependency['label'] = 'Updated Dependency';
    $sync->write($name_dependency, $values_dependency);
    $values_dependent = [
        'id' => 'dependent',
        'label' => 'Dependent',
        'weight' => 0,
        'uuid' => $uuid->generate(),
        // Add a dependency on dependency, to make sure that is synced first.
'dependencies' => [
            'config' => [
                $name_dependency,
            ],
        ],
    ];
    $storage->write($name_dependent, $values_dependent);
    $values_dependent['label'] = 'Updated Child';
    $sync->write($name_dependent, $values_dependent);
    // Ensure that import will continue after the error.
    $values_other = [
        'id' => 'other',
        'label' => 'Other',
        'weight' => 0,
        'uuid' => $uuid->generate(),
        // Add a dependency on dependency, to make sure that is synced first. This
        // will also be synced after the dependent due to alphabetical ordering.
'dependencies' => [
            'config' => [
                $name_dependency,
            ],
        ],
    ];
    $storage->write($name_other, $values_other);
    $values_other['label'] = 'Updated other';
    $sync->write($name_other, $values_other);
    // Check update changelist order.
    $config_importer = $this->configImporter();
    $updates = $config_importer->getStorageComparer()
        ->getChangelist('update');
    $expected = [
        $name_dependency,
        $name_dependent,
        $name_other,
    ];
    $this->assertSame($expected, $updates);
    // Import.
    $config_importer->import();
    $entity_storage = \Drupal::entityTypeManager()->getStorage('config_test');
    $dependency = $entity_storage->load('dependency');
    $this->assertEquals('dependency', $dependency->id());
    $this->assertEquals($values_dependency['uuid'], $dependency->uuid());
    $this->assertEquals($values_dependency['label'], $dependency->label());
    // The dependent was deleted in
    // \Drupal\config_test\Entity\ConfigTest::postSave().
    $this->assertNull($entity_storage->load('dependent'));
    $other = $entity_storage->load('other');
    $this->assertEquals('other', $other->id());
    $this->assertEquals($values_other['uuid'], $other->uuid());
    $this->assertEquals($values_other['label'], $other->label());
    $logs = $config_importer->getErrors();
    $this->assertCount(1, $logs);
    $this->assertEquals(new FormattableMarkup('Update target "@name" is missing.', [
        '@name' => $name_dependent,
    ]), $logs[0]);
}

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