Commit 40ca713e authored by catch's avatar catch

Issue #1594260 by aspilicious: Convert user tests to PSR-0.

parent c034303b
<?php
/**
* @file
* Definition of Drupal\user\Tests\UserAccountLinksTests.
*/
namespace Drupal\user\Tests;
use Drupal\simpletest\WebTestBase;
/**
* Test user-links in secondary menu.
*/
class UserAccountLinksTests extends WebTestBase {
public static function getInfo() {
return array(
'name' => 'User account links',
'description' => 'Test user-account links.',
'group' => 'User'
);
}
/**
* Tests the secondary menu.
*/
function testSecondaryMenu() {
// Create a regular user.
$user = $this->drupalCreateUser(array());
// Log in and get the homepage.
$this->drupalLogin($user);
$this->drupalGet('<front>');
// For a logged-in user, expect the secondary menu to have links for "My
// account" and "Log out".
$link = $this->xpath('//ul[@id=:menu_id]/li/a[contains(@href, :href) and text()=:text]', array(
':menu_id' => 'secondary-menu',
':href' => 'user',
':text' => 'My account',
));
$this->assertEqual(count($link), 1, 'My account link is in secondary menu.');
$link = $this->xpath('//ul[@id=:menu_id]/li/a[contains(@href, :href) and text()=:text]', array(
':menu_id' => 'secondary-menu',
':href' => 'user/logout',
':text' => 'Log out',
));
$this->assertEqual(count($link), 1, 'Log out link is in secondary menu.');
// Log out and get the homepage.
$this->drupalLogout();
$this->drupalGet('<front>');
// For a logged-out user, expect no secondary links.
$element = $this->xpath('//ul[@id=:menu_id]', array(':menu_id' => 'secondary-menu'));
$this->assertEqual(count($element), 0, 'No secondary-menu for logged-out users.');
}
}
<?php
/**
* @file
* Definition of Drupal\user\Tests\UserAdminTest.
*/
namespace Drupal\user\Tests;
use Drupal\simpletest\WebTestBase;
class UserAdminTest extends WebTestBase {
public static function getInfo() {
return array(
'name' => 'User administration',
'description' => 'Test user administration page functionality.',
'group' => 'User'
);
}
function setUp() {
parent::setUp(array('taxonomy'));
}
/**
* Registers a user and deletes it.
*/
function testUserAdmin() {
$user_a = $this->drupalCreateUser(array());
$user_b = $this->drupalCreateUser(array('administer taxonomy'));
$user_c = $this->drupalCreateUser(array('administer taxonomy'));
// Create admin user to delete registered user.
$admin_user = $this->drupalCreateUser(array('administer users'));
$this->drupalLogin($admin_user);
$this->drupalGet('admin/people');
$this->assertText($user_a->name, t('Found user A on admin users page'));
$this->assertText($user_b->name, t('Found user B on admin users page'));
$this->assertText($user_c->name, t('Found user C on admin users page'));
$this->assertText($admin_user->name, t('Found Admin user on admin users page'));
// Test for existence of edit link in table.
$link = l(t('edit'), "user/$user_a->uid/edit", array('query' => array('destination' => 'admin/people')));
$this->assertRaw($link, t('Found user A edit link on admin users page'));
// Filter the users by permission 'administer taxonomy'.
$edit = array();
$edit['permission'] = 'administer taxonomy';
$this->drupalPost('admin/people', $edit, t('Filter'));
// Check if the correct users show up.
$this->assertNoText($user_a->name, t('User A not on filtered by perm admin users page'));
$this->assertText($user_b->name, t('Found user B on filtered by perm admin users page'));
$this->assertText($user_c->name, t('Found user C on filtered by perm admin users page'));
// Filter the users by role. Grab the system-generated role name for User C.
$edit['role'] = max(array_flip($user_c->roles));
$this->drupalPost('admin/people', $edit, t('Refine'));
// Check if the correct users show up when filtered by role.
$this->assertNoText($user_a->name, t('User A not on filtered by role on admin users page'));
$this->assertNoText($user_b->name, t('User B not on filtered by role on admin users page'));
$this->assertText($user_c->name, t('User C on filtered by role on admin users page'));
// Test blocking of a user.
$account = user_load($user_c->uid);
$this->assertEqual($account->status, 1, 'User C not blocked');
$edit = array();
$edit['operation'] = 'block';
$edit['accounts[' . $account->uid . ']'] = TRUE;
$this->drupalPost('admin/people', $edit, t('Update'));
$account = user_load($user_c->uid, TRUE);
$this->assertEqual($account->status, 0, 'User C blocked');
// Test unblocking of a user from /admin/people page and sending of activation mail
$editunblock = array();
$editunblock['operation'] = 'unblock';
$editunblock['accounts[' . $account->uid . ']'] = TRUE;
$this->drupalPost('admin/people', $editunblock, t('Update'));
$account = user_load($user_c->uid, TRUE);
$this->assertEqual($account->status, 1, 'User C unblocked');
$this->assertMail("to", $account->mail, "Activation mail sent to user C");
// Test blocking and unblocking another user from /user/[uid]/edit form and sending of activation mail
$user_d = $this->drupalCreateUser(array());
$account1 = user_load($user_d->uid, TRUE);
$this->drupalPost('user/' . $account1->uid . '/edit', array('status' => 0), t('Save'));
$account1 = user_load($user_d->uid, TRUE);
$this->assertEqual($account1->status, 0, 'User D blocked');
$this->drupalPost('user/' . $account1->uid . '/edit', array('status' => TRUE), t('Save'));
$account1 = user_load($user_d->uid, TRUE);
$this->assertEqual($account1->status, 1, 'User D unblocked');
$this->assertMail("to", $account1->mail, "Activation mail sent to user D");
}
}
<?php
/**
* @file
* Definition of Drupal\user\Tests\UserAuthmapAssignmentTest.
*/
namespace Drupal\user\Tests;
use Drupal\simpletest\WebTestBase;
/**
* Unit test for authmap assignment.
*/
class UserAuthmapAssignmentTest extends WebTestBase {
public static function getInfo() {
return array(
'name' => t('Authmap assignment'),
'description' => t('Tests that users can be assigned and unassigned authmaps.'),
'group' => t('User')
);
}
/**
* Test authmap assignment and retrieval.
*/
function testAuthmapAssignment() {
$account = $this->drupalCreateUser();
// Assign authmaps to the user.
$authmaps = array(
'authname_poll' => 'external username one',
'authname_book' => 'external username two',
);
user_set_authmaps($account, $authmaps);
// Test for expected authmaps.
$expected_authmaps = array(
'external username one' => array(
'poll' => 'external username one',
),
'external username two' => array(
'book' => 'external username two',
),
);
foreach ($expected_authmaps as $authname => $expected_output) {
$this->assertIdentical(user_get_authmaps($authname), $expected_output, t('Authmap for authname %authname was set correctly.', array('%authname' => $authname)));
}
// Remove authmap for module poll, add authmap for module blog.
$authmaps = array(
'authname_poll' => NULL,
'authname_blog' => 'external username three',
);
user_set_authmaps($account, $authmaps);
// Assert that external username one does not have authmaps.
$remove_username = 'external username one';
unset($expected_authmaps[$remove_username]);
$this->assertFalse(user_get_authmaps($remove_username), t('Authmap for %authname was removed.', array('%authname' => $remove_username)));
// Assert that a new authmap was created for external username three, and
// existing authmaps for external username two were unchanged.
$expected_authmaps['external username three'] = array('blog' => 'external username three');
foreach ($expected_authmaps as $authname => $expected_output) {
$this->assertIdentical(user_get_authmaps($authname), $expected_output, t('Authmap for authname %authname was set correctly.', array('%authname' => $authname)));
}
}
}
<?php
/**
* @file
* Definition of Drupal\user\Tests\UserAutocompleteTest.
*/
namespace Drupal\user\Tests;
use Drupal\simpletest\WebTestBase;
/**
* Test user autocompletion.
*/
class UserAutocompleteTest extends WebTestBase {
public static function getInfo() {
return array(
'name' => 'User autocompletion',
'description' => 'Test user autocompletion functionality.',
'group' => 'User'
);
}
function setUp() {
parent::setUp();
// Set up two users with different permissions to test access.
$this->unprivileged_user = $this->drupalCreateUser();
$this->privileged_user = $this->drupalCreateUser(array('access user profiles'));
}
/**
* Tests access to user autocompletion and verify the correct results.
*/
function testUserAutocomplete() {
// Check access from unprivileged user, should be denied.
$this->drupalLogin($this->unprivileged_user);
$this->drupalGet('user/autocomplete/' . $this->unprivileged_user->name[0]);
$this->assertResponse(403, t('Autocompletion access denied to user without permission.'));
// Check access from privileged user.
$this->drupalLogout();
$this->drupalLogin($this->privileged_user);
$this->drupalGet('user/autocomplete/' . $this->unprivileged_user->name[0]);
$this->assertResponse(200, t('Autocompletion access allowed.'));
// Using first letter of the user's name, make sure the user's full name is in the results.
$this->assertRaw($this->unprivileged_user->name, t('User name found in autocompletion results.'));
}
}
<?php
/**
* @file
* Definition of Drupal\user\Tests\UserBlocksTests.
*/
namespace Drupal\user\Tests;
use Drupal\simpletest\WebTestBase;
/**
* Test user blocks.
*/
class UserBlocksTests extends WebTestBase {
public static function getInfo() {
return array(
'name' => 'User blocks',
'description' => 'Test user blocks.',
'group' => 'User'
);
}
function setUp() {
parent::setUp(array('block'));
// Enable user login block.
db_merge('block')
->key(array(
'module' => 'user',
'delta' => 'login',
'theme' => variable_get('theme_default', 'stark'),
))
->fields(array(
'status' => 1,
'weight' => 0,
'region' => 'sidebar_first',
'pages' => '',
'cache' => -1,
))
->execute();
}
/**
* Test the user login block.
*/
function testUserLoginBlock() {
// Create a user with some permission that anonymous users lack.
$user = $this->drupalCreateUser(array('administer permissions'));
// Log in using the block.
$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'), t('Logged in.'));
// Check that we are still on the same page.
$this->assertEqual(url('admin/people/permissions', array('absolute' => TRUE)), $this->getUrl(), t('Still on the same page after login for access denied page'));
// Now, log out and repeat with a non-403 page.
$this->drupalLogout();
$this->drupalPost('filter/tips', $edit, t('Log in'));
$this->assertNoText(t('User login'), t('Logged in.'));
$this->assertPattern('!<title.*?' . t('Compose tips') . '.*?</title>!', t('Still on the same page after login for allowed page'));
// Check that the user login block is not vulnerable to information
// disclosure to third party sites.
$this->drupalLogout();
$this->drupalPost('http://example.com/', $edit, t('Log in'), array('external' => FALSE));
// Check that we remain on the site after login.
$this->assertEqual(url('user/' . $user->uid, array('absolute' => TRUE)), $this->getUrl(), t('Redirected to user profile page after login from the frontpage'));
}
/**
* Test the Who's Online block.
*/
function testWhosOnlineBlock() {
// Generate users and make sure there are no current user sessions.
$user1 = $this->drupalCreateUser(array());
$user2 = $this->drupalCreateUser(array());
$user3 = $this->drupalCreateUser(array());
$this->assertEqual(db_query("SELECT COUNT(*) FROM {sessions}")->fetchField(), 0, t('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, t('Duplicate user session has been inserted.'));
// 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 - variable_get('user_block_seconds_online', 900) - 1)));
// Insert two anonymous user sessions.
$this->insertSession();
$this->insertSession();
// Test block output.
$block = user_block_view('online');
$this->drupalSetContent($block['content']);
$this->assertRaw(t('2 users'), t('Correct number of online users (2 users).'));
$this->assertText($user1->name, t('Active user 1 found in online list.'));
$this->assertText($user2->name, t('Active user 2 found in online list.'));
$this->assertNoText($user3->name, t("Inactive user not found in online list."));
$this->assertTrue(strpos($this->drupalGetContent(), $user1->name) > strpos($this->drupalGetContent(), $user2->name), t('Online users are ordered correctly.'));
}
/**
* 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.
*/
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)
->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, t('Session record inserted.'));
}
}
<?php
/**
* @file
* Definition of Drupal\user\Tests\UserCancelTest.
*/
namespace Drupal\user\Tests;
use Drupal\simpletest\WebTestBase;
/**
* Test cancelling a user.
*/
class UserCancelTest extends WebTestBase {
protected $profile = 'standard';
public static function getInfo() {
return array(
'name' => 'Cancel account',
'description' => 'Ensure that account cancellation methods work as expected.',
'group' => 'User',
);
}
function setUp() {
parent::setUp('comment');
}
/**
* Attempt to cancel account without permission.
*/
function testUserCancelWithoutPermission() {
variable_set('user_cancel_method', 'user_cancel_reassign');
// Create a user.
$account = $this->drupalCreateUser(array());
$this->drupalLogin($account);
// Load real user object.
$account = user_load($account->uid, TRUE);
// Create a node.
$node = $this->drupalCreateNode(array('uid' => $account->uid));
// Attempt to cancel account.
$this->drupalGet('user/' . $account->uid . '/edit');
$this->assertNoRaw(t('Cancel account'), t('No cancel account button displayed.'));
// Attempt bogus account cancellation request confirmation.
$timestamp = $account->login;
$this->drupalGet("user/$account->uid/cancel/confirm/$timestamp/" . user_pass_rehash($account->pass, $timestamp, $account->login));
$this->assertResponse(403, t('Bogus cancelling request rejected.'));
$account = user_load($account->uid);
$this->assertTrue($account->status == 1, t('User account was not canceled.'));
// Confirm user's content has not been altered.
$test_node = node_load($node->nid, NULL, TRUE);
$this->assertTrue(($test_node->uid == $account->uid && $test_node->status == 1), t('Node of the user has not been altered.'));
}
/**
* Tests that user account for uid 1 cannot be cancelled.
*
* This should never be possible, or the site owner would become unable to
* administer the site.
*/
function testUserCancelUid1() {
// Update uid 1's name and password to we know it.
$password = user_password();
require_once DRUPAL_ROOT . '/' . variable_get('password_inc', 'core/includes/password.inc');
$account = array(
'name' => 'user1',
'pass' => user_hash_password(trim($password)),
);
// We cannot use $account->save() here, because this would result in the
// password being hashed again.
db_update('users')
->fields($account)
->condition('uid', 1)
->execute();
// Reload and log in uid 1.
$user1 = user_load(1, TRUE);
$user1->pass_raw = $password;
// Try to cancel uid 1's account with a different user.
$this->admin_user = $this->drupalCreateUser(array('administer users'));
$this->drupalLogin($this->admin_user);
$edit = array(
'operation' => 'cancel',
'accounts[1]' => TRUE,
);
$this->drupalPost('admin/people', $edit, t('Update'));
// Verify that uid 1's account was not cancelled.
$user1 = user_load(1, TRUE);
$this->assertEqual($user1->status, 1, t('User #1 still exists and is not blocked.'));
}
/**
* Attempt invalid account cancellations.
*/
function testUserCancelInvalid() {
variable_set('user_cancel_method', 'user_cancel_reassign');
// Create a user.
$account = $this->drupalCreateUser(array('cancel account'));
$this->drupalLogin($account);
// Load real user object.
$account = user_load($account->uid, TRUE);
// Create a node.
$node = $this->drupalCreateNode(array('uid' => $account->uid));
// Attempt to cancel account.
$this->drupalPost('user/' . $account->uid . '/edit', NULL, t('Cancel account'));
// Confirm account cancellation.
$timestamp = time();
$this->drupalPost(NULL, NULL, t('Cancel account'));
$this->assertText(t('A confirmation request to cancel your account has been sent to your e-mail address.'), t('Account cancellation request mailed message displayed.'));
// Attempt bogus account cancellation request confirmation.
$bogus_timestamp = $timestamp + 60;
$this->drupalGet("user/$account->uid/cancel/confirm/$bogus_timestamp/" . user_pass_rehash($account->pass, $bogus_timestamp, $account->login));
$this->assertText(t('You have tried to use an account cancellation link that has expired. Please request a new one using the form below.'), t('Bogus cancelling request rejected.'));
$account = user_load($account->uid);
$this->assertTrue($account->status == 1, t('User account was not canceled.'));
// Attempt expired account cancellation request confirmation.
$bogus_timestamp = $timestamp - 86400 - 60;
$this->drupalGet("user/$account->uid/cancel/confirm/$bogus_timestamp/" . user_pass_rehash($account->pass, $bogus_timestamp, $account->login));
$this->assertText(t('You have tried to use an account cancellation link that has expired. Please request a new one using the form below.'), t('Expired cancel account request rejected.'));
$accounts = user_load_multiple(array($account->uid), array('status' => 1));
$this->assertTrue(reset($accounts), t('User account was not canceled.'));
// Confirm user's content has not been altered.
$test_node = node_load($node->nid, NULL, TRUE);
$this->assertTrue(($test_node->uid == $account->uid && $test_node->status == 1), t('Node of the user has not been altered.'));
}
/**
* Disable account and keep all content.
*/
function testUserBlock() {
variable_set('user_cancel_method', 'user_cancel_block');
// Create a user.
$web_user = $this->drupalCreateUser(array('cancel account'));
$this->drupalLogin($web_user);
// Load real user object.
$account = user_load($web_user->uid, TRUE);
// Attempt to cancel account.
$this->drupalGet('user/' . $account->uid . '/edit');
$this->drupalPost(NULL, NULL, t('Cancel account'));
$this->assertText(t('Are you sure you want to cancel your account?'), t('Confirmation form to cancel account displayed.'));
$this->assertText(t('Your account will be blocked and you will no longer be able to log in. All of your content will remain attributed to your user name.'), t('Informs that all content will be remain as is.'));
$this->assertNoText(t('Select the method to cancel the account above.'), t('Does not allow user to select account cancellation method.'));
// Confirm account cancellation.
$timestamp = time();
$this->drupalPost(NULL, NULL, t('Cancel account'));
$this->assertText(t('A confirmation request to cancel your account has been sent to your e-mail address.'), t('Account cancellation request mailed message displayed.'));
// Confirm account cancellation request.
$this->drupalGet("user/$account->uid/cancel/confirm/$timestamp/" . user_pass_rehash($account->pass, $timestamp, $account->login));
$account = user_load($account->uid, TRUE);
$this->assertTrue($account->status == 0, t('User has been blocked.'));
// Confirm user is logged out.
$this->assertNoText($account->name, t('Logged out.'));
}
/**
* Disable account and unpublish all content.
*/
function testUserBlockUnpublish() {
variable_set('user_cancel_method', 'user_cancel_block_unpublish');
// Create a user.
$account = $this->drupalCreateUser(array('cancel account'));
$this->drupalLogin($account);
// Load real user object.
$account = user_load($account->uid, TRUE);
// Create a node with two revisions.
$node = $this->drupalCreateNode(array('uid' => $account->uid));
$settings = get_object_vars($node);