function BookTest::testBookNavigationCacheContext

Same name and namespace in other branches
  1. 9 core/modules/book/tests/src/Functional/BookTest.php \Drupal\Tests\book\Functional\BookTest::testBookNavigationCacheContext()
  2. 8.9.x core/modules/book/tests/src/Functional/BookTest.php \Drupal\Tests\book\Functional\BookTest::testBookNavigationCacheContext()
  3. 11.x core/modules/book/tests/src/Functional/BookTest.php \Drupal\Tests\book\Functional\BookTest::testBookNavigationCacheContext()

Tests the book navigation cache context.

See also

\Drupal\book\Cache\BookNavigationCacheContext

File

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

Class

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

Namespace

Drupal\Tests\book\Functional

Code

public function testBookNavigationCacheContext() : void {
  // Create a page node.
  $this->drupalCreateContentType([
    'type' => 'page',
  ]);
  $page = $this->drupalCreateNode();
  // Create a book, consisting of book nodes.
  $book_nodes = $this->createBook();
  // Enable the debug output.
  \Drupal::state()->set('book_test.debug_book_navigation_cache_context', TRUE);
  Cache::invalidateTags([
    'book_test.debug_book_navigation_cache_context',
  ]);
  $this->drupalLogin($this->bookAuthor);
  // On non-node route.
  $this->drupalGet($this->adminUser
    ->toUrl());
  $this->assertSession()
    ->responseContains('[route.book_navigation]=book.none');
  // On non-book node route.
  $this->drupalGet($page->toUrl());
  $this->assertSession()
    ->responseContains('[route.book_navigation]=book.none');
  // On book node route.
  $this->drupalGet($book_nodes[0]->toUrl());
  $this->assertSession()
    ->responseContains('[route.book_navigation]=0|2|3');
  $this->drupalGet($book_nodes[1]->toUrl());
  $this->assertSession()
    ->responseContains('[route.book_navigation]=0|2|3|4');
  $this->drupalGet($book_nodes[2]->toUrl());
  $this->assertSession()
    ->responseContains('[route.book_navigation]=0|2|3|5');
  $this->drupalGet($book_nodes[3]->toUrl());
  $this->assertSession()
    ->responseContains('[route.book_navigation]=0|2|6');
  $this->drupalGet($book_nodes[4]->toUrl());
  $this->assertSession()
    ->responseContains('[route.book_navigation]=0|2|7');
}

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