You are here

class ContentAccessModuleTestCase in Content Access 5

Same name and namespace in other branches
  1. 6 tests/content_access.test \ContentAccessModuleTestCase
  2. 7 tests/content_access.test \ContentAccessModuleTestCase

Hierarchy

Expanded class hierarchy of ContentAccessModuleTestCase

File

tests/content_access.test, line 10

View source
class ContentAccessModuleTestCase extends ContentAccessTestCase {
  var $node1;
  var $node2;

  /**
   * Implementation of get_info() for information
   */
  function get_info() {
    return array(
      'name' => t('Content Access Module Tests'),
      'desc' => t('Various tests to check permission settings on nodes.'),
      'group' => 'Content Access',
    );
  }
  function setUp() {
    parent::setUp();

    // Create test nodes
    $this->node1 = $this
      ->createNode();
    $this->node2 = $this
      ->createNode();
  }

  /**
   * Test for viewing nodes
   */
  function testViewAccess() {

    // Restrict access to the content type (access is only allowed for the author)
    $access_permissions = array(
      'view[author]' => TRUE,
      'view[1]' => FALSE,
      'view[2]' => FALSE,
    );
    $this
      ->changeAccessContentType($access_permissions);

    // Logout admin and try to access the node anonymously
    $this
      ->drupalGet(url('logout'));
    $this
      ->drupalGet(url('node/' . $this->node1->nid));
    $this
      ->assertText(t('Access denied'), 'node is not viewable');

    // Login test user, view node, access must be denied
    $this
      ->drupalLoginUser($this->test_user);
    $this
      ->drupalGet(url('node/' . $this->node1->nid));
    $this
      ->assertText(t('Access denied'), 'node is not viewable');

    // Login admin and grant access for viewing to the test user
    $this
      ->drupalGet(url('logout'));
    $this
      ->drupalLoginUser($this->admin_user);
    $this
      ->changeAccessContentTypeKeyword('view');

    // Logout admin and try to access the node anonymously
    // access must be denied again
    $this
      ->drupalGet(url('logout'));
    $this
      ->drupalGet(url('node/' . $this->node1->nid));
    $this
      ->assertText(t('Access denied'), 'node is not viewable');

    // Login test user, view node, access must be granted
    $this
      ->drupalLoginUser($this->test_user);
    $this
      ->drupalGet(url('node/' . $this->node1->nid));
    $this
      ->assertNoText(t('Access denied'), 'node is viewable');

    // Login admin and enable per node access
    $this
      ->drupalGet(url('logout'));
    $this
      ->drupalLoginUser($this->admin_user);
    $this
      ->changeAccessPerNode();

    // Restrict access on node2 for the test user role
    $this
      ->changeAccessNodeKeyword($this->node2, 'view', FALSE);

    // Logout admin and try to access both nodes anonymously
    $this
      ->drupalGet(url('logout'));
    $this
      ->drupalGet(url('node/' . $this->node1->nid));
    $this
      ->assertText(t('Access denied'), 'node1 is not viewable');
    $this
      ->drupalGet(url('node/' . $this->node2->nid));
    $this
      ->assertText(t('Access denied'), 'node2 is not viewable');

    // Login test user, view node1, access must be granted
    $this
      ->drupalLoginUser($this->test_user);
    $this
      ->drupalGet(url('node/' . $this->node1->nid));
    $this
      ->assertNoText(t('Access denied'), 'node1 is viewable');

    // View node2, access must be denied
    $this
      ->drupalGet(url('node/' . $this->node2->nid));
    $this
      ->assertText(t('Access denied'), 'node2 is not viewable');

    // Login admin, swap permissions between content type and node2
    $this
      ->drupalGet(url('logout'));
    $this
      ->drupalLoginUser($this->admin_user);

    // Restrict access to content type
    $this
      ->changeAccessContentTypeKeyword('view', FALSE);

    // Grant access to node2
    $this
      ->changeAccessNodeKeyword($this->node2, 'view');

    // Logout admin and try to access both nodes anonymously
    $this
      ->drupalGet(url('logout'));
    $this
      ->drupalGet(url('node/' . $this->node1->nid));
    $this
      ->assertText(t('Access denied'), 'node1 is not viewable');
    $this
      ->drupalGet(url('node/' . $this->node2->nid));
    $this
      ->assertText(t('Access denied'), 'node2 is not viewable');

    // Login test user, view node1, access must be denied
    $this
      ->drupalLoginUser($this->test_user);
    $this
      ->drupalGet(url('node/' . $this->node1->nid));
    $this
      ->assertText(t('Access denied'), 'node1 is not viewable');

    // View node2, access must be granted
    $this
      ->drupalGet(url('node/' . $this->node2->nid));
    $this
      ->assertNoText(t('Access denied'), 'node2 is viewable');
  }

  /**
   * Test for editing nodes
   */
  function testEditAccess() {

    // Logout admin and try to edit the node anonymously
    $this
      ->drupalGet(url('logout'));
    $this
      ->drupalGet(url('node/' . $this->node1->nid . '/edit'));
    $this
      ->assertText(t('Access denied'), 'edit access denied for anonymous');

    // Login test user, edit node, access must be denied
    $this
      ->drupalLoginUser($this->test_user);
    $this
      ->drupalGet(url('node/' . $this->node1->nid . '/edit'));
    $this
      ->assertText(t('Access denied'), 'edit access denied for test user');

    // Login admin and grant access for editing to the test user
    $this
      ->drupalGet(url('logout'));
    $this
      ->drupalLoginUser($this->admin_user);
    $this
      ->changeAccessContentTypeKeyword('update');

    // Logout admin and try to edit the node anonymously
    // access must be denied again
    $this
      ->drupalGet(url('logout'));
    $this
      ->drupalGet(url('node/' . $this->node1->nid . '/edit'));
    $this
      ->assertText(t('Access denied'), 'edit access denied for anonymous');

    // Login test user, edit node, access must be granted
    $this
      ->drupalLoginUser($this->test_user);
    $this
      ->drupalGet(url('node/' . $this->node1->nid . '/edit'));
    $this
      ->assertNoText(t('Access denied'), 'node1 is editable');

    // Login admin and enable per node access
    $this
      ->drupalGet(url('logout'));
    $this
      ->drupalLoginUser($this->admin_user);
    $this
      ->changeAccessPerNode();

    // Restrict edit access on node2 for the test user
    $this
      ->changeAccessNodeKeyword($this->node2, 'update', FALSE);

    // Logout admin and try to edit both nodes anonymously
    $this
      ->drupalGet(url('logout'));
    $this
      ->drupalGet(url('node/' . $this->node1->nid . '/edit'));
    $this
      ->assertText(t('Access denied'), 'node1 is not editable');
    $this
      ->drupalGet(url('node/' . $this->node2->nid . '/edit'));
    $this
      ->assertText(t('Access denied'), 'node2 is not editable');

    // Login test user, edit node1, access must be granted
    $this
      ->drupalLoginUser($this->test_user);
    $this
      ->drupalGet(url('node/' . $this->node1->nid . '/edit'));
    $this
      ->assertNoText(t('Access denied'), 'node1 is editable');

    // Edit node2, access must be denied
    $this
      ->drupalGet(url('node/' . $this->node2->nid . '/edit'));
    $this
      ->assertText(t('Access denied'), 'node2 is not editable');

    // Login admin, swap permissions between content type and node2
    $this
      ->drupalGet(url('logout'));
    $this
      ->drupalLoginUser($this->admin_user);

    // Restrict edit access to content type
    $this
      ->changeAccessContentTypeKeyword('update', FALSE);

    // Grant edit access to node2
    $this
      ->changeAccessNodeKeyword($this->node2, 'update');

    // Logout admin and try to edit both nodes anonymously
    $this
      ->drupalGet(url('logout'));
    $this
      ->drupalGet(url('node/' . $this->node1->nid . '/edit'));
    $this
      ->assertText(t('Access denied'), 'node1 is not editable');
    $this
      ->drupalGet(url('node/' . $this->node2->nid . '/edit'));
    $this
      ->assertText(t('Access denied'), 'node2 is not editable');

    // Login test user, edit node1, access must be denied
    $this
      ->drupalLoginUser($this->test_user);
    $this
      ->drupalGet(url('node/' . $this->node1->nid . '/edit'));
    $this
      ->assertText(t('Access denied'), 'node1 is not editable');

    // Edit node2, access must be granted
    $this
      ->drupalGet(url('node/' . $this->node2->nid . '/edit'));
    $this
      ->assertNoText(t('Access denied'), 'node2 is editable');
  }

  /**
   * Test for deleting nodes
   */
  function testDeleteAccess() {

    // Logout admin and try to delete the node anonymously
    $this
      ->drupalGet(url('logout'));
    $this
      ->drupalGet(url('node/' . $this->node1->nid . '/delete'));
    $this
      ->assertText(t('Access denied'), 'delete access denied for anonymous');

    // Login test user, delete node, access must be denied
    $this
      ->drupalLoginUser($this->test_user);
    $this
      ->drupalGet(url('node/' . $this->node1->nid . '/delete'));
    $this
      ->assertText(t('Access denied'), 'delete access denied for test user');

    // Login admin and grant access for deleting to the test user
    $this
      ->drupalGet(url('logout'));
    $this
      ->drupalLoginUser($this->admin_user);
    $this
      ->changeAccessContentTypeKeyword('delete');

    // Logout admin and try to edit the node anonymously
    // access must be denied again
    $this
      ->drupalGet(url('logout'));
    $this
      ->drupalGet(url('node/' . $this->node1->nid . '/delete'));
    $this
      ->assertText(t('Access denied'), 'delete access denied for anonymous');

    // Login test user, delete node, access must be granted
    $this
      ->drupalLoginUser($this->test_user);
    $this
      ->drupalPostRequest('node/' . $this->node1->nid . '/delete', array(), 'Delete');
    $this
      ->assertWantedRaw(t('%node has been deleted', array(
      '%node' => $this->node1->title,
    )), 'Test node was deleted successfully by test user');

    // Login admin and recreate test node1
    $this
      ->drupalGet(url('logout'));
    $this
      ->drupalLoginUser($this->admin_user);
    $this->node1 = $this
      ->createNode();

    // Enable per node access
    $this
      ->changeAccessPerNode();

    // Restrict delete access on node2 for the test user
    $this
      ->changeAccessNodeKeyword($this->node2, 'delete', FALSE);

    // Logout admin and try to delete both nodes anonymously
    $this
      ->drupalGet(url('logout'));
    $this
      ->drupalGet(url('node/' . $this->node1->nid . '/delete'));
    $this
      ->assertText(t('Access denied'), 'node1 is not deletable');
    $this
      ->drupalGet(url('node/' . $this->node2->nid . '/delete'));
    $this
      ->assertText(t('Access denied'), 'node2 is not deletable');

    // Login test user, delete node1, access must be granted
    $this
      ->drupalLoginUser($this->test_user);
    $this
      ->drupalGet(url('node/' . $this->node1->nid . '/delete'));
    $this
      ->assertNoText(t('Access denied'), 'node1 is deletable');

    // Delete node2, access must be denied
    $this
      ->drupalGet(url('node/' . $this->node2->nid . '/delete'));
    $this
      ->assertText(t('Access denied'), 'node2 is not deletable');

    // Login admin, swap permissions between content type and node2
    $this
      ->drupalGet(url('logout'));
    $this
      ->drupalLoginUser($this->admin_user);

    // Restrict delete access to content type
    $this
      ->changeAccessContentTypeKeyword('delete', FALSE);

    // Grant delete access to node2
    $this
      ->changeAccessNodeKeyword($this->node2, 'delete');

    // Logout admin and try to delete both nodes anonymously
    $this
      ->drupalGet(url('logout'));
    $this
      ->drupalGet(url('node/' . $this->node1->nid . '/delete'));
    $this
      ->assertText(t('Access denied'), 'node1 is not deletable');
    $this
      ->drupalGet(url('node/' . $this->node2->nid . '/delete'));
    $this
      ->assertText(t('Access denied'), 'node2 is not deletable');

    // Login test user, delete node1, access must be denied
    $this
      ->drupalLoginUser($this->test_user);
    $this
      ->drupalGet(url('node/' . $this->node1->nid . '/delete'));
    $this
      ->assertText(t('Access denied'), 'node1 is not deletable');

    // Delete node2, access must be granted
    $this
      ->drupalGet(url('node/' . $this->node2->nid . '/delete'));
    $this
      ->assertNoText(t('Access denied'), 'node2 is deletable');
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ContentAccessModuleTestCase::$node1 property Overrides ContentAccessTestCase::$node1
ContentAccessModuleTestCase::$node2 property Overrides ContentAccessTestCase::$node2
ContentAccessModuleTestCase::get_info function Implementation of get_info() for information
ContentAccessModuleTestCase::setUp function Preparation work that is done before each test. Test users, content types, nodes etc. are created. Overrides ContentAccessTestCase::setUp
ContentAccessModuleTestCase::testDeleteAccess function Test for deleting nodes
ContentAccessModuleTestCase::testEditAccess function Test for editing nodes
ContentAccessModuleTestCase::testViewAccess function Test for viewing nodes
ContentAccessTestCase::$admin_user property
ContentAccessTestCase::$content_type_name property
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::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