ConfigFieldDefinitionTest.php 2.15 KB
Newer Older
1 2
<?php

3
namespace Drupal\Tests\field\Kernel;
4

5
use Drupal\Core\Field\FieldDefinitionInterface;
6 7 8 9
use Drupal\Core\Field\FieldStorageDefinitionInterface;

/**
 * Tests exposing field definitions for configurable fields.
10 11
 *
 * @group field
12
 */
13
class ConfigFieldDefinitionTest extends FieldKernelTestBase {
14

15 16 17 18 19 20 21 22 23 24
  /**
   * @var string
   */
  private $entityType;

  /**
   * @var string
   */
  private $bundle;

25 26 27
  /**
   * {@inheritdoc}
   */
28
  protected function setUp(): void {
29 30
    parent::setUp();

31
    // Create a field and a storage of type 'test_field', on the 'entity_test'
32 33 34
    // entity type.
    $this->entityType = 'entity_test';
    $this->bundle = 'entity_test';
35
    $this->createFieldWithStorage('', $this->entityType, $this->bundle);
36

37
    // Create a second field on 'entity_test_rev'.
38
    $this->installEntitySchema('entity_test_rev');
39
    $this->createFieldWithStorage('_rev', 'entity_test_rev', 'entity_test_rev');
40 41 42 43 44 45
  }

  /**
   * Makes sure a field definition is exposed for a configurable field.
   */
  public function testBundleFieldDefinition() {
46
    $definitions = \Drupal::service('entity_field.manager')->getFieldDefinitions($this->entityType, $this->bundle);
47
    $this->assertTrue(isset($definitions[$this->fieldTestData->field->getName()]));
48
    $this->assertInstanceOf(FieldDefinitionInterface::class, $definitions[$this->fieldTestData->field->getName()]);
49 50
    // Make sure fields on other entity types are not exposed.
    $this->assertFalse(isset($definitions[$this->fieldTestData->field_rev->getName()]));
51 52 53 54 55 56
  }

  /**
   * Makes sure a field storage definition is exposed for a configurable field.
   */
  public function testFieldStorageDefinition() {
57
    $field_storage_definitions = \Drupal::service('entity_field.manager')->getFieldStorageDefinitions($this->entityType);
58
    $this->assertTrue(isset($field_storage_definitions[$this->fieldTestData->field->getName()]));
59
    $this->assertInstanceOf(FieldStorageDefinitionInterface::class, $field_storage_definitions[$this->fieldTestData->field->getName()]);
60 61
    // Make sure storages on other entity types are not exposed.
    $this->assertFalse(isset($field_storage_definitions[$this->fieldTestData->field_rev->getName()]));
62 63 64
  }

}