You are here

public function BlockFieldSelectionTest::testIsSerializable in Block field 8

Test if BlockFieldSelectionInterface plugins are serializable.

File

tests/src/Kernel/BlockFieldSelectionTest.php, line 39

Class

BlockFieldSelectionTest
Tests blockFieldSelectionManager plugins.

Namespace

Drupal\Tests\block_field\Kernel

Code

public function testIsSerializable() {

  // Create a plugin instance of 'categories'.
  $plugin = $this
    ->setUpSelectionInstance('categories', [
    'categories' => [
      'core',
    ],
  ]);

  // Prophesize translations to force service properties.
  $translation = $this
    ->prophesize(TranslationInterface::class);
  $translation
    ->willImplement(\Serializable::class);
  $translation
    ->serialize()
    ->willThrow(\Exception::class);
  $translation_service = $translation
    ->reveal();
  $translation_service->_serviceId = 'string_translation';
  $plugin
    ->setStringTranslation($translation_service);

  // Attempt to serialize and unserialize plugin.
  $string = serialize($plugin);
  $object = unserialize($string);

  // Confirm plugin_id is unchanged.
  $this
    ->assertEquals('categories', $object
    ->getPluginId());

  // Repeat steps with instance of 'blocks'.
  $plugin = $this
    ->setUpSelectionInstance('blocks', [
    'plugin_ids' => [
      'system_powered_by_block',
      'page_title_block',
    ],
  ]);
  $plugin
    ->setStringTranslation($translation_service);

  // Attempt to serialize and unserialize plugin.
  $string = serialize($plugin);
  $object = unserialize($string);

  // Confirm plugin_id is unchanged.
  $this
    ->assertEquals('blocks', $object
    ->getPluginId());
}