You are here

public function EmailItemTest::testEmailItem in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/field/src/Tests/Email/EmailItemTest.php \Drupal\field\Tests\Email\EmailItemTest::testEmailItem()

Tests using entity fields of the email field type.

File

core/modules/field/src/Tests/Email/EmailItemTest.php, line 47
Contains \Drupal\field\Tests\Email\EmailItemTest.

Class

EmailItemTest
Tests the new entity API for the email field type.

Namespace

Drupal\field\Tests\Email

Code

public function testEmailItem() {

  // Verify entity creation.
  $entity = entity_create('entity_test');
  $value = 'test@example.com';
  $entity->field_email = $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_email instanceof FieldItemListInterface, 'Field implements interface.');
  $this
    ->assertTrue($entity->field_email[0] instanceof FieldItemInterface, 'Field item implements interface.');
  $this
    ->assertEqual($entity->field_email->value, $value);
  $this
    ->assertEqual($entity->field_email[0]->value, $value);

  // Verify changing the email value.
  $new_value = $this
    ->randomMachineName();
  $entity->field_email->value = $new_value;
  $this
    ->assertEqual($entity->field_email->value, $new_value);

  // Read changed entity and assert changed values.
  $entity
    ->save();
  $entity = entity_load('entity_test', $id);
  $this
    ->assertEqual($entity->field_email->value, $new_value);

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