function BookTest::testBookDelete
Same name in other branches
- 9 core/modules/book/tests/src/Functional/BookTest.php \Drupal\Tests\book\Functional\BookTest::testBookDelete()
- 8.9.x core/modules/book/tests/src/Functional/BookTest.php \Drupal\Tests\book\Functional\BookTest::testBookDelete()
- 10 core/modules/book/tests/src/Functional/BookTest.php \Drupal\Tests\book\Functional\BookTest::testBookDelete()
Tests the access for deleting top-level book nodes.
File
-
core/
modules/ book/ tests/ src/ Functional/ BookTest.php, line 418
Class
- BookTest
- Create a book, add pages, and test book interface.
Namespace
Drupal\Tests\book\FunctionalCode
public function testBookDelete() {
$node_storage = $this->container
->get('entity_type.manager')
->getStorage('node');
$nodes = $this->createBook();
$this->drupalLogin($this->adminUser);
$edit = [];
// Ensure that the top-level book node cannot be deleted.
$this->drupalGet('node/' . $this->book
->id() . '/outline/remove');
$this->assertSession()
->statusCodeEquals(403);
// Ensure that a child book node can be deleted.
$this->drupalGet('node/' . $nodes[4]->id() . '/outline/remove');
$this->submitForm($edit, 'Remove');
$node_storage->resetCache([
$nodes[4]->id(),
]);
$node4 = $node_storage->load($nodes[4]->id());
$this->assertEmpty($node4->book, 'Deleting child book node properly allowed.');
// $nodes[4] is stale, trying to delete it directly will cause an error.
$node4->delete();
unset($nodes[4]);
// Delete all child book nodes and retest top-level node deletion.
$node_storage->delete($nodes);
$this->drupalGet('node/' . $this->book
->id() . '/outline/remove');
$this->submitForm($edit, 'Remove');
$node_storage->resetCache([
$this->book
->id(),
]);
$node = $node_storage->load($this->book
->id());
$this->assertEmpty($node->book, 'Deleting childless top-level book node properly allowed.');
// Tests directly deleting a book parent.
$nodes = $this->createBook();
$this->drupalLogin($this->adminUser);
$this->drupalGet($this->book
->toUrl('delete-form'));
$this->assertSession()
->pageTextContains($this->book
->label() . ' is part of a book outline, and has associated child pages. If you proceed with deletion, the child pages will be relocated automatically.');
// Delete parent, and visit a child page.
$this->drupalGet($this->book
->toUrl('delete-form'));
$this->submitForm([], 'Delete');
$this->drupalGet($nodes[0]->toUrl());
$this->assertSession()
->statusCodeEquals(200);
$this->assertSession()
->pageTextContains($nodes[0]->label());
// The book parents should be updated.
$node_storage = \Drupal::entityTypeManager()->getStorage('node');
$node_storage->resetCache();
$child = $node_storage->load($nodes[0]->id());
$this->assertEquals($child->id(), $child->book['bid'], 'Child node book ID updated when parent is deleted.');
// 3rd-level children should now be 2nd-level.
$second = $node_storage->load($nodes[1]->id());
$this->assertEquals($child->id(), $second->book['bid'], '3rd-level child node is now second level when top-level node is deleted.');
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.