3 namespace Drupal\Tests\book\Kernel;
5 use Drupal\node\Entity\Node;
6 use Drupal\node\Entity\NodeType;
7 use Drupal\KernelTests\KernelTestBase;
10 * Tests that the Book module handles pending revisions correctly.
14 class BookPendingRevisionTest extends KernelTestBase {
21 public static $modules = ['system', 'user', 'field', 'filter', 'text', 'node', 'book'];
26 protected function setUp() {
29 $this->installEntitySchema('user');
30 $this->installEntitySchema('node');
31 $this->installSchema('book', ['book']);
32 $this->installSchema('node', ['node_access']);
33 $this->installConfig(['node', 'book', 'field']);
37 * Tests pending revision handling for books.
39 public function testBookWithPendingRevisions() {
40 $content_type = NodeType::create([
41 'type' => $this->randomMachineName(),
42 'name' => $this->randomString(),
44 $content_type->save();
45 $book_config = $this->config('book.settings');
46 $allowed_types = $book_config->get('allowed_types');
47 $allowed_types[] = $content_type->id();
48 $book_config->set('allowed_types', $allowed_types)->save();
50 // Create two top-level books a child.
51 $book_1 = Node::create(['title' => $this->randomString(), 'type' => $content_type->id()]);
52 $book_1->book['bid'] = 'new';
54 $book_1_child = Node::create(['title' => $this->randomString(), 'type' => $content_type->id()]);
55 $book_1_child->book['bid'] = $book_1->id();
56 $book_1_child->book['pid'] = $book_1->id();
57 $book_1_child->save();
59 $book_2 = Node::create(['title' => $this->randomString(), 'type' => $content_type->id()]);
60 $book_2->book['bid'] = 'new';
63 $child = Node::create(['title' => $this->randomString(), 'type' => $content_type->id()]);
64 $child->book['bid'] = $book_1->id();
65 $child->book['pid'] = $book_1->id();
68 // Try to move the child to a different book while saving it as a pending
70 /** @var \Drupal\book\BookManagerInterface $book_manager */
71 $book_manager = $this->container->get('book.manager');
73 // Check that the API doesn't allow us to change the book outline for
75 $child->book['bid'] = $book_2->id();
76 $child->setNewRevision(TRUE);
77 $child->isDefaultRevision(FALSE);
79 $this->assertFalse($book_manager->updateOutline($child), 'A pending revision can not change the book outline.');
81 // Check that the API doesn't allow us to change the book parent for
83 $child = \Drupal::entityTypeManager()->getStorage('node')->loadUnchanged($child->id());
84 $child->book['pid'] = $book_1_child->id();
85 $child->setNewRevision(TRUE);
86 $child->isDefaultRevision(FALSE);
88 $this->assertFalse($book_manager->updateOutline($child), 'A pending revision can not change the book outline.');
90 // Check that the API doesn't allow us to change the book weight for
92 $child = \Drupal::entityTypeManager()->getStorage('node')->loadUnchanged($child->id());
93 $child->book['weight'] = 2;
94 $child->setNewRevision(TRUE);
95 $child->isDefaultRevision(FALSE);
97 $this->assertFalse($book_manager->updateOutline($child), 'A pending revision can not change the book outline.');