You are here

public function AccessCheckTest::testBypassNodeAccess in Permissions by Term 8.2

Same name and namespace in other branches
  1. 8 tests/src/Kernel/AccessCheckTest.php \Drupal\Tests\permissions_by_term\Kernel\AccessCheckTest::testBypassNodeAccess()

File

tests/src/Kernel/AccessCheckTest.php, line 162

Class

AccessCheckTest
Class AccessCheckTest

Namespace

Drupal\Tests\permissions_by_term\Kernel

Code

public function testBypassNodeAccess() : void {
  Vocabulary::create([
    'name' => 'Test Multilingual',
    'vid' => 'test_multilingual',
    'langcode' => 'de',
  ])
    ->save();
  $term = Term::create([
    'name' => 'term1',
    'vid' => 'test',
    'langcode' => 'de',
  ]);
  $term
    ->save();
  $node = Node::create([
    'type' => 'page',
    'title' => 'test_title',
    'field_tags' => [
      [
        'target_id' => $term
          ->id(),
      ],
    ],
  ]);
  $node
    ->save();
  $this->accessStorage
    ->addTermPermissionsByUserIds([
    99,
  ], $term
    ->id(), 'de');
  $this
    ->assertFalse($this->accessCheck
    ->canUserAccessByNode($node, \Drupal::currentUser()
    ->id(), 'de'));
  $editorRole = Role::create([
    'id' => 'editor',
  ]);
  $editorRole
    ->grantPermission('bypass node access');
  $editorRole
    ->save();
  $user = User::load(\Drupal::currentUser()
    ->id());
  $user
    ->addRole('editor');
  $user
    ->save();
  $accountSwitcher = \Drupal::service('account_switcher');
  $accountSwitcher
    ->switchTo($user);
  $this
    ->assertTrue($this->accessCheck
    ->canUserAccessByNode($node, \Drupal::currentUser()
    ->id(), 'de'));
}