3 namespace Drupal\field\Tests\Email;
5 use Drupal\Component\Utility\Unicode;
6 use Drupal\entity_test\Entity\EntityTest;
7 use Drupal\field\Entity\FieldConfig;
8 use Drupal\simpletest\WebTestBase;
9 use Drupal\field\Entity\FieldStorageConfig;
12 * Tests email field functionality.
16 class EmailFieldTest extends WebTestBase {
23 public static $modules = ['node', 'entity_test', 'field_ui'];
26 * A field storage to use in this test class.
28 * @var \Drupal\field\Entity\FieldStorageConfig
30 protected $fieldStorage;
33 * The field used in this test class.
35 * @var \Drupal\field\Entity\FieldConfig
39 protected function setUp() {
42 $this->drupalLogin($this->drupalCreateUser([
44 'administer entity_test content',
45 'administer content types',
52 public function testEmailField() {
53 // Create a field with settings to validate.
54 $field_name = Unicode::strtolower($this->randomMachineName());
55 $this->fieldStorage = FieldStorageConfig::create([
56 'field_name' => $field_name,
57 'entity_type' => 'entity_test',
60 $this->fieldStorage->save();
61 $this->field = FieldConfig::create([
62 'field_storage' => $this->fieldStorage,
63 'bundle' => 'entity_test',
67 // Create a form display for the default form mode.
68 entity_get_form_display('entity_test', 'entity_test', 'default')
69 ->setComponent($field_name, [
70 'type' => 'email_default',
72 'placeholder' => 'example@example.com',
76 // Create a display for the full view mode.
77 entity_get_display('entity_test', 'entity_test', 'full')
78 ->setComponent($field_name, [
79 'type' => 'email_mailto',
83 // Display creation form.
84 $this->drupalGet('entity_test/add');
85 $this->assertFieldByName("{$field_name}[0][value]", '', 'Widget found.');
86 $this->assertRaw('placeholder="example@example.com"');
88 // Submit a valid email address and ensure it is accepted.
89 $value = 'test@example.com';
91 "{$field_name}[0][value]" => $value,
93 $this->drupalPostForm(NULL, $edit, t('Save'));
94 preg_match('|entity_test/manage/(\d+)|', $this->url, $match);
96 $this->assertText(t('entity_test @id has been created.', ['@id' => $id]));
97 $this->assertRaw($value);
99 // Verify that a mailto link is displayed.
100 $entity = EntityTest::load($id);
101 $display = entity_get_display($entity->getEntityTypeId(), $entity->bundle(), 'full');
102 $content = $display->build($entity);
103 $this->setRawContent(\Drupal::service('renderer')->renderRoot($content));
104 $this->assertLinkByHref('mailto:test@example.com');