You are here

public function LinkFieldTest::testLinkSeparateFormatter in Drupal 8

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

Tests the 'link_separate' formatter.

This test is mostly the same as testLinkFormatter(), but they cannot be merged, since they involve different configuration and output.

File

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

Class

LinkFieldTest
Tests link field widgets and formatters.

Namespace

Drupal\Tests\link\Functional

Code

public function testLinkSeparateFormatter() {
  $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,
    'bundle' => 'entity_test',
    'settings' => [
      'title' => DRUPAL_OPTIONAL,
      'link_type' => LinkItemInterface::LINK_GENERIC,
    ],
  ])
    ->save();
  $display_options = [
    'type' => 'link_separate',
    'label' => 'hidden',
  ];

  /** @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_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';

  // 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,
    "{$field_name}[1][uri]" => $url2,
    "{$field_name}[1][title]" => $title2,
    "{$field_name}[2][uri]" => $url3,
    "{$field_name}[2][title]" => $title3,
  ];
  $this
    ->drupalPostForm(NULL, $edit, t('Save'));
  preg_match('|entity_test/manage/(\\d+)|', $this
    ->getUrl(), $match);
  $id = $match[1];
  $this
    ->assertText(t('entity_test @id has been created.', [
    '@id' => $id,
  ]));

  // Verify that the link is output according to the formatter settings.
  $options = [
    'trim_length' => [
      NULL,
      6,
    ],
    'rel' => [
      NULL,
      'nofollow',
    ],
    'target' => [
      NULL,
      '_blank',
    ],
  ];
  foreach ($options as $setting => $values) {
    foreach ($values as $new_value) {

      // Update the field formatter settings.
      $display_options['settings'] = [
        $setting => $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;
          $url_title = isset($new_value) ? Unicode::truncate($url, $new_value, FALSE, TRUE) : $url;
          $expected = '<div class="link-item">';
          $expected .= '<div class="link-url"><a href="' . Html::escape($url) . '">' . Html::escape($url_title) . '</a></div>';
          $expected .= '</div>';
          $this
            ->assertStringContainsString($expected, $output);
          $url = $url2;
          $url_title = isset($new_value) ? Unicode::truncate($url, $new_value, FALSE, TRUE) : $url;
          $title = isset($new_value) ? Unicode::truncate($title2, $new_value, FALSE, TRUE) : $title2;
          $expected = '<div class="link-item">';
          $expected .= '<div class="link-title">' . Html::escape($title) . '</div>';
          $expected .= '<div class="link-url"><a href="' . Html::escape($url) . '">' . Html::escape($url_title) . '</a></div>';
          $expected .= '</div>';
          $this
            ->assertStringContainsString($expected, $output);
          $url = $url3;
          $url_title = isset($new_value) ? Unicode::truncate($url, $new_value, FALSE, TRUE) : $url;
          $title = isset($new_value) ? Unicode::truncate($title3, $new_value, FALSE, TRUE) : $title3;
          $expected = '<div class="link-item">';
          $expected .= '<div class="link-title">' . Html::escape($title) . '</div>';
          $expected .= '<div class="link-url"><a href="' . Html::escape($url) . '">' . Html::escape($url_title) . '</a></div>';
          $expected .= '</div>';
          $this
            ->assertStringContainsString($expected, $output);
          break;
        case 'rel':
          $rel = isset($new_value) ? ' rel="' . $new_value . '"' : '';
          $this
            ->assertStringContainsString('<div class="link-url"><a href="' . Html::escape($url1) . '"' . $rel . '>' . Html::escape($url1) . '</a></div>', $output);
          $this
            ->assertStringContainsString('<div class="link-url"><a href="' . Html::escape($url2) . '"' . $rel . '>' . Html::escape($url2) . '</a></div>', $output);
          $this
            ->assertStringContainsString('<div class="link-url"><a href="' . Html::escape($url3) . '"' . $rel . '>' . Html::escape($url3) . '</a></div>', $output);
          break;
        case 'target':
          $target = isset($new_value) ? ' target="' . $new_value . '"' : '';
          $this
            ->assertStringContainsString('<div class="link-url"><a href="' . Html::escape($url1) . '"' . $target . '>' . Html::escape($url1) . '</a></div>', $output);
          $this
            ->assertStringContainsString('<div class="link-url"><a href="' . Html::escape($url2) . '"' . $target . '>' . Html::escape($url2) . '</a></div>', $output);
          $this
            ->assertStringContainsString('<div class="link-url"><a href="' . Html::escape($url3) . '"' . $target . '>' . Html::escape($url3) . '</a></div>', $output);
          break;
      }
    }
  }
}