You are here

protected function FieldEntityLinkTest::doTestEntityLink in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/views/tests/src/Kernel/Handler/FieldEntityLinkTest.php \Drupal\Tests\views\Kernel\Handler\FieldEntityLinkTest::doTestEntityLink()

Tests whether entity links behave as expected.

Parameters

\Drupal\Core\Session\AccountInterface $account: The user account to be used to run the test;

bool[] $expected_results: An associative array of expected results keyed by link template name.

1 call to FieldEntityLinkTest::doTestEntityLink()
FieldEntityLinkTest::testEntityLink in core/modules/views/tests/src/Kernel/Handler/FieldEntityLinkTest.php
Tests entity link fields.

File

core/modules/views/tests/src/Kernel/Handler/FieldEntityLinkTest.php, line 88

Class

FieldEntityLinkTest
Tests the core Drupal\views\Plugin\views\field\EntityOperations handler.

Namespace

Drupal\Tests\views\Kernel\Handler

Code

protected function doTestEntityLink(AccountInterface $account, $expected_results) {
  \Drupal::currentUser()
    ->setAccount($account);
  $view = Views::getView('test_entity_test_link');
  $view
    ->preview();
  $info = [
    'canonical' => [
      'label' => 'View entity test',
      'field_id' => 'view_entity_test',
      'destination' => FALSE,
      'link' => TRUE,
      'options' => [],
      'relationship' => 'canonical',
    ],
    'edit-form' => [
      'label' => 'Edit entity test',
      'field_id' => 'edit_entity_test',
      'destination' => TRUE,
      'link' => TRUE,
      'options' => [],
      'relationship' => 'edit-form',
    ],
    'delete-form' => [
      'label' => 'Delete entity test',
      'field_id' => 'delete_entity_test',
      'destination' => TRUE,
      'link' => TRUE,
      'options' => [],
      'relationship' => 'delete-form',
    ],
    'canonical_raw' => [
      'field_id' => 'canonical_entity_test',
      'destination' => FALSE,
      'link' => FALSE,
      'options' => [],
      'relationship' => 'canonical',
    ],
    'canonical_raw_absolute' => [
      'field_id' => 'absolute_entity_test',
      'destination' => FALSE,
      'link' => FALSE,
      'options' => [
        'absolute' => TRUE,
      ],
      'relationship' => 'canonical',
    ],
  ];
  $index = 0;
  foreach (EntityTest::loadMultiple() as $entity) {
    foreach ($expected_results as $template => $expected_result) {
      $expected_link = '';
      if ($expected_result) {
        $path = $entity
          ->toUrl($info[$template]['relationship'], $info[$template]['options'])
          ->toString();
        $destination = $info[$template]['destination'] ? '?destination=/' : '';
        if ($info[$template]['link']) {
          $expected_link = '<a href="' . $path . $destination . '" hreflang="en">' . $info[$template]['label'] . '</a>';
        }
        else {
          $expected_link = $path;
        }
      }
      $link = $view->style_plugin
        ->getField($index, $info[$template]['field_id']);
      $this
        ->assertEquals($expected_link, $link);
    }
    $index++;
  }
}