<?php
namespace Drupal\user\Tests;
use Drupal\simpletest\WebTestBase;
class UserBlocksTests extends WebTestBase {
public static $modules = array(
'block',
);
protected $adminUser;
public static function getInfo() {
return array(
'name' => 'User blocks',
'description' => 'Test user blocks.',
'group' => 'User',
);
}
function setUp() {
parent::setUp();
$this->adminUser = $this
->drupalCreateUser(array(
'administer blocks',
));
$this
->drupalLogin($this->adminUser);
$this
->drupalPlaceBlock('user_login_block');
$this
->drupalLogout($this->adminUser);
}
function testUserLoginBlock() {
$user = $this
->drupalCreateUser(array(
'administer permissions',
));
$edit = array();
$edit['name'] = $user->name;
$edit['pass'] = $user->pass_raw;
$this
->drupalPost('admin/people/permissions', $edit, t('Log in'));
$this
->assertNoText(t('User login'), 'Logged in.');
$this
->assertEqual(url('admin/people/permissions', array(
'absolute' => TRUE,
)), $this
->getUrl(), 'Still on the same page after login for access denied page');
$this
->drupalLogout();
$this
->drupalPost('filter/tips', $edit, t('Log in'));
$this
->assertNoText(t('User login'), 'Logged in.');
$this
->assertPattern('!<title.*?' . t('Compose tips') . '.*?</title>!', 'Still on the same page after login for allowed page');
$this
->drupalLogout();
$this
->drupalPost('http://example.com/', $edit, t('Log in'), array(
'external' => FALSE,
));
$this
->assertEqual(url('user/' . $user->uid, array(
'absolute' => TRUE,
)), $this
->getUrl(), 'Redirected to user profile page after login from the frontpage');
}
function testWhosOnlineBlock() {
$block = $this
->drupalPlaceBlock('user_online_block');
$config = $block
->get('settings');
$user1 = $this
->drupalCreateUser(array());
$user2 = $this
->drupalCreateUser(array());
$user3 = $this
->drupalCreateUser(array());
$this
->updateAccess($user1->uid);
$this
->updateAccess($user2->uid, REQUEST_TIME + 1);
$inactive_time = REQUEST_TIME - $config['seconds_online'] - 1;
$this
->updateAccess($user3->uid, $inactive_time);
$this
->updateAccess($this->adminUser->uid, $inactive_time);
$content = entity_view($block, 'block');
$this
->drupalSetContent(render($content));
$this
->assertRaw(t('2 users'), 'Correct number of online users (2 users).');
$this
->assertText($user1->name, 'Active user 1 found in online list.');
$this
->assertText($user2->name, 'Active user 2 found in online list.');
$this
->assertNoText($user3->name, 'Inactive user not found in online list.');
$this
->assertTrue(strpos($this
->drupalGetContent(), $user1->name) > strpos($this
->drupalGetContent(), $user2->name), 'Online users are ordered correctly.');
}
private function updateAccess($uid, $access = REQUEST_TIME) {
db_update('users')
->condition('uid', $uid)
->fields(array(
'access' => $access,
))
->execute();
}
}