annotate core/modules/book/tests/src/Kernel/BookUninstallTest.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 129ea1e6d783
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\Tests\book\Kernel;
Chris@0 4
Chris@0 5 use Drupal\node\Entity\Node;
Chris@0 6 use Drupal\node\Entity\NodeType;
Chris@0 7 use Drupal\KernelTests\KernelTestBase;
Chris@0 8
Chris@0 9 /**
Chris@0 10 * Tests that the Book module cannot be uninstalled if books exist.
Chris@0 11 *
Chris@0 12 * @group book
Chris@0 13 */
Chris@0 14 class BookUninstallTest extends KernelTestBase {
Chris@0 15
Chris@0 16 /**
Chris@0 17 * Modules to enable.
Chris@0 18 *
Chris@0 19 * @var array
Chris@0 20 */
Chris@0 21 public static $modules = ['system', 'user', 'field', 'filter', 'text', 'node', 'book'];
Chris@0 22
Chris@0 23 /**
Chris@0 24 * {@inheritdoc}
Chris@0 25 */
Chris@0 26 protected function setUp() {
Chris@0 27 parent::setUp();
Chris@0 28 $this->installEntitySchema('user');
Chris@0 29 $this->installEntitySchema('node');
Chris@0 30 $this->installSchema('book', ['book']);
Chris@0 31 $this->installSchema('node', ['node_access']);
Chris@0 32 $this->installConfig(['node', 'book', 'field']);
Chris@0 33 // For uninstall to work.
Chris@0 34 $this->installSchema('user', ['users_data']);
Chris@0 35 }
Chris@0 36
Chris@0 37 /**
Chris@0 38 * Tests the book_system_info_alter() method.
Chris@0 39 */
Chris@0 40 public function testBookUninstall() {
Chris@0 41 // No nodes exist.
Chris@0 42 $validation_reasons = \Drupal::service('module_installer')->validateUninstall(['book']);
Chris@0 43 $this->assertEqual([], $validation_reasons, 'The book module is not required.');
Chris@0 44
Chris@0 45 $content_type = NodeType::create([
Chris@0 46 'type' => $this->randomMachineName(),
Chris@0 47 'name' => $this->randomString(),
Chris@0 48 ]);
Chris@0 49 $content_type->save();
Chris@0 50 $book_config = $this->config('book.settings');
Chris@0 51 $allowed_types = $book_config->get('allowed_types');
Chris@0 52 $allowed_types[] = $content_type->id();
Chris@0 53 $book_config->set('allowed_types', $allowed_types)->save();
Chris@0 54
Chris@0 55 $node = Node::create(['title' => $this->randomString(), 'type' => $content_type->id()]);
Chris@0 56 $node->book['bid'] = 'new';
Chris@0 57 $node->save();
Chris@0 58
Chris@0 59 // One node in a book but not of type book.
Chris@0 60 $validation_reasons = \Drupal::service('module_installer')->validateUninstall(['book']);
Chris@0 61 $this->assertEqual(['To uninstall Book, delete all content that is part of a book'], $validation_reasons['book']);
Chris@0 62
Chris@0 63 $book_node = Node::create(['title' => $this->randomString(), 'type' => 'book']);
Chris@0 64 $book_node->book['bid'] = FALSE;
Chris@0 65 $book_node->save();
Chris@0 66
Chris@0 67 // Two nodes, one in a book but not of type book and one book node (which is
Chris@0 68 // not in a book).
Chris@0 69 $validation_reasons = \Drupal::service('module_installer')->validateUninstall(['book']);
Chris@0 70 $this->assertEqual(['To uninstall Book, delete all content that is part of a book'], $validation_reasons['book']);
Chris@0 71
Chris@0 72 $node->delete();
Chris@0 73 // One node of type book but not actually part of a book.
Chris@0 74 $validation_reasons = \Drupal::service('module_installer')->validateUninstall(['book']);
Chris@0 75 $this->assertEqual(['To uninstall Book, delete all content that has the Book content type'], $validation_reasons['book']);
Chris@0 76
Chris@0 77 $book_node->delete();
Chris@0 78 // No nodes exist therefore the book module is not required.
Chris@17 79 $module_data = \Drupal::service('extension.list.module')->reset()->getList();
Chris@0 80 $this->assertFalse(isset($module_data['book']->info['required']), 'The book module is not required.');
Chris@0 81
Chris@0 82 $node = Node::create(['title' => $this->randomString(), 'type' => $content_type->id()]);
Chris@0 83 $node->save();
Chris@0 84 // One node exists but is not part of a book therefore the book module is
Chris@0 85 // not required.
Chris@0 86 $validation_reasons = \Drupal::service('module_installer')->validateUninstall(['book']);
Chris@0 87 $this->assertEqual([], $validation_reasons, 'The book module is not required.');
Chris@0 88
Chris@0 89 // Uninstall the Book module and check the node type is deleted.
Chris@0 90 \Drupal::service('module_installer')->uninstall(['book']);
Chris@0 91 $this->assertNull(NodeType::load('book'), "The book node type does not exist.");
Chris@0 92 }
Chris@0 93
Chris@0 94 }