You are here

public function PhoneInternationalItemTest::testTestItem in International Phone 3.x

Same name and namespace in other branches
  1. 8.2 tests/src/Kernel/PhoneInternationalItemTest.php \Drupal\Tests\phone_international\Kernel\PhoneInternationalItemTest::testTestItem()
  2. 8 tests/src/Kernel/PhoneInternationalItemTest.php \Drupal\Tests\phone_international\Kernel\PhoneInternationalItemTest::testTestItem()

Tests using entity fields of the phone_international field type.

File

tests/src/Kernel/PhoneInternationalItemTest.php, line 29

Class

PhoneInternationalItemTest
Tests the new entity API for the phone_international field type.

Namespace

Drupal\Tests\phone_international\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
    ->assertTrue($entity->field_test instanceof FieldItemListInterface, 'Field implements interface.');
  $this
    ->assertTrue($entity->field_test[0] instanceof FieldItemInterface, 'Field item implements interface.');
  $this
    ->assertEquals($entity->field_test->value, $value);
  $this
    ->assertEquals($entity->field_test[0]->value, $value);

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

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

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