Skip to content
EmailFieldTest.php 2.92 KiB
Newer Older
 * Contains \Drupal\field\Tests\Email\EmailFieldTest.
namespace Drupal\field\Tests\Email;
use Drupal\Component\Utility\Unicode;
 */
class EmailFieldTest extends WebTestBase {

  /**
   * Modules to enable.
   *
   * @var array
   */
  public static $modules = array('node', 'entity_test', 'field_ui');
   * A field storage to use in this test class.
   * @var \Drupal\field\Entity\FieldStorageConfig
   * The field used in this test class.
   * @var \Drupal\field\Entity\FieldConfig
    $this->drupalLogin($this->drupalCreateUser(array(
      'view test entity',
      'administer entity_test content',
   */
  function testEmailField() {
    // Create a field with settings to validate.
    $field_name = Unicode::strtolower($this->randomMachineName());
    $this->fieldStorage = entity_create('field_storage_config', array(
      'entity_type' => 'entity_test',
    $this->field = entity_create('field_config', array(
      'field_storage' => $this->fieldStorage,

    // Create a form display for the default form mode.
    entity_get_form_display('entity_test', 'entity_test', 'default')
        'settings' => array(
          'placeholder' => 'example@example.com',
        ),
    // Create a display for the full view mode.
    entity_get_display('entity_test', 'entity_test', 'full')
    $this->drupalGet('entity_test/add');
    $this->assertFieldByName("{$field_name}[0][value]", '', 'Widget found.');
    $this->assertRaw('placeholder="example@example.com"');
    // Submit a valid email address and ensure it is accepted.
    $value = 'test@example.com';
    $edit = array(
    $this->drupalPostForm(NULL, $edit, t('Save'));
    preg_match('|entity_test/manage/(\d+)|', $this->url, $match);
    $this->assertText(t('entity_test @id has been created.', array('@id' => $id)));
    $this->assertRaw($value);

    // Verify that a mailto link is displayed.
    $entity = entity_load('entity_test', $id);
    $display = entity_get_display($entity->getEntityTypeId(), $entity->bundle(), 'full');
    $content = $display->build($entity);
    $this->setRawContent(\Drupal::service('renderer')->renderRoot($content));
    $this->assertLinkByHref('mailto:test@example.com');