Newer
Older
Angie Byron
committed
<?php
/**
* @file
* Contains \Drupal\field\Tests\Email\EmailFieldTest.
Angie Byron
committed
*/
namespace Drupal\field\Tests\Email;
Angie Byron
committed
use Drupal\Component\Utility\Unicode;
Angie Byron
committed
use Drupal\simpletest\WebTestBase;
/**
* Tests email field functionality.
*
* @group field
Angie Byron
committed
*/
class EmailFieldTest extends WebTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('node', 'entity_test', 'field_ui');
Angie Byron
committed
/**
* A field storage to use in this test class.
*
* @var \Drupal\field\Entity\FieldStorageConfig
*/
protected $fieldStorage;
/**
* The field used in this test class.
*
* @var \Drupal\field\Entity\FieldConfig
*/
protected $field;
Alex Pott
committed
protected function setUp() {
Angie Byron
committed
parent::setUp();
Alex Pott
committed
$this->drupalLogin($this->drupalCreateUser(array(
'view test entity',
'administer entity_test content',
Angie Byron
committed
'administer content types',
Alex Pott
committed
)));
Angie Byron
committed
}
/**
* Tests email field.
Angie Byron
committed
*/
function testEmailField() {
// Create a field with settings to validate.
$field_name = Unicode::strtolower($this->randomMachineName());
$this->fieldStorage = entity_create('field_storage_config', array(
Alex Pott
committed
'field_name' => $field_name,
'entity_type' => 'entity_test',
Angie Byron
committed
'type' => 'email',
));
$this->fieldStorage->save();
$this->field = entity_create('field_config', array(
'field_storage' => $this->fieldStorage,
'bundle' => 'entity_test',
));
$this->field->save();
Alex Pott
committed
// Create a form display for the default form mode.
entity_get_form_display('entity_test', 'entity_test', 'default')
->setComponent($field_name, array(
Angie Byron
committed
'type' => 'email_default',
'settings' => array(
'placeholder' => 'example@example.com',
),
Alex Pott
committed
))
->save();
// Create a display for the full view mode.
entity_get_display('entity_test', 'entity_test', 'full')
->setComponent($field_name, array(
'type' => 'email_mailto',
))
->save();
Angie Byron
committed
// Display creation form.
$this->drupalGet('entity_test/add');
Angie Byron
committed
$this->assertFieldByName("{$field_name}[0][value]", '', 'Widget found.');
$this->assertRaw('placeholder="example@example.com"');
Angie Byron
committed
// Submit a valid email address and ensure it is accepted.
Angie Byron
committed
$value = 'test@example.com';
$edit = array(
Angie Byron
committed
"{$field_name}[0][value]" => $value,
Angie Byron
committed
);
$this->drupalPostForm(NULL, $edit, t('Save'));
Angie Byron
committed
preg_match('|entity_test/manage/(\d+)|', $this->url, $match);
Angie Byron
committed
$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->getEntityTypeId(), $entity->bundle(), 'full');
$content = $display->build($entity);
$this->setRawContent(\Drupal::service('renderer')->renderRoot($content));
$this->assertLinkByHref('mailto:test@example.com');
Angie Byron
committed
}