You are here

public function BookTest::testHookNodeLoadAccess in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/book/tests/src/Functional/BookTest.php \Drupal\Tests\book\Functional\BookTest::testHookNodeLoadAccess()
  2. 9 core/modules/book/tests/src/Functional/BookTest.php \Drupal\Tests\book\Functional\BookTest::testHookNodeLoadAccess()

Ensure the loaded book in hook_node_load() does not depend on the user.

File

core/modules/book/tests/src/Functional/BookTest.php, line 655

Class

BookTest
Create a book, add pages, and test book interface.

Namespace

Drupal\Tests\book\Functional

Code

public function testHookNodeLoadAccess() {
  \Drupal::service('module_installer')
    ->install([
    'node_access_test',
  ]);

  // Ensure that the loaded book in hook_node_load() does NOT depend on the
  // current user.
  $this
    ->drupalLogin($this->bookAuthor);
  $this->book = $this
    ->createBookNode('new');

  // Reset any internal static caching.
  $node_storage = \Drupal::entityTypeManager()
    ->getStorage('node');
  $node_storage
    ->resetCache();

  // Log in as user without access to the book node, so no 'node test view'
  // permission.
  // @see node_access_test_node_grants().
  $this
    ->drupalLogin($this->webUserWithoutNodeAccess);
  $book_node = $node_storage
    ->load($this->book
    ->id());
  $this
    ->assertNotEmpty($book_node->book);
  $this
    ->assertEquals($this->book
    ->id(), $book_node->book['bid']);

  // Reset the internal cache to retrigger the hook_node_load() call.
  $node_storage
    ->resetCache();
  $this
    ->drupalLogin($this->webUser);
  $book_node = $node_storage
    ->load($this->book
    ->id());
  $this
    ->assertNotEmpty($book_node->book);
  $this
    ->assertEquals($this->book
    ->id(), $book_node->book['bid']);
}