You are here

function BookTest::createBookNode in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/book/src/Tests/BookTest.php \Drupal\book\Tests\BookTest::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.

6 calls to BookTest::createBookNode()
BookTest::createBook in core/modules/book/src/Tests/BookTest.php
Creates a new book with a page hierarchy.
BookTest::testBook in core/modules/book/src/Tests/BookTest.php
Tests book functionality through node interfaces.
BookTest::testBookOrdering in core/modules/book/src/Tests/BookTest.php
Tests re-ordering of books.
BookTest::testBookOutline in core/modules/book/src/Tests/BookTest.php
Tests outline of a book.
BookTest::testEmptyBook in core/modules/book/src/Tests/BookTest.php
Tests saving the book outline on an empty book.

... See full list

File

core/modules/book/src/Tests/BookTest.php, line 342
Contains \Drupal\book\Tests\BookTest.

Class

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

Namespace

Drupal\book\Tests

Code

function createBookNode($book_nid, $parent = NULL) {

  // $number does not use drupal_static as it should not be reset
  // since it uniquely identifies each call to createBookNode().
  static $number = 0;

  // Used to ensure that when sorted nodes stay in same order.
  $edit = array();
  $edit['title[0][value]'] = str_pad($number, 2, '0', STR_PAD_LEFT) . ' - SimpleTest test node ' . $this
    ->randomMachineName(10);
  $edit['body[0][value]'] = 'SimpleTest test body ' . $this
    ->randomMachineName(32) . ' ' . $this
    ->randomMachineName(32);
  $edit['book[bid]'] = $book_nid;
  if ($parent !== NULL) {
    $this
      ->drupalPostForm('node/add/book', $edit, t('Change book (update list of parents)'));
    $edit['book[pid]'] = $parent;
    $this
      ->drupalPostForm(NULL, $edit, t('Save'));

    // Make sure the parent was flagged as having children.
    $parent_node = \Drupal::entityManager()
      ->getStorage('node')
      ->loadUnchanged($parent);
    $this
      ->assertFalse(empty($parent_node->book['has_children']), 'Parent node is marked as having children');
  }
  else {
    $this
      ->drupalPostForm('node/add/book', $edit, t('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;
}