You are here

class EmailItemTest in Drupal 10

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

Tests the new entity API for the email field type.

@group field

Hierarchy

  • class \Drupal\Tests\field\Kernel\Email\EmailItemTest extends \Drupal\Tests\field\Kernel\FieldKernelTestBase

Expanded class hierarchy of EmailItemTest

File

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

Namespace

Drupal\Tests\field\Kernel\Email
View source
class EmailItemTest extends FieldKernelTestBase {
  protected function setUp() : void {
    parent::setUp();

    // Create an email field storage and field for validation.
    FieldStorageConfig::create([
      'field_name' => 'field_email',
      'entity_type' => 'entity_test',
      'type' => 'email',
    ])
      ->save();
    FieldConfig::create([
      'entity_type' => 'entity_test',
      'field_name' => 'field_email',
      'bundle' => 'entity_test',
    ])
      ->save();

    // Create a form display for the default form mode.
    \Drupal::service('entity_display.repository')
      ->getFormDisplay('entity_test', 'entity_test')
      ->setComponent('field_email', [
      'type' => 'email_default',
    ])
      ->save();
  }

  /**
   * Tests using entity fields of the email field type.
   */
  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);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EmailItemTest::setUp protected function
EmailItemTest::testEmailItem public function Tests using entity fields of the email field type.