You are here

public function ConfigDependencyTest::testConfigEntityUninstall in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/config/src/Tests/ConfigDependencyTest.php \Drupal\config\Tests\ConfigDependencyTest::testConfigEntityUninstall()

Tests ConfigManager::uninstall() and config entity dependency management.

File

core/modules/config/src/Tests/ConfigDependencyTest.php, line 192
Contains \Drupal\config\Tests\ConfigDependencyTest.

Class

ConfigDependencyTest
Tests for configuration dependencies.

Namespace

Drupal\config\Tests

Code

public function testConfigEntityUninstall() {

  /** @var \Drupal\Core\Config\ConfigManagerInterface $config_manager */
  $config_manager = \Drupal::service('config.manager');

  /** @var \Drupal\Core\Config\Entity\ConfigEntityStorage $storage */
  $storage = $this->container
    ->get('entity.manager')
    ->getStorage('config_test');

  // Test dependencies between modules.
  $entity1 = $storage
    ->create(array(
    'id' => 'entity1',
    'dependencies' => array(
      'enforced' => array(
        'module' => array(
          'node',
          'config_test',
        ),
      ),
    ),
  ));
  $entity1
    ->save();
  $entity2 = $storage
    ->create(array(
    'id' => 'entity2',
    'dependencies' => array(
      'enforced' => array(
        'config' => array(
          $entity1
            ->getConfigDependencyName(),
        ),
      ),
    ),
  ));
  $entity2
    ->save();

  // Perform a module rebuild so we can know where the node module is located
  // and uninstall it.
  // @todo Remove as part of https://www.drupal.org/node/2186491
  system_rebuild_module_data();

  // Test that doing a config uninstall of the node module deletes entity2
  // since it is dependent on entity1 which is dependent on the node module.
  $config_manager
    ->uninstall('module', 'node');
  $this
    ->assertFalse($storage
    ->load('entity1'), 'Entity 1 deleted');
  $this
    ->assertFalse($storage
    ->load('entity2'), 'Entity 2 deleted');

  // Set a more complicated test where dependencies will be fixed.
  \Drupal::state()
    ->set('config_test.fix_dependencies', array(
    $entity1
      ->getConfigDependencyName(),
  ));

  // Entity1 will be deleted because it depends on node.
  $entity1 = $storage
    ->create(array(
    'id' => 'entity1',
    'dependencies' => array(
      'enforced' => array(
        'module' => array(
          'node',
          'config_test',
        ),
      ),
    ),
  ));
  $entity1
    ->save();

  // Entity2 has a dependency on Entity1 but it can be fixed because
  // \Drupal\config_test\Entity::onDependencyRemoval() will remove the
  // dependency before config entities are deleted.
  $entity2 = $storage
    ->create(array(
    'id' => 'entity2',
    'dependencies' => array(
      'enforced' => array(
        'config' => array(
          $entity1
            ->getConfigDependencyName(),
        ),
      ),
    ),
  ));
  $entity2
    ->save();

  // Entity3 will be unchanged because it is dependent on Entity2 which can
  // be fixed.
  $entity3 = $storage
    ->create(array(
    'id' => 'entity3',
    'dependencies' => array(
      'enforced' => array(
        'config' => array(
          $entity2
            ->getConfigDependencyName(),
        ),
      ),
    ),
  ));
  $entity3
    ->save();

  // Entity4's config dependency will be fixed but it will still be deleted
  // because it also depends on the node module.
  $entity4 = $storage
    ->create(array(
    'id' => 'entity4',
    'dependencies' => array(
      'enforced' => array(
        'config' => array(
          $entity1
            ->getConfigDependencyName(),
        ),
        'module' => array(
          'node',
          'config_test',
        ),
      ),
    ),
  ));
  $entity4
    ->save();

  // Do a dry run using
  // \Drupal\Core\Config\ConfigManager::getConfigEntitiesToChangeOnDependencyRemoval().
  $config_entities = $config_manager
    ->getConfigEntitiesToChangeOnDependencyRemoval('module', [
    'node',
  ]);
  $this
    ->assertEqual($entity1
    ->uuid(), $config_entities['delete'][0]
    ->uuid(), 'Entity 1 will be deleted.');
  $this
    ->assertEqual($entity2
    ->uuid(), reset($config_entities['update'])
    ->uuid(), 'Entity 2 will be updated.');
  $this
    ->assertEqual($entity3
    ->uuid(), reset($config_entities['unchanged'])
    ->uuid(), 'Entity 3 is not changed.');
  $this
    ->assertEqual($entity4
    ->uuid(), $config_entities['delete'][1]
    ->uuid(), 'Entity 4 will be deleted.');

  // Perform the uninstall.
  $config_manager
    ->uninstall('module', 'node');

  // Test that expected actions have been performed.
  $this
    ->assertFalse($storage
    ->load('entity1'), 'Entity 1 deleted');
  $entity2 = $storage
    ->load('entity2');
  $this
    ->assertTrue($entity2, 'Entity 2 not deleted');
  $this
    ->assertEqual($entity2
    ->calculateDependencies()
    ->getDependencies()['config'], array(), 'Entity 2 dependencies updated to remove dependency on Entity1.');
  $entity3 = $storage
    ->load('entity3');
  $this
    ->assertTrue($entity3, 'Entity 3 not deleted');
  $this
    ->assertEqual($entity3
    ->calculateDependencies()
    ->getDependencies()['config'], [
    $entity2
      ->getConfigDependencyName(),
  ], 'Entity 3 still depends on Entity 2.');
  $this
    ->assertFalse($storage
    ->load('entity4'), 'Entity 4 deleted');
}