ConfigFieldDefinitionTest.php 2.25 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

  /**
   * The entity manager service.
   *
18
   * @var \Drupal\Core\Entity\EntityManagerInterface
19 20 21
   */
  protected $entityManager;

22 23 24 25 26 27 28 29 30 31
  /**
   * @var string
   */
  private $entityType;

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

32 33 34
  /**
   * {@inheritdoc}
   */
35
  protected function setUp() {
36 37
    parent::setUp();

38
    // Create a field and a storage of type 'test_field', on the 'entity_test'
39 40 41
    // entity type.
    $this->entityType = 'entity_test';
    $this->bundle = 'entity_test';
42
    $this->createFieldWithStorage('', $this->entityType, $this->bundle);
43 44
    $this->entityManager = $this->container->get('entity.manager');

45 46
    // Create a second field on 'entity_test_rev'.
    $this->createFieldWithStorage('_rev', 'entity_test_rev', 'entity_test_rev');
47 48 49 50 51 52 53
  }

  /**
   * Makes sure a field definition is exposed for a configurable field.
   */
  public function testBundleFieldDefinition() {
    $definitions = $this->entityManager->getFieldDefinitions($this->entityType, $this->bundle);
54 55 56 57
    $this->assertTrue(isset($definitions[$this->fieldTestData->field->getName()]));
    $this->assertTrue($definitions[$this->fieldTestData->field->getName()] instanceof FieldDefinitionInterface);
    // Make sure fields on other entity types are not exposed.
    $this->assertFalse(isset($definitions[$this->fieldTestData->field_rev->getName()]));
58 59 60 61 62 63
  }

  /**
   * Makes sure a field storage definition is exposed for a configurable field.
   */
  public function testFieldStorageDefinition() {
64
    $field_storage_definitions = $this->entityManager->getFieldStorageDefinitions($this->entityType);
65 66 67 68
    $this->assertTrue(isset($field_storage_definitions[$this->fieldTestData->field->getName()]));
    $this->assertTrue($field_storage_definitions[$this->fieldTestData->field->getName()] instanceof FieldStorageDefinitionInterface);
    // Make sure storages on other entity types are not exposed.
    $this->assertFalse(isset($field_storage_definitions[$this->fieldTestData->field_rev->getName()]));
69 70 71
  }

}