You are here

public function UrlTest::testFormUrl in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/system/tests/src/Functional/Form/UrlTest.php \Drupal\Tests\system\Functional\Form\UrlTest::testFormUrl()

Tests that #type 'url' fields are properly validated and trimmed.

File

core/modules/system/tests/src/Functional/Form/UrlTest.php, line 32

Class

UrlTest
Tests the form API URL element.

Namespace

Drupal\Tests\system\Functional\Form

Code

public function testFormUrl() {
  $edit = [];
  $edit['url'] = 'http://';
  $edit['url_required'] = ' ';
  $this
    ->drupalGet('form-test/url');
  $this
    ->submitForm($edit, 'Submit');
  $this
    ->assertSession()
    ->pageTextContains("The URL http:// is not valid.");
  $this
    ->assertSession()
    ->pageTextContains("Required URL field is required.");
  $edit = [];
  $edit['url'] = "\n";
  $edit['url_required'] = 'http://example.com/   ';
  $this
    ->drupalGet('form-test/url');
  $this
    ->submitForm($edit, 'Submit');
  $values = Json::decode($this
    ->getSession()
    ->getPage()
    ->getContent());
  $this
    ->assertSame('', $values['url']);
  $this
    ->assertEquals('http://example.com/', $values['url_required']);
  $edit = [];
  $edit['url'] = 'http://foo.bar.example.com/';
  $edit['url_required'] = 'https://www.drupal.org/node/1174630?page=0&foo=bar#new';
  $this
    ->drupalGet('form-test/url');
  $this
    ->submitForm($edit, 'Submit');
  $values = Json::decode($this
    ->getSession()
    ->getPage()
    ->getContent());
  $this
    ->assertEquals($edit['url'], $values['url']);
  $this
    ->assertEquals($edit['url_required'], $values['url_required']);
}