function LinkFieldWidgetTest::testLinkWidgetCaughtExceptionEditingInvalidUrl

Test link widget exception handled if link uri value is invalid.

File

core/modules/link/tests/src/Kernel/LinkFieldWidgetTest.php, line 69

Class

LinkFieldWidgetTest
Tests link field widgets.

Namespace

Drupal\Tests\link\Kernel

Code

public function testLinkWidgetCaughtExceptionEditingInvalidUrl() : void {
  $field_name = $this->entitySetUp(LinkItemInterface::LINK_GENERIC, LinkTitleVisibility::Optional);
  // Entities can be saved without validation, for example via migration.
  // Link fields may contain invalid uris such as external URLs without
  // scheme.
  $invalidUri = 'www.example.com';
  $invalidLinkUrlEntity = $this->container
    ->get('entity_type.manager')
    ->getStorage('entity_test')
    ->create([
    'name' => 'Test entity with invalid link URL',
    $field_name => [
      'uri' => $invalidUri,
    ],
  ]);
  $invalidLinkUrlEntity->save();
  // If a user without 'link to any page' permission edits an entity, widget
  // checks access by converting uri to Url object, which will throw an
  // InvalidArgumentException if uri is invalid.
  $this->setCurrentUser($this->createUser([
    'view test entity',
    'administer entity_test content',
  ]));
  $this->drupalGet("/entity_test/manage/{$invalidLinkUrlEntity->id()}/edit");
  $this->assertSession()
    ->statusCodeEquals(200);
  $this->assertSession()
    ->fieldValueEquals("{$field_name}[0][uri]", $invalidUri);
}

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