user_form_test.module 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. /**
  3. * @file
  4. * Dummy module implementing a form to test user password validation
  5. */
  6. /**
  7. * Implements hook_menu().
  8. *
  9. * Sets up a form that allows a user to validate password.
  10. */
  11. function user_form_test_menu() {
  12. $items = array();
  13. $items['user_form_test_current_password/%user'] = array(
  14. 'title' => 'User form test for current password validation',
  15. 'page callback' => 'drupal_get_form',
  16. 'page arguments' => array('user_form_test_current_password',1),
  17. 'access arguments' => array('administer users'),
  18. 'type' => MENU_SUGGESTED_ITEM,
  19. );
  20. return $items;
  21. }
  22. /**
  23. * A test form for user_validate_current_pass().
  24. */
  25. function user_form_test_current_password($form, &$form_state, $account) {
  26. $account->user_form_test_field = '';
  27. $form['#user'] = $account;
  28. $form['user_form_test_field'] = array(
  29. '#type' => 'textfield',
  30. '#title' => t('Test field'),
  31. '#description' => t('A field that would require a correct password to change.'),
  32. '#required' => TRUE,
  33. );
  34. $form['current_pass'] = array(
  35. '#type' => 'password',
  36. '#title' => t('Current password'),
  37. '#size' => 25,
  38. '#description' => t('Enter your current password'),
  39. );
  40. $form['current_pass_required_values'] = array(
  41. '#type' => 'value',
  42. '#value' => array('user_form_test_field' => t('Test field')),
  43. );
  44. $form['#validate'][] = 'user_validate_current_pass';
  45. $form['submit'] = array(
  46. '#type' => 'submit',
  47. '#value' => t('Test'),
  48. );
  49. return $form;
  50. }
  51. /**
  52. * Submit function for the test form for user_validate_current_pass().
  53. */
  54. function user_form_test_current_password_submit($form, &$form_state) {
  55. drupal_set_message(t('The password has been validated and the form submitted successfully.'));
  56. }
  57. /**
  58. * Implements hook_form_FORM_ID_alter().
  59. */
  60. function user_form_test_form_user_profile_form_alter(&$form, &$form_state) {
  61. if (variable_get('user_form_test_user_profile_form_rebuild', FALSE)) {
  62. $form['#submit'][] = 'user_form_test_user_account_submit';
  63. }
  64. }
  65. /**
  66. * Submit function for user_profile_form().
  67. */
  68. function user_form_test_user_account_submit($form, &$form_state) {
  69. // Rebuild the form instead of letting the process end. This allows us to
  70. // test for bugs that can be triggered in contributed modules.
  71. $form_state['rebuild'] = TRUE;
  72. }
  73. /**
  74. * Implements hook_form_FORM_ID_alter().
  75. */
  76. function user_form_test_form_user_pass_reset_alter(&$form, &$form_state) {
  77. // An unaltered form has no form values; the uid/timestmap/"confirm" are in
  78. // the URL arguments. (If for some reason a form_alter method needs the
  79. // hash, it can be retrieved from $form['#action'].)
  80. if (!is_numeric(arg(2)) || !is_numeric(arg(3)) || !is_string(arg(4)) || arg(4) !== 'confirm') {
  81. throw new Exception("Something unexpected changed in the user_pass_reset form; we are not getting the UID/timestamp/'confirm' passed anymore.");
  82. }
  83. // Use the variable system to communicate to the test code, since we don't
  84. // share a session with it.
  85. variable_set('user_test_pass_reset_form_build_' . arg(2), TRUE);
  86. $form['#submit'][] = 'user_form_test_form_user_pass_reset_submit';
  87. // We must cache the form to ensure the form builder (user_pass_reset()) is
  88. // skipped when processing the submitted form, otherwise we get redirected
  89. // already during form build.
  90. $form_state['cache'] = TRUE;
  91. }
  92. /**
  93. * Submit function for user_pass_reset().
  94. */
  95. function user_form_test_form_user_pass_reset_submit($form, &$form_state) {
  96. // On submit, the hash is in arg(4).
  97. if (!is_numeric(arg(2)) || !is_numeric(arg(3)) || !is_string(arg(4)) || strlen(arg(4)) < 32) {
  98. throw new Exception("Something unexpected changed in the user_pass_reset form; we are not getting the UID/timestamp/hash passed anymore.");
  99. }
  100. variable_set('user_test_pass_reset_form_submit_' . arg(2), TRUE);
  101. // Because the form does no further processing and has no redirect set,
  102. // drupal_redirect_form() will redirect back to ourselves
  103. // (user/reset/UID/TIMESTAMP/HASH/login); we will be logged in and redirected
  104. // while the form is built again. FYI: we cannot set $form_state['rebuild']
  105. // to get around the first redirect without further hacks, because then the
  106. // form won't pass the hash. (Our original $form_state['build_info']['args']
  107. // contains "confirm" for the 3rd argument.)
  108. }