BookTest.php 19.2 KB
Newer Older
1 2
<?php

3 4
/**
 * @file
5
 * Definition of Drupal\book\Tests\BookTest.
6 7
 */

8 9
namespace Drupal\book\Tests;

10
use Drupal\Core\Entity\EntityInterface;
11
use Drupal\simpletest\WebTestBase;
12

13 14 15
/**
 * Tests the functionality of the Book module.
 */
16
class BookTest extends WebTestBase {
17 18 19 20 21 22 23 24

  /**
   * Modules to enable.
   *
   * @var array
   */
  public static $modules = array('book', 'block', 'node_access_test');

25 26 27 28 29
  /**
   * A book node.
   *
   * @var object
   */
30
  protected $book;
31 32 33 34 35 36

  /**
   * A user with permission to create and edit books.
   *
   * @var object
   */
37
  protected $book_author;
38 39 40 41 42 43

  /**
   * A user with permission to view a book and access printer-friendly version.
   *
   * @var object
   */
44
  protected $web_user;
45 46 47 48 49 50

  /**
   * A user with permission to create and edit books and to administer blocks.
   *
   * @var object
   */
51 52
  protected $admin_user;

53
  public static function getInfo() {
54
    return array(
55 56 57
      'name' => 'Book functionality',
      'description' => 'Create a book, add pages, and test book interface.',
      'group' => 'Book',
58 59 60 61
    );
  }

  function setUp() {
62
    parent::setUp();
63 64 65

    // node_access_test requires a node_access_rebuild().
    node_access_rebuild();
66

67 68
    // Create users.
    $this->book_author = $this->drupalCreateUser(array('create new books', 'create book content', 'edit own book content', 'add content to books'));
69
    $this->web_user = $this->drupalCreateUser(array('access printer-friendly version', 'node test view'));
70
    $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', 'administer content types', 'administer site configuration'));
71
  }
72

73
  /**
74
   * Creates a new book with a page hierarchy.
75
   */
76
  function createBook() {
77
    // Create new book.
78
    $this->drupalLogin($this->book_author);
79 80 81 82 83

    $this->book = $this->createBookNode('new');
    $book = $this->book;

    /*
84
     * Add page hierarchy to book.
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
     * Book
     *  |- Node 0
     *   |- Node 1
     *   |- Node 2
     *  |- Node 3
     *  |- Node 4
     */
    $nodes = array();
    $nodes[] = $this->createBookNode($book->nid); // Node 0.
    $nodes[] = $this->createBookNode($book->nid, $nodes[0]->book['mlid']); // Node 1.
    $nodes[] = $this->createBookNode($book->nid, $nodes[0]->book['mlid']); // Node 2.
    $nodes[] = $this->createBookNode($book->nid); // Node 3.
    $nodes[] = $this->createBookNode($book->nid); // Node 4.

    $this->drupalLogout();
100

101 102 103 104
    return $nodes;
  }

  /**
105
   * Tests book functionality through node interfaces.
106 107 108 109 110
   */
  function testBook() {
    // Create new book.
    $nodes = $this->createBook();
    $book = $this->book;
111

112
    $this->drupalLogin($this->web_user);
113

114 115 116 117 118 119 120 121
    // Check that book pages display along with the correct outlines and
    // previous/next links.
    $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));
122 123 124 125

    $this->drupalLogout();

    // Create a second book, and move an existing book page into it.
126
    $this->drupalLogin($this->book_author);
127 128 129 130 131 132
    $other_book = $this->createBookNode('new');
    $node = $this->createBookNode($book->nid);
    $edit = array('book[bid]' => $other_book->nid);
    $this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));

    $this->drupalLogout();
133
    $this->drupalLogin($this->web_user);
134 135 136 137 138

    // Check that the nodes in the second book are displayed correctly.
    // First we must set $this->book to the second book, so that the
    // correct regex will be generated for testing the outline.
    $this->book = $other_book;
139 140
    $this->checkBookNode($other_book, array($node), FALSE, FALSE, $node, array());
    $this->checkBookNode($node, NULL, $other_book, $other_book, FALSE, array($other_book));
141 142 143
  }

  /**
144 145 146
   * Checks the outline of sub-pages; previous, up, and next.
   *
   * Also checks the printer friendly version of the outline.
147
   *
148
   * @param \Drupal\Core\Entity\EntityInterface $node
149
   *   Node to check.
150
   * @param $nodes
151
   *   Nodes that should be in outline.
152
   * @param $previous
153
   *   (optional) Previous link node. Defaults to FALSE.
154
   * @param $up
155
   *   (optional) Up link node. Defaults to FALSE.
156
   * @param $next
157 158
   *   (optional) Next link node. Defaults to FALSE.
   * @param array $breadcrumb
159
   *   The nodes that should be displayed in the breadcrumb.
160
   */
161
  function checkBookNode(EntityInterface $node, $nodes, $previous = FALSE, $up = FALSE, $next = FALSE, array $breadcrumb) {
162 163
    // $number does not use drupal_static as it should not be reset
    // since it uniquely identifies each call to checkBookNode().
164 165 166 167 168
    static $number = 0;
    $this->drupalGet('node/' . $node->nid);

    // Check outline structure.
    if ($nodes !== NULL) {
169
      $this->assertPattern($this->generateOutlinePattern($nodes), format_string('Node @number outline confirmed.', array('@number' => $number)));
170 171
    }
    else {
172
      $this->pass(format_string('Node %number does not have outline.', array('%number' => $number)));
173 174 175 176
    }

    // Check previous, up, and next links.
    if ($previous) {
177
      $this->assertRaw(l('<b>‹</b> ' . $previous->label(), 'node/' . $previous->nid, array('html' => TRUE, 'attributes' => array('rel' => array('prev'), 'title' => t('Go to previous page')))), 'Previous page link found.');
178
    }
179

180
    if ($up) {
181
      $this->assertRaw(l('Up', 'node/' . $up->nid, array('html'=> TRUE, 'attributes' => array('title' => t('Go to parent page')))), 'Up page link found.');
182
    }
183

184
    if ($next) {
185
      $this->assertRaw(l($next->label() . ' <b>›</b>', 'node/' . $next->nid, array('html'=> TRUE, 'attributes' => array('rel' => array('next'), 'title' => t('Go to next page')))), 'Next page link found.');
186 187
    }

188 189 190 191 192 193 194 195
    // Compute the expected breadcrumb.
    $expected_breadcrumb = array();
    $expected_breadcrumb[] = url('');
    foreach ($breadcrumb as $a_node) {
      $expected_breadcrumb[] = url('node/' . $a_node->nid);
    }

    // Fetch links in the current breadcrumb.
196
    $links = $this->xpath('//nav[@class="breadcrumb"]/ol/li/a');
197 198 199 200 201 202
    $got_breadcrumb = array();
    foreach ($links as $link) {
      $got_breadcrumb[] = (string) $link['href'];
    }

    // Compare expected and got breadcrumbs.
203
    $this->assertIdentical($expected_breadcrumb, $got_breadcrumb, 'The breadcrumb is correctly displayed on the page.');
204

205 206
    // Check printer friendly version.
    $this->drupalGet('book/export/html/' . $node->nid);
207 208
    $this->assertText($node->label(), 'Printer friendly title found.');
    $this->assertRaw(check_markup($node->body[LANGUAGE_NOT_SPECIFIED][0]['value'], $node->body[LANGUAGE_NOT_SPECIFIED][0]['format']), 'Printer friendly body found.');
209 210 211 212 213

    $number++;
  }

  /**
214 215 216 217
   * Creates a regular expression to check for the sub-nodes in the outline.
   *
   * @param array $nodes
   *   An array of nodes to check in outline.
218
   *
219 220
   * @return string
   *   A regular expression that locates sub-nodes of the outline.
221 222 223 224
   */
  function generateOutlinePattern($nodes) {
    $outline = '';
    foreach ($nodes as $node) {
225
      $outline .= '(node\/' . $node->nid . ')(.*?)(' . $node->label() . ')(.*?)';
226 227
    }

228
    return '/<nav id="book-navigation-' . $this->book->nid . '"(.*?)<ul(.*?)' . $outline . '<\/ul>/s';
229 230 231
  }

  /**
232
   * Creates a book node.
233
   *
234 235 236 237
   * @param int|string $book_nid
   *   A book node ID or set to 'new' to create a new book.
   * @param int|null $parent
   *   (optional) Parent book reference ID. Defaults to NULL.
238 239
   */
  function createBookNode($book_nid, $parent = NULL) {
240 241
    // $number does not use drupal_static as it should not be reset
    // since it uniquely identifies each call to createBookNode().
242 243 244
    static $number = 0; // Used to ensure that when sorted nodes stay in same order.

    $edit = array();
245
    $langcode = LANGUAGE_NOT_SPECIFIED;
246
    $edit["title"] = $number . ' - SimpleTest test node ' . $this->randomName(10);
247
    $edit["body[$langcode][0][value]"] = 'SimpleTest test body ' . $this->randomName(32) . ' ' . $this->randomName(32);
248 249 250 251 252 253 254 255 256 257 258 259 260
    $edit['book[bid]'] = $book_nid;

    if ($parent !== NULL) {
      $this->drupalPost('node/add/book', $edit, t('Change book (update list of parents)'));

      $edit['book[plid]'] = $parent;
      $this->drupalPost(NULL, $edit, t('Save'));
    }
    else {
      $this->drupalPost('node/add/book', $edit, t('Save'));
    }

    // Check to make sure the book node was created.
261
    $node = $this->drupalGetNodeByTitle($edit['title']);
262
    $this->assertNotNull(($node === FALSE ? NULL : $node), 'Book node found in database.');
263 264 265 266
    $number++;

    return $node;
  }
267

268 269 270 271 272 273
  /**
   * Tests book export ("printer-friendly version") functionality.
   */
  function testBookExport() {
    // Create a book.
    $nodes = $this->createBook();
274

275 276 277 278
    // Login as web user and view printer-friendly version.
    $this->drupalLogin($this->web_user);
    $this->drupalGet('node/' . $this->book->nid);
    $this->clickLink(t('Printer-friendly version'));
279

280 281
    // Make sure each part of the book is there.
    foreach ($nodes as $node) {
282 283
      $this->assertText($node->label(), 'Node title found in printer friendly version.');
      $this->assertRaw(check_markup($node->body[LANGUAGE_NOT_SPECIFIED][0]['value'], $node->body[LANGUAGE_NOT_SPECIFIED][0]['format']), 'Node body found in printer friendly version.');
284
    }
285

286 287
    // Make sure we can't export an unsupported format.
    $this->drupalGet('book/export/foobar/' . $this->book->nid);
288
    $this->assertResponse('404', 'Unsupported export format returned "not found".');
289

290 291
    // Make sure we get a 404 on a not existing book node.
    $this->drupalGet('book/export/html/123');
292
    $this->assertResponse('404', 'Not existing book node returned "not found".');
293

294 295
    // Make sure an anonymous user cannot view printer-friendly version.
    $this->drupalLogout();
296

297 298
    // Load the book and verify there is no printer-friendly version link.
    $this->drupalGet('node/' . $this->book->nid);
299
    $this->assertNoLink(t('Printer-friendly version'), 'Anonymous user is not shown link to printer-friendly version.');
300

301 302
    // Try getting the URL directly, and verify it fails.
    $this->drupalGet('book/export/html/' . $this->book->nid);
303
    $this->assertResponse('403', 'Anonymous user properly forbidden.');
304 305 306 307 308 309 310

    // Now grant anonymous users permission to view the printer-friendly
    // version and verify that node access restrictions still prevent them from
    // seeing it.
    user_role_grant_permissions(DRUPAL_ANONYMOUS_RID, array('access printer-friendly version'));
    $this->drupalGet('book/export/html/' . $this->book->nid);
    $this->assertResponse('403', 'Anonymous user properly forbidden from seeing the printer-friendly version when denied by node access.');
311
  }
312

313 314 315
  /**
   * Tests the functionality of the book navigation block.
   */
316
  function testBookNavigationBlock() {
317 318
    $this->drupalLogin($this->admin_user);

319
    // Enable the block.
320
    $block = $this->drupalPlaceBlock('book_navigation');
321

322
    // Give anonymous users the permission 'node test view'.
323
    $edit = array();
324 325 326
    $edit[DRUPAL_ANONYMOUS_RID . '[node test view]'] = TRUE;
    $this->drupalPost('admin/people/permissions/' . DRUPAL_ANONYMOUS_RID, $edit, t('Save permissions'));
    $this->assertText(t('The changes have been saved.'), "Permission 'node test view' successfully assigned to anonymous users.");
327

328 329 330
    // Test correct display of the block.
    $nodes = $this->createBook();
    $this->drupalGet('<front>');
331
    $this->assertText($block->label(), 'Book navigation block is displayed.');
332 333
    $this->assertText($this->book->label(), format_string('Link to book root (@title) is displayed.', array('@title' => $nodes[0]->label())));
    $this->assertNoText($nodes[0]->label(), 'No links to individual book pages are displayed.');
334
  }
335 336

  /**
337
   * Tests the book navigation block when an access module is enabled.
338
   */
339 340
  function testNavigationBlockOnAccessModuleEnabled() {
    $this->drupalLogin($this->admin_user);
341
    $block = $this->drupalPlaceBlock('book_navigation', array(), array('block_mode' => 'book pages'));
342 343 344 345 346 347 348 349 350 351 352 353 354

    // Give anonymous users the permission 'node test view'.
    $edit = array();
    $edit[DRUPAL_ANONYMOUS_RID . '[node test view]'] = TRUE;
    $this->drupalPost('admin/people/permissions/' . DRUPAL_ANONYMOUS_RID, $edit, t('Save permissions'));
    $this->assertText(t('The changes have been saved.'), "Permission 'node test view' successfully assigned to anonymous users.");

    // Create a book.
    $this->createBook();

    // Test correct display of the block to registered users.
    $this->drupalLogin($this->web_user);
    $this->drupalGet('node/' . $this->book->nid);
355
    $this->assertText($block->label(), 'Book navigation block is displayed to registered users.');
356 357 358 359
    $this->drupalLogout();

    // Test correct display of the block to anonymous users.
    $this->drupalGet('node/' . $this->book->nid);
360 361 362 363 364
    $this->assertText($block->label(), 'Book navigation block is displayed to anonymous users.');

    // Test the 'book pages' block_mode setting.
    $this->drupalGet('<front>');
    $this->assertNoText($block->label(), 'Book navigation block is not shown on non-book pages.');
365
  }
366 367 368 369 370 371 372 373 374 375 376

  /**
   * Tests the access for deleting top-level book nodes.
   */
   function testBookDelete() {
     $nodes = $this->createBook();
     $this->drupalLogin($this->admin_user);
     $edit = array();

     // Test access to delete top-level and child book nodes.
     $this->drupalGet('node/' . $this->book->nid . '/outline/remove');
377
     $this->assertResponse('403', 'Deleting top-level book node properly forbidden.');
378
     $this->drupalPost('node/' . $nodes[4]->nid . '/outline/remove', $edit, t('Remove'));
379
     $node4 = node_load($nodes[4]->nid, TRUE);
380
     $this->assertTrue(empty($node4->book), 'Deleting child book node properly allowed.');
381 382 383 384 385 386 387

     // Delete all child book nodes and retest top-level node deletion.
     foreach ($nodes as $node) {
       $nids[] = $node->nid;
     }
     node_delete_multiple($nids);
     $this->drupalPost('node/' . $this->book->nid . '/outline/remove', $edit, t('Remove'));
388
     $node = node_load($this->book->nid, TRUE);
389
     $this->assertTrue(empty($node->book), 'Deleting childless top-level book node properly allowed.');
390
   }
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407

  /*
   * Tests node type changing machine name when type is a book allowed type.
   */
  function testBookNodeTypeChange() {
    $this->drupalLogin($this->admin_user);
    // Change the name, machine name and description.
    $edit = array(
      'name' => 'Bar',
      'type' => 'bar',
    );
    $this->drupalPost('admin/structure/types/manage/book', $edit, t('Save content type'));

    // Ensure that the config book.settings:allowed_types has been updated with
    // the new machine and the old one has been removed.
    $this->assertTrue(book_type_is_allowed('bar'), 'Config book.settings:allowed_types contains the updated node type machine name "bar".');
    $this->assertFalse(book_type_is_allowed('book'), 'Config book.settings:allowed_types does not contain the old node type machine name "book".');
408 409 410 411 412 413 414 415 416 417 418 419 420 421

    $edit = array(
      'name' => 'Basic page',
      'title_label' => 'Title for basic page',
      'type' => 'page',
    );
    $this->drupalPost('admin/structure/types/add', $edit, t('Save content type'));

    // Add page to the allowed node types.
    $edit = array(
      'book_allowed_types[page]' => 'page',
      'book_allowed_types[bar]' => 'bar',
    );

422
    $this->drupalPost('admin/structure/book/settings', $edit, t('Save configuration'));
423 424 425 426 427
    $this->assertTrue(book_type_is_allowed('bar'), 'Config book.settings:allowed_types contains the bar node type.');
    $this->assertTrue(book_type_is_allowed('page'), 'Config book.settings:allowed_types contains the page node type.');

    // Test the order of the book.settings::allowed_types configuration is as
    // expected. The point of this test is to prove that after changing a node
428
    // type going to admin/structure/book/settings and pressing save without
429 430 431 432 433 434 435 436 437
    // changing anything should not alter the book.settings configuration. The
    // order will be:
    // @code
    // array(
    //   'bar',
    //   'page',
    // );
    // @endcode
    $current_config = config('book.settings')->init()->get();
438
    $this->drupalPost('admin/structure/book/settings', array(), t('Save configuration'));
439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458
    $this->assertIdentical($current_config, config('book.settings')->init()->get());

    // Change the name, machine name and description.
    $edit = array(
      'name' => 'Zebra book',
      'type' => 'zebra',
    );
    $this->drupalPost('admin/structure/types/manage/bar', $edit, t('Save content type'));
    $this->assertTrue(book_type_is_allowed('zebra'), 'Config book.settings:allowed_types contains the zebra node type.');
    $this->assertTrue(book_type_is_allowed('page'), 'Config book.settings:allowed_types contains the page node type.');

    // Test the order of the book.settings::allowed_types configuration is as
    // expected. The order should be:
    // @code
    // array(
    //   'page',
    //   'zebra',
    // );
    // @endcode
    $current_config = config('book.settings')->init()->get();
459
    $this->drupalPost('admin/structure/book/settings', array(), t('Save configuration'));
460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476
    $this->assertIdentical($current_config, config('book.settings')->init()->get());

    $edit = array(
      'name' => 'Animal book',
      'type' => 'zebra',
    );
    $this->drupalPost('admin/structure/types/manage/zebra', $edit, t('Save content type'));

    // Test the order of the book.settings::allowed_types configuration is as
    // expected. The order should be:
    // @code
    // array(
    //   'page',
    //   'zebra',
    // );
    // @endcode
    $current_config = config('book.settings')->init()->get();
477
    $this->drupalPost('admin/structure/book/settings', array(), t('Save configuration'));
478 479 480 481 482
    $this->assertIdentical($current_config, config('book.settings')->init()->get());

    // Ensure that after all the node type changes book.settings:child_type has
    // the expected value.
    $this->assertEqual(config('book.settings')->get('child_type'), 'zebra');
483
  }
484

485 486 487 488 489 490 491 492 493 494 495 496 497 498
  /**
   * Tests re-ordering of books.
   */
  public function testBookOrdering() {
    // Create new book.
    $nodes = $this->createBook();
    $book = $this->book;

    $this->drupalLogin($this->admin_user);
    $node1 = $this->createBookNode($book->nid);
    $node2 = $this->createBookNode($book->nid);
    $plid = $node1->book['mlid'];

    // Head to admin screen and attempt to re-order.
499
    $this->drupalGet('admin/structure/book/' . $book->nid);
500 501 502 503 504 505 506 507 508 509 510 511 512
    $edit = array(
      "table[book-admin-{$node1->nid}][weight]" => 1,
      "table[book-admin-{$node2->nid}][weight]" => 2,
      // Put node 2 under node 1.
      "table[book-admin-{$node2->nid}][plid]" => $plid,
    );
    $this->drupalPost(NULL, $edit, t('Save book pages'));
    // Verify weight was updated.
    $this->assertFieldByName("table[book-admin-{$node1->nid}][weight]", 1);
    $this->assertFieldByName("table[book-admin-{$node2->nid}][weight]", 2);
    $this->assertFieldByName("table[book-admin-{$node2->nid}][plid]", $plid);
  }
}