Mercurial > hg > cmmr2012-drupal-site
diff core/modules/link/tests/src/Kernel/LinkItemSerializationTest.php @ 4:a9cd425dd02b
Update, including to Drupal core 8.6.10
author | Chris Cannam |
---|---|
date | Thu, 28 Feb 2019 13:11:55 +0000 |
parents | |
children | 12f9dff5fda9 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/core/modules/link/tests/src/Kernel/LinkItemSerializationTest.php Thu Feb 28 13:11:55 2019 +0000 @@ -0,0 +1,83 @@ +<?php + +namespace Drupal\Tests\link\Kernel; + +use Drupal\field\Entity\FieldStorageConfig; +use Drupal\field\Entity\FieldConfig; +use Drupal\link\LinkItemInterface; +use Drupal\entity_test\Entity\EntityTest; +use Drupal\Component\Utility\UrlHelper; +use Drupal\Tests\field\Kernel\FieldKernelTestBase; + +/** + * Tests link field serialization. + * + * @group link + */ +class LinkItemSerializationTest extends FieldKernelTestBase { + + /** + * {@inheritdoc} + */ + public static $modules = ['link', 'serialization']; + + /** + * The serializer service. + * + * @var \Symfony\Component\Serializer\SerializerInterface + */ + protected $serializer; + + /** + * {@inheritdoc} + */ + protected function setUp() { + parent::setUp(); + + $this->installEntitySchema('user'); + $this->serializer = \Drupal::service('serializer'); + + // Create a generic, external, and internal link fields for validation. + FieldStorageConfig::create([ + 'entity_type' => 'entity_test', + 'field_name' => 'field_test', + 'type' => 'link', + ])->save(); + + FieldConfig::create([ + 'entity_type' => 'entity_test', + 'field_name' => 'field_test', + 'bundle' => 'entity_test', + 'settings' => ['link_type' => LinkItemInterface::LINK_GENERIC], + ])->save(); + } + + /** + * Tests the serialization. + */ + 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); + } + +}