You are here

public function BookTest::testBookNavigationBlock in Drupal 9

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

Tests the functionality of the book navigation block.

File

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

Class

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

Namespace

Drupal\Tests\book\Functional

Code

public function testBookNavigationBlock() {
  $this
    ->drupalLogin($this->adminUser);

  // Enable the block.
  $block = $this
    ->drupalPlaceBlock('book_navigation');

  // Give anonymous users the permission 'node test view'.
  $edit = [];
  $edit[RoleInterface::ANONYMOUS_ID . '[node test view]'] = TRUE;
  $this
    ->drupalGet('admin/people/permissions/' . RoleInterface::ANONYMOUS_ID);
  $this
    ->submitForm($edit, 'Save permissions');
  $this
    ->assertSession()
    ->pageTextContains('The changes have been saved.');

  // Test correct display of the block.
  $nodes = $this
    ->createBook();
  $this
    ->drupalGet('<front>');

  // Book navigation block.
  $this
    ->assertSession()
    ->pageTextContains($block
    ->label());

  // Link to book root.
  $this
    ->assertSession()
    ->pageTextContains($this->book
    ->label());

  // No links to individual book pages.
  $this
    ->assertSession()
    ->pageTextNotContains($nodes[0]
    ->label());

  // Ensure that an unpublished node does not appear in the navigation for a
  // user without access. By unpublishing a parent page, child pages should
  // not appear in the navigation. The node_access_test module is disabled
  // since it interferes with this logic.

  /** @var \Drupal\Core\Extension\ModuleInstaller $installer */
  $installer = \Drupal::service('module_installer');
  $installer
    ->uninstall([
    'node_access_test',
  ]);
  node_access_rebuild();
  $nodes[0]
    ->setUnPublished();
  $nodes[0]
    ->save();

  // Verify the user does not have access to the unpublished node.
  $this
    ->assertFalse($nodes[0]
    ->access('view', $this->webUser));

  // Verify the unpublished book page does not appear in the navigation.
  $this
    ->drupalLogin($this->webUser);
  $this
    ->drupalGet($nodes[0]
    ->toUrl());
  $this
    ->assertSession()
    ->statusCodeEquals(403);
  $this
    ->drupalGet($this->book
    ->toUrl());
  $this
    ->assertSession()
    ->responseNotContains($nodes[0]
    ->getTitle());
  $this
    ->assertSession()
    ->responseNotContains($nodes[1]
    ->getTitle());
  $this
    ->assertSession()
    ->responseNotContains($nodes[2]
    ->getTitle());
}