You are here

public function EmailItemTest::testEmailItem in Drupal 9

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

Tests using entity fields of the email field type.

File

core/modules/field/tests/src/Kernel/Email/EmailItemTest.php, line 46

Class

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

Namespace

Drupal\Tests\field\Kernel\Email

Code

public function testEmailItem() {

  // Verify entity creation.
  $entity = EntityTest::create();
  $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 = EntityTest::load($id);
  $this
    ->assertInstanceOf(FieldItemListInterface::class, $entity->field_email);
  $this
    ->assertInstanceOf(FieldItemInterface::class, $entity->field_email[0]);
  $this
    ->assertEquals($value, $entity->field_email->value);
  $this
    ->assertEquals($value, $entity->field_email[0]->value);

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

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

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