Commit e4bd0c45 authored by webchick's avatar webchick

Issue #1848874 by sun: Clean up and simplify user cancel methods and processing.

parent 26902ff1
...@@ -14,15 +14,6 @@ ...@@ -14,15 +14,6 @@
*/ */
class UserCancelTest extends WebTestBase { class UserCancelTest extends WebTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('comment');
protected $profile = 'standard';
public static function getInfo() { public static function getInfo() {
return array( return array(
'name' => 'Cancel account', 'name' => 'Cancel account',
...@@ -31,6 +22,12 @@ public static function getInfo() { ...@@ -31,6 +22,12 @@ public static function getInfo() {
); );
} }
function setUp() {
parent::setUp();
$this->drupalCreateContentType(array('type' => 'page', 'name' => 'Basic page'));
}
/** /**
* Attempt to cancel account without permission. * Attempt to cancel account without permission.
*/ */
...@@ -277,6 +274,8 @@ function testUserAnonymize() { ...@@ -277,6 +274,8 @@ function testUserAnonymize() {
*/ */
function testUserDelete() { function testUserDelete() {
config('user.settings')->set('cancel_method', 'user_cancel_delete')->save(); config('user.settings')->set('cancel_method', 'user_cancel_delete')->save();
module_enable(array('comment'));
$this->resetAll();
// Create a user. // Create a user.
$account = $this->drupalCreateUser(array('cancel account', 'post comments', 'skip comment approval')); $account = $this->drupalCreateUser(array('cancel account', 'post comments', 'skip comment approval'));
......
...@@ -346,23 +346,20 @@ function user_admin_settings($form, &$form_state) { ...@@ -346,23 +346,20 @@ function user_admin_settings($form, &$form_state) {
'#default_value' => $config->get('verify_mail'), '#default_value' => $config->get('verify_mail'),
'#description' => t('New users will be required to validate their e-mail address prior to logging into the site, and will be assigned a system-generated password. With this setting disabled, users will be logged in immediately upon registering, and may select their own passwords during registration.') '#description' => t('New users will be required to validate their e-mail address prior to logging into the site, and will be assigned a system-generated password. With this setting disabled, users will be logged in immediately upon registering, and may select their own passwords during registration.')
); );
module_load_include('inc', 'user', 'user.pages'); form_load_include($form_state, 'inc', 'user', 'user.pages');
$form['registration_cancellation']['user_cancel_method'] = array( $form['registration_cancellation']['user_cancel_method'] = array(
'#type' => 'item', '#type' => 'radios',
'#title' => t('When cancelling a user account'), '#title' => t('When cancelling a user account'),
'#default_value' => $config->get('cancel_method'), '#default_value' => $config->get('cancel_method'),
'#description' => t('Users with the %select-cancel-method or %administer-users <a href="@permissions-url">permissions</a> can override this default method.', array('%select-cancel-method' => t('Select method for cancelling account'), '%administer-users' => t('Administer users'), '@permissions-url' => url('admin/people/permissions'))), '#description' => t('Users with the %select-cancel-method or %administer-users <a href="@permissions-url">permissions</a> can override this default method.', array('%select-cancel-method' => t('Select method for cancelling account'), '%administer-users' => t('Administer users'), '@permissions-url' => url('admin/people/permissions'))),
); );
$form['registration_cancellation']['user_cancel_method'] += user_cancel_methods(); $form['registration_cancellation']['user_cancel_method'] += user_cancel_methods();
foreach (element_children($form['registration_cancellation']['user_cancel_method']) as $element) { foreach (element_children($form['registration_cancellation']['user_cancel_method']) as $key) {
// Remove all account cancellation methods that have #access defined, as // All account cancellation methods that specify #access cannot be
// those cannot be configured as default method. // configured as default method.
if (isset($form['registration_cancellation']['user_cancel_method'][$element]['#access'])) { // @see hook_user_cancel_methods_alter()
$form['registration_cancellation']['user_cancel_method'][$element]['#access'] = FALSE; if (isset($form['registration_cancellation']['user_cancel_method'][$key]['#access'])) {
} $form['registration_cancellation']['user_cancel_method'][$key]['#access'] = FALSE;
// Remove the description (only displayed on the confirmation form).
else {
unset($form['registration_cancellation']['user_cancel_method'][$element]['#description']);
} }
} }
......
...@@ -143,8 +143,8 @@ function hook_user_cancel($edit, $account, $method) { ...@@ -143,8 +143,8 @@ function hook_user_cancel($edit, $account, $method) {
* description is NOT used for the radio button, but instead should provide * description is NOT used for the radio button, but instead should provide
* additional explanation to the user seeking to cancel their account. * additional explanation to the user seeking to cancel their account.
* - access: (optional) A boolean value indicating whether the user can access * - access: (optional) A boolean value indicating whether the user can access
* a method. If #access is defined, the method cannot be configured as default * a method. If 'access' is defined, the method cannot be configured as
* method. * default method.
* *
* @param $methods * @param $methods
* An array containing user account cancellation methods, keyed by method id. * An array containing user account cancellation methods, keyed by method id.
......
...@@ -2475,16 +2475,12 @@ function user_multiple_cancel_confirm($form, &$form_state) { ...@@ -2475,16 +2475,12 @@ function user_multiple_cancel_confirm($form, &$form_state) {
$form['operation'] = array('#type' => 'hidden', '#value' => 'cancel'); $form['operation'] = array('#type' => 'hidden', '#value' => 'cancel');
module_load_include('inc', 'user', 'user.pages'); form_load_include($form_state, 'inc', 'user', 'user.pages');
$form['user_cancel_method'] = array( $form['user_cancel_method'] = array(
'#type' => 'item', '#type' => 'radios',
'#title' => t('When cancelling these accounts'), '#title' => t('When cancelling these accounts'),
); );
$form['user_cancel_method'] += user_cancel_methods(); $form['user_cancel_method'] += user_cancel_methods();
// Remove method descriptions.
foreach (element_children($form['user_cancel_method']) as $element) {
unset($form['user_cancel_method'][$element]['#description']);
}
// Allow to send the account cancellation confirmation mail. // Allow to send the account cancellation confirmation mail.
$form['user_cancel_confirm'] = array( $form['user_cancel_confirm'] = array(
......
...@@ -232,7 +232,7 @@ function user_cancel_confirm_form($form, &$form_state, $account) { ...@@ -232,7 +232,7 @@ function user_cancel_confirm_form($form, &$form_state, $account) {
$admin_access = user_access('administer users'); $admin_access = user_access('administer users');
$can_select_method = $admin_access || user_access('select account cancellation method'); $can_select_method = $admin_access || user_access('select account cancellation method');
$form['user_cancel_method'] = array( $form['user_cancel_method'] = array(
'#type' => 'item', '#type' => 'radios',
'#title' => ($account->uid == $user->uid ? t('When cancelling your account') : t('When cancelling the account')), '#title' => ($account->uid == $user->uid ? t('When cancelling your account') : t('When cancelling the account')),
'#access' => $can_select_method, '#access' => $can_select_method,
); );
...@@ -266,22 +266,15 @@ function user_cancel_confirm_form($form, &$form_state, $account) { ...@@ -266,22 +266,15 @@ function user_cancel_confirm_form($form, &$form_state, $account) {
else { else {
$question = t('Are you sure you want to cancel the account %name?', array('%name' => $account->name)); $question = t('Are you sure you want to cancel the account %name?', array('%name' => $account->name));
} }
$description = ''; $default_method = config('user.settings')->get('cancel_method');
$description = NULL;
if ($can_select_method) { if ($can_select_method) {
$description = t('Select the method to cancel the account above.'); $description = t('Select the method to cancel the account above.');
foreach (element_children($form['user_cancel_method']) as $element) {
unset($form['user_cancel_method'][$element]['#description']);
}
} }
else { // Options supplied via user_cancel_methods() can have a custom
// The radio button #description is used as description for the confirmation // #confirm_description property for the confirmation form description.
// form. elseif (isset($form['user_cancel_method'][$default_method]['#confirm_description'])) {
foreach (element_children($form['user_cancel_method']) as $element) { $description = $form['user_cancel_method'][$default_method]['#confirm_description'];
if ($form['user_cancel_method'][$element]['#default_value'] == $form['user_cancel_method'][$element]['#return_value']) {
$description = $form['user_cancel_method'][$element]['#description'];
}
unset($form['user_cancel_method'][$element]['#description']);
}
} }
// Always provide entity id in the same form key as in the entity edit form. // Always provide entity id in the same form key as in the entity edit form.
...@@ -363,15 +356,22 @@ function user_cancel_methods() { ...@@ -363,15 +356,22 @@ function user_cancel_methods() {
drupal_alter('user_cancel_methods', $methods); drupal_alter('user_cancel_methods', $methods);
// Turn all methods into real form elements. // Turn all methods into real form elements.
$default_method = config('user.settings')->get('cancel_method');
$form = array(
'#options' => array(),
'#default_value' => $default_method,
);
foreach ($methods as $name => $method) { foreach ($methods as $name => $method) {
$form[$name] = array( $form['#options'][$name] = $method['title'];
'#type' => 'radio', // Add the description for the confirmation form. This description is never
'#title' => $method['title'], // shown for the cancel method option, only on the confirmation form.
'#description' => (isset($method['description']) ? $method['description'] : NULL), // Therefore, we use a custom #confirm_description property.
'#return_value' => $name, if (isset($method['description'])) {
'#default_value' => config('user.settings')->get('cancel_method'), $form[$name]['#confirm_description'] = $method['description'];
'#parents' => array('user_cancel_method'), }
); if (isset($method['access'])) {
$form[$name]['#access'] = $method['access'];
}
} }
return $form; return $form;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment