You are here

public function FileTest::testBuildLink in Freelinking 4.0.x

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

Assert that buildLink is functional.

@dataProvider buildLinkProvider

Parameters

string $path: The file path.

int $availability: The file availability.

File

tests/src/Unit/Plugin/freelinking/FileTest.php, line 100

Class

FileTest
Tests the freelinking file plugin.

Namespace

Drupal\Tests\freelinking\Unit\Plugin\freelinking

Code

public function testBuildLink($path, $availability) {

  // Reset the module_handler and stream_wrapper_manager services based on the
  // data for the test.
  $this->container
    ->set('module_handler', $this
    ->getModuleHandlerMock($availability));
  $this->container
    ->set('stream_wrapper_manager', $this
    ->getStreamWrapperMock($availability));
  \Drupal::setContainer($this->container);

  // Setup expected result.
  if ($availability > 0) {
    $expected = [
      '#type' => 'link',
      '#title' => 'Test File',
      '#url' => Url::fromUri('http://example.com/logo.png', [
        'absolute' => TRUE,
        'language' => NULL,
      ]),
      '#attributes' => [
        'title' => new TranslatableMarkup('Click to view a local file.', [], [], $this->translationInterfaceMock),
      ],
    ];
  }
  else {
    $expected = [
      '#theme' => 'freelink_error',
      '#plugin' => 'file',
      '#message' => new TranslatableMarkup('File @name not found', [
        '@name' => 'logo.png',
      ], [], $this->translationInterfaceMock),
    ];
  }
  $target = [
    'target' => 'file:logo.png|Test File',
    'dest' => 'logo.png',
    'text' => 'Test File',
    'language' => NULL,
  ];
  $plugin = $this
    ->getPlugin();
  $this
    ->assertEquals($expected, $plugin
    ->buildLink($target));
}