function ConfigEntityUpdaterTest::testUpdateDefaultCallback

Same name and namespace in other branches
  1. 8.9.x core/tests/Drupal/KernelTests/Core/Config/Entity/ConfigEntityUpdaterTest.php \Drupal\KernelTests\Core\Config\Entity\ConfigEntityUpdaterTest::testUpdateDefaultCallback()
  2. 10 core/tests/Drupal/KernelTests/Core/Config/Entity/ConfigEntityUpdaterTest.php \Drupal\KernelTests\Core\Config\Entity\ConfigEntityUpdaterTest::testUpdateDefaultCallback()
  3. 11.x core/tests/Drupal/KernelTests/Core/Config/Entity/ConfigEntityUpdaterTest.php \Drupal\KernelTests\Core\Config\Entity\ConfigEntityUpdaterTest::testUpdateDefaultCallback()

@covers ::update

File

core/tests/Drupal/KernelTests/Core/Config/Entity/ConfigEntityUpdaterTest.php, line 80

Class

ConfigEntityUpdaterTest
Tests <a href="/api/drupal/core%21lib%21Drupal%21Core%21Config%21Entity%21ConfigEntityUpdater.php/class/ConfigEntityUpdater/9" title="A utility class to make updating configuration entities simple." class="local">\Drupal\Core\Config\Entity\ConfigEntityUpdater</a>.

Namespace

Drupal\KernelTests\Core\Config\Entity

Code

public function testUpdateDefaultCallback() {
    // Create some entities to update.
    $storage = $this->container
        ->get('entity_type.manager')
        ->getStorage('config_test');
    for ($i = 0; $i < 15; $i++) {
        $entity_id = 'config_test_' . $i;
        $storage->create([
            'id' => $entity_id,
            'label' => $entity_id,
        ])
            ->save();
    }
    // Set up the updater.
    $sandbox = [];
    $settings = Settings::getInstance() ? Settings::getAll() : [];
    $settings['entity_update_batch_size'] = 9;
    new Settings($settings);
    $updater = $this->container
        ->get('class_resolver')
        ->getInstanceFromDefinition(ConfigEntityUpdater::class);
    // Cause a dependency to be added during an update.
    \Drupal::state()->set('config_test_new_dependency', 'added_dependency');
    // This should run against the first 10 entities.
    $updater->update($sandbox, 'config_test');
    $entities = $storage->loadMultiple();
    $this->assertEquals([
        'added_dependency',
    ], $entities['config_test_7']->getDependencies()['module']);
    $this->assertEquals([
        'added_dependency',
    ], $entities['config_test_8']->getDependencies()['module']);
    $this->assertEquals([], $entities['config_test_9']->getDependencies());
    $this->assertEquals([], $entities['config_test_14']->getDependencies());
    $this->assertEquals(15, $sandbox['config_entity_updater']['count']);
    $this->assertCount(6, $sandbox['config_entity_updater']['entities']);
    $this->assertEquals(9 / 15, $sandbox['#finished']);
    // Update the rest.
    $updater->update($sandbox, 'config_test');
    $entities = $storage->loadMultiple();
    $this->assertEquals([
        'added_dependency',
    ], $entities['config_test_9']->getDependencies()['module']);
    $this->assertEquals([
        'added_dependency',
    ], $entities['config_test_14']->getDependencies()['module']);
    $this->assertEquals(1, $sandbox['#finished']);
    $this->assertCount(0, $sandbox['config_entity_updater']['entities']);
}

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