function ConfigEntityBaseUnitTest::testOnDependencyRemovalWithPluginCollections

Test dependency removal on entities with plugin collections.

@legacy-covers ::onDependencyRemoval

Attributes

#[DataProvider('providerOnDependencyRemovalWithPluginCollections')]

File

core/tests/Drupal/Tests/Core/Config/Entity/ConfigEntityBaseUnitTest.php, line 353

Class

ConfigEntityBaseUnitTest
Tests Drupal\Core\Config\Entity\ConfigEntityBase.

Namespace

Drupal\Tests\Core\Config\Entity

Code

public function testOnDependencyRemovalWithPluginCollections(RemovableDependentPluginReturn $on_dependency_removal_status, array $dependencies_after_removal, bool $expectation) : void {
  $dependencies = [
    'config' => [
      'bar',
      'baz',
    ],
  ];
  // Create an entity with a plugin to test.
  $instance = $this->getMockBuilder('Drupal\\Tests\\Core\\Plugin\\Fixtures\\TestConfigurablePlugin')
    ->setConstructorArgs([
    [],
    $this->randomMachineName(),
    [
      'provider' => 'foo',
    ],
  ])
    ->onlyMethods([
    'calculateDependencies',
    'onCollectionDependencyRemoval',
  ])
    ->getMock();
  // Make sure the plugin's onCollectionDependencyRemoval() method is invoked
  // from $entity->onDependencyRemoval().
  $instance->expects($this->exactly(1))
    ->method('onCollectionDependencyRemoval')
    ->willReturnCallback(static fn(array $dependencies): RemovableDependentPluginReturn => $on_dependency_removal_status);
  // The calculateDependencies() method will be called before and after
  // onCollectionDependencyRemoval(), so determine what
  // calculateDependencies() should return based on when the call is made.
  $before_on_dependency_removal = TRUE;
  // If the plugin should be removed from the collection, then
  // the plugin's calculateDependencies() should be called only once, when the
  // entity's dependencies are calculated before the call to
  // $entity->onDependencyRemoval(). Otherwise, if the plugin is not removed,
  // the plugin's calculateDependencies() should be called before and after
  // $entity->onDependencyRemoval().
  $instance->expects($on_dependency_removal_status == RemovableDependentPluginReturn::Remove ? $this->once() : $this->exactly(2))
    ->method('calculateDependencies')
    ->willReturnCallback(static function () use (&$before_on_dependency_removal, $dependencies, $dependencies_after_removal) : array {
    return $before_on_dependency_removal ? $dependencies : $dependencies_after_removal;
  });
  // Confirm the calculated entity dependencies before removing dependencies.
  $entity = $this->getMockEntityWithPluginCollection($instance);
  $collections = $entity->getPluginCollections();
  $collection = reset($collections);
  $this->assertEquals(1, count($collection));
  $entity->calculateDependencies();
  $calculated_dependencies = $entity->getDependencies();
  $this->assertEquals($dependencies, $calculated_dependencies);
  // Confirm the calculated entity dependencies after removing dependencies.
  $before_on_dependency_removal = FALSE;
  $changed = $entity->onDependencyRemoval($dependencies);
  $entity->calculateDependencies();
  $recalculated_dependencies = $entity->getDependencies();
  // If the plugin has been removed from the collection, then the collection
  // should be empty. Otherwise, there should be one plugin instance in the
  // collection.
  $this->assertEquals($on_dependency_removal_status == RemovableDependentPluginReturn::Remove ? 0 : 1, count($collection));
  $this->assertEquals($dependencies_after_removal, $recalculated_dependencies);
  $this->assertEquals($expectation, $changed);
}

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