comparison 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
comparison
equal deleted inserted replaced
3:307d7a7fd348 4:a9cd425dd02b
1 <?php
2
3 namespace Drupal\Tests\link\Kernel;
4
5 use Drupal\field\Entity\FieldStorageConfig;
6 use Drupal\field\Entity\FieldConfig;
7 use Drupal\link\LinkItemInterface;
8 use Drupal\entity_test\Entity\EntityTest;
9 use Drupal\Component\Utility\UrlHelper;
10 use Drupal\Tests\field\Kernel\FieldKernelTestBase;
11
12 /**
13 * Tests link field serialization.
14 *
15 * @group link
16 */
17 class LinkItemSerializationTest extends FieldKernelTestBase {
18
19 /**
20 * {@inheritdoc}
21 */
22 public static $modules = ['link', 'serialization'];
23
24 /**
25 * The serializer service.
26 *
27 * @var \Symfony\Component\Serializer\SerializerInterface
28 */
29 protected $serializer;
30
31 /**
32 * {@inheritdoc}
33 */
34 protected function setUp() {
35 parent::setUp();
36
37 $this->installEntitySchema('user');
38 $this->serializer = \Drupal::service('serializer');
39
40 // Create a generic, external, and internal link fields for validation.
41 FieldStorageConfig::create([
42 'entity_type' => 'entity_test',
43 'field_name' => 'field_test',
44 'type' => 'link',
45 ])->save();
46
47 FieldConfig::create([
48 'entity_type' => 'entity_test',
49 'field_name' => 'field_test',
50 'bundle' => 'entity_test',
51 'settings' => ['link_type' => LinkItemInterface::LINK_GENERIC],
52 ])->save();
53 }
54
55 /**
56 * Tests the serialization.
57 */
58 public function testLinkSerialization() {
59 // Create entity.
60 $entity = EntityTest::create();
61 $url = 'https://www.drupal.org?test_param=test_value';
62 $parsed_url = UrlHelper::parse($url);
63 $title = $this->randomMachineName();
64 $class = $this->randomMachineName();
65 $entity->field_test->uri = $parsed_url['path'];
66 $entity->field_test->title = $title;
67 $entity->field_test->first()
68 ->get('options')
69 ->set('query', $parsed_url['query']);
70 $entity->field_test->first()
71 ->get('options')
72 ->set('attributes', ['class' => $class]);
73 $entity->save();
74 $serialized = $this->serializer->serialize($entity, 'json');
75 $deserialized = $this->serializer->deserialize($serialized, EntityTest::class, 'json');
76 $options_expected = [
77 'query' => $parsed_url['query'],
78 'attributes' => ['class' => $class],
79 ];
80 $this->assertSame($options_expected, $deserialized->field_test->options);
81 }
82
83 }