function EmailFieldTest::testEmailField

Same name in other branches
  1. 9 core/modules/field/tests/src/Functional/Email/EmailFieldTest.php \Drupal\Tests\field\Functional\Email\EmailFieldTest::testEmailField()
  2. 8.9.x core/modules/field/tests/src/Functional/Email/EmailFieldTest.php \Drupal\Tests\field\Functional\Email\EmailFieldTest::testEmailField()
  3. 11.x core/modules/field/tests/src/Functional/Email/EmailFieldTest.php \Drupal\Tests\field\Functional\Email\EmailFieldTest::testEmailField()

Tests email field.

File

core/modules/field/tests/src/Functional/Email/EmailFieldTest.php, line 59

Class

EmailFieldTest
Tests email field functionality.

Namespace

Drupal\Tests\field\Functional\Email

Code

public function testEmailField() : void {
    // Create a field with settings to validate.
    $field_name = $this->randomMachineName();
    $this->fieldStorage = FieldStorageConfig::create([
        'field_name' => $field_name,
        'entity_type' => 'entity_test',
        'type' => 'email',
    ]);
    $this->fieldStorage
        ->save();
    $this->field = FieldConfig::create([
        'field_storage' => $this->fieldStorage,
        'bundle' => 'entity_test',
    ]);
    $this->field
        ->save();
    
    /** @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface $display_repository */
    $display_repository = \Drupal::service('entity_display.repository');
    // Create a form display for the default form mode.
    $display_repository->getFormDisplay('entity_test', 'entity_test')
        ->setComponent($field_name, [
        'type' => 'email_default',
        'settings' => [
            'placeholder' => 'example@example.com',
        ],
    ])
        ->save();
    // Create a display for the full view mode.
    $display_repository->getViewDisplay('entity_test', 'entity_test', 'full')
        ->setComponent($field_name, [
        'type' => 'email_mailto',
    ])
        ->save();
    // Display creation form.
    $this->drupalGet('entity_test/add');
    $this->assertSession()
        ->fieldValueEquals("{$field_name}[0][value]", '');
    $this->assertSession()
        ->responseContains('placeholder="example@example.com"');
    // Submit a valid email address and ensure it is accepted.
    $value = 'test@example.com';
    $edit = [
        "{$field_name}[0][value]" => $value,
    ];
    $this->submitForm($edit, 'Save');
    preg_match('|entity_test/manage/(\\d+)|', $this->getUrl(), $match);
    $id = $match[1];
    $this->assertSession()
        ->pageTextContains('entity_test ' . $id . ' has been created.');
    $this->assertSession()
        ->responseContains($value);
    // Verify that a mailto link is displayed.
    $entity = EntityTest::load($id);
    $display = $display_repository->getViewDisplay($entity->getEntityTypeId(), $entity->bundle(), 'full');
    $content = $display->build($entity);
    $rendered_content = (string) \Drupal::service('renderer')->renderRoot($content);
    $this->assertStringContainsString('href="mailto:test@example.com"', $rendered_content);
    // Test Email validation message.
    $this->drupalGet('entity_test/add');
    $value = 'abc.@in';
    $edit = [
        "{$field_name}[0][value]" => $value,
    ];
    $this->submitForm($edit, 'Save');
    $this->assertSession()
        ->statusMessageContains("The email address {$value} is not valid. Use the format user@example.com.", 'error');
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.