function ConfigEntityBaseUnitTest::testPreSaveWithPluginCollections

Same name and namespace in other branches
  1. 11.x core/tests/Drupal/Tests/Core/Config/Entity/ConfigEntityBaseUnitTest.php \Drupal\Tests\Core\Config\Entity\ConfigEntityBaseUnitTest::testPreSaveWithPluginCollections()

@covers ::preSave
@dataProvider providerTestSetAndPreSaveWithPluginCollections

File

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

Class

ConfigEntityBaseUnitTest
@coversDefaultClass \Drupal\Core\Config\Entity\ConfigEntityBase[[api-linebreak]] @group Config

Namespace

Drupal\Tests\Core\Config\Entity

Code

public function testPreSaveWithPluginCollections(bool $syncing, string $expected_value) : void {
  $instance_id = 'the_instance_id';
  $instance = new TestConfigurablePlugin([
    'foo' => 'original_value',
  ], $instance_id, [
    'provider' => 'core',
  ]);
  $plugin_manager = $this->prophesize(PluginManagerInterface::class);
  if ($syncing) {
    $plugin_manager->createInstance(Argument::cetera())
      ->shouldNotBeCalled();
  }
  else {
    $plugin_manager->createInstance($instance_id, Argument::any())
      ->willReturn($instance);
  }
  $entity_values = [
    'the_plugin_collection_config' => [
      $instance_id => [
        'id' => $instance_id,
        'foo' => 'original_value',
      ],
    ],
  ];
  $entity = new TestConfigEntityWithPluginCollections($entity_values, $this->entityTypeId);
  $entity->setSyncing($syncing);
  $entity->setPluginManager($plugin_manager->reveal());
  // After creating the entity, change the plugin configuration.
  $instance->setConfiguration([
    'foo' => 'new_value',
  ]);
  $query = $this->createMock('\\Drupal\\Core\\Entity\\Query\\QueryInterface');
  $storage = $this->createMock('\\Drupal\\Core\\Config\\Entity\\ConfigEntityStorageInterface');
  $query->expects($this->any())
    ->method('execute')
    ->willReturn([]);
  $query->expects($this->any())
    ->method('condition')
    ->willReturn($query);
  $storage->expects($this->any())
    ->method('getQuery')
    ->willReturn($query);
  $storage->expects($this->any())
    ->method('loadUnchanged')
    ->willReturn($entity);
  $entity->preSave($storage);
  $this->assertSame($expected_value, $entity->get('the_plugin_collection_config')[$instance_id]['foo']);
}

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