You are here

class PmPermissionTestCase in Drupal PM (Project Management) 8

Same name and namespace in other branches
  1. 7.3 pm.permission.test \PmPermissionTestCase
  2. 7.2 pmpermission/pmpermission.test \PmpermissionTestCase

Define a test case for PM Permission.

Hierarchy

Expanded class hierarchy of PmPermissionTestCase

File

./pm.permission.test, line 10
Test definitions for PM Permission.

View source
class PmPermissionTestCase extends DrupalWebTestCase {

  /**
   * Returns meta data for PM Permission tests.
   */
  public static function getInfo() {
    return array(
      'name' => 'PM Permission functionality',
      'description' => 'Test the functionality of PM Permission.',
      'group' => 'Project Management',
    );
  }

  /**
   * Standard set up for all tests.
   */
  public function setUp() {
    parent::setUp('pm', 'pmorganization', 'pmproject');
    node_access_rebuild();
  }

  /**
   * Tests "view all" permission.
   */
  public function testpmpermissionViewAll() {

    // Allow PM Permission to override organization permissions.
    variable_set('node_permissions_pmorganization', 0);

    // Setup users.
    $users = array();

    // Create a node to test against.
    $organization_settings = array(
      'type' => 'pmorganization',
    );
    $organization = $this
      ->drupalCreateNode($organization_settings);
    $this
      ->assertTrue(isset($organization->nid), 'Node successfully created.');

    // Test that anonymous users can not view the node.
    $this
      ->drupalGet('node/' . $organization->nid);
    $this
      ->assertResponse(403, 'Access is denied to anonymous user.');

    // Create basic user and test that user cannot view the node.
    $users['basic'] = $this
      ->drupalCreateUser(array());
    $this
      ->assertTrue(is_object($users['basic']), 'Basic user successfully created.');
    $this
      ->drupalLogin($users['basic']);
    $this
      ->drupalGet('node/' . $organization->nid);
    $this
      ->assertResponse(403, 'Access is denied to basic user.');

    // Create privileged user and test that user can view the node.
    $user_permissions = array(
      'PM permission pmorganization: view all',
    );
    $this
      ->checkPermissions($user_permissions, TRUE);
    $users['privileged'] = $this
      ->drupalCreateUser($user_permissions);
    $this
      ->assertTrue(is_object($users['privileged']), 'Privileged user successfully created.');
    $this
      ->drupalLogin($users['privileged']);
    $this
      ->drupalGet('node/' . $organization->nid);
    $this
      ->assertResponse(200, 'Access granted to privileged user.');
  }

  /**
   * Tests "belonged" permission.
   */
  public function testpmpermissionBelonged() {

    // Allow PM Permission to override organization permissions.
    variable_set('node_permissions_pmorganization', 0);

    // Setup users.
    $users = array();

    // Create two users for testing view permissions (A & B).
    $user_permissions = array(
      'PM permission pmorganization: view own Organization',
    );
    $this
      ->checkPermissions($user_permissions, TRUE);
    $users['a'] = $this
      ->drupalCreateUser($user_permissions);
    $users['b'] = $this
      ->drupalCreateUser($user_permissions);
    $this
      ->assertTrue(is_object($users['a']), 'User A successfully created.');
    $this
      ->assertTrue(is_object($users['b']), 'User B successfully created.');
    $this
      ->assertTrue(is_numeric($users['b']->uid), 'User B uid is numeric.');
    $this
      ->assertTrue(!is_null($users['b']->uid), 'User B uid is not null.');

    // Create a node to test against.
    $organization_settings = array(
      'type' => 'pmorganization',
      'uid' => 1,
    );
    $organization_settings['pmorganization_members'][LANGUAGE_NONE][]['target_id'] = $users['b']->uid;
    $organization = $this
      ->drupalCreateNode($organization_settings);
    $this
      ->assertTrue(isset($organization->nid), 'Node successfully created.');

    // Test that anonymous users can not view the node.
    $this
      ->drupalLogout();
    $this
      ->drupalGet('node/' . $organization->nid);
    $this
      ->assertResponse(403, 'Access is denied to anonymous user.');

    // Test that user A can not view the node.
    $this
      ->drupalLogin($users['a']);
    $this
      ->drupalGet('node/' . $organization->nid);
    $this
      ->assertResponse(403, 'Access is denied to user A.');

    // Test that user B can view the node.
    $this
      ->drupalLogin($users['b']);
    $this
      ->drupalGet('node/' . $organization->nid);
    $this
      ->assertResponse(200, 'Access granted to user B.');
  }

  /**
   * Tests permissions after parent change.
   */
  public function testpmpermissionChangeParent() {

    // Allow PM Permission to override project permissions.
    variable_set('node_permissions_pmproject', 0);

    // Setup users.
    $users = array();

    // Create two users for testing view permissions (A & B).
    $user_permissions = array(
      'PM permission pmproject: view own Organization',
    );
    $this
      ->checkPermissions($user_permissions, TRUE);
    $users['a'] = $this
      ->drupalCreateUser($user_permissions);
    $users['b'] = $this
      ->drupalCreateUser($user_permissions);
    $this
      ->assertTrue(is_object($users['a']), 'User A successfully created.');
    $this
      ->assertTrue(is_object($users['b']), 'User B successfully created.');

    // Create two organization nodes (A & B) with respective users as members.
    $organization_a_settings = array(
      'type' => 'pmorganization',
      'uid' => 1,
    );
    $organization_a_settings['pmorganization_members'][LANGUAGE_NONE][]['target_id'] = $users['a']->uid;
    $organization_a = $this
      ->drupalCreateNode($organization_a_settings);
    $organization_b_settings = array(
      'type' => 'pmorganization',
      'uid' => 1,
    );
    $organization_b_settings['pmorganization_members'][LANGUAGE_NONE][]['target_id'] = $users['b']->uid;
    $organization_b = $this
      ->drupalCreateNode($organization_b_settings);

    // Create one project node, attached to organization A.
    $project_settings = array(
      'type' => 'pmproject',
      'uid' => 1,
    );
    $project_settings['pmproject_parent'][LANGUAGE_NONE][]['target_id'] = $organization_a->nid;
    $project = $this
      ->drupalCreateNode($project_settings);

    // Test that user A can view the project.
    $this
      ->drupalLogin($users['a']);
    $this
      ->drupalGet('node/' . $project->nid);
    $this
      ->assertResponse(200, 'Access granted to user A when project is attached to organization A.');

    // Test that user B can not view the project.
    $this
      ->drupalLogin($users['b']);
    $this
      ->drupalGet('node/' . $project->nid);
    $this
      ->assertResponse(403, 'Access is denied to user B when project is attached to organization A.');

    // Attach the project to organization B.
    $project->pmproject_parent[LANGUAGE_NONE][0]['target_id'] = $organization_b->nid;
    node_save($project);

    // Test that user A can not view the project.
    $this
      ->drupalLogin($users['a']);
    $this
      ->drupalGet('node/' . $project->nid);
    $this
      ->assertResponse(403, 'Access is denied to user A when project is attached to organization B.');

    // Test that user B can view the project.
    $this
      ->drupalLogin($users['b']);
    $this
      ->drupalGet('node/' . $project->nid);
    $this
      ->assertResponse(200, 'Access granted to user B when project is attached to organization B.');
  }

  /**
   * Tests module uninstall path.
   */
  public function testpmpermissionUninstall() {
    $module = array(
      'pmpermission',
    );
    module_disable($module);
    $result = drupal_uninstall_modules($module);
    $this
      ->AssertTrue($result, t('Module successfully uninstalled.'));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PmPermissionTestCase::getInfo public static function Returns meta data for PM Permission tests.
PmPermissionTestCase::setUp public function Standard set up for all tests.
PmPermissionTestCase::testpmpermissionBelonged public function Tests "belonged" permission.
PmPermissionTestCase::testpmpermissionChangeParent public function Tests permissions after parent change.
PmPermissionTestCase::testpmpermissionUninstall public function Tests module uninstall path.
PmPermissionTestCase::testpmpermissionViewAll public function Tests "view all" permission.