Fix for book_access.test

  1. <?php
  2. // $Id$
  3.  
  4. /**
  5.  * @file
  6.  * Test file for Book Access module.
  7.  *
  8.  */
  9.  
  10. class BookAccessTestCase extends DrupalWebTestCase {
  11.   /**
  12.    * Check that the current user can/cannot view/update/delete the given book.
  13.    * @param $book
  14.    *   A book-array as returned by createBookPage().
  15.    * @param $view
  16.    *   If TRUE, then check that the current user CAN view the given book.
  17.    * @param $update
  18.    *   If TRUE, then check that the current user CAN edit the given book.
  19.    * @param $delete
  20.    *   If TRUE, then check that the current user CAN delete the given book.
  21.    */
  22.   protected function checkBookAccess($book, $view = TRUE, $update = TRUE, $delete = TRUE) {
  23.     $this->drupalGet($book['url']);
  24.  
  25.     if ($view) {
  26.       $this->assertNoText('Access denied');
  27.     }
  28.     else {
  29.       $this->assertText('Access denied');
  30.     }
  31.  
  32.     if ($update) {
  33.       $this->assertLink('Edit');
  34.       $this->drupalGet($book['url'] . '/edit');
  35.       $this->assertNoText('Access denied');
  36.     }
  37.     else {
  38.       $this->assertNoLink('Edit');
  39.       $this->drupalGet($book['url'] . '/edit');
  40.       $this->assertText('Access denied');
  41.     }
  42.  
  43.     if ($delete) {
  44.       $this->drupalGet($book['url'] . '/delete');
  45.       $this->assertNoText('Access denied');
  46.       $this->assertText('Are you sure you want to delete');
  47.     }
  48.     else {
  49.       $this->drupalGet($book['url'] . '/delete');
  50.       $this->assertText('Access denied');
  51.       $this->assertNoText('Are you sure you want to delete');
  52.     }
  53.   }
  54.  
  55.   /**
  56.    * Create a book page, optionally with parent book.
  57.    *
  58.    * @param $prefix
  59.    *   (Optional) A string prefix to use on the Book title and body. Defaults to
  60.    *   "BOOK".
  61.    * @param $book
  62.    *   (Optional) If $book is NULL (default) then create a book page without
  63.    *   naming a book. If $book is 'new' then create a book page and a new book.
  64.    *   If $book is an array (as returned from this function) use it as the
  65.    *   parent for a new book page.
  66.    *
  67.    * @return
  68.    *   An array of information about the new book created, including 'nid',
  69.    *   'url', 'bid', and 'plid'.
  70.    */
  71.   protected function createBookPage($prefix = 'BOOK', $book) {
  72.     $edit = array(
  73.       'title' => $this->randomName(10, $prefix . '_TITLE_'),
  74.       'body' => $this->randomName(20, $prefix . '_BODY_'),
  75.     );
  76.  
  77.     $options = array();
  78.     if ($book == 'new') {
  79.       // Create a new Book.
  80.       $edit['book[bid]'] = 'new';
  81.     }
  82.     elseif (is_array($book)) {
  83.       // Name the given Book as parent.
  84.       $options['query'] = array('parent' => $book['mlid']); // Need to load a different form.
  85.       $edit['book[bid]'] = $book['bid'];
  86.       $edit['book[plid]'] = $book['mlid'];
  87.     }
  88.     else {
  89.       // No Book.
  90.       $edit['book[bid]'] = 0;
  91.     }
  92.  
  93.     // Create the book, and parse for NID
  94.     $this->drupalPost('node/add/book', $edit, 'Save', $options);
  95.     $edit['url'] = $this->getUrl();
  96.  
  97.     $bits = explode('/', $this->getUrl());
  98.  
  99.     $node = node_load(array_pop($bits));
  100.     $edit['mlid'] = $node->book['mlid'];
  101.     $edit['nid'] = $node->book['nid'];
  102.     $edit['bid'] = $node->book['bid'];
  103.     return $edit;
  104.   }
  105.  
  106.   static public function getInfo() {
  107.     return array(
  108.       'name'        => t('Access test'),
  109.       'description' => t('Test access control to books and book pages.'),
  110.       'group'       => t('Book access')
  111.     );
  112.   }
  113.  
  114.   function setUp() {
  115.     parent::setUp('book', 'book_access');
  116.  
  117.   }
  118.  
  119.   /**
  120.    * Exercise basic functionality of Book Access module.  Note: this is NOT a
  121.    * complete test of Book module itself, so it is reccommended that you run the
  122.    * Book module test at the same time.
  123.    */
  124.   function testBasicOperation() {
  125.     // Create book content and check the owner has full access to it.
  126.     $book_author = $this->drupalCreateUser(
  127.       array(
  128.         // Node permissions
  129.         'create book content',
  130.         'edit own book content',
  131.         'delete own book content',
  132.         // Book permissions
  133.         'create new books',
  134.         'add content to books',
  135.       )
  136.     );
  137.  
  138.     $this->drupalLogin($book_author);
  139.  
  140.     // Create a book with some nested pages.
  141.     $book = $this->createBookPage('BOOK', 'new');
  142.     $bookpage1 = $this->createBookPage('PAGE1', $book);
  143.     $booksubpage1 = $this->createBookPage('SUBPAGE1', $bookpage1);
  144.     $booksubpage2 = $this->createBookPage('SUBPAGE2', $bookpage1);
  145.     $bookpage2 = $this->createBookPage('PAGE2', $book);
  146.  
  147.     $this->checkBookAccess($book);
  148.     $this->checkBookAccess($bookpage1);
  149.     $this->checkBookAccess($booksubpage1);
  150.     $this->checkBookAccess($booksubpage2);
  151.     $this->checkBookAccess($bookpage2);
  152.  
  153.     // Check if anonymous user can read, but not write/delete this (using the
  154.     // default permissions).
  155.     $this->drupalLogout();
  156.     $this->checkBookAccess($book, TRUE, FALSE, FALSE);
  157.     $this->checkBookAccess($bookpage1, TRUE, FALSE, FALSE);
  158.     $this->checkBookAccess($booksubpage1, TRUE, FALSE, FALSE);
  159.     $this->checkBookAccess($booksubpage2, TRUE, FALSE, FALSE);
  160.     $this->checkBookAccess($bookpage2, TRUE, FALSE, FALSE);
  161.  
  162.     // Remove all permissions for the book from Anonymous User, and check it
  163.     // can no longer read/write/delete the book.
  164.     $admin_user = $this->drupalCreateUser(
  165.       array(
  166.         // Book Access permissions
  167.         'administer book access'
  168.       )
  169.     );
  170.  
  171.     $this->drupalLogin($admin_user);
  172.  
  173.     $nid = $book['nid'];
  174.     $rid = DRUPAL_ANONYMOUS_RID;
  175.     $edit = array(
  176.       "access[$nid][view][$rid]" => FALSE,
  177.       "access[$nid][update][$rid]" => FALSE,
  178.       "access[$nid][delete][$rid]" => FALSE,
  179.     );
  180.  
  181.     $this->drupalPost('admin/content/book/access', $edit, 'Save configuration');
  182.  
  183.     $this->drupalLogout();
  184.     $this->checkBookAccess($book, FALSE, FALSE, FALSE);
  185.     $this->checkBookAccess($bookpage1, FALSE, FALSE, FALSE);
  186.     $this->checkBookAccess($booksubpage1, FALSE, FALSE, FALSE);
  187.     $this->checkBookAccess($booksubpage2, FALSE, FALSE, FALSE);
  188.     $this->checkBookAccess($bookpage2, FALSE, FALSE, FALSE);
  189.  
  190.     // Remove all permissions for the book to Anonymous User, and check it
  191.     // can now read/write/delete the book.
  192.     $admin_user = $this->drupalCreateUser(
  193.       array(
  194.         // Book Access permissions
  195.         'administer book access'
  196.       )
  197.     );
  198.  
  199.     $this->drupalLogin($admin_user);
  200.  
  201.     $nid = $book['nid'];
  202.     $rid = DRUPAL_ANONYMOUS_RID;
  203.     $edit = array(
  204.       "access[$nid][view][$rid]" => TRUE,
  205.       "access[$nid][update][$rid]" => TRUE,
  206.       "access[$nid][delete][$rid]" => TRUE,
  207.     );
  208.     $this->drupalPost('admin/content/book/access', $edit, 'Save configuration');
  209.  
  210.     $this->drupalLogout();
  211.     $this->checkBookAccess($book);
  212.     $this->checkBookAccess($bookpage1);
  213.     $this->checkBookAccess($booksubpage1);
  214.     $this->checkBookAccess($booksubpage2);
  215.     $this->checkBookAccess($bookpage2);
  216.   }
  217.  
  218.  
  219.   /**
  220.    * Write the given data to the given filename, relative to the Drupal
  221.    * temporary directory. This method is really useful for determining what was
  222.    * in the HTML page that DrupalWebTestCase actually saw.
  223.    *
  224.    * @param $filename
  225.    *   The filename to write (defaults to out.html).
  226.    * @param $data
  227.    *   The data that will be written to the above file (defaults to the content
  228.    *   of the current page).
  229.    */
  230.   function writeFile($filename = 'out.html', $data = NULL) {
  231.     $data = !isset($data) ?  $this->drupalGetContent() : $data;
  232.     $filename = file_directory_temp() . '/' . $filename;
  233.     $this->assertTrue(file_put_contents($filename, $data), t('Wrote content to %filename', array('%filename' => $filename)));
  234.   }
  235. }

Submit Fix

Any tags you'd like to associate with your code, delimitered by commas (example: Views, CCK, Module, etc).
Select the syntax highlighting mode to use.