function ConfigEntityUpdaterTest::testUpdate

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

@covers ::update

File

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

Class

ConfigEntityUpdaterTest
Tests \Drupal\Core\Config\Entity\ConfigEntityUpdater.

Namespace

Drupal\KernelTests\Core\Config\Entity

Code

public function testUpdate() : void {
    // 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'] = 10;
    new Settings($settings);
    $updater = $this->container
        ->get('class_resolver')
        ->getInstanceFromDefinition(ConfigEntityUpdater::class);
    $callback = function ($config_entity) {
        
        /** @var \Drupal\config_test\Entity\ConfigTest $config_entity */
        $number = (int) str_replace('config_test_', '', $config_entity->id());
        // Only update even numbered entities.
        if ($number % 2 == 0) {
            $config_entity->set('label', $config_entity->label . ' (updated)');
            return TRUE;
        }
        return FALSE;
    };
    // This should run against the first 10 entities. The even numbered labels
    // will have been updated.
    $updater->update($sandbox, 'config_test', $callback);
    $entities = $storage->loadMultiple();
    $this->assertEquals('config_test_8 (updated)', $entities['config_test_8']->label());
    $this->assertEquals('config_test_9', $entities['config_test_9']->label());
    $this->assertEquals('config_test_10', $entities['config_test_10']->label());
    $this->assertEquals('config_test_14', $entities['config_test_14']->label());
    $this->assertEquals(15, $sandbox['config_entity_updater']['count']);
    $this->assertEquals('config_test', $sandbox['config_entity_updater']['entity_type']);
    $this->assertCount(5, $sandbox['config_entity_updater']['entities']);
    $this->assertEquals(10 / 15, $sandbox['#finished']);
    // Update the rest.
    $updater->update($sandbox, 'config_test', $callback);
    $entities = $storage->loadMultiple();
    $this->assertEquals('config_test_8 (updated)', $entities['config_test_8']->label());
    $this->assertEquals('config_test_9', $entities['config_test_9']->label());
    $this->assertEquals('config_test_10 (updated)', $entities['config_test_10']->label());
    $this->assertEquals('config_test_14 (updated)', $entities['config_test_14']->label());
    $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.