You are here

public function BookTest::testHookNodeLoadAccess in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/book/tests/src/Functional/BookTest.php \Drupal\Tests\book\Functional\BookTest::testHookNodeLoadAccess()
  2. 10 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 590

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
    ->assertTrue(!empty($book_node->book));
  $this
    ->assertEqual($book_node->book['bid'], $this->book
    ->id());

  // 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
    ->assertTrue(!empty($book_node->book));
  $this
    ->assertEqual($book_node->book['bid'], $this->book
    ->id());
}