You are here

public function LinkProviderTest::testLinkProviders in JSON:API Hypermedia 8

Tests that link provider plugins properly affect the JSON:API output.

@dataProvider pluginDefinitionTemplates

File

tests/src/Functional/LinkProviderTest.php, line 91

Class

LinkProviderTest
Class LinkProviderTest.

Namespace

Drupal\Tests\jsonapi_hypermedia\Functional

Code

public function testLinkProviders($plugin_template) {
  $link_location = $plugin_template['location'];
  $expected_on_document_types = $plugin_template['presence'] ?? TRUE;
  $this->state
    ->set('jsonapi_hypermedia_test_link_providers.template', $plugin_template);
  $this
    ->rebuildAll();
  $test_definitions = array_filter($this->linkManager
    ->getDefinitions(), function ($definition) {
    return $definition['provider'] === 'jsonapi_hypermedia_test_link_providers';
  });
  foreach ($test_definitions as $plugin_id => $test_definition) {
    $link_key = $test_definition['link_key'] ?? $test_definition['link_relation_type'];
    if (!empty($test_definition['_test_restrict_access'])) {
      $link = $this
        ->getLink($link_location, $link_key, []);
      $this
        ->assertNull($link);
      $this
        ->grantPermissions(Role::load(AccountInterface::ANONYMOUS_ROLE), [
        "view {$test_definition['link_key']} link",
      ]);
      $link = $this
        ->getLink($link_location, $link_key, $expected_on_document_types);
    }
    else {
      $link = $this
        ->getLink($link_location, $link_key, $expected_on_document_types);
    }
    if (!empty($expected_on_document_types)) {
      $this
        ->assertNotNull($link);
    }
    else {
      $this
        ->assertNull($link);
      continue;
    }
    if (!empty($test_definition['_test_target_attributes'])) {
      $actual = NestedArray::getValue($link, [
        'meta',
        'linkParams',
      ]);
      $this
        ->assertNotNull($actual);
      $this
        ->assertSame($test_definition['_test_target_attributes'], array_diff_key($actual, array_flip([
        'rel',
        'randomAttr',
      ])));
    }
    $rel_exists = NULL;
    $actual_rel = NestedArray::getValue($link, [
      'meta',
      'linkParams',
      'rel',
    ], $rel_exists);
    if ($test_definition['link_relation_type'] === 'test') {
      $this
        ->assertTrue($rel_exists);
      $this
        ->assertSame('https://drupal.org/project/jsonapi_hypermedia/link-relations/#test', $actual_rel[0]);
    }
    elseif (empty($test_definition['link_key'])) {
      $this
        ->assertFalse($rel_exists);
    }
    else {
      $this
        ->assertTrue($rel_exists);
      $this
        ->assertSame('related', $actual_rel[0]);
    }
    $cache_hit_link = $this
      ->getLink($link_location, $link_key, $expected_on_document_types);
    $this
      ->assertSame($link, $cache_hit_link);
    Cache::invalidateTags([
      'test_jsonapi_hypermedia_cache_tag',
    ]);
    $cache_miss_link = $this
      ->getLink($link_location, $link_key, $expected_on_document_types);
    $this
      ->assertNotSame($cache_hit_link, $cache_miss_link);
  }
}