function LinkItemSerializationTest::testLinkSerialization

Same name in other branches
  1. 9 core/modules/link/tests/src/Kernel/LinkItemSerializationTest.php \Drupal\Tests\link\Kernel\LinkItemSerializationTest::testLinkSerialization()
  2. 8.9.x core/modules/link/tests/src/Kernel/LinkItemSerializationTest.php \Drupal\Tests\link\Kernel\LinkItemSerializationTest::testLinkSerialization()
  3. 11.x 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 60

Class

LinkItemSerializationTest
Tests link field serialization.

Namespace

Drupal\Tests\link\Kernel

Code

public function testLinkSerialization() : void {
    // 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);
}

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