View source
<?php
namespace Drupal\Tests\book\Kernel;
use Drupal\node\Entity\Node;
use Drupal\node\Entity\NodeType;
use Drupal\KernelTests\KernelTestBase;
class BookUninstallTest extends KernelTestBase {
protected static $modules = [
'system',
'user',
'field',
'filter',
'text',
'node',
'book',
];
protected function setUp() : void {
parent::setUp();
$this
->installEntitySchema('user');
$this
->installEntitySchema('node');
$this
->installSchema('book', [
'book',
]);
$this
->installSchema('node', [
'node_access',
]);
$this
->installConfig([
'node',
'book',
'field',
]);
$this
->installSchema('user', [
'users_data',
]);
}
public function testBookUninstall() {
$validation_reasons = \Drupal::service('module_installer')
->validateUninstall([
'book',
]);
$this
->assertEquals([], $validation_reasons, 'The book module is not required.');
$content_type = NodeType::create([
'type' => $this
->randomMachineName(),
'name' => $this
->randomString(),
]);
$content_type
->save();
$book_config = $this
->config('book.settings');
$allowed_types = $book_config
->get('allowed_types');
$allowed_types[] = $content_type
->id();
$book_config
->set('allowed_types', $allowed_types)
->save();
$node = Node::create([
'title' => $this
->randomString(),
'type' => $content_type
->id(),
]);
$node->book['bid'] = 'new';
$node
->save();
$validation_reasons = \Drupal::service('module_installer')
->validateUninstall([
'book',
]);
$this
->assertEquals([
'To uninstall Book, delete all content that is part of a book',
], $validation_reasons['book']);
$book_node = Node::create([
'title' => $this
->randomString(),
'type' => 'book',
]);
$book_node->book['bid'] = FALSE;
$book_node
->save();
$validation_reasons = \Drupal::service('module_installer')
->validateUninstall([
'book',
]);
$this
->assertEquals([
'To uninstall Book, delete all content that is part of a book',
], $validation_reasons['book']);
$node
->delete();
$validation_reasons = \Drupal::service('module_installer')
->validateUninstall([
'book',
]);
$this
->assertEquals([
'To uninstall Book, delete all content that has the Book content type',
], $validation_reasons['book']);
$book_node
->delete();
$module_data = \Drupal::service('extension.list.module')
->getList();
$this
->assertFalse(isset($module_data['book']->info['required']), 'The book module is not required.');
$node = Node::create([
'title' => $this
->randomString(),
'type' => $content_type
->id(),
]);
$node
->save();
$validation_reasons = \Drupal::service('module_installer')
->validateUninstall([
'book',
]);
$this
->assertEquals([], $validation_reasons, 'The book module is not required.');
\Drupal::service('module_installer')
->uninstall([
'book',
]);
$this
->assertNull(NodeType::load('book'), "The book node type does not exist.");
}
}