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 cannot be uninstalled if books exist.
14 class BookUninstallTest extends KernelTestBase {
21 public static $modules = ['system', 'user', 'field', 'filter', 'text', 'node', 'book'];
26 protected function setUp() {
28 $this->installEntitySchema('user');
29 $this->installEntitySchema('node');
30 $this->installSchema('book', ['book']);
31 $this->installSchema('node', ['node_access']);
32 $this->installConfig(['node', 'book', 'field']);
33 // For uninstall to work.
34 $this->installSchema('user', ['users_data']);
38 * Tests the book_system_info_alter() method.
40 public function testBookUninstall() {
42 $validation_reasons = \Drupal::service('module_installer')->validateUninstall(['book']);
43 $this->assertEqual([], $validation_reasons, 'The book module is not required.');
45 $content_type = NodeType::create([
46 'type' => $this->randomMachineName(),
47 'name' => $this->randomString(),
49 $content_type->save();
50 $book_config = $this->config('book.settings');
51 $allowed_types = $book_config->get('allowed_types');
52 $allowed_types[] = $content_type->id();
53 $book_config->set('allowed_types', $allowed_types)->save();
55 $node = Node::create(['title' => $this->randomString(), 'type' => $content_type->id()]);
56 $node->book['bid'] = 'new';
59 // One node in a book but not of type book.
60 $validation_reasons = \Drupal::service('module_installer')->validateUninstall(['book']);
61 $this->assertEqual(['To uninstall Book, delete all content that is part of a book'], $validation_reasons['book']);
63 $book_node = Node::create(['title' => $this->randomString(), 'type' => 'book']);
64 $book_node->book['bid'] = FALSE;
67 // Two nodes, one in a book but not of type book and one book node (which is
69 $validation_reasons = \Drupal::service('module_installer')->validateUninstall(['book']);
70 $this->assertEqual(['To uninstall Book, delete all content that is part of a book'], $validation_reasons['book']);
73 // One node of type book but not actually part of a book.
74 $validation_reasons = \Drupal::service('module_installer')->validateUninstall(['book']);
75 $this->assertEqual(['To uninstall Book, delete all content that has the Book content type'], $validation_reasons['book']);
78 // No nodes exist therefore the book module is not required.
79 $module_data = \Drupal::service('extension.list.module')->reset()->getList();
80 $this->assertFalse(isset($module_data['book']->info['required']), 'The book module is not required.');
82 $node = Node::create(['title' => $this->randomString(), 'type' => $content_type->id()]);
84 // One node exists but is not part of a book therefore the book module is
86 $validation_reasons = \Drupal::service('module_installer')->validateUninstall(['book']);
87 $this->assertEqual([], $validation_reasons, 'The book module is not required.');
89 // Uninstall the Book module and check the node type is deleted.
90 \Drupal::service('module_installer')->uninstall(['book']);
91 $this->assertNull(NodeType::load('book'), "The book node type does not exist.");