Commit d5e42155 authored by Drew Webber's avatar Drew Webber
Browse files

Issue #512042 by vbouchet, kbasarab, stefanos.petrakis, poker10, felribeiro,...

Issue #512042 by vbouchet, kbasarab, stefanos.petrakis, poker10, felribeiro, ryan.gibson, scottrigby, Caligan, tim.plunkett, perarnet, stpaultim, bojanz, xjm, Fabianx, David_Rothstein, mcdruid: Add user action "unblock current user" to user.module
parent 64b8a9a1
Loading
Loading
Loading
Loading
+6 −0
Original line number Diff line number Diff line
name = Anonymous user unblock action tests
description = Support module for anonymous user unblock action testing.
package = Testing
version = VERSION
core = 7.x
hidden = TRUE
+20 −0
Original line number Diff line number Diff line
<?php

/**
 * @file
 * Dummy module implementing hook_user_load().
 */

/**
 * Implements hook_user_load().
 */
function anonymous_user_unblock_test_user_load($users) {
  foreach ($users as $user) {
    if ($user->uid == 0) {
      // Try to fool user_save() into unblocking the anonymous user.
      // See https://www.drupal.org/comment/14722637.
      $user->is_new = FALSE;
      $user->original = clone $user;
    }
  }
}
+43 −0
Original line number Diff line number Diff line
@@ -3762,6 +3762,12 @@ function user_action_info() {
      'configurable' => FALSE,
      'triggers' => array('any'),
    ),
    'user_unblock_user_action' => array(
      'label' => t('Unblock current user'),
      'type' => 'user',
      'configurable' => FALSE,
      'triggers' => array('any'),
     ),
  );
}

@@ -3795,6 +3801,43 @@ function user_block_user_action(&$entity, $context = array()) {
  watchdog('action', 'Blocked user %name.', array('%name' => $account->name));
}

/**
 * Unblocks a specific user or the current user, if one is not specified.
 *
 * @param $entity
 *   (optional) An entity object; if it is provided and it has a uid property,
 *   the user with that ID is unblocked.
 * @param $context
 *   (optional) An associative array; if no user ID is found in $entity, the
 *   'uid' element of this array determines the user to unblock.
 *
 * @ingroup actions
 */
function user_unblock_user_action(&$entity, $context = array()) {
  // First priority: If there is a $entity->uid, unblock that user.
  // This is most likely a user object or the author if a node or comment.
  if (isset($entity->uid)) {
    $uid = $entity->uid;
  }
  elseif (isset($context['uid'])) {
    $uid = $context['uid'];
  }
  // If neither of those are valid, then unblock the current user.
  else {
    $uid = $GLOBALS['user']->uid;
  }

  // The anonymous user should not be unblocked.
  if ($uid == 0) {
    watchdog('action', 'Anonymous user should not be unblocked.', array(), WATCHDOG_WARNING);
    return;
  }

  $account = user_load($uid);
  $account = user_save($account, array('status' => 1));
  watchdog('action', 'Unblocked user %name.', array('%name' => $account->name));
}

/**
 * Implements hook_form_FORM_ID_alter().
 *
+64 −0
Original line number Diff line number Diff line
@@ -2881,3 +2881,67 @@ class UserValidateCurrentPassCustomForm extends DrupalWebTestCase {
    $this->assertText(t('The password has been validated and the form submitted successfully.'));
  }
}

/**
 * Tests actions provided by the User module.
 */
class UserActionsTest extends DrupalWebTestCase {

  public static function getInfo() {
    return array(
      'name' => 'User actions',
      'description' => 'Test actions provided by the user module.',
      'group' => 'User',
    );
  }

  function setUp() {
    parent::setUp('dblog', 'anonymous_user_unblock_test');
  }

  /**
   * Tests user block and unblock actions.
   */
  function testUserBlockUnBlockActions() {
    $unblocked_user = $this->drupalCreateUser();

    db_truncate('watchdog')->execute();

    // Block a user.
    user_block_user_action($unblocked_user);

    $blocked_user = user_load($unblocked_user->uid, TRUE);
    $this->assertEqual($blocked_user->status, 0, 'User is blocked');

    // Assert that a watchdog message was logged.
    $status = (bool) db_query_range('SELECT 1 FROM {watchdog} WHERE message = :message AND variables = :variables', 0, 1, array(':message' => 'Blocked user %name.', ':variables' => serialize(array('%name' => $blocked_user->name))))->fetchField();
    $this->assert($status, 'A watchdog message was logged by user block action.');

    // Unblock a user.
    user_unblock_user_action($blocked_user);

    $unblocked_user = user_load($unblocked_user->uid, TRUE);
    $this->assertEqual($unblocked_user->status, 1, 'User is unblocked');

    // Assert that a watchdog message was logged.
    $status = (bool) db_query_range('SELECT 1 FROM {watchdog} WHERE message = :message AND variables = :variables', 0, 1, array(':message' => 'Unblocked user %name.', ':variables' => serialize(array('%name' => $unblocked_user->name))))->fetchField();
    $this->assert($status, 'A watchdog message was logged by user unblock action.');

    // Try to unblock the anonymous user.
    db_truncate('watchdog')->execute();
    $anonymous_user = user_load(0);

    // Assert anonymous user is blocked.
    $this->assertEqual($anonymous_user->status, 0, 'Anonymous user is blocked');

    user_unblock_user_action($anonymous_user);

    // Assert anonymous user remains blocked.
    $anonymous_user = user_load(0, TRUE);
    $this->assertEqual($anonymous_user->status, 0, 'Anonymous user remains blocked');

    // Assert that a watchdog message was logged.
    $status = (bool) db_query_range('SELECT 1 FROM {watchdog} WHERE message = :message', 0, 1, array(':message' => 'Anonymous user should not be unblocked.'))->fetchField();
    $this->assert($status, 'A watchdog message was logged by user unblock action.');
  }
}