From bcee34dbcc022ea67a225d8939550515af8b6ef7 Mon Sep 17 00:00:00 2001 From: Alex Pott Date: Tue, 11 Oct 2016 13:04:42 +0100 Subject: [PATCH] Issue #2743183 by szato, nielsvandermolen: Fatal error in Book navigation block for unpublished parent book item --- .../src/Plugin/Block/BookNavigationBlock.php | 9 ++++--- core/modules/book/src/Tests/BookTest.php | 25 +++++++++++++++++++ 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/core/modules/book/src/Plugin/Block/BookNavigationBlock.php b/core/modules/book/src/Plugin/Block/BookNavigationBlock.php index b232fbf4121..6967077f314 100644 --- a/core/modules/book/src/Plugin/Block/BookNavigationBlock.php +++ b/core/modules/book/src/Plugin/Block/BookNavigationBlock.php @@ -156,9 +156,12 @@ class BookNavigationBlock extends BlockBase implements ContainerFactoryPluginInt } } elseif ($current_bid) { - // Only display this block when the user is browsing a book. - $query = \Drupal::entityQuery('node'); - $nid = $query->condition('nid', $node->book['bid'], '=')->execute(); + // Only display this block when the user is browsing a book and do + // not show unpublished books. + $nid = \Drupal::entityQuery('node') + ->condition('nid', $node->book['bid'], '=') + ->condition('status', NODE_PUBLISHED) + ->execute(); // Only show the block if the user has view access for the top-level node. if ($nid) { diff --git a/core/modules/book/src/Tests/BookTest.php b/core/modules/book/src/Tests/BookTest.php index 015899295bc..e5367a2ad2a 100644 --- a/core/modules/book/src/Tests/BookTest.php +++ b/core/modules/book/src/Tests/BookTest.php @@ -752,4 +752,29 @@ class BookTest extends WebTestBase { $this->assertEqual($book_node->book['bid'], $this->book->id()); } + /** + * Tests the book navigation block when book is unpublished. + * + * There was a fatal error with "Show block only on book pages" block mode. + */ + public function testBookNavigationBlockOnUnpublishedBook() { + // Create a new book. + $this->createBook(); + + // Create administrator user. + $administratorUser = $this->drupalCreateUser(['administer blocks', 'administer nodes', 'bypass node access']); + $this->drupalLogin($administratorUser); + + // Enable the block with "Show block only on book pages" mode. + $this->drupalPlaceBlock('book_navigation', ['block_mode' => 'book pages']); + + // Unpublish book node. + $edit = []; + $this->drupalPostForm('node/' . $this->book->id() . '/edit', $edit, t('Save and unpublish')); + + // Test node page. + $this->drupalGet('node/' . $this->book->id()); + $this->assertText($this->book->label(), 'Unpublished book with "Show block only on book pages" book navigation settings.'); + } + }