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