You are here

public function MetadataGeneratorTest::testSimpleEntityType in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/quickedit/tests/src/Kernel/MetadataGeneratorTest.php \Drupal\Tests\quickedit\Kernel\MetadataGeneratorTest::testSimpleEntityType()

Tests a simple entity type, with two different simple fields.

File

core/modules/quickedit/tests/src/Kernel/MetadataGeneratorTest.php, line 63

Class

MetadataGeneratorTest
Tests in-place field editing metadata.

Namespace

Drupal\Tests\quickedit\Kernel

Code

public function testSimpleEntityType() {
  $field_1_name = 'field_text';
  $field_1_label = 'Plain text field';
  $this
    ->createFieldWithStorage($field_1_name, 'string', 1, $field_1_label, [], 'string_textfield', [
    'size' => 42,
  ], 'string', []);
  $field_2_name = 'field_nr';
  $field_2_label = 'Simple number field';
  $this
    ->createFieldWithStorage($field_2_name, 'integer', 1, $field_2_label, [], 'number', [], 'number_integer', []);

  // Create an entity with values for this text field.
  $entity = EntityTest::create();
  $entity->{$field_1_name}->value = 'Test';
  $entity->{$field_2_name}->value = 42;
  $entity
    ->save();
  $entity = EntityTest::load($entity
    ->id());

  // Verify metadata for field 1.
  $items_1 = $entity
    ->get($field_1_name);
  $metadata_1 = $this->metadataGenerator
    ->generateFieldMetadata($items_1, 'default');
  $expected_1 = [
    'access' => TRUE,
    'label' => 'Plain text field',
    'editor' => 'plain_text',
  ];
  $this
    ->assertEqual($expected_1, $metadata_1, 'The correct metadata is generated for the first field.');

  // Verify metadata for field 2.
  $items_2 = $entity
    ->get($field_2_name);
  $metadata_2 = $this->metadataGenerator
    ->generateFieldMetadata($items_2, 'default');
  $expected_2 = [
    'access' => TRUE,
    'label' => 'Simple number field',
    'editor' => 'form',
  ];
  $this
    ->assertEqual($expected_2, $metadata_2, 'The correct metadata is generated for the second field.');
}