You are here

public function TelephoneItemTest::testTestItem in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/telephone/tests/src/Kernel/TelephoneItemTest.php \Drupal\Tests\telephone\Kernel\TelephoneItemTest::testTestItem()

Tests using entity fields of the telephone field type.

File

core/modules/telephone/tests/src/Kernel/TelephoneItemTest.php, line 46

Class

TelephoneItemTest
Tests the new entity API for the telephone field type.

Namespace

Drupal\Tests\telephone\Kernel

Code

public function testTestItem() {

  // Verify entity creation.
  $entity = EntityTest::create();
  $value = '+0123456789';
  $entity->field_test = $value;
  $entity->name->value = $this
    ->randomMachineName();
  $entity
    ->save();

  // Verify entity has been created properly.
  $id = $entity
    ->id();
  $entity = EntityTest::load($id);
  $this
    ->assertInstanceOf(FieldItemListInterface::class, $entity->field_test);
  $this
    ->assertInstanceOf(FieldItemInterface::class, $entity->field_test[0]);
  $this
    ->assertEqual($entity->field_test->value, $value);
  $this
    ->assertEqual($entity->field_test[0]->value, $value);

  // Verify changing the field value.
  $new_value = '+41' . rand(1000000, 9999999);
  $entity->field_test->value = $new_value;
  $this
    ->assertEqual($entity->field_test->value, $new_value);

  // Read changed entity and assert changed values.
  $entity
    ->save();
  $entity = EntityTest::load($id);
  $this
    ->assertEqual($entity->field_test->value, $new_value);

  // Test sample item generation.
  $entity = EntityTest::create();
  $entity->field_test
    ->generateSampleItems();
  $this
    ->entityValidateAndSave($entity);
}