book.test 15 KB

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