LinkItemTest.php 6.72 KB
Newer Older
1 2
<?php

3
namespace Drupal\Tests\link\Kernel;
4

5
use Drupal\Component\Utility\UrlHelper;
6 7
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FieldItemInterface;
8
use Drupal\Core\Url;
9
use Drupal\entity_test\Entity\EntityTest;
10 11
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
12
use Drupal\Tests\field\Kernel\FieldKernelTestBase;
13
use Drupal\link\LinkItemInterface;
14 15 16

/**
 * Tests the new entity API for the link field type.
17 18
 *
 * @group link
19
 */
20
class LinkItemTest extends FieldKernelTestBase {
21 22 23 24 25 26

  /**
   * Modules to enable.
   *
   * @var array
   */
27
  protected static $modules = ['link'];
28

29
  protected function setUp(): void {
30 31
    parent::setUp();

32 33
    // Create a generic, external, and internal link fields for validation.
    FieldStorageConfig::create([
34
      'field_name' => 'field_test',
35
      'entity_type' => 'entity_test',
36
      'type' => 'link',
37 38
    ])->save();
    FieldConfig::create([
39 40 41
      'entity_type' => 'entity_test',
      'field_name' => 'field_test',
      'bundle' => 'entity_test',
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
      'settings' => ['link_type' => LinkItemInterface::LINK_GENERIC],
    ])->save();
    FieldStorageConfig::create([
      'field_name' => 'field_test_external',
      'entity_type' => 'entity_test',
      'type' => 'link',
    ])->save();
    FieldConfig::create([
      'entity_type' => 'entity_test',
      'field_name' => 'field_test_external',
      'bundle' => 'entity_test',
      'settings' => ['link_type' => LinkItemInterface::LINK_EXTERNAL],
    ])->save();
    FieldStorageConfig::create([
      'field_name' => 'field_test_internal',
      'entity_type' => 'entity_test',
      'type' => 'link',
    ])->save();
    FieldConfig::create([
      'entity_type' => 'entity_test',
      'field_name' => 'field_test_internal',
      'bundle' => 'entity_test',
      'settings' => ['link_type' => LinkItemInterface::LINK_INTERNAL],
    ])->save();
66 67 68 69 70 71 72
  }

  /**
   * Tests using entity fields of the link field type.
   */
  public function testLinkItem() {
    // Create entity.
73
    $entity = EntityTest::create();
74
    $url = 'https://www.drupal.org?test_param=test_value';
75
    $parsed_url = UrlHelper::parse($url);
76 77
    $title = $this->randomMachineName();
    $class = $this->randomMachineName();
78
    $entity->field_test->uri = $parsed_url['path'];
79
    $entity->field_test->title = $title;
80
    $entity->field_test->first()->get('options')->set('query', $parsed_url['query']);
81
    $entity->field_test->first()->get('options')->set('attributes', ['class' => $class]);
82 83 84 85 86 87 88
    $this->assertEquals([
      'query' => $parsed_url['query'],
      'attributes' => [
        'class' => $class,
      ],
      'external' => TRUE,
    ], $entity->field_test->first()->getUrl()->getOptions());
89
    $entity->name->value = $this->randomMachineName();
90 91 92 93
    $entity->save();

    // Verify that the field value is changed.
    $id = $entity->id();
94
    $entity = EntityTest::load($id);
95 96
    $this->assertInstanceOf(FieldItemListInterface::class, $entity->field_test);
    $this->assertInstanceOf(FieldItemInterface::class, $entity->field_test[0]);
97 98
    $this->assertEqual($entity->field_test->uri, $parsed_url['path']);
    $this->assertEqual($entity->field_test[0]->uri, $parsed_url['path']);
99 100
    $this->assertEqual($entity->field_test->title, $title);
    $this->assertEqual($entity->field_test[0]->title, $title);
101 102 103 104 105
    $this->assertEqual($entity->field_test->options['attributes']['class'], $class);
    $this->assertEqual($entity->field_test->options['query'], $parsed_url['query']);

    // Update only the entity name property to check if the link field data will
    // remain intact.
106
    $entity->name->value = $this->randomMachineName();
107 108
    $entity->save();
    $id = $entity->id();
109
    $entity = EntityTest::load($id);
110
    $this->assertEqual($entity->field_test->uri, $parsed_url['path']);
111 112
    $this->assertEqual($entity->field_test->options['attributes']['class'], $class);
    $this->assertEqual($entity->field_test->options['query'], $parsed_url['query']);
113 114

    // Verify changing the field value.
115
    $new_url = 'https://www.drupal.org';
116 117
    $new_title = $this->randomMachineName();
    $new_class = $this->randomMachineName();
118
    $entity->field_test->uri = $new_url;
119
    $entity->field_test->title = $new_title;
120
    $entity->field_test->first()->get('options')->set('query', NULL);
121
    $entity->field_test->first()->get('options')->set('attributes', ['class' => $new_class]);
122
    $this->assertEqual($entity->field_test->uri, $new_url);
123
    $this->assertEqual($entity->field_test->title, $new_title);
124 125
    $this->assertEqual($entity->field_test->options['attributes']['class'], $new_class);
    $this->assertNull($entity->field_test->options['query']);
126 127 128

    // Read changed entity and assert changed values.
    $entity->save();
129
    $entity = EntityTest::load($id);
130
    $this->assertEqual($entity->field_test->uri, $new_url);
131
    $this->assertEqual($entity->field_test->title, $new_title);
132
    $this->assertEqual($entity->field_test->options['attributes']['class'], $new_class);
133

134 135 136 137 138 139 140
    // Check that if we only set uri the default values for title and options
    // are also initialized.
    $entity->field_test = ['uri' => 'internal:/node/add'];
    $this->assertEqual($entity->field_test->uri, 'internal:/node/add');
    $this->assertNull($entity->field_test->title);
    $this->assertIdentical($entity->field_test->options, []);

141 142
    // Check that if we set uri and options then the default values are properly
    // initialized.
143 144
    $entity->field_test = [
      'uri' => 'internal:/node/add',
145
      'options' => ['query' => NULL],
146 147 148 149 150 151 152 153 154 155 156 157
    ];
    $this->assertEqual($entity->field_test->uri, 'internal:/node/add');
    $this->assertNull($entity->field_test->title);
    $this->assertNull($entity->field_test->options['query']);

    // Check that if we set the direct value of link field it correctly set the
    // uri and the default values of the field.
    $entity->field_test = 'internal:/node/add';
    $this->assertEqual($entity->field_test->uri, 'internal:/node/add');
    $this->assertNull($entity->field_test->title);
    $this->assertIdentical($entity->field_test->options, []);

158 159 160 161 162
    // Check that setting options to NULL does not trigger an error when
    // calling getUrl();
    $entity->field_test->options = NULL;
    $this->assertInstanceOf(Url::class, $entity->field_test[0]->getUrl());

163 164 165 166 167
    // Check that setting LinkItem value NULL doesn't generate any error or
    // warning.
    $entity->field_test[0] = NULL;
    $this->assertNull($entity->field_test[0]->getValue());

168 169
    // Test the generateSampleValue() method for generic, external, and internal
    // link types.
170
    $entity = EntityTest::create();
171
    $entity->field_test->generateSampleItems();
172 173
    $entity->field_test_external->generateSampleItems();
    $entity->field_test_internal->generateSampleItems();
174
    $this->entityValidateAndSave($entity);
175 176 177
  }

}