- <?php
- * @file
- * Tests for book.module.
- */
-
- * Tests the functionality of the Book module.
- */
- class BookTestCase extends BackdropWebTestCase {
-
-
- * A book node.
- *
- * @var object
- */
- protected $book;
-
-
- * A user with permission to create and edit books.
- *
- * @var object
- */
- protected $book_author;
-
-
- * A user with permission to view a book.
- *
- * @var object
- */
- protected $web_user;
-
-
- * A user with permission to create and edit books and to administer blocks.
- *
- * @var object
- */
- protected $admin_user;
-
- function setUp() {
- parent::setUp(array('book', 'node_access_test'));
-
-
- node_access_rebuild();
-
-
- $this->book_author = $this->backdropCreateUser(array('create new books', 'create book content', 'edit own book content', 'add content to books'));
- $this->web_user = $this->backdropCreateUser(array('node test view'));
- $this->admin_user = $this->backdropCreateUser(array('create new books', 'create book content', 'edit own book content', 'add content to books', 'administer layouts', 'administer permissions', 'administer book outlines', 'node test view'));
-
-
- config_set('path.settings', 'node_pattern', '');
- }
-
-
- * Creates a new book with a page hierarchy.
- */
- function createBook() {
-
- $this->backdropLogin($this->book_author);
-
- $this->book = $this->createBookNode('new');
- $book = $this->book;
-
-
- * Add page hierarchy to book.
- * Book
- * |- Node 0
- * |- Node 1
- * |- Node 2
- * |- Node 3
- * |- Node 4
- */
- $nodes = array();
- $nodes[] = $this->createBookNode($book->nid);
- $nodes[] = $this->createBookNode($book->nid, $nodes[0]->book['mlid']);
- $nodes[] = $this->createBookNode($book->nid, $nodes[0]->book['mlid']);
- $nodes[] = $this->createBookNode($book->nid);
- $nodes[] = $this->createBookNode($book->nid);
-
- $this->backdropLogout();
-
- return $nodes;
- }
-
-
- * Tests book functionality through node interfaces.
- */
- function testBook() {
-
- $nodes = $this->createBook();
- $book = $this->book;
-
- $this->backdropLogin($this->web_user);
-
-
-
- $this->checkBookNode($book, array($nodes[0], $nodes[3], $nodes[4]), FALSE, FALSE, $nodes[0], array());
- $this->checkBookNode($nodes[0], array($nodes[1], $nodes[2]), $book, $book, $nodes[1], array($book));
- $this->checkBookNode($nodes[1], NULL, $nodes[0], $nodes[0], $nodes[2], array($book, $nodes[0]));
- $this->checkBookNode($nodes[2], NULL, $nodes[1], $nodes[0], $nodes[3], array($book, $nodes[0]));
- $this->checkBookNode($nodes[3], NULL, $nodes[2], $book, $nodes[4], array($book));
- $this->checkBookNode($nodes[4], NULL, $nodes[3], $book, FALSE, array($book));
-
- $this->backdropLogout();
-
-
- $this->backdropLogin($this->book_author);
- $other_book = $this->createBookNode('new');
- $node = $this->createBookNode($book->nid);
- $edit = array('book[bid]' => $other_book->nid);
- $this->backdropPost('node/' . $node->nid . '/edit', $edit, t('Save'));
-
- $this->backdropLogout();
- $this->backdropLogin($this->web_user);
-
-
-
-
- $this->book = $other_book;
- $this->checkBookNode($other_book, array($node), FALSE, FALSE, $node, array());
- $this->checkBookNode($node, NULL, $other_book, $other_book, FALSE, array($other_book));
- }
-
-
- * Checks the outline of sub-pages; previous, up, and next.
- *
- * Also checks the printer friendly version of the outline.
- *
- * @param Node $node
- * Node to check.
- * @param $nodes
- * Nodes that should be in outline.
- * @param $previous
- * (optional) Previous link node. Defaults to FALSE.
- * @param $up
- * (optional) Up link node. Defaults to FALSE.
- * @param $next
- * (optional) Next link node. Defaults to FALSE.
- * @param $breadcrumb
- * The nodes that should be displayed in the breadcrumb.
- */
- function checkBookNode(Node $node, $nodes, $previous = FALSE, $up = FALSE, $next = FALSE, array $breadcrumb) {
-
-
- static $number = 0;
- $this->backdropGet('node/' . $node->nid);
-
-
- if ($nodes !== NULL) {
- $this->assertPattern($this->generateOutlinePattern($nodes), format_string('Node %number outline confirmed.', array('%number' => $number)));
- }
- else {
- $this->pass(format_string('Node %number does not have outline.', array('%number' => $number)));
- }
-
-
- if ($previous) {
- $prev_link = '<a href="/node/' . $previous->nid . '" rel="prev" title="Go to previous page"><b>‹</b> ' . $previous->title . '</a>';
- $this->assertRaw($prev_link, 'Previous page link found.');
- }
-
- if ($up) {
- $up_link = '<a href="/node/' . $up->nid . '" title="Go to parent page">up</a>';
- $this->assertRaw($up_link, 'Up page link found.');
- }
-
- if ($next) {
- $next_link = '<a href="/node/' . $next->nid . '" rel="next" title="Go to next page">' . $next->title . ' <b>›</b></a>';
- $this->assertRaw($next_link, 'Next page link found.');
- }
-
-
- $expected_breadcrumb = array();
- $expected_breadcrumb[] = url('');
- foreach ($breadcrumb as $a_node) {
- $expected_breadcrumb[] = url('node/' . $a_node->nid);
- }
-
-
- $links = $this->xpath('//nav[@class="breadcrumb"]/ol/li/a');
- $got_breadcrumb = array();
- foreach ($links as $link) {
- $got_breadcrumb[] = (string) $link['href'];
- }
-
-
- $this->assertIdentical($expected_breadcrumb, $got_breadcrumb, 'The breadcrumb is correctly displayed on the page.');
-
- $number++;
- }
-
-
- * Creates a regular expression to check for the sub-nodes in the outline.
- *
- * @param array $nodes
- * An array of nodes to check in outline.
- *
- * @return
- * A regular expression that locates sub-nodes of the outline.
- */
- function generateOutlinePattern($nodes) {
- $outline = '';
- foreach ($nodes as $node) {
- $outline .= '(node\/' . $node->nid . ')(.*?)(' . $node->title . ')(.*?)';
- }
-
- return '/<nav id="book-navigation-' . $this->book->nid . '"(.*?)<ul(.*?)' . $outline . '<\/ul>/s';
- }
-
-
- * Creates a book node.
- *
- * @param $book_nid
- * A book node ID or set to 'new' to create a new book.
- * @param $parent
- * (optional) Parent book reference ID. Defaults to NULL.
- */
- function createBookNode($book_nid, $parent = NULL) {
-
-
- static $number = 0;
-
- $edit = array();
- $langcode = LANGUAGE_NONE;
- $edit["title"] = $number . ' - SimpleTest test node ' . $this->randomName(10);
- $edit["body[$langcode][0][value]"] = 'SimpleTest test body ' . $this->randomName(32) . ' ' . $this->randomName(32);
- $edit['book[bid]'] = $book_nid;
-
- if ($parent !== NULL) {
- $this->backdropPost('node/add/book', $edit, t('Change book (update list of parents)'));
-
- $edit['book[plid]'] = $parent;
- $this->backdropPost(NULL, $edit, t('Save'));
- }
- else {
- $this->backdropPost('node/add/book', $edit, t('Save'));
- }
-
-
- $node = $this->backdropGetNodeByTitle($edit['title']);
- $this->assertNotNull(($node === FALSE ? NULL : $node), 'Book node found in database.');
- $number++;
-
- return $node;
- }
-
-
- * Tests the functionality of the book navigation block.
- */
- function testBookNavigationBlock() {
- $this->backdropLogin($this->admin_user);
-
-
- $layout_name = 'default';
- $region = 'sidebar';
- $block = array(
- 'module' => 'book',
- 'delta' => 'navigation',
- );
- $block_title = $this->randomName(16);
- $edit = array(
- 'region' => $region,
- 'title' => $block_title,
- 'title_display' => LAYOUT_TITLE_CUSTOM,
- );
- $this->backdropPost('admin/structure/layouts/manage/' . $layout_name . '/add-block/editor/' . $region . '/' . $block['module'] . ':' . $block['delta'], $edit, t('Add block'));
- $this->backdropPost('admin/structure/layouts/manage/' . $layout_name, array(), t('Save layout'));
-
-
- $edit = array();
- $edit['anonymous[node test view]'] = TRUE;
- $this->backdropPost('admin/config/people/permissions/anonymous', $edit, t('Save permissions'));
- $this->assertText(t('The changes have been saved.'), "Permission 'node test view' successfully assigned to anonymous users.");
-
-
- $nodes = $this->createBook();
- $this->backdropGet('user');
- $this->assertText($block_title, 'Book navigation block is displayed.');
- $this->assertText($this->book->title, format_string('Link to book root (@title) is displayed.', array('@title' => $nodes[0]->title)));
- $this->assertNoText($nodes[0]->title, 'No links to individual book pages are displayed.');
- }
-
-
- * Tests the book navigation block when an access module is enabled.
- */
- function testNavigationBlockOnAccessModuleEnabled() {
- $this->backdropLogin($this->admin_user);
-
-
- $block_title = $this->randomName(16);
-
-
- $layout_name = 'default';
- $region = 'sidebar';
- $block = array(
- 'module' => 'book',
- 'delta' => 'navigation',
- );
- $block_title = $this->randomName(16);
- $edit = array(
- 'region' => $region,
- 'title' => $block_title,
- 'title_display' => LAYOUT_TITLE_CUSTOM,
- 'block_settings[book_mode]' => 'book pages',
- );
- $this->backdropPost('admin/structure/layouts/manage/' . $layout_name . '/add-block/editor/' . $region . '/' . $block['module'] . ':' . $block['delta'], $edit, t('Add block'));
- $this->backdropPost('admin/structure/layouts/manage/' . $layout_name, array(), t('Save layout'));
-
-
- $edit = array();
- $edit['anonymous[node test view]'] = TRUE;
- $this->backdropPost('admin/config/people/permissions/anonymous', $edit, t('Save permissions'));
- $this->assertText(t('The changes have been saved.'), "Permission 'node test view' successfully assigned to anonymous users.");
-
-
- $this->createBook();
-
-
- $this->backdropLogin($this->web_user);
- $this->backdropGet('node/' . $this->book->nid);
- $this->assertText($block_title, 'Book navigation block is displayed to registered users.');
- $this->backdropLogout();
-
-
- $this->backdropGet('node/' . $this->book->nid);
- $this->assertText($block_title, 'Book navigation block is displayed to anonymous users.');
- }
-
-
- * Tests the access for deleting top-level book nodes.
- */
- function testBookDelete() {
- $nodes = $this->createBook();
- $this->backdropLogin($this->admin_user);
- $edit = array();
-
-
- $this->backdropGet('node/' . $this->book->nid . '/outline-remove');
- $this->assertResponse('403', 'Deleting top-level book node properly forbidden.');
- $this->backdropPost('node/' . $nodes[4]->nid . '/outline-remove', $edit, t('Remove'));
- $node4 = node_load($nodes[4]->nid, NULL, TRUE);
- $this->assertTrue(empty($node4->book), 'Deleting child book node properly allowed.');
-
-
- foreach ($nodes as $node) {
- $nids[] = $node->nid;
- }
- node_delete_multiple($nids);
- $this->backdropPost('node/' . $this->book->nid . '/outline-remove', $edit, t('Remove'));
- $node = node_load($this->book->nid, NULL, TRUE);
- $this->assertTrue(empty($node->book), 'Deleting childless top-level book node properly allowed.');
- }
- }
-