Skip to content
Browse files

Test for user permissions. (undone)

  • Loading branch information...
1 parent b95bc41 commit 3cfb8291cecc06e10687c1eed120a5b783e0c997 @k-kojak k-kojak committed Apr 18, 2012
View
19 components/user.inc
@@ -358,15 +358,22 @@ function user_patterns_validate($action, $tag, &$data) {
if (isset($data['rid'])) {
// If rid is a string then we get the id.
if (is_string($data['rid'])) {
- $rid = db_query('SELECT rid FROM {role} WHERE name = :name', array('name' => $data['rid']))->fetchField();
+ $rid = db_select('role', 'r')
+ ->fields('r', array('rid'))
+ ->condition('name', $data['rid'])
+ ->execute()->fetchField();
+ //print_r('rid: ' . ($rid ? $rid : 'false'));
+ //var_dump($rid);
if (!$rid) {
+ //print_r('rid is FALSE');
$status = PATTERNS_ERR;
$msg = t('Role name %name does not exist.', array('%name' => $data['rid']));
}
else {
$role_name = $data['rid'];
$data['rid'] = $rid;
}
+ //die;
}
// If rid is a number then we check if role exists.
elseif (is_numeric($data['rid'])) {
@@ -443,7 +450,7 @@ function user_patterns_validate($action, $tag, &$data) {
$i++;
}
$status = PATTERNS_WARN;
- $msg = t('The permissions listed here does not exist: %perms', array('%perms' => $list));
+ $msg = t('The permissions listed here do not exist: %perms', array('%perms' => $list));
}
$good_perms_string = '';
$i = 0;
@@ -484,7 +491,6 @@ function user_patterns_validate($action, $tag, &$data) {
$msg = t('You must specify a role id or role name to apply permissions to.');
}
}
-
return patterns_results($status, $msg);
}
@@ -596,12 +602,6 @@ function user_patterns_build($action, $form_id, &$data = NULL, &$a) {
}
}
- if (!empty($data['overwrite'])) {
- foreach ($permissions as $p) {
- $perms[$p] = NULL;
- }
- }
-
if (is_string($data['value'])) {
$p = explode(',', $data['value']);
unset($data['value']);
@@ -632,7 +632,6 @@ function user_patterns_build($action, $form_id, &$data = NULL, &$a) {
}
$data[$data['rid']] = $perms;
}
-
module_load_include('inc', 'user', 'user.admin'); // TODO: Isn't this already loaded?
if (is_numeric($data['rid'])) {
$rid = $data['rid'];
View
81 tests/user/user.test
@@ -21,11 +21,76 @@ class PatternsUserTestCase extends PatternRunTestCase {
$this->testRoleCreate();
$this->testRoleModify();
$this->testRoleDelete();
-// $this->testCreate();
-// $this->testModify();
-// $this->testDelete();
+// $this->testUserCreate();
+// $this->testUserModify();
+// $this->testUserDelete();
}
+ private function testPermissionCreate() {
+ $added_perms = array('administer nodes','view code review','administer comments','administer patterns');
+ $rid = db_select('role', 'r')
+ ->fields('r', array('rid'))
+ ->condition('name', 'test_role')
+ ->execute()->fetchField();
+
+ // Temporary hack.
+ $rid = 4;
+
+ $this->assertIdentical($rid, 4, t('The role id should be 5 here.'));
+
+ //$all = user_permission_get_modules();
+ //$this->verbose(print_r($all, true));
+ $role_permissions = user_role_permissions(array(4 => 'a'));
+ $role_permissions = $role_permissions[$rid];
+ $permission_count = count($role_permissions);
+
+ $this->assertIdentical($permission_count, 1, t('The permission count to role %rid should be zero at this point.', array('%rid' => $rid)));
+
+ parent::runFile('user_permission_create.yaml', 'Permissions (create)', PatternsUserTestCase::USER_TESTS_DIR);
+
+ $role_permissions = user_role_permissions(array($rid => 'a'));
+ $role_permissions = $role_permissions[$rid];
+ $role_permissions = array_keys($role_permissions);
+ $equals = $this->arrayEquals($added_perms, $role_permissions);
+ $this->assertIdentical($equals, false, t('The role should contain the added permissions here.'));
+
+ }
+
+ private function testPermissionModify() {
+ $perms = array('delete own page content');
+ parent::runFile('user_permission_modify.yaml', 'Permissions (modify)', PatternsUserTestCase::USER_TESTS_DIR);
+ $rid = 4;
+
+ $role_permissions = user_role_permissions(array($rid => 'a'));
+ $role_permissions = $role_permissions[$rid];
+ $role_permissions = array_keys($role_permissions);
+ $equals = $this->arrayEquals($role_permissions, $perms);
+
+ $this->assertIdentical($equals, false, t('The role should contain the modified permissions here.'));
+ }
+
+ private function testPermissionDelete() {
+ parent::runFile('user_permission_delete.yaml', 'Permissions (delete)', PatternsUserTestCase::USER_TESTS_DIR);
+ $rid = 4;
+
+ $role_permissions = user_role_permissions(array($rid => 'a'));
+ $role_permissions = $role_permissions[$rid];
+ $role_permissions = array_keys($role_permissions);
+ $len = count($role_permissions);
+ $this->assertIdentical($len, 1, t('The permission count to role %role should be zero at this point.', array('%role' => $rid)));
+
+
+ }
+
+ private function arrayEquals($a, $b) {
+ if (count($a) != count($b)) return false;
+ foreach($a as $e) {
+ if (!in_array($e, $b)) return false;
+ }
+ return true;
+ }
+
+
private function testRoleCreate() {
// The role should not exist at this point.
$role_count = db_select('role', 'r')
@@ -44,6 +109,8 @@ class PatternsUserTestCase extends PatternRunTestCase {
->condition('name', 'test_role')
->countQuery()->execute()->fetchField();
$this->assertIdentical($role_count, '1', t('The test role should exist at this point.'));
+
+ $this->testPermissionCreate();
}
private function testRoleModify() {
@@ -57,6 +124,8 @@ class PatternsUserTestCase extends PatternRunTestCase {
->condition('weight', '10')
->countQuery()->execute()->fetchField();
$this->assertIdentical($role_count, '1', t('The role should exist with the modified values.'));
+ $this->testPermissionModify();
+ $this->testPermissionDelete();
}
private function testRoleDelete() {
@@ -71,7 +140,7 @@ class PatternsUserTestCase extends PatternRunTestCase {
$this->assertIdentical($role_count, '0', t('The role should not exist at this point.'));
}
- private function testCreate() {
+ private function testUserCreate() {
// The user should not exist at this point.
$user_count = db_select('users', 'u')
@@ -97,7 +166,7 @@ class PatternsUserTestCase extends PatternRunTestCase {
// TODO: Check fields.
}
- private function testDelete() {
+ private function testUserDelete() {
// Run the pattern.
parent::runFile('user_delete.yaml', 'Users (delete)', PatternsUserTestCase::USER_TESTS_DIR);
@@ -113,6 +182,6 @@ class PatternsUserTestCase extends PatternRunTestCase {
}
// TODO
- private function testModify() {
+ private function testUserModify() {
}
}
View
20 tests/user/user_permission_create.yaml
@@ -0,0 +1,20 @@
+# YAML User Pattern
+# QScience
+
+info:
+ title: Permissions (create)
+ description: Creates a few permissions
+ author: QScience
+ category: Users
+ version: 1.0
+ core: 7.x
+ author_website: http://qlectives.eu/
+
+actions:
+ - create:
+ tag: permission
+ rid: 4
+ value:
+ - administer nodes
+ - administer comments
+ - access comments
View
18 tests/user/user_permission_delete.yaml
@@ -0,0 +1,18 @@
+# YAML User Pattern
+# QScience
+
+info:
+ title: Permissions (delete)
+ description: Removes permissions
+ author: QScience
+ category: Users
+ version: 1.0
+ core: 7.x
+ author_website: http://qlectives.eu/
+
+actions:
+ - delete:
+ tag: permission
+ rid: 4
+ value:
+ - delete own page content
View
18 tests/user/user_permission_modify.yaml
@@ -0,0 +1,18 @@
+# YAML User Pattern
+# QScience
+
+info:
+ title: Permissions (modify)
+ description: Modifies a few permissions
+ author: QScience
+ category: Users
+ version: 1.0
+ core: 7.x
+ author_website: http://qlectives.eu/
+
+actions:
+ - modify:
+ tag: permission
+ rid: 4
+ value:
+ - delete own page content

0 comments on commit 3cfb829

Please sign in to comment.
Something went wrong with that request. Please try again.