You are here

public function ExternalTest::testBuildLink in Freelinking 8.3

Same name and namespace in other branches
  1. 4.0.x tests/src/Unit/Plugin/freelinking/ExternalTest.php \Drupal\Tests\freelinking\Unit\Plugin\freelinking\ExternalTest::testBuildLink()

Asserts that buildLink returns appropriate render array.

A data provider is not used for this test because Guzzle mocking is a bit weird and unorthodox.

File

tests/src/Unit/Plugin/freelinking/ExternalTest.php, line 96

Class

ExternalTest
Tests the external plugin.

Namespace

Drupal\Tests\freelinking\Unit\Plugin\freelinking

Code

public function testBuildLink() {
  $plugin = $this
    ->getPlugin();
  $target = [
    'dest' => '//www.example.com',
    'indicator' => 'http',
    'language' => NULL,
    'text' => '',
  ];
  $expected = [
    '#type' => 'link',
    '#title' => new TranslatableMarkup('Ext. link: “@title”', [
      '@title' => 'Test Page',
    ], [], $this->translationInterfaceMock),
    '#url' => Url::fromUri('http://www.example.com', [
      'absolute' => TRUE,
      'language' => NULL,
    ]),
    '#attributes' => [
      'title' => new TranslatableMarkup('Click to visit an external URL.', [], [], $this->translationInterfaceMock),
    ],
  ];

  // Assert that 200 Response with title.
  $this
    ->assertEquals($expected, $plugin
    ->buildLink($target));

  // Assert that 200 Response without title has URL as title.
  $expected['#title'] = 'http://www.example.com';
  $this
    ->assertEquals($expected, $plugin
    ->buildLink($target));

  // Assert that RequestException is handled correctly.
  $error_expected = [
    '#theme' => 'freelink_error',
    '#plugin' => 'external',
    '#message' => new TranslatableMarkup('External target “@url” not found', [
      '@url' => 'http://www.example.com',
    ], [], $this->translationInterfaceMock),
  ];
  $this
    ->assertEquals($error_expected, $plugin
    ->buildLink($target));

  // Assert that 200 Response with title is not displayed when text is set.
  $target['text'] = 'Custom Title';
  $expected['#title'] = 'Custom Title';
  $this
    ->assertEquals($expected, $plugin
    ->buildLink($target));
}