function EmailFieldTest::testEmailField

Tests e-mail field.

File

drupal/core/modules/email/lib/Drupal/email/Tests/EmailFieldTest.php, line 47
Definition of Drupal\email\Tests\EmailFieldTest.

Class

EmailFieldTest
Tests e-mail field functionality.

Namespace

Drupal\email\Tests

Code

function testEmailField() {

  // Create a field with settings to validate.
  $this->field = array(
    'field_name' => drupal_strtolower($this
      ->randomName()),
    'type' => 'email',
  );
  field_create_field($this->field);
  $this->instance = array(
    'field_name' => $this->field['field_name'],
    'entity_type' => 'entity_test',
    'bundle' => 'entity_test',
  );
  field_create_instance($this->instance);

  // Create a form display for the default form mode.
  entity_get_form_display('entity_test', 'entity_test', 'default')
    ->setComponent($this->field['field_name'], array(
    'type' => 'email_default',
    'settings' => array(
      'placeholder' => 'example@example.com',
    ),
  ))
    ->save();

  // Create a display for the full view mode.
  entity_get_display('entity_test', 'entity_test', 'full')
    ->setComponent($this->field['field_name'], array(
    'type' => 'email_mailto',
  ))
    ->save();

  // Display creation form.
  $this
    ->drupalGet('entity_test/add');
  $langcode = Language::LANGCODE_NOT_SPECIFIED;
  $this
    ->assertFieldByName("{$this->field['field_name']}[{$langcode}][0][value]", '', 'Widget found.');
  $this
    ->assertRaw('placeholder="example@example.com"');

  // Submit a valid e-mail address and ensure it is accepted.
  $value = 'test@example.com';
  $edit = array(
    'user_id' => 1,
    'name' => $this
      ->randomName(),
    "{$this->field['field_name']}[{$langcode}][0][value]" => $value,
  );
  $this
    ->drupalPost(NULL, $edit, t('Save'));
  preg_match('|entity_test/manage/(\\d+)/edit|', $this->url, $match);
  $id = $match[1];
  $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
    ->entityType(), $entity
    ->bundle(), 'full');
  $entity->content = field_attach_view($entity, $display);
  $this
    ->drupalSetContent(drupal_render($entity->content));
  $this
    ->assertLinkByHref('mailto:test@example.com');
}