book.test 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. <?php
  2. /**
  3. * @file
  4. * Tests for book.module.
  5. */
  6. /**
  7. * Tests the functionality of the Book module.
  8. */
  9. class BookTestCase extends DrupalWebTestCase {
  10. /**
  11. * A book node.
  12. *
  13. * @var object
  14. */
  15. protected $book;
  16. /**
  17. * A user with permission to create and edit books.
  18. *
  19. * @var object
  20. */
  21. protected $book_author;
  22. /**
  23. * A user with permission to view a book and access printer-friendly version.
  24. *
  25. * @var object
  26. */
  27. protected $web_user;
  28. /**
  29. * A user with permission to create and edit books and to administer blocks.
  30. *
  31. * @var object
  32. */
  33. protected $admin_user;
  34. public static function getInfo() {
  35. return array(
  36. 'name' => 'Book functionality',
  37. 'description' => 'Create a book, add pages, and test book interface.',
  38. 'group' => 'Book',
  39. );
  40. }
  41. function setUp() {
  42. parent::setUp(array('book', 'node_access_test'));
  43. // node_access_test requires a node_access_rebuild().
  44. node_access_rebuild();
  45. // Create users.
  46. $this->book_author = $this->drupalCreateUser(array('create new books', 'create book content', 'edit own book content', 'add content to books'));
  47. $this->web_user = $this->drupalCreateUser(array('access printer-friendly version', 'node test view'));
  48. $this->admin_user = $this->drupalCreateUser(array('create new books', 'create book content', 'edit own book content', 'add content to books', 'administer blocks', 'administer permissions', 'administer book outlines', 'node test view'));
  49. }
  50. /**
  51. * Creates a new book with a page hierarchy.
  52. */
  53. function createBook() {
  54. // Create new book.
  55. $this->drupalLogin($this->book_author);
  56. $this->book = $this->createBookNode('new');
  57. $book = $this->book;
  58. /*
  59. * Add page hierarchy to book.
  60. * Book
  61. * |- Node 0
  62. * |- Node 1
  63. * |- Node 2
  64. * |- Node 3
  65. * |- Node 4
  66. */
  67. $nodes = array();
  68. $nodes[] = $this->createBookNode($book->nid); // Node 0.
  69. $nodes[] = $this->createBookNode($book->nid, $nodes[0]->book['mlid']); // Node 1.
  70. $nodes[] = $this->createBookNode($book->nid, $nodes[0]->book['mlid']); // Node 2.
  71. $nodes[] = $this->createBookNode($book->nid); // Node 3.
  72. $nodes[] = $this->createBookNode($book->nid); // Node 4.
  73. $this->drupalLogout();
  74. return $nodes;
  75. }
  76. /**
  77. * Tests book functionality through node interfaces.
  78. */
  79. function testBook() {
  80. // Create new book.
  81. $nodes = $this->createBook();
  82. $book = $this->book;
  83. $this->drupalLogin($this->web_user);
  84. // Check that book pages display along with the correct outlines and
  85. // previous/next links.
  86. $this->checkBookNode($book, array($nodes[0], $nodes[3], $nodes[4]), FALSE, FALSE, $nodes[0]);
  87. $this->checkBookNode($nodes[0], array($nodes[1], $nodes[2]), $book, $book, $nodes[1], array($book));
  88. $this->checkBookNode($nodes[1], NULL, $nodes[0], $nodes[0], $nodes[2], array($book, $nodes[0]));
  89. $this->checkBookNode($nodes[2], NULL, $nodes[1], $nodes[0], $nodes[3], array($book, $nodes[0]));
  90. $this->checkBookNode($nodes[3], NULL, $nodes[2], $book, $nodes[4], array($book));
  91. $this->checkBookNode($nodes[4], NULL, $nodes[3], $book, FALSE, array($book));
  92. $this->drupalLogout();
  93. // Create a second book, and move an existing book page into it.
  94. $this->drupalLogin($this->book_author);
  95. $other_book = $this->createBookNode('new');
  96. $node = $this->createBookNode($book->nid);
  97. $edit = array('book[bid]' => $other_book->nid);
  98. $this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
  99. $this->drupalLogout();
  100. $this->drupalLogin($this->web_user);
  101. // Check that the nodes in the second book are displayed correctly.
  102. // First we must set $this->book to the second book, so that the
  103. // correct regex will be generated for testing the outline.
  104. $this->book = $other_book;
  105. $this->checkBookNode($other_book, array($node), FALSE, FALSE, $node);
  106. $this->checkBookNode($node, NULL, $other_book, $other_book, FALSE, array($other_book));
  107. }
  108. /**
  109. * Checks the outline of sub-pages; previous, up, and next.
  110. *
  111. * Also checks the printer friendly version of the outline.
  112. *
  113. * @param $node
  114. * Node to check.
  115. * @param $nodes
  116. * Nodes that should be in outline.
  117. * @param $previous
  118. * (optional) Previous link node. Defaults to FALSE.
  119. * @param $up
  120. * (optional) Up link node. Defaults to FALSE.
  121. * @param $next
  122. * (optional) Next link node. Defaults to FALSE.
  123. * @param $breadcrumb
  124. * (optional) The nodes that should be displayed in the breadcrumb.
  125. */
  126. function checkBookNode($node, $nodes = NULL, $previous = FALSE, $up = FALSE, $next = FALSE, array $breadcrumb = array()) {
  127. // $number does not use drupal_static as it should not be reset
  128. // since it uniquely identifies each call to checkBookNode().
  129. static $number = 0;
  130. $this->drupalGet('node/' . $node->nid);
  131. // Check outline structure.
  132. if ($nodes !== NULL) {
  133. $this->assertPattern($this->generateOutlinePattern($nodes), format_string('Node %number outline confirmed.', array('%number' => $number)));
  134. }
  135. else {
  136. $this->pass(format_string('Node %number does not have outline.', array('%number' => $number)));
  137. }
  138. // Check previous, up, and next links.
  139. if ($previous) {
  140. $this->assertRaw(l('‹ ' . $previous->title, 'node/' . $previous->nid, array('attributes' => array('class' => array('page-previous'), 'title' => t('Go to previous page')))), 'Previous page link found.');
  141. }
  142. if ($up) {
  143. $this->assertRaw(l('up', 'node/' . $up->nid, array('attributes' => array('class' => array('page-up'), 'title' => t('Go to parent page')))), 'Up page link found.');
  144. }
  145. if ($next) {
  146. $this->assertRaw(l($next->title . ' ›', 'node/' . $next->nid, array('attributes' => array('class' => array('page-next'), 'title' => t('Go to next page')))), 'Next page link found.');
  147. }
  148. // Compute the expected breadcrumb.
  149. $expected_breadcrumb = array();
  150. $expected_breadcrumb[] = url('');
  151. foreach ($breadcrumb as $a_node) {
  152. $expected_breadcrumb[] = url('node/' . $a_node->nid);
  153. }
  154. // Fetch links in the current breadcrumb.
  155. $links = $this->xpath('//div[@class="breadcrumb"]/a');
  156. $got_breadcrumb = array();
  157. foreach ($links as $link) {
  158. $got_breadcrumb[] = (string) $link['href'];
  159. }
  160. // Compare expected and got breadcrumbs.
  161. $this->assertIdentical($expected_breadcrumb, $got_breadcrumb, 'The breadcrumb is correctly displayed on the page.');
  162. // Check printer friendly version.
  163. $this->drupalGet('book/export/html/' . $node->nid);
  164. $this->assertText($node->title, 'Printer friendly title found.');
  165. $this->assertRaw(check_markup($node->body[LANGUAGE_NONE][0]['value'], $node->body[LANGUAGE_NONE][0]['format']), 'Printer friendly body found.');
  166. $number++;
  167. }
  168. /**
  169. * Creates a regular expression to check for the sub-nodes in the outline.
  170. *
  171. * @param array $nodes
  172. * An array of nodes to check in outline.
  173. *
  174. * @return
  175. * A regular expression that locates sub-nodes of the outline.
  176. */
  177. function generateOutlinePattern($nodes) {
  178. $outline = '';
  179. foreach ($nodes as $node) {
  180. $outline .= '(node\/' . $node->nid . ')(.*?)(' . $node->title . ')(.*?)';
  181. }
  182. return '/<div id="book-navigation-' . $this->book->nid . '"(.*?)<ul(.*?)' . $outline . '<\/ul>/s';
  183. }
  184. /**
  185. * Creates a book node.
  186. *
  187. * @param $book_nid
  188. * A book node ID or set to 'new' to create a new book.
  189. * @param $parent
  190. * (optional) Parent book reference ID. Defaults to NULL.
  191. */
  192. function createBookNode($book_nid, $parent = NULL) {
  193. // $number does not use drupal_static as it should not be reset
  194. // since it uniquely identifies each call to createBookNode().
  195. static $number = 0; // Used to ensure that when sorted nodes stay in same order.
  196. $edit = array();
  197. $langcode = LANGUAGE_NONE;
  198. $edit["title"] = $number . ' - SimpleTest test node ' . $this->randomName(10);
  199. $edit["body[$langcode][0][value]"] = 'SimpleTest test body ' . $this->randomName(32) . ' ' . $this->randomName(32);
  200. $edit['book[bid]'] = $book_nid;
  201. if ($parent !== NULL) {
  202. $this->drupalPost('node/add/book', $edit, t('Change book (update list of parents)'));
  203. $edit['book[plid]'] = $parent;
  204. $this->drupalPost(NULL, $edit, t('Save'));
  205. }
  206. else {
  207. $this->drupalPost('node/add/book', $edit, t('Save'));
  208. }
  209. // Check to make sure the book node was created.
  210. $node = $this->drupalGetNodeByTitle($edit['title']);
  211. $this->assertNotNull(($node === FALSE ? NULL : $node), 'Book node found in database.');
  212. $number++;
  213. return $node;
  214. }
  215. /**
  216. * Tests book export ("printer-friendly version") functionality.
  217. */
  218. function testBookExport() {
  219. // Create a book.
  220. $nodes = $this->createBook();
  221. // Login as web user and view printer-friendly version.
  222. $this->drupalLogin($this->web_user);
  223. $this->drupalGet('node/' . $this->book->nid);
  224. $this->clickLink(t('Printer-friendly version'));
  225. // Make sure each part of the book is there.
  226. foreach ($nodes as $node) {
  227. $this->assertText($node->title, 'Node title found in printer friendly version.');
  228. $this->assertRaw(check_markup($node->body[LANGUAGE_NONE][0]['value'], $node->body[LANGUAGE_NONE][0]['format']), 'Node body found in printer friendly version.');
  229. }
  230. // Make sure we can't export an unsupported format.
  231. $this->drupalGet('book/export/foobar/' . $this->book->nid);
  232. $this->assertResponse('404', 'Unsupported export format returned "not found".');
  233. // Make sure we get a 404 on a not existing book node.
  234. $this->drupalGet('book/export/html/123');
  235. $this->assertResponse('404', 'Not existing book node returned "not found".');
  236. // Make sure an anonymous user cannot view printer-friendly version.
  237. $this->drupalLogout();
  238. // Load the book and verify there is no printer-friendly version link.
  239. $this->drupalGet('node/' . $this->book->nid);
  240. $this->assertNoLink(t('Printer-friendly version'), 'Anonymous user is not shown link to printer-friendly version.');
  241. // Try getting the URL directly, and verify it fails.
  242. $this->drupalGet('book/export/html/' . $this->book->nid);
  243. $this->assertResponse('403', 'Anonymous user properly forbidden.');
  244. // Now grant anonymous users permission to view the printer-friendly
  245. // version and verify that node access restrictions still prevent them from
  246. // seeing it.
  247. user_role_grant_permissions(DRUPAL_ANONYMOUS_RID, array('access printer-friendly version'));
  248. $this->drupalGet('book/export/html/' . $this->book->nid);
  249. $this->assertResponse('403', 'Anonymous user properly forbidden from seeing the printer-friendly version when denied by node access.');
  250. }
  251. /**
  252. * Tests the functionality of the book navigation block.
  253. */
  254. function testBookNavigationBlock() {
  255. $this->drupalLogin($this->admin_user);
  256. // Set block title to confirm that the interface is available.
  257. $block_title = $this->randomName(16);
  258. $this->drupalPost('admin/structure/block/manage/book/navigation/configure', array('title' => $block_title), t('Save block'));
  259. $this->assertText(t('The block configuration has been saved.'), 'Block configuration set.');
  260. // Set the block to a region to confirm block is available.
  261. $edit = array();
  262. $edit['blocks[book_navigation][region]'] = 'footer';
  263. $this->drupalPost('admin/structure/block', $edit, t('Save blocks'));
  264. $this->assertText(t('The block settings have been updated.'), 'Block successfully move to footer region.');
  265. // Give anonymous users the permission 'node test view'.
  266. $edit = array();
  267. $edit[DRUPAL_ANONYMOUS_RID . '[node test view]'] = TRUE;
  268. $this->drupalPost('admin/people/permissions/' . DRUPAL_ANONYMOUS_RID, $edit, t('Save permissions'));
  269. $this->assertText(t('The changes have been saved.'), "Permission 'node test view' successfully assigned to anonymous users.");
  270. // Test correct display of the block.
  271. $nodes = $this->createBook();
  272. $this->drupalGet('<front>');
  273. $this->assertText($block_title, 'Book navigation block is displayed.');
  274. $this->assertText($this->book->title, format_string('Link to book root (@title) is displayed.', array('@title' => $nodes[0]->title)));
  275. $this->assertNoText($nodes[0]->title, 'No links to individual book pages are displayed.');
  276. }
  277. /**
  278. * Tests the book navigation block when an access module is enabled.
  279. */
  280. function testNavigationBlockOnAccessModuleEnabled() {
  281. $this->drupalLogin($this->admin_user);
  282. $edit = array();
  283. // Set the block title.
  284. $block_title = $this->randomName(16);
  285. $edit['title'] = $block_title;
  286. // Set block display to 'Show block only on book pages'.
  287. $edit['book_block_mode'] = 'book pages';
  288. $this->drupalPost('admin/structure/block/manage/book/navigation/configure', $edit, t('Save block'));
  289. $this->assertText(t('The block configuration has been saved.'), 'Block configuration set.');
  290. // Set the block to a region to confirm block is available.
  291. $edit = array();
  292. $edit['blocks[book_navigation][region]'] = 'footer';
  293. $this->drupalPost('admin/structure/block', $edit, t('Save blocks'));
  294. $this->assertText(t('The block settings have been updated.'), 'Block successfully move to footer region.');
  295. // Give anonymous users the permission 'node test view'.
  296. $edit = array();
  297. $edit[DRUPAL_ANONYMOUS_RID . '[node test view]'] = TRUE;
  298. $this->drupalPost('admin/people/permissions/' . DRUPAL_ANONYMOUS_RID, $edit, t('Save permissions'));
  299. $this->assertText(t('The changes have been saved.'), "Permission 'node test view' successfully assigned to anonymous users.");
  300. // Create a book.
  301. $this->createBook();
  302. // Test correct display of the block to registered users.
  303. $this->drupalLogin($this->web_user);
  304. $this->drupalGet('node/' . $this->book->nid);
  305. $this->assertText($block_title, 'Book navigation block is displayed to registered users.');
  306. $this->drupalLogout();
  307. // Test correct display of the block to anonymous users.
  308. $this->drupalGet('node/' . $this->book->nid);
  309. $this->assertText($block_title, 'Book navigation block is displayed to anonymous users.');
  310. }
  311. /**
  312. * Tests the access for deleting top-level book nodes.
  313. */
  314. function testBookDelete() {
  315. $nodes = $this->createBook();
  316. $this->drupalLogin($this->admin_user);
  317. $edit = array();
  318. // Test access to delete top-level and child book nodes.
  319. $this->drupalGet('node/' . $this->book->nid . '/outline/remove');
  320. $this->assertResponse('403', 'Deleting top-level book node properly forbidden.');
  321. $this->drupalPost('node/' . $nodes[4]->nid . '/outline/remove', $edit, t('Remove'));
  322. $node4 = node_load($nodes[4]->nid, NULL, TRUE);
  323. $this->assertTrue(empty($node4->book), 'Deleting child book node properly allowed.');
  324. // Delete all child book nodes and retest top-level node deletion.
  325. foreach ($nodes as $node) {
  326. $nids[] = $node->nid;
  327. }
  328. node_delete_multiple($nids);
  329. $this->drupalPost('node/' . $this->book->nid . '/outline/remove', $edit, t('Remove'));
  330. $node = node_load($this->book->nid, NULL, TRUE);
  331. $this->assertTrue(empty($node->book), 'Deleting childless top-level book node properly allowed.');
  332. }
  333. }