You are here

function BookTest::testBookExport in Zircon Profile 8.0

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

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

File

core/modules/book/src/Tests/BookTest.php, line 376
Contains \Drupal\book\Tests\BookTest.

Class

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

Namespace

Drupal\book\Tests

Code

function testBookExport() {

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

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

  // Make sure each part of the book is there.
  foreach ($nodes as $node) {
    $this
      ->assertText($node
      ->label(), 'Node title found in printer friendly version.');
    $this
      ->assertRaw($node->body->processed, 'Node body found in printer friendly version.');
  }

  // Make sure we can't export an unsupported format.
  $this
    ->drupalGet('book/export/foobar/' . $this->book
    ->id());
  $this
    ->assertResponse('404', 'Unsupported export format returned "not found".');

  // Make sure we get a 404 on a not existing book node.
  $this
    ->drupalGet('book/export/html/123');
  $this
    ->assertResponse('404', 'Not existing book node returned "not found".');

  // 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
    ->assertNoLink(t('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
    ->assertResponse('403', 'Anonymous user properly forbidden.');

  // 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, array(
    'access printer-friendly version',
  ));
  $this
    ->drupalGet('book/export/html/' . $this->book
    ->id());
  $this
    ->assertResponse('403', 'Anonymous user properly forbidden from seeing the printer-friendly version when denied by node access.');
}