View source
<?php
namespace Drupal\Tests\field\Functional\Email;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\Tests\BrowserTestBase;
class EmailFieldTest extends BrowserTestBase {
public static $modules = [
'node',
'entity_test',
'field_ui',
];
protected $defaultTheme = 'stark';
protected $fieldStorage;
protected $field;
protected function setUp() {
parent::setUp();
$this
->drupalLogin($this
->drupalCreateUser([
'view test entity',
'administer entity_test content',
'administer content types',
]));
}
public function testEmailField() {
$field_name = mb_strtolower($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();
$display_repository = \Drupal::service('entity_display.repository');
$display_repository
->getFormDisplay('entity_test', 'entity_test')
->setComponent($field_name, [
'type' => 'email_default',
'settings' => [
'placeholder' => 'example@example.com',
],
])
->save();
$display_repository
->getViewDisplay('entity_test', 'entity_test', 'full')
->setComponent($field_name, [
'type' => 'email_mailto',
])
->save();
$this
->drupalGet('entity_test/add');
$this
->assertFieldByName("{$field_name}[0][value]", '', 'Widget found.');
$this
->assertRaw('placeholder="example@example.com"');
$value = 'test@example.com';
$edit = [
"{$field_name}[0][value]" => $value,
];
$this
->drupalPostForm(NULL, $edit, t('Save'));
preg_match('|entity_test/manage/(\\d+)|', $this
->getUrl(), $match);
$id = $match[1];
$this
->assertText(t('entity_test @id has been created.', [
'@id' => $id,
]));
$this
->assertRaw($value);
$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);
}
}