You are here

public function BookTest::testBookOutline in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/book/tests/src/Functional/BookTest.php \Drupal\Tests\book\Functional\BookTest::testBookOutline()
  2. 9 core/modules/book/tests/src/Functional/BookTest.php \Drupal\Tests\book\Functional\BookTest::testBookOutline()

Tests outline of a book.

File

core/modules/book/tests/src/Functional/BookTest.php, line 469

Class

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

Namespace

Drupal\Tests\book\Functional

Code

public function testBookOutline() {
  $this
    ->drupalLogin($this->bookAuthor);

  // Create new node not yet a book.
  $empty_book = $this
    ->drupalCreateNode([
    'type' => 'book',
  ]);
  $this
    ->drupalGet('node/' . $empty_book
    ->id() . '/outline');
  $this
    ->assertSession()
    ->linkNotExists('Book outline', 'Book Author is not allowed to outline');
  $this
    ->drupalLogin($this->adminUser);
  $this
    ->drupalGet('node/' . $empty_book
    ->id() . '/outline');
  $this
    ->assertSession()
    ->pageTextContains('Book outline');

  // Verify that the node does not belong to a book.
  $this
    ->assertTrue($this
    ->assertSession()
    ->optionExists('edit-book-bid', 0)
    ->isSelected());
  $this
    ->assertSession()
    ->linkNotExists('Remove from book outline');
  $edit = [];
  $edit['book[bid]'] = '1';
  $this
    ->drupalGet('node/' . $empty_book
    ->id() . '/outline');
  $this
    ->submitForm($edit, 'Add to book outline');
  $node = \Drupal::entityTypeManager()
    ->getStorage('node')
    ->load($empty_book
    ->id());

  // Test the book array.
  $this
    ->assertEquals($empty_book
    ->id(), $node->book['nid']);
  $this
    ->assertEquals($empty_book
    ->id(), $node->book['bid']);
  $this
    ->assertEquals(1, $node->book['depth']);
  $this
    ->assertEquals($empty_book
    ->id(), $node->book['p1']);
  $this
    ->assertEquals('0', $node->book['pid']);

  // Create new book.
  $this
    ->drupalLogin($this->bookAuthor);
  $book = $this
    ->createBookNode('new');
  $this
    ->drupalLogin($this->adminUser);
  $this
    ->drupalGet('node/' . $book
    ->id() . '/outline');
  $this
    ->assertSession()
    ->pageTextContains('Book outline');
  $this
    ->clickLink('Remove from book outline');
  $this
    ->assertSession()
    ->pageTextContains('Are you sure you want to remove ' . $book
    ->label() . ' from the book hierarchy?');

  // Create a new node and set the book after the node was created.
  $node = $this
    ->drupalCreateNode([
    'type' => 'book',
  ]);
  $edit = [];
  $edit['book[bid]'] = $node
    ->id();
  $this
    ->drupalGet('node/' . $node
    ->id() . '/edit');
  $this
    ->submitForm($edit, 'Save');
  $node = \Drupal::entityTypeManager()
    ->getStorage('node')
    ->load($node
    ->id());

  // Test the book array.
  $this
    ->assertEquals($node
    ->id(), $node->book['nid']);
  $this
    ->assertEquals($node
    ->id(), $node->book['bid']);
  $this
    ->assertEquals(1, $node->book['depth']);
  $this
    ->assertEquals($node
    ->id(), $node->book['p1']);
  $this
    ->assertEquals('0', $node->book['pid']);

  // Test the form itself.
  $this
    ->drupalGet('node/' . $node
    ->id() . '/edit');
  $this
    ->assertTrue($this
    ->assertSession()
    ->optionExists('edit-book-bid', $node
    ->id())
    ->isSelected());
}