You are here

public function LinkItemSerializationTest::testLinkSerialization in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/link/tests/src/Kernel/LinkItemSerializationTest.php \Drupal\Tests\link\Kernel\LinkItemSerializationTest::testLinkSerialization()
  2. 10 core/modules/link/tests/src/Kernel/LinkItemSerializationTest.php \Drupal\Tests\link\Kernel\LinkItemSerializationTest::testLinkSerialization()

Tests the serialization.

File

core/modules/link/tests/src/Kernel/LinkItemSerializationTest.php, line 58

Class

LinkItemSerializationTest
Tests link field serialization.

Namespace

Drupal\Tests\link\Kernel

Code

public function testLinkSerialization() {

  // Create entity.
  $entity = EntityTest::create();
  $url = 'https://www.drupal.org?test_param=test_value';
  $parsed_url = UrlHelper::parse($url);
  $title = $this
    ->randomMachineName();
  $class = $this
    ->randomMachineName();
  $entity->field_test->uri = $parsed_url['path'];
  $entity->field_test->title = $title;
  $entity->field_test
    ->first()
    ->get('options')
    ->set('query', $parsed_url['query']);
  $entity->field_test
    ->first()
    ->get('options')
    ->set('attributes', [
    'class' => $class,
  ]);
  $entity
    ->save();
  $serialized = $this->serializer
    ->serialize($entity, 'json');
  $deserialized = $this->serializer
    ->deserialize($serialized, EntityTest::class, 'json');
  $options_expected = [
    'query' => $parsed_url['query'],
    'attributes' => [
      'class' => $class,
    ],
  ];
  $this
    ->assertSame($options_expected, $deserialized->field_test->options);
}