You are here

public function LinkFieldUITest::runFieldUIItem in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/link/tests/src/Functional/LinkFieldUITest.php \Drupal\Tests\link\Functional\LinkFieldUITest::runFieldUIItem()

Tests one link field UI item.

Parameters

int $cardinality: The field cardinality.

int $link_type: Determine if the link is external, internal or both.

int $title: Determine if the field will display the link text field.

string $label: The field label.

string $field_name: The unique machine name for the field.

1 call to LinkFieldUITest::runFieldUIItem()
LinkFieldUITest::testFieldUI in core/modules/link/tests/src/Functional/LinkFieldUITest.php
Tests the link field UI.

File

core/modules/link/tests/src/Functional/LinkFieldUITest.php, line 159

Class

LinkFieldUITest
Tests link field UI functionality.

Namespace

Drupal\Tests\link\Functional

Code

public function runFieldUIItem($cardinality, $link_type, $title, $label, $field_name) {
  $this
    ->drupalLogin($this->adminUser);
  $type_path = 'admin/structure/types/manage/' . $this->firstContentType
    ->id();

  // Add a link field to the newly-created type. It defaults to allowing both
  // internal and external links.
  $field_label = str_replace('_', ' ', $field_name);
  $description = 'link field description';
  $field_edit = [
    'description' => $description,
  ];
  $this
    ->fieldUIAddNewField($type_path, $field_name, $field_label, 'link', [], $field_edit);

  // Load the formatter page to check that the settings summary does not
  // generate warnings.
  // @todo Mess with the formatter settings a bit here.
  $this
    ->drupalGet("{$type_path}/display");
  $this
    ->assertText(t('Link text trimmed to @limit characters', [
    '@limit' => 80,
  ]));
  $storage = FieldStorageConfig::create([
    'field_name' => $field_name,
    'entity_type' => 'node',
    'type' => 'link',
    'cardinality' => $cardinality,
  ]);
  $storage
    ->save();
  FieldConfig::create([
    'field_storage' => $storage,
    'label' => $label,
    'bundle' => $this->secondContentType
      ->id(),
    'settings' => [
      'title' => $title,
      'link_type' => $link_type,
    ],
  ])
    ->save();

  // Make the fields visible in the form display.
  $form_display_id = implode('.', [
    'node',
    $this->secondContentType
      ->id(),
    'default',
  ]);
  $form_display = EntityFormDisplay::load($form_display_id);
  $form_display
    ->setComponent($field_name, [
    'region' => 'content',
  ]);
  $form_display
    ->save();

  // Log in a user that is allowed to create this content type, see if
  // the user can see the expected help text.
  $this
    ->drupalLogin($this->helpTextUser);
  $add_path = 'node/add/' . $this->secondContentType
    ->id();
  $this
    ->drupalGet($add_path);
  $expected_help_texts = [
    LinkItemInterface::LINK_EXTERNAL => 'This must be an external URL such as <em class="placeholder">http://example.com</em>.',
    LinkItemInterface::LINK_GENERIC => 'You can also enter an internal path such as <em class="placeholder">/node/add</em> or an external URL such as <em class="placeholder">http://example.com</em>. Enter <em class="placeholder">&lt;front&gt;</em> to link to the front page. Enter <em class="placeholder">&lt;nolink&gt;</em> to display link text only',
    LinkItemInterface::LINK_INTERNAL => rtrim(Url::fromRoute('<front>', [], [
      'absolute' => TRUE,
    ])
      ->toString(), '/'),
  ];

  // Check that the help texts we assume should be there, is there.
  $this
    ->assertFieldContainsRawText($field_name, $expected_help_texts[$link_type]);
  if ($link_type === LinkItemInterface::LINK_INTERNAL) {

    // Internal links have no "system" description. Test that none
    // of the other help texts show here.
    $this
      ->assertNoFieldContainsRawText($field_name, $expected_help_texts[LinkItemInterface::LINK_EXTERNAL]);
    $this
      ->assertNoFieldContainsRawText($field_name, $expected_help_texts[LinkItemInterface::LINK_GENERIC]);
  }

  // Also assert that the description we made is here, no matter what the
  // cardinality or link setting.
  if (!empty($label)) {
    $this
      ->assertFieldContainsRawText($field_name, $label);
  }
}