Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / book / tests / src / Kernel / BookUninstallTest.php
1 <?php
2
3 namespace Drupal\Tests\book\Kernel;
4
5 use Drupal\node\Entity\Node;
6 use Drupal\node\Entity\NodeType;
7 use Drupal\KernelTests\KernelTestBase;
8
9 /**
10  * Tests that the Book module cannot be uninstalled if books exist.
11  *
12  * @group book
13  */
14 class BookUninstallTest extends KernelTestBase {
15
16   /**
17    * Modules to enable.
18    *
19    * @var array
20    */
21   public static $modules = ['system', 'user', 'field', 'filter', 'text', 'node', 'book'];
22
23   /**
24    * {@inheritdoc}
25    */
26   protected function setUp() {
27     parent::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']);
35   }
36
37   /**
38    * Tests the book_system_info_alter() method.
39    */
40   public function testBookUninstall() {
41     // No nodes exist.
42     $validation_reasons = \Drupal::service('module_installer')->validateUninstall(['book']);
43     $this->assertEqual([], $validation_reasons, 'The book module is not required.');
44
45     $content_type = NodeType::create([
46       'type' => $this->randomMachineName(),
47       'name' => $this->randomString(),
48     ]);
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();
54
55     $node = Node::create(['title' => $this->randomString(), 'type' => $content_type->id()]);
56     $node->book['bid'] = 'new';
57     $node->save();
58
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']);
62
63     $book_node = Node::create(['title' => $this->randomString(), 'type' => 'book']);
64     $book_node->book['bid'] = FALSE;
65     $book_node->save();
66
67     // Two nodes, one in a book but not of type book and one book node (which is
68     // not in a book).
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']);
71
72     $node->delete();
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']);
76
77     $book_node->delete();
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.');
81
82     $node = Node::create(['title' => $this->randomString(), 'type' => $content_type->id()]);
83     $node->save();
84     // One node exists but is not part of a book therefore the book module is
85     // not required.
86     $validation_reasons = \Drupal::service('module_installer')->validateUninstall(['book']);
87     $this->assertEqual([], $validation_reasons, 'The book module is not required.');
88
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.");
92   }
93
94 }