ConfigEntityStorageTest.php

Same filename in this branch
  1. 11.x core/tests/Drupal/Tests/Core/Config/Entity/ConfigEntityStorageTest.php
Same filename in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Config/ConfigEntityStorageTest.php
  2. 9 core/tests/Drupal/Tests/Core/Config/Entity/ConfigEntityStorageTest.php
  3. 8.9.x core/tests/Drupal/KernelTests/Core/Config/ConfigEntityStorageTest.php
  4. 8.9.x core/tests/Drupal/Tests/Core/Config/Entity/ConfigEntityStorageTest.php
  5. 10 core/tests/Drupal/KernelTests/Core/Config/ConfigEntityStorageTest.php
  6. 10 core/tests/Drupal/Tests/Core/Config/Entity/ConfigEntityStorageTest.php

Namespace

Drupal\KernelTests\Core\Config

File

core/tests/Drupal/KernelTests/Core/Config/ConfigEntityStorageTest.php

View source
<?php

declare (strict_types=1);
namespace Drupal\KernelTests\Core\Config;

use Drupal\Core\Config\ConfigDuplicateUUIDException;
use Drupal\KernelTests\KernelTestBase;

/**
 * Tests configuration entity storage.
 *
 * @group config
 */
class ConfigEntityStorageTest extends KernelTestBase {
    
    /**
     * {@inheritdoc}
     */
    protected static $modules = [
        'config_test',
    ];
    
    /**
     * Tests creating configuration entities with changed UUIDs.
     */
    public function testUUIDConflict() : void {
        $entity_type = 'config_test';
        $id = 'test_1';
        // Load the original configuration entity.
        $storage = $this->container
            ->get('entity_type.manager')
            ->getStorage($entity_type);
        $storage->create([
            'id' => $id,
        ])
            ->save();
        $entity = $storage->load($id);
        $original_properties = $entity->toArray();
        // Override with a new UUID and try to save.
        $new_uuid = $this->container
            ->get('uuid')
            ->generate();
        $entity->set('uuid', $new_uuid);
        try {
            $entity->save();
            $this->fail('Exception thrown when attempting to save a configuration entity with a UUID that does not match the existing UUID.');
        } catch (ConfigDuplicateUUIDException) {
            // Expected exception; just continue testing.
        }
        // Ensure that the config entity was not corrupted.
        $entity = $storage->loadUnchanged($entity->id());
        $this->assertSame($original_properties, $entity->toArray());
    }
    
    /**
     * Tests the hasData() method for config entity storage.
     *
     * @covers \Drupal\Core\Config\Entity\ConfigEntityStorage::hasData
     */
    public function testHasData() : void {
        $storage = \Drupal::entityTypeManager()->getStorage('config_test');
        $this->assertFalse($storage->hasData());
        // Add a test config entity and check again.
        $storage->create([
            'id' => $this->randomMachineName(),
        ])
            ->save();
        $this->assertTrue($storage->hasData());
    }

}

Classes

Title Deprecated Summary
ConfigEntityStorageTest Tests configuration entity storage.

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