You are here

public function ConfigEntityBaseUnitTest::testSleepWithPluginCollections in Drupal 9

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

@covers ::__sleep

File

core/tests/Drupal/Tests/Core/Config/Entity/ConfigEntityBaseUnitTest.php, line 351
Contains \Drupal\Tests\Core\Config\Entity\ConfigEntityBaseUnitTest.

Class

ConfigEntityBaseUnitTest
@coversDefaultClass \Drupal\Core\Config\Entity\ConfigEntityBase @group Config

Namespace

Drupal\Tests\Core\Config\Entity

Code

public function testSleepWithPluginCollections() {
  $instance_id = 'the_instance_id';
  $instance = new TestConfigurablePlugin([], $instance_id, []);
  $plugin_manager = $this
    ->prophesize(PluginManagerInterface::class);
  $plugin_manager
    ->createInstance($instance_id, [
    'id' => $instance_id,
  ])
    ->willReturn($instance);

  // Also set up a container with the plugin manager so that we can assert
  // that the plugin manager itself is also not serialized.
  $container = new ContainerBuilder();
  $container
    ->set('plugin.manager.foo', $plugin_manager);
  \Drupal::setContainer($container);
  $entity_values = [
    'the_plugin_collection_config' => [
      $instance_id => [
        'foo' => 'original_value',
      ],
    ],
  ];
  $entity = new TestConfigEntityWithPluginCollections($entity_values, $this->entityTypeId);
  $entity
    ->setPluginManager($plugin_manager
    ->reveal());

  // After creating the entity, change the plugin configuration.
  $instance
    ->setConfiguration([
    'foo' => 'new_value',
  ]);

  // After changing the plugin configuration, the entity still has the
  // original value.
  $expected_plugin_config = [
    $instance_id => [
      'foo' => 'original_value',
    ],
  ];
  $this
    ->assertSame($expected_plugin_config, $entity
    ->get('the_plugin_collection_config'));

  // Ensure the plugin collection and manager is not stored.
  $vars = $entity
    ->__sleep();
  $this
    ->assertNotContains('pluginCollection', $vars);
  $this
    ->assertNotContains('pluginManager', $vars);
  $this
    ->assertSame([
    'pluginManager' => 'plugin.manager.foo',
  ], $entity
    ->get('_serviceIds'));
  $expected_plugin_config = [
    $instance_id => [
      'foo' => 'new_value',
    ],
  ];

  // Ensure the updated values are stored in the entity.
  $this
    ->assertSame($expected_plugin_config, $entity
    ->get('the_plugin_collection_config'));
}