You are here

public function LinkCheckerLinkAccessTest::testLinkAccess in Link checker 8

Runs basic tests for link access.

File

tests/src/Kernel/LinkCheckerLinkAccessTest.php, line 107

Class

LinkCheckerLinkAccessTest
Tests basic linkchecker link access functionality.

Namespace

Drupal\Tests\linkchecker\Kernel

Code

public function testLinkAccess() {
  $webUsers = [];

  // Ensures user with 'access content' permission can view links.
  $webUsers[] = $this
    ->drupalCreateUser([
    'administer linkchecker',
    'access content',
  ]);

  // Ensures user with 'access content' permission can view links.
  $webUsers[] = $this
    ->drupalCreateUser([
    'edit linkchecker link settings',
    'access content',
  ]);

  // Ensures user without 'access content' permission can do nothing.
  $webUsers[] = $this
    ->drupalCreateUser([
    'administer linkchecker',
  ]);

  // Ensures user with 'access content' permission can do nothing.
  $webUsers[] = $this
    ->drupalCreateUser([
    'edit linkchecker link settings',
  ]);

  // Create each fieldable entity and test link access against it.
  foreach ($this->entityTypeDefinitions as $entityTypeDefinition) {
    $bundleId = $this
      ->createBundle($entityTypeDefinition);

    // Create dummy field to which link will be assigned.
    $field_storage = [
      'field_name' => 'test_text_field',
      'entity_type' => $entityTypeDefinition
        ->id(),
      'type' => 'text_long',
    ];
    FieldStorageConfig::create($field_storage)
      ->save();
    $field = [
      'field_name' => $field_storage['field_name'],
      'entity_type' => $entityTypeDefinition
        ->id(),
      'bundle' => $bundleId,
    ];
    FieldConfig::create($field)
      ->save();
    $entity = $this
      ->createEntity($entityTypeDefinition, $bundleId);
    $link = LinkCheckerLink::create([
      'url' => 'http://example.com/',
      'entity_id' => [
        'target_id' => $entity
          ->id(),
        'target_type' => $entity
          ->getEntityTypeId(),
      ],
      'entity_field' => $field_storage['field_name'],
      'entity_langcode' => $entity
        ->language()
        ->getId(),
    ]);
    $link
      ->save();
    foreach ($webUsers as $user) {
      $access = $entity
        ->get($field_storage['field_name'])
        ->access('view', $user, FALSE);
      $access = $access && $entity
        ->access('view', $user, FALSE);
      $this
        ->assertLinkAccess([
        'view' => $access,
      ], $link, $user);
    }
  }

  // Check access if parent entity is not exists.
  // By default such links will be cleaned up, but in a case if somebody will
  // remove parent entity directly from database we should handle it.
  $link = LinkCheckerLink::create([
    'url' => 'http://example.com/',
    'entity_id' => [
      'target_id' => 9999,
      'target_type' => 'node',
    ],
    'entity_field' => 'test_text_field',
    'entity_langcode' => 'en',
  ]);
  $link
    ->save();
  foreach ($webUsers as $user) {
    $this
      ->assertLinkAccess([
      'view' => FALSE,
    ], $link, $user);
  }
}