You are here

administerusersbyrole.test in Administer Users by Role 6

Same filename and directory in other branches
  1. 7.2 administerusersbyrole.test
  2. 7 administerusersbyrole.test

Test Administer Users by Role.

File

administerusersbyrole.test
View source
<?php

/**
 * @file
 * Test Administer Users by Role.
 */
class AdministerUsersByRoleTestCase extends DrupalWebTestCase {
  public static function getInfo() {
    return array(
      'name' => 'administerusersbyrole',
      'description' => 'Ensure that Administer Users by Role functions properly.',
      'group' => 'Administer Users by Role',
    );
  }
  private $roles = array();
  private $users = array();
  private function createUserWithRole($userName, $roleNames) {
    $roleIDs = array();
    foreach ($roleNames as $roleName) {
      $roleIDs[$this->roles[$roleName]] = $this->roles[$roleName];
    }
    $userInfo = array(
      'name' => $userName,
      'mail' => "{$userName}@example.com",
      'pass' => 'cheese',
      'roles' => $roleIDs,
      'status' => 1,
    );
    $this->users[$userName] = user_save(NULL, $userInfo);
    $this->users[$userName]->pass_raw = 'cheese';
    $this
      ->assertTrue($this->users[$userName]->uid > 0, "Unable to create user {$userName}.");
  }
  private function createRolesAndUsers($roleName, $allowEditorToDelete) {

    // create basic role
    $this->roles[$roleName] = $this
      ->drupalCreateRole(array(
      'access content',
    ), $roleName);
    $this
      ->createUserWithRole($roleName, array(
      $roleName,
    ));

    // clear permissions cache, so we can use the 'edit users with ...' permission for this role
    $this
      ->checkPermissions(array(), TRUE);

    // create role to edit above role
    $perms = array(
      'access administration pages',
      'access content',
      'administer users',
      _administerusersbyrole_build_perm_string($roleName, 'edit', FALSE),
    );
    if ($allowEditorToDelete) {
      $perms[] = _administerusersbyrole_build_perm_string($roleName, 'delete', FALSE);
    }
    $this->roles["{$roleName}_editor"] = $this
      ->drupalCreateRole($perms, "{$roleName}_editor");
    $this
      ->createUserWithRole("{$roleName}_editor", array(
      "{$roleName}_editor",
    ));
  }
  public function setUp() {
    parent::setUp('administerusersbyrole');

    // make sure our immediate dependencies are enabled, since parent::setUp() doesn't check this for us
    $this
      ->assertTrue(module_exists('administerusersbyrole'), "administerusersbyrole.module isn't installed");
    $this
      ->createUserWithRole('noroles', array());
    $this
      ->createRolesAndUsers('alpha', FALSE);

    // 'alpha and other' editor
    $perms = array(
      'access administration pages',
      'access content',
      'administer users',
      _administerusersbyrole_build_perm_string('alpha', 'edit', TRUE),
    );
    $this
      ->checkPermissions(array(), TRUE);
    $this->roles['alpha_o_ed'] = $this
      ->drupalCreateRole($perms, 'alpha_o_ed');
    $this
      ->createUserWithRole('alpha_o_ed', array(
      'alpha_o_ed',
    ));

    // 'alpha and other' deletor
    $perms = array(
      'access administration pages',
      'access content',
      'administer users',
      _administerusersbyrole_build_perm_string('alpha', 'delete', TRUE),
    );
    $this
      ->checkPermissions(array(), TRUE);
    $this->roles['alpha_o_del'] = $this
      ->drupalCreateRole($perms, 'alpha_o_del');
    $this
      ->createUserWithRole('alpha_o_del', array(
      'alpha_o_del',
    ));
    $this
      ->createRolesAndUsers('beta', TRUE);

    // 'beta and other' editor
    $perms = array(
      'access administration pages',
      'access content',
      'administer users',
      _administerusersbyrole_build_perm_string('beta', 'edit', TRUE),
    );
    $this
      ->checkPermissions(array(), TRUE);
    $this->roles['beta_o_ed'] = $this
      ->drupalCreateRole($perms, 'beta_o_ed');
    $this
      ->createUserWithRole('beta_o_ed', array(
      'beta_o_ed',
    ));
    $this
      ->createUserWithRole('alphabeta', array(
      'alpha',
      'beta',
    ));

    // all_editor
    $perms = array(
      'access administration pages',
      'access content',
      'administer users',
      'edit users with no custom roles',
    );
    foreach ($this->roles as $roleName => $roleID) {
      $perms[] = _administerusersbyrole_build_perm_string($roleName, 'edit', FALSE);
    }
    $this
      ->checkPermissions(array(), TRUE);
    $this->roles['all_editor'] = $this
      ->drupalCreateRole($perms, 'all_editor');
    $this
      ->createUserWithRole('all_editor', array(
      'all_editor',
    ));

    // all_deletor
    $perms = array(
      'access administration pages',
      'access content',
      'administer users',
      'delete users with no custom roles',
    );
    foreach ($this->roles as $roleName => $roleID) {
      $perms[] = _administerusersbyrole_build_perm_string($roleName, 'delete', FALSE);
    }
    $this
      ->checkPermissions(array(), TRUE);
    $this->roles['all_deletor'] = $this
      ->drupalCreateRole($perms, 'all_deletor');
    $this
      ->createUserWithRole('all_deletor', array(
      'all_deletor',
    ));

    // creator
    $perms = array(
      'access administration pages',
      'access content',
      'administer users',
      'create users',
    );
    $this
      ->checkPermissions(array(), TRUE);
    $this->roles['creator'] = $this
      ->drupalCreateRole($perms, 'creator');
    $this
      ->createUserWithRole('creator', array(
      'creator',
    ));
  }
  public function testPermissions() {
    $expectations = array(
      // When I'm logged in as...
      'nobody' => array(
        // ...I can perform these actions on this other user...
        'noroles' => array(
          'edit' => FALSE,
          'delete' => FALSE,
        ),
        'alpha' => array(
          'edit' => FALSE,
          'delete' => FALSE,
        ),
        'alpha_editor' => array(
          'edit' => FALSE,
          'delete' => FALSE,
        ),
        'alpha_o_ed' => array(
          'edit' => FALSE,
          'delete' => FALSE,
        ),
        'alpha_o_del' => array(
          'edit' => FALSE,
          'delete' => FALSE,
        ),
        'beta' => array(
          'edit' => FALSE,
          'delete' => FALSE,
        ),
        'beta_editor' => array(
          'edit' => FALSE,
          'delete' => FALSE,
        ),
        'beta_o_ed' => array(
          'edit' => FALSE,
          'delete' => FALSE,
        ),
        'alphabeta' => array(
          'edit' => FALSE,
          'delete' => FALSE,
        ),
        'creator' => array(
          'edit' => FALSE,
          'delete' => FALSE,
        ),
        'all_editor' => array(
          'edit' => FALSE,
          'delete' => FALSE,
        ),
        'all_deletor' => array(
          'edit' => FALSE,
          'delete' => FALSE,
        ),
        'create users' => FALSE,
      ),
      'noroles' => array(
        'noroles' => array(
          'edit' => TRUE,
          'delete' => FALSE,
        ),
        'alpha' => array(
          'edit' => FALSE,
          'delete' => FALSE,
        ),
        'alpha_editor' => array(
          'edit' => FALSE,
          'delete' => FALSE,
        ),
        'alpha_o_ed' => array(
          'edit' => FALSE,
          'delete' => FALSE,
        ),
        'alpha_o_del' => array(
          'edit' => FALSE,
          'delete' => FALSE,
        ),
        'beta' => array(
          'edit' => FALSE,
          'delete' => FALSE,
        ),
        'beta_editor' => array(
          'edit' => FALSE,
          'delete' => FALSE,
        ),
        'beta_o_ed' => array(
          'edit' => FALSE,
          'delete' => FALSE,
        ),
        'alphabeta' => array(
          'edit' => FALSE,
          'delete' => FALSE,
        ),
        'creator' => array(
          'edit' => FALSE,
          'delete' => FALSE,
        ),
        'all_editor' => array(
          'edit' => FALSE,
          'delete' => FALSE,
        ),
        'all_deletor' => array(
          'edit' => FALSE,
          'delete' => FALSE,
        ),
        'create users' => FALSE,
      ),
      'alpha' => array(
        'noroles' => array(
          'edit' => FALSE,
          'delete' => FALSE,
        ),
        'alpha' => array(
          'edit' => TRUE,
          'delete' => FALSE,
        ),
        'alpha_editor' => array(
          'edit' => FALSE,
          'delete' => FALSE,
        ),
        'alpha_o_ed' => array(
          'edit' => FALSE,
          'delete' => FALSE,
        ),
        'alpha_o_del' => array(
          'edit' => FALSE,
          'delete' => FALSE,
        ),
        'beta' => array(
          'edit' => FALSE,
          'delete' => FALSE,
        ),
        'beta_editor' => array(
          'edit' => FALSE,
          'delete' => FALSE,
        ),
        'beta_o_ed' => array(
          'edit' => FALSE,
          'delete' => FALSE,
        ),
        'alphabeta' => array(
          'edit' => FALSE,
          'delete' => FALSE,
        ),
        'creator' => array(
          'edit' => FALSE,
          'delete' => FALSE,
        ),
        'all_editor' => array(
          'edit' => FALSE,
          'delete' => FALSE,
        ),
        'all_deletor' => array(
          'edit' => FALSE,
          'delete' => FALSE,
        ),
        'create users' => FALSE,
      ),
      'alpha_editor' => array(
        'noroles' => array(
          'edit' => FALSE,
          'delete' => FALSE,
        ),
        'alpha' => array(
          'edit' => TRUE,
          'delete' => FALSE,
        ),
        'alpha_editor' => array(
          'edit' => TRUE,
          'delete' => FALSE,
        ),
        'alpha_o_ed' => array(
          'edit' => FALSE,
          'delete' => FALSE,
        ),
        'alpha_o_del' => array(
          'edit' => FALSE,
          'delete' => FALSE,
        ),
        'beta' => array(
          'edit' => FALSE,
          'delete' => FALSE,
        ),
        'beta_editor' => array(
          'edit' => FALSE,
          'delete' => FALSE,
        ),
        'beta_o_ed' => array(
          'edit' => FALSE,
          'delete' => FALSE,
        ),
        'alphabeta' => array(
          'edit' => FALSE,
          'delete' => FALSE,
        ),
        'creator' => array(
          'edit' => FALSE,
          'delete' => FALSE,
        ),
        'all_editor' => array(
          'edit' => FALSE,
          'delete' => FALSE,
        ),
        'all_deletor' => array(
          'edit' => FALSE,
          'delete' => FALSE,
        ),
        'create users' => FALSE,
      ),
      'alpha_o_ed' => array(
        'noroles' => array(
          'edit' => FALSE,
          'delete' => FALSE,
        ),
        'alpha' => array(
          'edit' => TRUE,
          'delete' => FALSE,
        ),
        'alpha_editor' => array(
          'edit' => FALSE,
          'delete' => FALSE,
        ),
        'alpha_o_ed' => array(
          'edit' => TRUE,
          'delete' => FALSE,
        ),
        'alpha_o_del' => array(
          'edit' => FALSE,
          'delete' => FALSE,
        ),
        'beta' => array(
          'edit' => FALSE,
          'delete' => FALSE,
        ),
        'beta_editor' => array(
          'edit' => FALSE,
          'delete' => FALSE,
        ),
        'beta_o_ed' => array(
          'edit' => FALSE,
          'delete' => FALSE,
        ),
        'alphabeta' => array(
          'edit' => TRUE,
          'delete' => FALSE,
        ),
        'creator' => array(
          'edit' => FALSE,
          'delete' => FALSE,
        ),
        'all_editor' => array(
          'edit' => FALSE,
          'delete' => FALSE,
        ),
        'all_deletor' => array(
          'edit' => FALSE,
          'delete' => FALSE,
        ),
        'create users' => FALSE,
      ),
      'alpha_o_del' => array(
        'noroles' => array(
          'edit' => FALSE,
          'delete' => FALSE,
        ),
        'alpha' => array(
          'edit' => FALSE,
          'delete' => TRUE,
        ),
        'alpha_editor' => array(
          'edit' => FALSE,
          'delete' => FALSE,
        ),
        'alpha_o_ed' => array(
          'edit' => FALSE,
          'delete' => FALSE,
        ),
        'alpha_o_del' => array(
          'edit' => TRUE,
          'delete' => FALSE,
        ),
        'beta' => array(
          'edit' => FALSE,
          'delete' => FALSE,
        ),
        'beta_editor' => array(
          'edit' => FALSE,
          'delete' => FALSE,
        ),
        'beta_o_ed' => array(
          'edit' => FALSE,
          'delete' => FALSE,
        ),
        'alphabeta' => array(
          'edit' => FALSE,
          'delete' => TRUE,
        ),
        'creator' => array(
          'edit' => FALSE,
          'delete' => FALSE,
        ),
        'all_editor' => array(
          'edit' => FALSE,
          'delete' => FALSE,
        ),
        'all_deletor' => array(
          'edit' => FALSE,
          'delete' => FALSE,
        ),
        'create users' => FALSE,
      ),
      'beta' => array(
        'noroles' => array(
          'edit' => FALSE,
          'delete' => FALSE,
        ),
        'alpha' => array(
          'edit' => FALSE,
          'delete' => FALSE,
        ),
        'alpha_editor' => array(
          'edit' => FALSE,
          'delete' => FALSE,
        ),
        'alpha_o_ed' => array(
          'edit' => FALSE,
          'delete' => FALSE,
        ),
        'alpha_o_del' => array(
          'edit' => FALSE,
          'delete' => FALSE,
        ),
        'beta' => array(
          'edit' => TRUE,
          'delete' => FALSE,
        ),
        'beta_editor' => array(
          'edit' => FALSE,
          'delete' => FALSE,
        ),
        'beta_o_ed' => array(
          'edit' => FALSE,
          'delete' => FALSE,
        ),
        'alphabeta' => array(
          'edit' => FALSE,
          'delete' => FALSE,
        ),
        'creator' => array(
          'edit' => FALSE,
          'delete' => FALSE,
        ),
        'all_editor' => array(
          'edit' => FALSE,
          'delete' => FALSE,
        ),
        'all_deletor' => array(
          'edit' => FALSE,
          'delete' => FALSE,
        ),
        'create users' => FALSE,
      ),
      'beta_editor' => array(
        'noroles' => array(
          'edit' => FALSE,
          'delete' => FALSE,
        ),
        'alpha' => array(
          'edit' => FALSE,
          'delete' => FALSE,
        ),
        'alpha_editor' => array(
          'edit' => FALSE,
          'delete' => FALSE,
        ),
        'alpha_o_ed' => array(
          'edit' => FALSE,
          'delete' => FALSE,
        ),
        'alpha_o_del' => array(
          'edit' => FALSE,
          'delete' => FALSE,
        ),
        'beta' => array(
          'edit' => TRUE,
          'delete' => TRUE,
        ),
        'beta_editor' => array(
          'edit' => TRUE,
          'delete' => FALSE,
        ),
        'beta_o_ed' => array(
          'edit' => FALSE,
          'delete' => FALSE,
        ),
        'alphabeta' => array(
          'edit' => FALSE,
          'delete' => FALSE,
        ),
        'creator' => array(
          'edit' => FALSE,
          'delete' => FALSE,
        ),
        'all_editor' => array(
          'edit' => FALSE,
          'delete' => FALSE,
        ),
        'all_deletor' => array(
          'edit' => FALSE,
          'delete' => FALSE,
        ),
        'create users' => FALSE,
      ),
      'beta_o_ed' => array(
        'noroles' => array(
          'edit' => FALSE,
          'delete' => FALSE,
        ),
        'alpha' => array(
          'edit' => FALSE,
          'delete' => FALSE,
        ),
        'alpha_editor' => array(
          'edit' => FALSE,
          'delete' => FALSE,
        ),
        'alpha_o_ed' => array(
          'edit' => FALSE,
          'delete' => FALSE,
        ),
        'alpha_o_del' => array(
          'edit' => FALSE,
          'delete' => FALSE,
        ),
        'beta' => array(
          'edit' => TRUE,
          'delete' => FALSE,
        ),
        'beta_editor' => array(
          'edit' => FALSE,
          'delete' => FALSE,
        ),
        'beta_o_ed' => array(
          'edit' => TRUE,
          'delete' => FALSE,
        ),
        'alphabeta' => array(
          'edit' => TRUE,
          'delete' => FALSE,
        ),
        'creator' => array(
          'edit' => FALSE,
          'delete' => FALSE,
        ),
        'all_editor' => array(
          'edit' => FALSE,
          'delete' => FALSE,
        ),
        'all_deletor' => array(
          'edit' => FALSE,
          'delete' => FALSE,
        ),
        'create users' => FALSE,
      ),
      'alphabeta' => array(
        'noroles' => array(
          'edit' => FALSE,
          'delete' => FALSE,
        ),
        'alpha' => array(
          'edit' => FALSE,
          'delete' => FALSE,
        ),
        'alpha_editor' => array(
          'edit' => FALSE,
          'delete' => FALSE,
        ),
        'alpha_o_ed' => array(
          'edit' => FALSE,
          'delete' => FALSE,
        ),
        'alpha_o_del' => array(
          'edit' => FALSE,
          'delete' => FALSE,
        ),
        'beta' => array(
          'edit' => FALSE,
          'delete' => FALSE,
        ),
        'beta_editor' => array(
          'edit' => FALSE,
          'delete' => FALSE,
        ),
        'beta_o_ed' => array(
          'edit' => FALSE,
          'delete' => FALSE,
        ),
        'alphabeta' => array(
          'edit' => TRUE,
          'delete' => FALSE,
        ),
        'creator' => array(
          'edit' => FALSE,
          'delete' => FALSE,
        ),
        'all_editor' => array(
          'edit' => FALSE,
          'delete' => FALSE,
        ),
        'all_deletor' => array(
          'edit' => FALSE,
          'delete' => FALSE,
        ),
        'create users' => FALSE,
      ),
      'all_editor' => array(
        'noroles' => array(
          'edit' => TRUE,
          'delete' => FALSE,
        ),
        'alpha' => array(
          'edit' => TRUE,
          'delete' => FALSE,
        ),
        'alpha_editor' => array(
          'edit' => TRUE,
          'delete' => FALSE,
        ),
        'alpha_o_ed' => array(
          'edit' => TRUE,
          'delete' => FALSE,
        ),
        'alpha_o_del' => array(
          'edit' => TRUE,
          'delete' => FALSE,
        ),
        'beta' => array(
          'edit' => TRUE,
          'delete' => FALSE,
        ),
        'beta_editor' => array(
          'edit' => TRUE,
          'delete' => FALSE,
        ),
        'beta_o_ed' => array(
          'edit' => TRUE,
          'delete' => FALSE,
        ),
        'alphabeta' => array(
          'edit' => TRUE,
          'delete' => FALSE,
        ),
        'creator' => array(
          'edit' => FALSE,
          'delete' => FALSE,
        ),
        'all_editor' => array(
          'edit' => TRUE,
          'delete' => FALSE,
        ),
        'all_deletor' => array(
          'edit' => FALSE,
          'delete' => FALSE,
        ),
        'create users' => FALSE,
      ),
      'all_deletor' => array(
        'noroles' => array(
          'edit' => FALSE,
          'delete' => TRUE,
        ),
        'alpha' => array(
          'edit' => FALSE,
          'delete' => TRUE,
        ),
        'alpha_editor' => array(
          'edit' => FALSE,
          'delete' => TRUE,
        ),
        'alpha_o_ed' => array(
          'edit' => FALSE,
          'delete' => TRUE,
        ),
        'alpha_o_del' => array(
          'edit' => FALSE,
          'delete' => TRUE,
        ),
        'beta' => array(
          'edit' => FALSE,
          'delete' => TRUE,
        ),
        'beta_editor' => array(
          'edit' => FALSE,
          'delete' => TRUE,
        ),
        'beta_o_ed' => array(
          'edit' => FALSE,
          'delete' => TRUE,
        ),
        'alphabeta' => array(
          'edit' => FALSE,
          'delete' => TRUE,
        ),
        'creator' => array(
          'edit' => FALSE,
          'delete' => FALSE,
        ),
        'all_editor' => array(
          'edit' => FALSE,
          'delete' => TRUE,
        ),
        'all_deletor' => array(
          'edit' => TRUE,
          'delete' => FALSE,
        ),
        'create users' => FALSE,
      ),
      'creator' => array(
        'noroles' => array(
          'edit' => FALSE,
          'delete' => FALSE,
        ),
        'alpha' => array(
          'edit' => FALSE,
          'delete' => FALSE,
        ),
        'alpha_editor' => array(
          'edit' => FALSE,
          'delete' => FALSE,
        ),
        'alpha_o_ed' => array(
          'edit' => FALSE,
          'delete' => FALSE,
        ),
        'alpha_o_del' => array(
          'edit' => FALSE,
          'delete' => FALSE,
        ),
        'beta' => array(
          'edit' => FALSE,
          'delete' => FALSE,
        ),
        'beta_editor' => array(
          'edit' => FALSE,
          'delete' => FALSE,
        ),
        'beta_o_ed' => array(
          'edit' => FALSE,
          'delete' => FALSE,
        ),
        'alphabeta' => array(
          'edit' => FALSE,
          'delete' => FALSE,
        ),
        'creator' => array(
          'edit' => TRUE,
          'delete' => FALSE,
        ),
        'all_editor' => array(
          'edit' => FALSE,
          'delete' => FALSE,
        ),
        'all_deletor' => array(
          'edit' => FALSE,
          'delete' => FALSE,
        ),
        'create users' => TRUE,
      ),
    );
    foreach ($expectations as $loginUsername => $editUsernames) {
      if ($loginUsername !== 'nobody') {
        $this
          ->drupalLogin($this->users[$loginUsername]);
      }
      foreach ($editUsernames as $k => $v) {
        if ($k === 'create users') {
          $this
            ->drupalGet("admin/user/user/create");
          $expectedResult = $v;
          if ($expectedResult) {
            $this
              ->assertRaw('This web page allows administrators to register new users.', "My expectation is that {$loginUsername} should be able to create users, but it can't.");
          }
          else {
            $this
              ->assertRaw('You do not have permission to create users.', "My expectation is that {$loginUsername} shouldn't be able to create users, but it can.");
          }
        }
        else {
          $editUsername = $k;
          $operations = $v;
          $editUid = $this->users[$editUsername]->uid;
          foreach ($operations as $operation => $expectedResult) {
            $this
              ->drupalGet("user/{$editUid}/{$operation}");
            if ($expectedResult) {
              if ($operation === 'edit') {
                $this
                  ->assertRaw("All e-mails from the system will be sent to this address.", "My expectation is that {$loginUsername} should be able to {$operation} {$editUsername}, but it can't.");
              }
              elseif ($operation === 'delete') {
                $this
                  ->assertRaw("Are you sure you want to delete the account <em>{$editUsername}</em>?", "My expectation is that {$loginUsername} should be able to {$operation} {$editUsername}, but it can't.");
              }
            }
            else {
              $this
                ->assertTrue(strstr($this
                ->drupalGetContent(), "You do not have permission to {$operation} <em>{$editUsername}</em>.") || strstr($this
                ->drupalGetContent(), 'You are not authorized to access this page.'), "My expectation is that {$loginUsername} shouldn't be able to {$operation} {$editUsername}, but it can.");
            }
          }
        }
      }
      if ($loginUsername !== 'nobody') {
        $this
          ->drupalLogout();
      }
    }
  }

}

Classes

Namesort descending Description
AdministerUsersByRoleTestCase @file Test Administer Users by Role.