From 68d2dc4a144678595a30436d2a33752fc446c6e4 Mon Sep 17 00:00:00 2001
From: Brian Canini <canini.16@osu.edu>
Date: Mon, 24 Feb 2020 13:38:03 -0500
Subject: [PATCH] applying patch

- Book outline not limited to allowed content types
---
 composer.json                                 |   1 +
 vendor/composer/installed.json                |   1 +
 web/core/PATCHES.txt                          |   4 +
 web/core/modules/book/src/BookManager.php     | 140 +++++++++---------
 .../book/tests/src/Unit/BookManagerTest.php   |  82 +++++++++-
 5 files changed, 161 insertions(+), 67 deletions(-)

diff --git a/composer.json b/composer.json
index 3da9081955..d1ef7f2961 100644
--- a/composer.json
+++ b/composer.json
@@ -271,6 +271,7 @@
         "patches": {
             "drupal/core": {
                 "2799049": "patches/role_based_email_access-2799049-d87.patch",
+                "2862291": "https://www.drupal.org/files/issues/2019-07-02/2862291-21.patch",
                 "2949017": "https://www.drupal.org/files/issues/2019-12-12/2949017-59.patch"
             },
             "drupal/better_exposed_filters": {
diff --git a/vendor/composer/installed.json b/vendor/composer/installed.json
index 73859a4733..037d9c35b1 100644
--- a/vendor/composer/installed.json
+++ b/vendor/composer/installed.json
@@ -3701,6 +3701,7 @@
             },
             "patches_applied": {
                 "2799049": "patches/role_based_email_access-2799049-d87.patch",
+                "2862291": "https://www.drupal.org/files/issues/2019-07-02/2862291-21.patch",
                 "2949017": "https://www.drupal.org/files/issues/2019-12-12/2949017-59.patch"
             }
         },
diff --git a/web/core/PATCHES.txt b/web/core/PATCHES.txt
index cafe50e4be..4edba0f65b 100644
--- a/web/core/PATCHES.txt
+++ b/web/core/PATCHES.txt
@@ -5,6 +5,10 @@ Patches applied to this directory:
 Source: patches/role_based_email_access-2799049-d87.patch
 
 
+2862291
+Source: https://www.drupal.org/files/issues/2019-07-02/2862291-21.patch
+
+
 2949017
 Source: https://www.drupal.org/files/issues/2019-12-12/2949017-59.patch
 
diff --git a/web/core/modules/book/src/BookManager.php b/web/core/modules/book/src/BookManager.php
index bfe55881af..36bc38b892 100644
--- a/web/core/modules/book/src/BookManager.php
+++ b/web/core/modules/book/src/BookManager.php
@@ -177,78 +177,86 @@ public function addFormElements(array $form, FormStateInterface $form_state, Nod
     if ($form_state->hasValue('book')) {
       $node->book = $form_state->getValue('book');
     }
-    $form['book'] = [
-      '#type' => 'details',
-      '#title' => $this->t('Book outline'),
-      '#weight' => 10,
-      '#open' => !$collapsed,
-      '#group' => 'advanced',
-      '#attributes' => [
-        'class' => ['book-outline-form'],
-      ],
-      '#attached' => [
-        'library' => ['book/drupal.book'],
-      ],
-      '#tree' => TRUE,
-    ];
-    foreach (['nid', 'has_children', 'original_bid', 'parent_depth_limit'] as $key) {
-      $form['book'][$key] = [
-        '#type' => 'value',
-        '#value' => $node->book[$key],
+    $config = \Drupal::configFactory()->getEditable('book.settings');
+    $allowed_types = $config->get('allowed_types');
+
+    if (in_array($node->getType(), $allowed_types)) {
+      $form['book'] = [
+        '#type' => 'details',
+        '#title' => $this->t('Book outline'),
+        '#weight' => 10,
+        '#open' => !$collapsed,
+        '#group' => 'advanced',
+        '#attributes' => [
+          'class' => ['book-outline-form'],
+        ],
+        '#attached' => [
+          'library' => ['book/drupal.book'],
+        ],
+        '#tree' => TRUE,
       ];
-    }
 
-    $form['book']['pid'] = $this->addParentSelectFormElements($node->book);
-
-    // @see \Drupal\book\Form\BookAdminEditForm::bookAdminTableTree(). The
-    // weight may be larger than 15.
-    $form['book']['weight'] = [
-      '#type' => 'weight',
-      '#title' => $this->t('Weight'),
-      '#default_value' => $node->book['weight'],
-      '#delta' => max(15, abs($node->book['weight'])),
-      '#weight' => 5,
-      '#description' => $this->t('Pages at a given level are ordered first by weight and then by title.'),
-    ];
-    $options = [];
-    $nid = !$node->isNew() ? $node->id() : 'new';
-    if ($node->id() && ($nid == $node->book['original_bid']) && ($node->book['parent_depth_limit'] == 0)) {
-      // This is the top level node in a maximum depth book and thus cannot be
-      // moved.
-      $options[$node->id()] = $node->label();
-    }
-    else {
-      foreach ($this->getAllBooks() as $book) {
-        $options[$book['nid']] = $book['title'];
+      foreach (['nid', 'has_children', 'original_bid', 'parent_depth_limit'] as $key) {
+        $form['book'][$key] = [
+          '#type' => 'value',
+          '#value' => $node->book[$key],
+        ];
       }
-    }
 
-    if ($account->hasPermission('create new books') && ($nid == 'new' || ($nid != $node->book['original_bid']))) {
-      // The node can become a new book, if it is not one already.
-      $options = [$nid => $this->t('- Create a new book -')] + $options;
-    }
-    if (!$node->book['bid']) {
-      // The node is not currently in the hierarchy.
-      $options = [0 => $this->t('- None -')] + $options;
+      $form['book']['pid'] = $this->addParentSelectFormElements($node->book);
+
+      // @see \Drupal\book\Form\BookAdminEditForm::bookAdminTableTree(). The
+      // weight may be larger than 15.
+      $form['book']['weight'] = [
+        '#type' => 'weight',
+        '#title' => $this->t('Weight'),
+        '#default_value' => $node->book['weight'],
+        '#delta' => max(15, abs($node->book['weight'])),
+        '#weight' => 5,
+        '#description' => $this->t('Pages at a given level are ordered first by weight and then by title.'),
+      ];
+      $options = [];
+      $nid = !$node->isNew() ? $node->id() : 'new';
+
+      if ($node->id() && ($nid == $node->book['original_bid']) && ($node->book['parent_depth_limit'] == 0)) {
+        // This is the top level node in a maximum depth book and thus cannot be
+        // moved.
+        $options[$node->id()] = $node->label();
+      }
+      else {
+        foreach ($this->getAllBooks() as $book) {
+          $options[$book['nid']] = $book['title'];
+        }
+      }
+
+      if ($account->hasPermission('create new books') && ($nid == 'new' || ($nid != $node->book['original_bid']))) {
+        // The node can become a new book, if it is not one already.
+        $options = [$nid => $this->t('- Create a new book -')] + $options;
+      }
+      if (!$node->book['bid']) {
+        // The node is not currently in the hierarchy.
+            $options = [0 => $this->t('- None -')] + $options;
+        }
+
+      // Add a drop-down to select the destination book.
+      $form['book']['bid'] = [
+        '#type' => 'select',
+        '#title' => $this->t('Book'),
+        '#default_value' => $node->book['bid'],
+        '#options' => $options,
+        '#access' => (bool) $options,
+        '#description' => $this->t('Your page will be a part of the selected book.'),
+        '#weight' => -5,
+        '#attributes' => ['class' => ['book-title-select']],
+        '#ajax' => [
+          'callback' => 'book_form_update',
+          'wrapper' => 'edit-book-plid-wrapper',
+          'effect' => 'fade',
+          'speed' => 'fast',
+        ],
+      ];
     }
 
-    // Add a drop-down to select the destination book.
-    $form['book']['bid'] = [
-      '#type' => 'select',
-      '#title' => $this->t('Book'),
-      '#default_value' => $node->book['bid'],
-      '#options' => $options,
-      '#access' => (bool) $options,
-      '#description' => $this->t('Your page will be a part of the selected book.'),
-      '#weight' => -5,
-      '#attributes' => ['class' => ['book-title-select']],
-      '#ajax' => [
-        'callback' => 'book_form_update',
-        'wrapper' => 'edit-book-plid-wrapper',
-        'effect' => 'fade',
-        'speed' => 'fast',
-      ],
-    ];
     return $form;
   }
 
diff --git a/web/core/modules/book/tests/src/Unit/BookManagerTest.php b/web/core/modules/book/tests/src/Unit/BookManagerTest.php
index 503309870a..cbdb82baec 100644
--- a/web/core/modules/book/tests/src/Unit/BookManagerTest.php
+++ b/web/core/modules/book/tests/src/Unit/BookManagerTest.php
@@ -5,6 +5,7 @@
 use Drupal\book\BookManager;
 use Drupal\Core\Entity\EntityTypeManagerInterface;
 use Drupal\Tests\UnitTestCase;
+use Symfony\Component\DependencyInjection\ContainerBuilder;
 
 /**
  * @coversDefaultClass \Drupal\book\BookManager
@@ -54,15 +55,68 @@ class BookManagerTest extends UnitTestCase {
    */
   protected $bookOutlineStorage;
 
+  /**
+   * The mocked form state
+   *
+   * @var \Drupal\Core\Form\FormState
+   */
+  protected $form_state;
+
+  /**
+   * The mocked node Interface.
+   *
+   * @var \Drupal\node\NodeInterface
+   */
+  protected $node;
+
+  /**
+   * The mocked User
+   *
+   * @var \Drupal\user\Entity\User
+   */
+  protected $account;
+
   /**
    * {@inheritdoc}
    */
   protected function setUp() {
     $this->entityTypeManager = $this->createMock(EntityTypeManagerInterface::class);
+    $book_id = [
+      'nid' => 'new',
+      'has_children' => 0,
+      'original_bid' => 0,
+      'parent_depth_limit' => 0,
+      'bid' => 0,
+      'pid' => 0,
+      'weight' => 0,
+      'parent_depth_limit' => 8,
+      'options' => [],
+    ];
+    $this->form_state = $this->getMockBuilder('Drupal\Core\Form\FormState')
+      ->disableOriginalConstructor()->disableOriginalConstructor()
+      ->setMethods(['hasValue', 'getValue'])->getMock();
+    $this->form_state->expects($this->any())
+      ->method('getValue')
+      ->willReturn($book_id);
+    $this->node = $this->getMockBuilder('Drupal\node\NodeInterface')
+      ->disableOriginalConstructor()->getMock();
+    $this->node->book = $book_id;
+    $this->account = $this->getMockBuilder('Drupal\user\Entity\User')
+      ->disableOriginalConstructor()->getMock();
     $this->translation = $this->getStringTranslationStub();
-    $this->configFactory = $this->getConfigFactoryStub([]);
+    $config = [
+      'book.settings' => [
+        'allowed_types' => [
+          'page'
+        ]
+      ]
+    ];
+    $this->configFactory = $this->getConfigFactoryStub($config);
     $this->bookOutlineStorage = $this->createMock('Drupal\book\BookOutlineStorageInterface');
     $this->renderer = $this->createMock('\Drupal\Core\Render\RendererInterface');
+    $container = new ContainerBuilder();
+    $container->set('config.factory', $this->configFactory);
+    \Drupal::setContainer($container);
     $this->bookManager = new BookManager($this->entityTypeManager, $this->translation, $this->configFactory, $this->bookOutlineStorage, $this->renderer);
   }
 
@@ -115,4 +169,30 @@ public function providerTestGetBookParents() {
     ];
   }
 
+  /**
+   * Testing the Book Outline form element in node add page form.
+   * When the Book setting is enabled for the Content Type 'Page'.
+   */
+  public function testAddFormElementsNodeAddWithBook() {
+    $form = [];
+    $this->node->expects($this->any())
+      ->method('getType')
+      ->willReturn('page');
+    $addform = $this->bookManager->addFormElements($form, $this->form_state, $this->node, $this->account);
+    $this->assertArrayHasKey('book', $addform);
+  }
+
+  /**
+   * Testing the Book Outline form element in node add article form.
+   * When the Book setting is not enabled for the Content Type 'Article'
+   */
+  public function testAddFormElementsNodeAddWithoutBook() {
+    $form = [];
+    $this->node->expects($this->any())
+      ->method('getType')
+      ->willReturn('artilce');
+    $addform = $this->bookManager->addFormElements($form, $this->form_state, $this->node, $this->account);
+    $this->assertArrayNotHasKey('book', $addform);
+  }
+
 }
-- 
GitLab