Version 1
[yaffs-website] / web / core / modules / field / src / Tests / Email / EmailFieldTest.php
1 <?php
2
3 namespace Drupal\field\Tests\Email;
4
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;
10
11 /**
12  * Tests email field functionality.
13  *
14  * @group field
15  */
16 class EmailFieldTest extends WebTestBase {
17
18   /**
19    * Modules to enable.
20    *
21    * @var array
22    */
23   public static $modules = ['node', 'entity_test', 'field_ui'];
24
25   /**
26    * A field storage to use in this test class.
27    *
28    * @var \Drupal\field\Entity\FieldStorageConfig
29    */
30   protected $fieldStorage;
31
32   /**
33    * The field used in this test class.
34    *
35    * @var \Drupal\field\Entity\FieldConfig
36    */
37   protected $field;
38
39   protected function setUp() {
40     parent::setUp();
41
42     $this->drupalLogin($this->drupalCreateUser([
43       'view test entity',
44       'administer entity_test content',
45       'administer content types',
46     ]));
47   }
48
49   /**
50    * Tests email field.
51    */
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',
58       'type' => 'email',
59     ]);
60     $this->fieldStorage->save();
61     $this->field = FieldConfig::create([
62       'field_storage' => $this->fieldStorage,
63       'bundle' => 'entity_test',
64     ]);
65     $this->field->save();
66
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',
71         'settings' => [
72           'placeholder' => 'example@example.com',
73         ],
74       ])
75       ->save();
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',
80       ])
81       ->save();
82
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"');
87
88     // Submit a valid email address and ensure it is accepted.
89     $value = 'test@example.com';
90     $edit = [
91       "{$field_name}[0][value]" => $value,
92     ];
93     $this->drupalPostForm(NULL, $edit, t('Save'));
94     preg_match('|entity_test/manage/(\d+)|', $this->url, $match);
95     $id = $match[1];
96     $this->assertText(t('entity_test @id has been created.', ['@id' => $id]));
97     $this->assertRaw($value);
98
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');
105   }
106
107 }