function BookTestTrait::createBookNode

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

Creates a book node.

Parameters

int|string $book_nid: A book node ID or set to 'new' to create a new book.

int|null $parent: (optional) Parent book reference ID. Defaults to NULL.

array $edit: (optional) Field data in an associative array. Changes the current input fields (where possible) to the values indicated. Defaults to an empty array.

Return value

\Drupal\node\NodeInterface The created node.

6 calls to BookTestTrait::createBookNode()
BookTest::testBook in core/modules/book/tests/src/Functional/BookTest.php
Tests book functionality through node interfaces.
BookTest::testBookOutline in core/modules/book/tests/src/Functional/BookTest.php
Tests outline of a book.
BookTest::testEmptyBook in core/modules/book/tests/src/Functional/BookTest.php
Tests saving the book outline on an empty book.
BookTest::testGetTableOfContents in core/modules/book/tests/src/Functional/BookTest.php
Tests BookManager::getTableOfContents().
BookTest::testHookNodeLoadAccess in core/modules/book/tests/src/Functional/BookTest.php
Ensure the loaded book in hook_node_load() does not depend on the user.

... See full list

File

core/modules/book/tests/src/Functional/BookTestTrait.php, line 191

Class

BookTestTrait
Provides common functionality for Book test classes.

Namespace

Drupal\Tests\book\Functional

Code

public function createBookNode($book_nid, $parent = NULL, $edit = []) {
    // $number does not use drupal_static as it should not be reset
    // since it uniquely identifies each call to createBookNode().
    // Used to ensure that when sorted nodes stay in same order.
    static $number = 0;
    $edit['title[0][value]'] = str_pad((string) $number, 2, '0', STR_PAD_LEFT) . ' - test node ' . $this->randomMachineName(10);
    $edit['body[0][value]'] = 'test body ' . $this->randomMachineName(32) . ' ' . $this->randomMachineName(32);
    $edit['book[bid]'] = $book_nid;
    if ($parent !== NULL) {
        $this->drupalGet('node/add/book');
        $this->submitForm($edit, 'Change book (update list of parents)');
        $edit['book[pid]'] = $parent;
        $this->submitForm($edit, 'Save');
        // Make sure the parent was flagged as having children.
        $parent_node = \Drupal::entityTypeManager()->getStorage('node')
            ->loadUnchanged($parent);
        $this->assertNotEmpty($parent_node->book['has_children'], 'Parent node is marked as having children');
    }
    else {
        $this->drupalGet('node/add/book');
        $this->submitForm($edit, 'Save');
    }
    // Check to make sure the book node was created.
    $node = $this->drupalGetNodeByTitle($edit['title[0][value]']);
    $this->assertNotNull($node === FALSE ? NULL : $node, 'Book node found in database.');
    $number++;
    return $node;
}

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