EntityKeysTest.php 1.39 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php

namespace Drupal\KernelTests\Core\Entity;

use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\entity_test\Entity\EntityTest;

/**
 * Test the behavior of entity keys.
 *
 * @group entity
 */
class EntityKeysTest extends EntityKernelTestBase {

  /**
16
   * Tests the cache when multiple keys reference a single field.
17
18
19
20
21
22
23
24
25
26
27
   *
   * @dataProvider multipleKeysCacheTestCases
   */
  public function testMultipleKeysCache($translatable) {
    $this->state->set('entity_test.additional_base_field_definitions', [
      'test_field' => BaseFieldDefinition::create('string')->setTranslatable($translatable),
    ]);
    $this->state->set('entity_test.entity_keys', [
      'key_1' => 'test_field',
      'key_2' => 'test_field',
    ]);
28

29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
    $this->installEntitySchema('entity_test');

    $entity = EntityTest::create([]);

    $entity->set('test_field', 'foo');
    $this->assertEquals('foo', $entity->getEntityKey('key_1'));
    $this->assertEquals('foo', $entity->getEntityKey('key_2'));

    $entity->set('test_field', 'bar');
    $this->assertEquals('bar', $entity->getEntityKey('key_1'));
    $this->assertEquals('bar', $entity->getEntityKey('key_2'));
  }

  /**
   * Data provider for ::testMultipleKeysCache.
   */
  public function multipleKeysCacheTestCases() {
    return [
      'translatable Entity Key' => [
        TRUE,
      ],
      'Non-translatable entity key' => [
        FALSE,
      ],
    ];
  }

}