new test for user perms

This commit is contained in:
John Bintz 2009-10-17 12:22:13 -04:00
parent f665cbf16d
commit fa96119c3a
2 changed files with 61 additions and 38 deletions

View File

@ -55,18 +55,22 @@ class WhatDidTheySayAdmin {
return $result;
}
/**
* Initialize the object.
*/
function init() {
global $wpdb;
function _set_up_capabilities() {
// set up capabilities
$this->capabilities = array(
'submit_transcriptions' => __('Submit transcriptions to a post', 'what-did-they-say'),
'approve_transcriptions' => __('Approve transcriptions to a post', 'what-did-they-say'),
'change_languages' => __('Change the available languages', 'what-did-they-say')
);
}
/**
* Initialize the object.
*/
function init() {
global $wpdb;
$this->_set_up_capabilities();
// does this copy of wdts?!? reference new options?
$options = get_option('what-did-they-say-options');
@ -847,20 +851,8 @@ class WhatDidTheySayAdmin {
$users = $wpdb->get_results("SELECT ID, user_login from $wpdb->users ORDER BY user_login");
foreach ((array)$users as $user) {
$user_capabilities = array();
foreach (array_keys($this->capabilities) as $key) {
if (isset($info['user_capabilities'][$user->ID])) {
if (isset($info['user_capabilities'][$user->ID][$key])) {
$user_capabilities[$key] = true;
}
}
}
if (!empty($user_capabilities)) {
update_usermeta($user->ID, 'transcript_capabilities', $user_capabilities);
} else {
delete_usermeta($user->ID, 'transcript_capabilities');
$this->_update_user_perms($user->ID, $info['user_capabilities'][$user->ID]);
}
}
@ -869,6 +861,20 @@ class WhatDidTheySayAdmin {
return $updated;
}
function _update_user_perms($user_id, $perms) {
$user_capabilities = array();
foreach (array_keys($this->capabilities) as $key) {
if (isset($perms[$key])) { $user_capabilities[$key] = true; }
}
if (!empty($user_capabilities)) {
update_usermeta($user_id, 'transcript_capabilities', $user_capabilities);
} else {
delete_usermeta($user_id, 'transcript_capabilities');
}
}
/**
* Handle resettings what-did-they-say-options.
* @param array $info The part of the $_POST array for What Did They Say?!?

View File

@ -61,6 +61,23 @@ class WhatDidTheySayAdminTest extends PHPUnit_Framework_TestCase {
'change_languages' => 'reader'
), $result['capabilities']);
}
function testUpdateUserPerms() {
$admin = new WhatDidTheySayAdmin();
$admin->_set_up_capabilities();
update_usermeta(1, 'transcript_capabilities', array(
'submit_transcriptions' => true
));
$admin->_update_user_perms(1, array(
'approve_transcriptions' => 'yes'
));
$this->assertEquals(array(
'approve_transcriptions' => true
), get_usermeta(1, 'transcript_capabilities'));
}
}
?>