You are here

public function ConfigImporterTest::testSecondaryUpdateDeletedDeleterFirst in Drupal 8

Same name and namespace in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Config/ConfigImporterTest.php \Drupal\KernelTests\Core\Config\ConfigImporterTest::testSecondaryUpdateDeletedDeleterFirst()
  2. 10 core/tests/Drupal/KernelTests/Core/Config/ConfigImporterTest.php \Drupal\KernelTests\Core\Config\ConfigImporterTest::testSecondaryUpdateDeletedDeleterFirst()

Tests that secondary updates for deleted files work as expected.

File

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

Class

ConfigImporterTest
Tests importing configuration from files into active configuration.

Namespace

Drupal\KernelTests\Core\Config

Code

public function testSecondaryUpdateDeletedDeleterFirst() {
  $name_deleter = 'config_test.dynamic.deleter';
  $name_deletee = 'config_test.dynamic.deletee';
  $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_deleter = [
    'id' => 'deleter',
    'label' => 'Deleter',
    'weight' => 0,
    'uuid' => $uuid
      ->generate(),
  ];
  $storage
    ->write($name_deleter, $values_deleter);
  $values_deleter['label'] = 'Updated Deleter';
  $sync
    ->write($name_deleter, $values_deleter);
  $values_deletee = [
    'id' => 'deletee',
    'label' => 'Deletee',
    'weight' => 0,
    'uuid' => $uuid
      ->generate(),
    // Add a dependency on deleter, to make sure that is synced first.
    'dependencies' => [
      'config' => [
        $name_deleter,
      ],
    ],
  ];
  $storage
    ->write($name_deletee, $values_deletee);
  $values_deletee['label'] = 'Updated Deletee';
  $sync
    ->write($name_deletee, $values_deletee);

  // Ensure that import will continue after the error.
  $values_other = [
    'id' => 'other',
    'label' => 'Other',
    'weight' => 0,
    'uuid' => $uuid
      ->generate(),
    // Add a dependency on deleter, to make sure that is synced first. This
    // will also be synced after the deletee due to alphabetical ordering.
    'dependencies' => [
      'config' => [
        $name_deleter,
      ],
    ],
  ];
  $storage
    ->write($name_other, $values_other);
  $values_other['label'] = 'Updated other';
  $sync
    ->write($name_other, $values_other);

  // Check update changelist order.
  $updates = $this->configImporter
    ->reset()
    ->getStorageComparer()
    ->getChangelist('update');
  $expected = [
    $name_deleter,
    $name_deletee,
    $name_other,
  ];
  $this
    ->assertSame($expected, $updates);

  // Import.
  $this->configImporter
    ->import();
  $entity_storage = \Drupal::entityTypeManager()
    ->getStorage('config_test');
  $deleter = $entity_storage
    ->load('deleter');
  $this
    ->assertEqual($deleter
    ->id(), 'deleter');
  $this
    ->assertEqual($deleter
    ->uuid(), $values_deleter['uuid']);
  $this
    ->assertEqual($deleter
    ->label(), $values_deleter['label']);

  // The deletee was deleted in
  // \Drupal\config_test\Entity\ConfigTest::postSave().
  $this
    ->assertNull($entity_storage
    ->load('deletee'));
  $other = $entity_storage
    ->load('other');
  $this
    ->assertEqual($other
    ->id(), 'other');
  $this
    ->assertEqual($other
    ->uuid(), $values_other['uuid']);
  $this
    ->assertEqual($other
    ->label(), $values_other['label']);
  $logs = $this->configImporter
    ->getErrors();
  $this
    ->assertCount(1, $logs);
  $this
    ->assertEqual($logs[0], new FormattableMarkup('Update target "@name" is missing.', [
    '@name' => $name_deletee,
  ]));
}