Skip navigation
Help

book.test

  1. drupal
    1. 7 drupal/modules/book/book.test

Tests for book.module.

Classes

NameDescription
BookTestCase

File

drupal/modules/book/book.test
View source
  1. <?php
  2. /**
  3. * @file
  4. * Tests for book.module.
  5. */
  6. class BookTestCase extends DrupalWebTestCase {
  7. protected $book;
  8. // $book_author is a user with permission to create and edit books.
  9. protected $book_author;
  10. // $web_user is a user with permission to view a book
  11. // and access the printer-friendly version.
  12. protected $web_user;
  13. // $admin_user is a user with permission to create and edit books and to administer blocks.
  14. protected $admin_user;
  15. public static function getInfo() {
  16. return array(
  17. 'name' => 'Book functionality',
  18. 'description' => 'Create a book, add pages, and test book interface.',
  19. 'group' => 'Book',
  20. );
  21. }
  22. function setUp() {
  23. parent::setUp('book');
  24. // Create users.
  25. $this->book_author = $this->drupalCreateUser(array('create new books', 'create book content', 'edit own book content', 'add content to books'));
  26. $this->web_user = $this->drupalCreateUser(array('access printer-friendly version'));
  27. $this->admin_user = $this->drupalCreateUser(array('create new books', 'create book content', 'edit own book content', 'add content to books', 'administer blocks'));
  28. }
  29. /**
  30. * Create a new book with a page hierarchy.
  31. */
  32. function createBook() {
  33. // Create new book.
  34. $this->drupalLogin($this->book_author);
  35. $this->book = $this->createBookNode('new');
  36. $book = $this->book;
  37. /*
  38. * Add page hierarchy to book.
  39. * Book
  40. * |- Node 0
  41. * |- Node 1
  42. * |- Node 2
  43. * |- Node 3
  44. * |- Node 4
  45. */
  46. $nodes = array();
  47. $nodes[] = $this->createBookNode($book->nid); // Node 0.
  48. $nodes[] = $this->createBookNode($book->nid, $nodes[0]->book['mlid']); // Node 1.
  49. $nodes[] = $this->createBookNode($book->nid, $nodes[0]->book['mlid']); // Node 2.
  50. $nodes[] = $this->createBookNode($book->nid); // Node 3.
  51. $nodes[] = $this->createBookNode($book->nid); // Node 4.
  52. $this->drupalLogout();
  53. return $nodes;
  54. }
  55. /**
  56. * Test book functionality through node interfaces.
  57. */
  58. function testBook() {
  59. // Create new book.
  60. $nodes = $this->createBook();
  61. $book = $this->book;
  62. $this->drupalLogin($this->web_user);
  63. // Check that book pages display along with the correct outlines and
  64. // previous/next links.
  65. $this->checkBookNode($book, array($nodes[0], $nodes[3], $nodes[4]), FALSE, FALSE, $nodes[0], array());
  66. $this->checkBookNode($nodes[0], array($nodes[1], $nodes[2]), $book, $book, $nodes[1], array($book));
  67. $this->checkBookNode($nodes[1], NULL, $nodes[0], $nodes[0], $nodes[2], array($book, $nodes[0]));
  68. $this->checkBookNode($nodes[2], NULL, $nodes[1], $nodes[0], $nodes[3], array($book, $nodes[0]));
  69. $this->checkBookNode($nodes[3], NULL, $nodes[2], $book, $nodes[4], array($book));
  70. $this->checkBookNode($nodes[4], NULL, $nodes[3], $book, FALSE, array($book));
  71. $this->drupalLogout();
  72. // Create a second book, and move an existing book page into it.
  73. $this->drupalLogin($this->book_author);
  74. $other_book = $this->createBookNode('new');
  75. $node = $this->createBookNode($book->nid);
  76. $edit = array('book[bid]' => $other_book->nid);
  77. $this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
  78. $this->drupalLogout();
  79. $this->drupalLogin($this->web_user);
  80. // Check that the nodes in the second book are displayed correctly.
  81. // First we must set $this->book to the second book, so that the
  82. // correct regex will be generated for testing the outline.
  83. $this->book = $other_book;
  84. $this->checkBookNode($other_book, array($node), FALSE, FALSE, $node, array());
  85. $this->checkBookNode($node, NULL, $other_book, $other_book, FALSE, array($other_book));
  86. }
  87. /**
  88. * Check the outline of sub-pages; previous, up, and next; and printer friendly version.
  89. *
  90. * @param $node
  91. * Node to check.
  92. * @param $nodes
  93. * Nodes that should be in outline.
  94. * @param $previous
  95. * Previous link node.
  96. * @param $up
  97. * Up link node.
  98. * @param $next
  99. * Next link node.
  100. * @param $breadcrumb
  101. * The nodes that should be displayed in the breadcrumb.
  102. */
  103. function checkBookNode($node, $nodes, $previous = FALSE, $up = FALSE, $next = FALSE, array $breadcrumb) {
  104. // $number does not use drupal_static as it should not be reset
  105. // since it uniquely identifies each call to checkBookNode().
  106. static $number = 0;
  107. $this->drupalGet('node/' . $node->nid);
  108. // Check outline structure.
  109. if ($nodes !== NULL) {
  110. $this->assertPattern($this->generateOutlinePattern($nodes), t('Node ' . $number . ' outline confirmed.'));
  111. }
  112. else {
  113. $this->pass(t('Node ' . $number . ' doesn\'t have outline.'));
  114. }
  115. // Check previous, up, and next links.
  116. if ($previous) {
  117. $this->assertRaw(l('‹ ' . $previous->title, 'node/' . $previous->nid, array('attributes' => array('class' => array('page-previous'), 'title' => t('Go to previous page')))), t('Previous page link found.'));
  118. }
  119. if ($up) {
  120. $this->assertRaw(l('up', 'node/' . $up->nid, array('attributes' => array('class' => array('page-up'), 'title' => t('Go to parent page')))), t('Up page link found.'));
  121. }
  122. if ($next) {
  123. $this->assertRaw(l($next->title . ' ›', 'node/' . $next->nid, array('attributes' => array('class' => array('page-next'), 'title' => t('Go to next page')))), t('Next page link found.'));
  124. }
  125. // Compute the expected breadcrumb.
  126. $expected_breadcrumb = array();
  127. $expected_breadcrumb[] = url('');
  128. foreach ($breadcrumb as $a_node) {
  129. $expected_breadcrumb[] = url('node/' . $a_node->nid);
  130. }
  131. // Fetch links in the current breadcrumb.
  132. $links = $this->xpath('//div[@class="breadcrumb"]/a');
  133. $got_breadcrumb = array();
  134. foreach ($links as $link) {
  135. $got_breadcrumb[] = (string) $link['href'];
  136. }
  137. // Compare expected and got breadcrumbs.
  138. $this->assertIdentical($expected_breadcrumb, $got_breadcrumb, t('The breadcrumb is correctly displayed on the page.'));
  139. // Check printer friendly version.
  140. $this->drupalGet('book/export/html/' . $node->nid);
  141. $this->assertText($node->title, t('Printer friendly title found.'));
  142. $this->assertRaw(check_markup($node->body[LANGUAGE_NONE][0]['value'], $node->body[LANGUAGE_NONE][0]['format']), t('Printer friendly body found.'));
  143. $number++;
  144. }
  145. /**
  146. * Create a regular expression to check for the sub-nodes in the outline.
  147. *
  148. * @param array $nodes Nodes to check in outline.
  149. */
  150. function generateOutlinePattern($nodes) {
  151. $outline = '';
  152. foreach ($nodes as $node) {
  153. $outline .= '(node\/' . $node->nid . ')(.*?)(' . $node->title . ')(.*?)';
  154. }
  155. return '/<div id="book-navigation-' . $this->book->nid . '"(.*?)<ul(.*?)' . $outline . '<\/ul>/s';
  156. }
  157. /**
  158. * Create book node.
  159. *
  160. * @param integer $book_nid Book node id or set to 'new' to create new book.
  161. * @param integer $parent Parent book reference id.
  162. */
  163. function createBookNode($book_nid, $parent = NULL) {
  164. // $number does not use drupal_static as it should not be reset
  165. // since it uniquely identifies each call to createBookNode().
  166. static $number = 0; // Used to ensure that when sorted nodes stay in same order.
  167. $edit = array();
  168. $langcode = LANGUAGE_NONE;
  169. $edit["title"] = $number . ' - SimpleTest test node ' . $this->randomName(10);
  170. $edit["body[$langcode][0][value]"] = 'SimpleTest test body ' . $this->randomName(32) . ' ' . $this->randomName(32);
  171. $edit['book[bid]'] = $book_nid;
  172. if ($parent !== NULL) {
  173. $this->drupalPost('node/add/book', $edit, t('Change book (update list of parents)'));
  174. $edit['book[plid]'] = $parent;
  175. $this->drupalPost(NULL, $edit, t('Save'));
  176. }
  177. else {
  178. $this->drupalPost('node/add/book', $edit, t('Save'));
  179. }
  180. // Check to make sure the book node was created.
  181. $node = $this->drupalGetNodeByTitle($edit['title']);
  182. $this->assertNotNull(($node === FALSE ? NULL : $node), t('Book node found in database.'));
  183. $number++;
  184. return $node;
  185. }
  186. /**
  187. * Tests book export ("printer-friendly version") functionality.
  188. */
  189. function testBookExport() {
  190. // Create a book.
  191. $nodes = $this->createBook();
  192. // Login as web user and view printer-friendly version.
  193. $this->drupalLogin($this->web_user);
  194. $this->drupalGet('node/' . $this->book->nid);
  195. $this->clickLink(t('Printer-friendly version'));
  196. // Make sure each part of the book is there.
  197. foreach ($nodes as $node) {
  198. $this->assertText($node->title, t('Node title found in printer friendly version.'));
  199. $this->assertRaw(check_markup($node->body[LANGUAGE_NONE][0]['value'], $node->body[LANGUAGE_NONE][0]['format']), t('Node body found in printer friendly version.'));
  200. }
  201. // Make sure we can't export an unsupported format.
  202. $this->drupalGet('book/export/foobar/' . $this->book->nid);
  203. $this->assertResponse('404', t('Unsupported export format returned "not found".'));
  204. // Make sure we get a 404 on a not existing book node.
  205. $this->drupalGet('book/export/html/123');
  206. $this->assertResponse('404', t('Not existing book node returned "not found".'));
  207. // Make sure an anonymous user cannot view printer-friendly version.
  208. $this->drupalLogout();
  209. // Load the book and verify there is no printer-friendly version link.
  210. $this->drupalGet('node/' . $this->book->nid);
  211. $this->assertNoLink(t('Printer-friendly version'), t('Anonymous user is not shown link to printer-friendly version.'));
  212. // Try getting the URL directly, and verify it fails.
  213. $this->drupalGet('book/export/html/' . $this->book->nid);
  214. $this->assertResponse('403', t('Anonymous user properly forbidden.'));
  215. }
  216. /**
  217. * Tests the functionality of the book navigation block.
  218. */
  219. function testBookNavigationBlock() {
  220. $this->drupalLogin($this->admin_user);
  221. // Set block title to confirm that the interface is available.
  222. $block_title = $this->randomName(16);
  223. $this->drupalPost('admin/structure/block/manage/book/navigation/configure', array('title' => $block_title), t('Save block'));
  224. $this->assertText(t('The block configuration has been saved.'), t('Block configuration set.'));
  225. // Set the block to a region to confirm block is available.
  226. $edit = array();
  227. $edit['blocks[book_navigation][region]'] = 'footer';
  228. $this->drupalPost('admin/structure/block', $edit, t('Save blocks'));
  229. $this->assertText(t('The block settings have been updated.'), t('Block successfully move to footer region.'));
  230. // Test correct display of the block.
  231. $nodes = $this->createBook();
  232. $this->drupalGet('<front>');
  233. $this->assertText($block_title, t('Book navigation block is displayed.'));
  234. $this->assertText($this->book->title, t('Link to book root (@title) is displayed.', array('@title' => $nodes[0]->title)));
  235. $this->assertNoText($nodes[0]->title, t('No links to individual book pages are displayed.'));
  236. }
  237. }