Commit 1fe9ba06 authored by Robin Celie's avatar Robin Celie Committed by Vladimir Roudakov
Browse files

Issue #2585033 by Rob C, a_thakur, Notament, Alex Bukach, mstrelan, awolfey,...

Issue #2585033 by Rob C, a_thakur, Notament, Alex Bukach, mstrelan, awolfey, alfaguru, spuky, VladimirAus: Alteration of validation and submit callbacks can cause conflicts
parent ebfaae27
Loading
Loading
Loading
Loading
+15 −0
Original line number Diff line number Diff line
@@ -4,6 +4,8 @@ namespace Drupal\Tests\user_registrationpassword\Functional;

use Drupal\Tests\BrowserTestBase;
use Drupal\Core\Test\AssertMailTrait;
use Drupal\user\Form\UserPasswordForm;


/**
 * Functionality tests for User registration password module privacy feature.
@@ -65,4 +67,17 @@ class UserRegistrationPasswordUserPasswordResetForm extends BrowserTestBase {
    $this->assertNotEqual($email['send'], 0);
  }

  /**
   * Implements testRegistrationFormDefaultValues().
   */
  public function testRegistrationFormDefaultValues() {
    // Load form object.
    $form_object = new UserPasswordForm(\Drupal::entityTypeManager()->getStorage('user'), \Drupal::languageManager(), \Drupal::configFactory(), \Drupal::flood());
    // Get the form array.
    $form = \Drupal::formBuilder()->getForm($form_object);

    // Test values.
    $this->assertEqual($form['#validate'][0], '_user_registrationpassword_user_pass_validate', 'Validate handler correctly changed.');
    $this->assertEqual($form['#submit'][0], '_user_registrationpassword_user_pass_submit', 'Submit handler correctly changed.');
  }
}
+0 −1
Original line number Diff line number Diff line
@@ -19,7 +19,6 @@ function user_registrationpassword_requirements($phase) {
    $incompatible_modules = [
      'logintoboggan',
      'username_enumeration_prevention',
      'sharedemail_pass_reset',
    ];
    $modules_found = [];
    // Test for incompatible modules.
+37 −2
Original line number Diff line number Diff line
@@ -225,6 +225,23 @@ function user_registrationpassword_admin_settings_submit(&$form, FormStateInterf
  $user_config->save();
}

/**
 * Implements hook_module_implements_alter().
 */
function user_registrationpassword_module_implements_alter(&$implementations, $hook) {
  // Make (almost) sure our hook is called last. The only thing that can run
  // after us (on purpose) is a module where it's weight thats larger then ours.
  // This offers a way for developers to override this, but opens the door for
  // user_registrationpassword to do the final heavy lifting (and in the future
  // maybe implement some hooks on top of this).
  if ($hook == 'form_alter' && isset($implementations['user_registrationpassword'])) {
    $group = $implementations['user_registrationpassword'];
    unset($implementations['user_registrationpassword']);
    $implementations['user_registrationpassword'] = $group;
  }
}


/**
 * Implements hook_form_FORM_ID_alter().
 *
@@ -428,8 +445,26 @@ function user_registrationpassword_set_message($type = 'welcome', $redirect = ''
 * Implements hook_form_FORM_ID_alter().
 */
function user_registrationpassword_form_user_pass_alter(&$form, FormStateInterface $form_state, $form_id) {
  $form['#validate'][0] = '_user_registrationpassword_user_pass_validate';
  $form['#submit'][0] = '_user_registrationpassword_user_pass_submit';

  // Define core handlers.
  $replacement_validate_key = '::validateForm';
  $replacement_submit_key = '::submitForm';

  // Replace the validate handler.
  $key_validate = array_search($replacement_validate_key, $form['#validate']);
  // If the handler is found.
  if ($key_validate !== FALSE) {
    // Replace it with ours.
    $form['#validate'][$key_validate] = '_user_registrationpassword_user_pass_validate';
  }

  // Replace the submit handler.
  $key_submit = array_search($replacement_submit_key, $form['#submit']);
  // If the handler is found.
  if ($key_submit !== FALSE) {
    // Replace it with ours.
    $form['#submit'][$key_submit] = '_user_registrationpassword_user_pass_submit';
  }
}

/**