Commit ce851476 authored by catch's avatar catch
Browse files

Issue #1488630 by Berdir: Fixed Who's online block doesn't work with swappable...

Issue #1488630 by Berdir: Fixed Who's online block doesn't work with swappable session backends and lazy session creation.
parent aa2fc49a
......@@ -84,26 +84,17 @@ function testUserLoginBlock() {
* Test the Who's Online block.
*/
function testWhosOnlineBlock() {
// Generate users and make sure there are no current user sessions.
// Generate users.
$user1 = $this->drupalCreateUser(array());
$user2 = $this->drupalCreateUser(array());
$user3 = $this->drupalCreateUser(array());
$this->assertEqual(db_query("SELECT COUNT(*) FROM {sessions}")->fetchField(), 0, 'Sessions table is empty.');
// Insert a user with two sessions.
$this->insertSession(array('uid' => $user1->uid));
$this->insertSession(array('uid' => $user1->uid));
$this->assertEqual(db_query("SELECT COUNT(*) FROM {sessions} WHERE uid = :uid", array(':uid' => $user1->uid))->fetchField(), 2, 'Duplicate user session has been inserted.');
// Update access of two users to be within the active timespan.
$this->updateAccess($user1->uid);
$this->updateAccess($user2->uid, REQUEST_TIME + 1);
// Insert a user with only one session.
$this->insertSession(array('uid' => $user2->uid, 'timestamp' => REQUEST_TIME + 1));
// Insert an inactive logged-in user who should not be seen in the block.
$this->insertSession(array('uid' => $user3->uid, 'timestamp' => (REQUEST_TIME - config('user.block')->get('seconds_online') - 1)));
// Insert two anonymous user sessions.
$this->insertSession();
$this->insertSession();
// Insert an inactive user who should not be seen in the block.
$this->updateAccess($user3->uid, REQUEST_TIME - config('user.block')->get('seconds_online') - 1);
// Test block output.
$block = user_block_view('online');
......@@ -117,18 +108,12 @@ function testWhosOnlineBlock() {
}
/**
* Insert a user session into the {sessions} table. This function is used
* since we cannot log in more than one user at the same time in tests.
* Updates the access column for a user.
*/
private function insertSession(array $fields = array()) {
$fields += array(
'uid' => 0,
'sid' => drupal_hash_base64(uniqid(mt_rand(), TRUE)),
'timestamp' => REQUEST_TIME,
);
db_insert('sessions')
->fields($fields)
private function updateAccess($uid, $access = REQUEST_TIME) {
db_update('users')
->condition('uid', $uid)
->fields(array('access' => $access))
->execute();
$this->assertEqual(db_query("SELECT COUNT(*) FROM {sessions} WHERE uid = :uid AND sid = :sid AND timestamp = :timestamp", array(':uid' => $fields['uid'], ':sid' => $fields['sid'], ':timestamp' => $fields['timestamp']))->fetchField(), 1, 'Session record inserted.');
}
}
......@@ -812,14 +812,15 @@ function user_block_view($delta = '') {
}
return $block;
// @todo: Move this block to statistics.module and remove dependency on
// user.access.
case 'online':
if (user_access('access content')) {
// Count users active within the defined period.
$interval = REQUEST_TIME - $block_config->get('seconds_online');
// Perform database queries to gather online user lists. We use s.timestamp
// rather than u.access because it is much faster.
$authenticated_count = db_query("SELECT COUNT(DISTINCT s.uid) FROM {sessions} s WHERE s.timestamp >= :timestamp AND s.uid > 0", array(':timestamp' => $interval))->fetchField();
// Perform database queries to gather online user lists.
$authenticated_count = db_query("SELECT COUNT(uid) FROM {users} WHERE access >= :timestamp", array(':timestamp' => $interval))->fetchField();
$block['subject'] = t('Who\'s online');
$block['content'] = array(
......@@ -831,9 +832,8 @@ function user_block_view($delta = '') {
// Display a list of currently online users.
$max_users = $block_config->get('max_list_count');
if ($authenticated_count && $max_users) {
$items = db_query_range('SELECT u.uid, u.name, MAX(s.timestamp) AS max_timestamp FROM {users} u INNER JOIN {sessions} s ON u.uid = s.uid WHERE s.timestamp >= :interval AND s.uid > 0 GROUP BY u.uid, u.name ORDER BY max_timestamp DESC', 0, $max_users, array(':interval' => $interval))->fetchAll();
foreach ($items as $account) {
$uids = db_query_range('SELECT uid FROM {users} WHERE access >= :interval AND uid > 0 ORDER BY access DESC', 0, $max_users, array(':interval' => $interval))->fetchCol();
foreach (user_load_multiple($uids) as $account) {
$block['content']['#items'][] = theme('username', array('account' => $account));
}
}
......
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