You are here

public function BookTest::testBookOutline 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::testBookOutline()
  2. 10 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 429

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(t('Book outline'), 'Book Author is not allowed to outline');
  $this
    ->drupalLogin($this->adminUser);
  $this
    ->drupalGet('node/' . $empty_book
    ->id() . '/outline');
  $this
    ->assertRaw(t('Book outline'));
  $this
    ->assertOptionSelected('edit-book-bid', 0, 'Node does not belong to a book');
  $this
    ->assertSession()
    ->linkNotExists(t('Remove from book outline'));
  $edit = [];
  $edit['book[bid]'] = '1';
  $this
    ->drupalPostForm('node/' . $empty_book
    ->id() . '/outline', $edit, t('Add to book outline'));
  $node = \Drupal::entityTypeManager()
    ->getStorage('node')
    ->load($empty_book
    ->id());

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

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

  // 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
    ->drupalPostForm('node/' . $node
    ->id() . '/edit', $edit, t('Save'));
  $node = \Drupal::entityTypeManager()
    ->getStorage('node')
    ->load($node
    ->id());

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

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