function EmailTest::testFormEmail

Same name in other branches
  1. 9 core/modules/system/tests/src/Functional/Form/EmailTest.php \Drupal\Tests\system\Functional\Form\EmailTest::testFormEmail()
  2. 10 core/modules/system/tests/src/Functional/Form/EmailTest.php \Drupal\Tests\system\Functional\Form\EmailTest::testFormEmail()
  3. 11.x core/modules/system/tests/src/Functional/Form/EmailTest.php \Drupal\Tests\system\Functional\Form\EmailTest::testFormEmail()

Tests that #type 'email' fields are properly validated.

File

core/modules/system/tests/src/Functional/Form/EmailTest.php, line 30

Class

EmailTest
Tests the form API email element.

Namespace

Drupal\Tests\system\Functional\Form

Code

public function testFormEmail() {
    $edit = [];
    $edit['email'] = 'invalid';
    $edit['email_required'] = ' ';
    $this->drupalPostForm('form-test/email', $edit, 'Submit');
    $this->assertRaw(t('The email address %mail is not valid.', [
        '%mail' => 'invalid',
    ]));
    $this->assertRaw(t('@name field is required.', [
        '@name' => 'Address',
    ]));
    $edit = [];
    $edit['email_required'] = '  foo.bar@example.com ';
    $this->drupalPostForm('form-test/email', $edit, 'Submit');
    $values = Json::decode($this->getSession()
        ->getPage()
        ->getContent());
    $this->assertIdentical($values['email'], '');
    $this->assertEqual($values['email_required'], 'foo.bar@example.com');
    $edit = [];
    $edit['email'] = 'foo@example.com';
    $edit['email_required'] = 'example@drupal.org';
    $this->drupalPostForm('form-test/email', $edit, 'Submit');
    $values = Json::decode($this->getSession()
        ->getPage()
        ->getContent());
    $this->assertEqual($values['email'], 'foo@example.com');
    $this->assertEqual($values['email_required'], 'example@drupal.org');
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.