You are here

public function TelephoneItemTest::testTestItem in Zircon Profile 8.0

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

Tests using entity fields of the telephone field type.

File

core/modules/telephone/src/Tests/TelephoneItemTest.php, line 47
Contains \Drupal\telephone\Tests\TelephoneItemTest.

Class

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

Namespace

Drupal\telephone\Tests

Code

public function testTestItem() {

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

  // Verify entity has been created properly.
  $id = $entity
    ->id();
  $entity = entity_load('entity_test', $id);
  $this
    ->assertTrue($entity->field_test instanceof FieldItemListInterface, 'Field implements interface.');
  $this
    ->assertTrue($entity->field_test[0] instanceof FieldItemInterface, 'Field item implements interface.');
  $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 = entity_load('entity_test', $id);
  $this
    ->assertEqual($entity->field_test->value, $new_value);

  // Test sample item generation.
  $entity = entity_create('entity_test');
  $entity->field_test
    ->generateSampleItems();
  $this
    ->entityValidateAndSave($entity);
}