You are here

public function LinkFieldTest::testLinkFormatter in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/link/tests/src/Functional/LinkFieldTest.php \Drupal\Tests\link\Functional\LinkFieldTest::testLinkFormatter()

Tests the default 'link' formatter.

File

core/modules/link/tests/src/Functional/LinkFieldTest.php, line 376

Class

LinkFieldTest
Tests link field widgets and formatters.

Namespace

Drupal\Tests\link\Functional

Code

public function testLinkFormatter() {
  $field_name = mb_strtolower($this
    ->randomMachineName());

  // Create a field with settings to validate.
  $this->fieldStorage = FieldStorageConfig::create([
    'field_name' => $field_name,
    'entity_type' => 'entity_test',
    'type' => 'link',
    'cardinality' => 3,
  ]);
  $this->fieldStorage
    ->save();
  FieldConfig::create([
    'field_storage' => $this->fieldStorage,
    'label' => 'Read more about this entity',
    'bundle' => 'entity_test',
    'settings' => [
      'title' => DRUPAL_OPTIONAL,
      'link_type' => LinkItemInterface::LINK_GENERIC,
    ],
  ])
    ->save();

  /** @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface $display_repository */
  $display_repository = \Drupal::service('entity_display.repository');
  $display_repository
    ->getFormDisplay('entity_test', 'entity_test')
    ->setComponent($field_name, [
    'type' => 'link_default',
  ])
    ->save();
  $display_options = [
    'type' => 'link',
    'label' => 'hidden',
  ];
  $display_repository
    ->getViewDisplay('entity_test', 'entity_test', 'full')
    ->setComponent($field_name, $display_options)
    ->save();

  // Create an entity with three link field values:
  // - The first field item uses a URL only.
  // - The second field item uses a URL and link text.
  // - The third field item uses a fragment-only URL with text.
  // For consistency in assertion code below, the URL is assigned to the title
  // variable for the first field.
  $this
    ->drupalGet('entity_test/add');
  $url1 = 'http://www.example.com/content/articles/archive?author=John&year=2012#com';
  $url2 = 'http://www.example.org/content/articles/archive?author=John&year=2012#org';
  $url3 = '#net';
  $title1 = $url1;

  // Intentionally contains an ampersand that needs sanitization on output.
  $title2 = 'A very long & strange example title that could break the nice layout of the site';
  $title3 = 'Fragment only';
  $edit = [
    "{$field_name}[0][uri]" => $url1,
    // Note that $title1 is not submitted.
    "{$field_name}[0][title]" => '',
    "{$field_name}[1][uri]" => $url2,
    "{$field_name}[1][title]" => $title2,
    "{$field_name}[2][uri]" => $url3,
    "{$field_name}[2][title]" => $title3,
  ];

  // Assert label is shown.
  $this
    ->assertSession()
    ->pageTextContains('Read more about this entity');
  $this
    ->submitForm($edit, 'Save');
  preg_match('|entity_test/manage/(\\d+)|', $this
    ->getUrl(), $match);
  $id = $match[1];
  $this
    ->assertSession()
    ->pageTextContains('entity_test ' . $id . ' has been created.');

  // Verify that the link is output according to the formatter settings.
  // Not using generatePermutations(), since that leads to 32 cases, which
  // would not test actual link field formatter functionality but rather
  // the link generator and options/attributes. Only 'url_plain' has a
  // dependency on 'url_only', so we have a total of ~10 cases.
  $options = [
    'trim_length' => [
      NULL,
      6,
    ],
    'rel' => [
      NULL,
      'nofollow',
    ],
    'target' => [
      NULL,
      '_blank',
    ],
    'url_only' => [
      [
        'url_only' => FALSE,
      ],
      [
        'url_only' => FALSE,
        'url_plain' => TRUE,
      ],
      [
        'url_only' => TRUE,
      ],
      [
        'url_only' => TRUE,
        'url_plain' => TRUE,
      ],
    ],
  ];
  foreach ($options as $setting => $values) {
    foreach ($values as $new_value) {

      // Update the field formatter settings.
      if (!is_array($new_value)) {
        $display_options['settings'] = [
          $setting => $new_value,
        ];
      }
      else {
        $display_options['settings'] = $new_value;
      }
      $display_repository
        ->getViewDisplay('entity_test', 'entity_test', 'full')
        ->setComponent($field_name, $display_options)
        ->save();
      $output = $this
        ->renderTestEntity($id);
      switch ($setting) {
        case 'trim_length':
          $url = $url1;
          $title = isset($new_value) ? Unicode::truncate($title1, $new_value, FALSE, TRUE) : $title1;
          $this
            ->assertStringContainsString('<a href="' . Html::escape($url) . '">' . Html::escape($title) . '</a>', $output);
          $url = $url2;
          $title = isset($new_value) ? Unicode::truncate($title2, $new_value, FALSE, TRUE) : $title2;
          $this
            ->assertStringContainsString('<a href="' . Html::escape($url) . '">' . Html::escape($title) . '</a>', $output);
          $url = $url3;
          $title = isset($new_value) ? Unicode::truncate($title3, $new_value, FALSE, TRUE) : $title3;
          $this
            ->assertStringContainsString('<a href="' . Html::escape($url) . '">' . Html::escape($title) . '</a>', $output);
          break;
        case 'rel':
          $rel = isset($new_value) ? ' rel="' . $new_value . '"' : '';
          $this
            ->assertStringContainsString('<a href="' . Html::escape($url1) . '"' . $rel . '>' . Html::escape($title1) . '</a>', $output);
          $this
            ->assertStringContainsString('<a href="' . Html::escape($url2) . '"' . $rel . '>' . Html::escape($title2) . '</a>', $output);
          $this
            ->assertStringContainsString('<a href="' . Html::escape($url3) . '"' . $rel . '>' . Html::escape($title3) . '</a>', $output);
          break;
        case 'target':
          $target = isset($new_value) ? ' target="' . $new_value . '"' : '';
          $this
            ->assertStringContainsString('<a href="' . Html::escape($url1) . '"' . $target . '>' . Html::escape($title1) . '</a>', $output);
          $this
            ->assertStringContainsString('<a href="' . Html::escape($url2) . '"' . $target . '>' . Html::escape($title2) . '</a>', $output);
          $this
            ->assertStringContainsString('<a href="' . Html::escape($url3) . '"' . $target . '>' . Html::escape($title3) . '</a>', $output);
          break;
        case 'url_only':

          // In this case, $new_value is an array.
          if (!$new_value['url_only']) {
            $this
              ->assertStringContainsString('<a href="' . Html::escape($url1) . '">' . Html::escape($title1) . '</a>', $output);
            $this
              ->assertStringContainsString('<a href="' . Html::escape($url2) . '">' . Html::escape($title2) . '</a>', $output);
            $this
              ->assertStringContainsString('<a href="' . Html::escape($url3) . '">' . Html::escape($title3) . '</a>', $output);
          }
          else {
            if (empty($new_value['url_plain'])) {
              $this
                ->assertStringContainsString('<a href="' . Html::escape($url1) . '">' . Html::escape($url1) . '</a>', $output);
              $this
                ->assertStringContainsString('<a href="' . Html::escape($url2) . '">' . Html::escape($url2) . '</a>', $output);
              $this
                ->assertStringContainsString('<a href="' . Html::escape($url3) . '">' . Html::escape($url3) . '</a>', $output);
            }
            else {
              $this
                ->assertStringNotContainsString('<a href="' . Html::escape($url1) . '">' . Html::escape($url1) . '</a>', $output);
              $this
                ->assertStringNotContainsString('<a href="' . Html::escape($url2) . '">' . Html::escape($url2) . '</a>', $output);
              $this
                ->assertStringNotContainsString('<a href="' . Html::escape($url3) . '">' . Html::escape($url3) . '</a>', $output);
              $this
                ->assertStringContainsString(Html::escape($url1), $output);
              $this
                ->assertStringContainsString(Html::escape($url2), $output);
              $this
                ->assertStringContainsString(Html::escape($url3), $output);
            }
          }
          break;
      }
    }
  }
}