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

Tests book export ("printer-friendly version") functionality.

File

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

Class

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

Namespace

Drupal\Tests\book\Functional

Code

public function testBookExport() {

  // Create a book.
  $nodes = $this
    ->createBook();

  // Log in as web user and view printer-friendly version.
  $this
    ->drupalLogin($this->webUser);
  $this
    ->drupalGet('node/' . $this->book
    ->id());
  $this
    ->clickLink('Printer-friendly version');

  // Make sure each part of the book is there.
  foreach ($nodes as $node) {
    $this
      ->assertSession()
      ->pageTextContains($node
      ->label());
    $this
      ->assertSession()
      ->responseContains($node->body->processed);
  }

  // Make sure we can't export an unsupported format.
  $this
    ->drupalGet('book/export/foobar/' . $this->book
    ->id());
  $this
    ->assertSession()
    ->statusCodeEquals(404);

  // Make sure we get a 404 on a non-existent book node.
  $this
    ->drupalGet('book/export/html/123');
  $this
    ->assertSession()
    ->statusCodeEquals(404);

  // Make sure an anonymous user cannot view printer-friendly version.
  $this
    ->drupalLogout();

  // Load the book and verify there is no printer-friendly version link.
  $this
    ->drupalGet('node/' . $this->book
    ->id());
  $this
    ->assertSession()
    ->linkNotExists('Printer-friendly version', 'Anonymous user is not shown link to printer-friendly version.');

  // Try getting the URL directly, and verify it fails.
  $this
    ->drupalGet('book/export/html/' . $this->book
    ->id());
  $this
    ->assertSession()
    ->statusCodeEquals(403);

  // Now grant anonymous users permission to view the printer-friendly
  // version and verify that node access restrictions still prevent them from
  // seeing it.
  user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, [
    'access printer-friendly version',
  ]);
  $this
    ->drupalGet('book/export/html/' . $this->book
    ->id());
  $this
    ->assertSession()
    ->statusCodeEquals(403);
}