You are here

public function BookTest::testBookDelete in Drupal 8

Same name and namespace in other branches
  1. 9 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 378

Class

BookTest
Create a book, add pages, and test book interface.

Namespace

Drupal\Tests\book\Functional

Code

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
    ->drupalPostForm('node/' . $nodes[4]
    ->id() . '/outline/remove', $edit, t('Remove'));
  $node_storage
    ->resetCache([
    $nodes[4]
      ->id(),
  ]);
  $node4 = $node_storage
    ->load($nodes[4]
    ->id());
  $this
    ->assertTrue(empty($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
    ->drupalPostForm('node/' . $this->book
    ->id() . '/outline/remove', $edit, t('Remove'));
  $node_storage
    ->resetCache([
    $this->book
      ->id(),
  ]);
  $node = $node_storage
    ->load($this->book
    ->id());
  $this
    ->assertTrue(empty($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
    ->assertRaw(t('%title is part of a book outline, and has associated child pages. If you proceed with deletion, the child pages will be relocated automatically.', [
    '%title' => $this->book
      ->label(),
  ]));

  // Delete parent, and visit a child page.
  $this
    ->drupalPostForm($this->book
    ->toUrl('delete-form'), [], t('Delete'));
  $this
    ->drupalGet($nodes[0]
    ->toUrl());
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $this
    ->assertText($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
    ->assertEqual($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
    ->assertEqual($child
    ->id(), $second->book['bid'], '3rd-level child node is now second level when top-level node is deleted.');
}