function BookTest::testHookNodeLoadAccess

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

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']);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.