You are here

public function LinkAttributesFieldTest::testWidgetWithoutAttributes in Link Attributes widget 8

Tests saving a node without any attributes enabled in the widget settings.

File

tests/src/Functional/LinkAttributesFieldTest.php, line 145

Class

LinkAttributesFieldTest
Tests link attributes functionality.

Namespace

Drupal\Tests\link_attributes\Functional

Code

public function testWidgetWithoutAttributes() {

  // Add a content type.
  $type = $this
    ->drupalCreateContentType();
  $type_path = 'admin/structure/types/manage/' . $type
    ->id();
  $add_path = 'node/add/' . $type
    ->id();

  // Add a link field to the newly-created type.
  $label = $this
    ->randomMachineName();
  $field_name = mb_strtolower($label);
  $storage_settings = [
    'cardinality' => 'number',
    'cardinality_number' => 2,
  ];
  $this
    ->fieldUIAddNewField($type_path, $field_name, $label, 'link', $storage_settings);

  // Manually clear cache on the tester side.
  \Drupal::service('entity_field.manager')
    ->clearCachedFieldDefinitions();
  \Drupal::entityTypeManager()
    ->getStorage('entity_form_display')
    ->load('node.' . $type
    ->id() . '.default')
    ->setComponent('field_' . $field_name, [
    'type' => 'link_attributes',
    'settings' => [
      'enabled_attributes' => [],
    ],
  ])
    ->save();
  $this
    ->drupalGet($add_path);
  $web_assert = $this
    ->assertSession();

  // Link attributes.
  $web_assert
    ->elementExists('css', '.field--widget-link-attributes');

  // Create a node.
  $edit = [
    'title[0][value]' => 'A multi field link test',
    'field_' . $field_name . '[0][title]' => 'Link One',
    'field_' . $field_name . '[0][uri]' => '<front>',
  ];
  $this
    ->submitForm($edit, 'Save');
  $node = $this
    ->drupalGetNodeByTitle($edit['title[0][value]']);
  $this
    ->drupalGet($node
    ->toUrl()
    ->toString());
  $web_assert
    ->linkExists('Link One');
}