You are here

class ContentAccessTestCase in Content Access 5

Same name and namespace in other branches
  1. 6 tests/content_access_test_help.php \ContentAccessTestCase
  2. 7 tests/content_access_test_help.php \ContentAccessTestCase

@file Helper class with auxiliary functions for content access module tests

Hierarchy

Expanded class hierarchy of ContentAccessTestCase

File

tests/content_access_test_help.php, line 8
Helper class with auxiliary functions for content access module tests

View source
class ContentAccessTestCase extends DrupalTestCase {
  var $test_user;
  var $admin_user;
  var $content_type_name;
  var $url_content_type_name;
  var $node1;
  var $node2;

  /**
   * Preparation work that is done before each test.
   * Test users, content types, nodes etc. are created.
   */
  function setUp() {
    parent::setUp();

    // Create test user with seperate role
    $this->test_user = $this
      ->drupalCreateUserRolePerm();

    // Create admin user
    $this->admin_user = $this
      ->drupalCreateUserRolePerm(array(
      'administer content types',
      'grant content access',
      'grant own content access',
      'administer nodes',
    ));
    $this
      ->drupalLoginUser($this->admin_user);

    // Create test content type
    $this->content_type_name = strtolower($this
      ->randomName(5));
    $edit = array(
      'name' => $this->content_type_name,
      'type' => $this->content_type_name,
    );
    $this
      ->drupalPostRequest('admin/content/types/add', $edit, 'op');
    $this
      ->assertWantedRaw(t('The content type %type has been added.', array(
      '%type' => $this->content_type_name,
    )), 'Test content type was added successfully');
    $this->url_content_type_name = str_replace('_', '-', $this->content_type_name);
  }

  /**
   * Clear up work that is done after each test.
   * Users get deleted automatically, so we just have to delete the content type.
   * (nodes get deleted automatically, too)
   */
  function tearDown() {

    // Login admin and delete test content type
    $this
      ->drupalGet(url('logout'));
    $this
      ->drupalLoginUser($this->admin_user);
    $this
      ->drupalPostRequest('admin/content/types/' . $this->url_content_type_name . '/delete', array(), 'op');
    parent::tearDown();
  }

  /**
   * Creates a node for testing purposes
   * @return the node object
   */
  function createNode() {
    $title = $this
      ->randomName(10);
    $edit = array(
      'title' => $title,
      'body' => $this
        ->randomName(20),
    );
    $this
      ->drupalPostRequest('node/add/' . $this->url_content_type_name, $edit, 'Submit');
    $this
      ->assertWantedRaw(t('Your %content has been created.', array(
      '%content' => $this->content_type_name,
    )), 'Test node was added successfully');
    return node_load(array(
      'title' => $title,
      'type' => $this->content_type_name,
    ));
  }

  /**
   * Change access permissions for a content type
   */
  function changeAccessContentType($access_settings) {
    $this
      ->drupalPostRequest('admin/content/types/' . $this->url_content_type_name . '/access', $access_settings, 'Submit');
    $this
      ->assertText(t('Your changes have been saved.'), 'access rules of content type were updated successfully');
  }

  /**
   * Change access permissions for a content type by a given keyword (view, update or delete)
   * for the role of the user
   */
  function changeAccessContentTypeKeyword($keyword, $access = TRUE) {
    $user = $this->test_user;
    $roles = $user->roles;
    end($roles);
    $access_settings = array(
      $keyword . '[' . key($roles) . ']' => $access,
    );
    $this
      ->changeAccessContentType($access_settings);
  }

  /**
   * Change access permission for a node
   */
  function changeAccessNode($node, $access_settings) {
    $this
      ->drupalPostRequest('node/' . $node->nid . '/access', $access_settings, 'Submit');
    $this
      ->assertText(t('Your changes have been saved.'), 'access rules of node were updated successfully');
  }

  /**
   * Change access permissions for a node by a given keyword (view, update or delete)
   */
  function changeAccessNodeKeyword($node, $keyword, $access = TRUE) {
    $user = $this->test_user;
    $roles = $user->roles;
    end($roles);
    $access_settings = array(
      $keyword . '[' . key($roles) . ']' => $access,
    );
    $this
      ->changeAccessNode($node, $access_settings);
  }

  /**
   * Change the per node access setting for a content type
   */
  function changeAccessPerNode($access = TRUE) {
    $access_permissions = array(
      'per_node' => $access,
    );
    $this
      ->changeAccessContentType($access_permissions);
  }

  /**
   * Replacement for drupalPostRequest() if we don't want to reload a path
   */
  function postToCurrentPage($edit = array(), $submit) {
    foreach ($edit as $field_name => $field_value) {
      $ret = $this->_browser
        ->setFieldByName($field_name, $field_value) || $this->_browser
        ->setFieldById("edit-{$field_name}", $field_value);
      $this
        ->assertTrue($ret, " [browser] Setting {$field_name}=\"{$field_value}\"");
    }
    $ret = $this->_browser
      ->clickSubmit(t($submit)) || $this->_browser
      ->clickSubmitById($submit) || $this->_browser
      ->clickSubmitByName($submit) || $this->_browser
      ->clickImageByName($submit);
    $this
      ->assertTrue($ret, ' [browser] POST by click on ' . t($submit));
    $this->_content = $this->_browser
      ->getContent();
  }

  /**
   * Stores the current page in the files directory, so it can be viewed by the developer.
   * Useful for debugging code.
   */
  function debugCurrentPage() {
    static $count = 0;
    $count++;
    $path = 'tests';
    if (!file_check_directory($path, FILE_CREATE_DIRECTORY)) {
      return FALSE;

      //unable to create directory
    }
    $filepath = file_create_filename("page-{$count}.htm", $path);
    file_put_contents($filepath, $this
      ->drupalGetContent());
    echo '<a href="' . $filepath . '">created debug file ' . $count . '</a><br/>';

    //echo l("created debug file $count", $filepath);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ContentAccessTestCase::$admin_user property
ContentAccessTestCase::$content_type_name property
ContentAccessTestCase::$node1 property 1
ContentAccessTestCase::$node2 property 1
ContentAccessTestCase::$test_user property
ContentAccessTestCase::$url_content_type_name property
ContentAccessTestCase::changeAccessContentType function Change access permissions for a content type
ContentAccessTestCase::changeAccessContentTypeKeyword function Change access permissions for a content type by a given keyword (view, update or delete) for the role of the user
ContentAccessTestCase::changeAccessNode function Change access permission for a node
ContentAccessTestCase::changeAccessNodeKeyword function Change access permissions for a node by a given keyword (view, update or delete)
ContentAccessTestCase::changeAccessPerNode function Change the per node access setting for a content type
ContentAccessTestCase::createNode function Creates a node for testing purposes
ContentAccessTestCase::debugCurrentPage function Stores the current page in the files directory, so it can be viewed by the developer. Useful for debugging code.
ContentAccessTestCase::postToCurrentPage function Replacement for drupalPostRequest() if we don't want to reload a path
ContentAccessTestCase::setUp function Preparation work that is done before each test. Test users, content types, nodes etc. are created. 2
ContentAccessTestCase::tearDown function Clear up work that is done after each test. Users get deleted automatically, so we just have to delete the content type. (nodes get deleted automatically, too) Overrides DrupalTestCase::tearDown
DrupalTestCase::$_cleanupModules property
DrupalTestCase::$_cleanupRoles property
DrupalTestCase::$_cleanupUsers property
DrupalTestCase::$_cleanupVariables property
DrupalTestCase::$_content property
DrupalTestCase::assertCopy function Will trigger a pass if both parameters refer to different objects. Fail otherwise.
DrupalTestCase::assertEqual function Will trigger a pass if the two parameters have the same value only. Otherwise a fail.
DrupalTestCase::assertError function Confirms that an error has occurred and optionally that the error text matches exactly.
DrupalTestCase::assertErrorPattern function Confirms that an error has occurred and that the error text matches a Perl regular expression.
DrupalTestCase::assertIdentical function Will trigger a pass if the two parameters have the same value and same type. Otherwise a fail.
DrupalTestCase::assertIsA function Type and class test. Will pass if class matches the type name or is a subclass or if not an object, but the type is correct.
DrupalTestCase::assertNoErrors function Confirms that no errors have occurred so far in the test method.
DrupalTestCase::assertNotA function Type and class mismatch test. Will pass if class name or underling type does not match the one specified.
DrupalTestCase::assertNotEqual function Will trigger a pass if the two parameters have a different value. Otherwise a fail.
DrupalTestCase::assertNotIdentical function Will trigger a pass if the two parameters have the different value or different type.
DrupalTestCase::assertNotNull function Will be true if the value is set.
DrupalTestCase::assertNoUnwantedPattern function Will trigger a pass if the Perl regex pattern is not present in subject. Fail if found.
DrupalTestCase::assertNoUnwantedRaw function Will trigger a pass if the raw text is NOT found on the loaded page Fail otherwise.
DrupalTestCase::assertNull function Will be true if the value is null.
DrupalTestCase::assertReference function Will trigger a pass if both parameters refer to the same object. Fail otherwise.
DrupalTestCase::assertWantedPattern function Will trigger a pass if the Perl regex pattern is found in the subject. Fail otherwise.
DrupalTestCase::assertWantedRaw function Will trigger a pass if the raw text is found on the loaded page Fail otherwise.
DrupalTestCase::clickLink function Follows a link by name. Will click the first link found with this link text by default, or a later one if an index is given. Match is case insensitive with normalised space. Does make assertations if the click was sucessful or not and it does…
DrupalTestCase::drupalCheckAuth function @abstract Checks to see if we need to send a http-auth header to authenticate when browsing a site.
DrupalTestCase::drupalCreateRolePerm function Create a role / perm combination specified by permissions
DrupalTestCase::drupalCreateUserRolePerm function Creates a user / role / permissions combination specified by permissions
DrupalTestCase::drupalGet function @abstract Brokder for the get function adds the authentication headers if necessary @author Earnest Berry III <earnest.berry@gmail.com>
DrupalTestCase::drupalGetContent function @TODO: needs documentation
DrupalTestCase::drupalLoginUser function Logs in a user with the internal browser
DrupalTestCase::drupalModuleDisable function Disables a drupal module
DrupalTestCase::drupalModuleEnable function Enables a drupal module
DrupalTestCase::drupalPostRequest function Do a post request on a drupal page. It will be done as usual post request with SimpleBrowser
DrupalTestCase::drupalRawPost function @abstract Broker for the post function adds the authentication headers if necessary @author Earnest Berry III <earnest.berry@gmail.com>
DrupalTestCase::DrupalTestCase function
DrupalTestCase::drupalVariableSet function Set a druapl variable and keep track of the changes for tearDown()
DrupalTestCase::randomName function Generates a random string, to be used as name or whatever
DrupalTestCase::run function Just some info for the reporter