function BookTest::testBook

Same name in other branches
  1. 9 core/modules/book/tests/src/Functional/BookTest.php \Drupal\Tests\book\Functional\BookTest::testBook()
  2. 8.9.x core/modules/book/tests/src/Functional/BookTest.php \Drupal\Tests\book\Functional\BookTest::testBook()
  3. 10 core/modules/book/tests/src/Functional/BookTest.php \Drupal\Tests\book\Functional\BookTest::testBook()

Tests book functionality through node interfaces.

File

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

Class

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

Namespace

Drupal\Tests\book\Functional

Code

public function testBook() {
    // Create new book.
    $nodes = $this->createBook();
    $book = $this->book;
    $this->drupalLogin($this->webUser);
    // Check that book pages display along with the correct outlines and
    // previous/next links.
    $this->checkBookNode($book, [
        $nodes[0],
        $nodes[3],
        $nodes[4],
    ], FALSE, FALSE, $nodes[0], []);
    $this->checkBookNode($nodes[0], [
        $nodes[1],
        $nodes[2],
    ], $book, $book, $nodes[1], [
        $book,
    ]);
    $this->checkBookNode($nodes[1], NULL, $nodes[0], $nodes[0], $nodes[2], [
        $book,
        $nodes[0],
    ]);
    $this->checkBookNode($nodes[2], NULL, $nodes[1], $nodes[0], $nodes[3], [
        $book,
        $nodes[0],
    ]);
    $this->checkBookNode($nodes[3], NULL, $nodes[2], $book, $nodes[4], [
        $book,
    ]);
    $this->checkBookNode($nodes[4], NULL, $nodes[3], $book, FALSE, [
        $book,
    ]);
    $this->drupalLogout();
    $this->drupalLogin($this->bookAuthor);
    // Check the presence of expected cache tags.
    $this->drupalGet('node/add/book');
    $this->assertSession()
        ->responseHeaderContains('X-Drupal-Cache-Tags', 'config:book.settings');
    
    /*
     * Add Node 5 under Node 3.
     * Book
     *  |- Node 0
     *   |- Node 1
     *   |- Node 2
     *  |- Node 3
     *   |- Node 5
     *  |- Node 4
     */
    // Node 5.
    $nodes[] = $this->createBookNode($book->id(), $nodes[3]->book['nid']);
    $this->drupalLogout();
    $this->drupalLogin($this->webUser);
    // Verify the new outline - make sure we don't get stale cached data.
    $this->checkBookNode($nodes[3], [
        $nodes[5],
    ], $nodes[2], $book, $nodes[5], [
        $book,
    ]);
    $this->checkBookNode($nodes[4], NULL, $nodes[5], $book, FALSE, [
        $book,
    ]);
    $this->drupalLogout();
    // Create a second book, and move an existing book page into it.
    $this->drupalLogin($this->bookAuthor);
    $other_book = $this->createBookNode('new');
    $node = $this->createBookNode($book->id());
    $edit = [
        'book[bid]' => $other_book->id(),
    ];
    $this->drupalGet('node/' . $node->id() . '/edit');
    $this->submitForm($edit, 'Save');
    $this->drupalLogout();
    $this->drupalLogin($this->webUser);
    // Check that the nodes in the second book are displayed correctly.
    // First we must set $this->book to the second book, so that the
    // correct regex will be generated for testing the outline.
    $this->book = $other_book;
    $this->checkBookNode($other_book, [
        $node,
    ], FALSE, FALSE, $node, []);
    $this->checkBookNode($node, NULL, $other_book, $other_book, FALSE, [
        $other_book,
    ]);
    // Test that we can save a book programmatically.
    $this->drupalLogin($this->bookAuthor);
    $book = $this->createBookNode('new');
    $book->save();
    // Confirm that an unpublished book page has the 'Add child page' link.
    $this->drupalGet('node/' . $nodes[4]->id());
    $this->assertSession()
        ->linkExists('Add child page');
    $nodes[4]->setUnPublished();
    $nodes[4]->save();
    $this->drupalGet('node/' . $nodes[4]->id());
    $this->assertSession()
        ->linkExists('Add child page');
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.