agreement.test in Agreement 7.2
Same filename and directory in other branches
Tests for Agreement module.
File
agreement.testView source
<?php
/**
* @file
* Tests for Agreement module.
*/
/**
* Agreement base test class.
*/
class AgreementTestCase extends DrupalWebTestCase {
protected $agreement;
protected $siteName;
protected $privilegedUser;
protected $unprivilegedUser;
protected $bypassUser;
protected $pageTitle;
protected $pageText;
protected $notSet;
protected $node;
/**
* {@inheritdoc}
*/
public function setUp() {
parent::setUp('agreement');
$this->siteName = ' | ' . variable_get('siteName', 'Drupal');
$this->pageTitle = $this
->randomName(16);
$this->pageText = $this
->randomName(16);
$this->notSet = $this
->randomName(32);
// Make a node with a title that is distinguishable when manually debugging
// tests.
$this->node = $this
->drupalCreateNode(array(
'title' => 'Node Page ' . $this
->randomName(8),
));
$this->agreement = agreement_type_load('default');
$settings = array(
"settings[title]" => $this->pageTitle,
"agreement[value]" => $this->pageText,
"settings[visibility_settings]" => 0,
"settings[visibility_pages]" => "<front>",
);
$this
->privilegedUserLogin();
$this
->configureAgreementSettings($settings);
$this->agreement = agreement_type_load('default', FALSE);
}
/**
* Creates a privileged user and login as the user.
*/
protected function privilegedUserLogin() {
// Create and log in our privileged user.
$this->privilegedUser = $this
->drupalCreateUser(array(
'administer agreements',
'access administration pages',
'administer site configuration',
'bypass node access',
));
$this
->drupalLogin($this->privilegedUser);
}
/**
* Creates an user with the "bypass agreement" permission and login.
*/
protected function bypassUserLogin() {
$this->bypassUser = $this
->drupalCreateUser(array(
'bypass agreement',
));
$this
->drupalLogin($this->bypassUser);
}
/**
* Creates an unprivileged user and login as that user.
*
* @param string $destination
* The destination parameter for ::drupalLogin().
*/
protected function unprivilegedUserLogin($destination = 'user') {
// Create and log in our unprivileged user.
$this->unprivilegedUser = $this
->drupalCreateUser(array(
'access content',
));
$this
->drupalLogin($this->unprivilegedUser, $destination);
}
/**
* Goes to the agreement settings page and save new configuration.
*
* @param array $settings
* The form values to save keyed by the form element name.
*/
protected function configureAgreementSettings($settings) {
$this
->drupalGet('admin/config/people/agreement');
$this
->assertResponse(200, t('User is allowed to access agreement settings page.'));
$this
->drupalPost('admin/config/people/agreement/manage/default', $settings, t('Save configuration'));
$this
->assertText('Agreement type ' . $this->agreement['type'] . ' saved successfully.', t('Agreement settings saved'));
}
/**
* Checks if the current page is the agreement page.
*
* @param array $agreement
* The agreement type.
*/
protected function isAgreementPage($agreement) {
$this
->assertUrl($agreement['path'], array(), t('URL of agreement is correct: !url', array(
'!url' => $this
->getUrl(),
)));
$this
->assertTitle($agreement['settings']['title'] . $this->siteName);
$this
->assertText($agreement['settings']['checkbox'], t('Checkbox text is correct'));
}
/**
* Checks if the current page is not the agreement page.
*
* @param array $agreement
* The agreement type.
*/
protected function isNotAgreementPage($agreement) {
$this
->assertNotEqual($this
->getUrl(), url($agreement['path'], array()), t('Not on agreement page'));
}
/**
* Checks if the current page is the front page (node/).
*/
protected function isFrontPage() {
$this
->assertUrl('node', array(), t('On front page: !url', array(
'!url' => $this
->getUrl(),
)));
}
/**
* Checks if the current page is the current user view page.
*
* @param int $uid
* The user ID to check. Unused currently.
*/
protected function isUserProfilePage($uid) {
$this
->assertUrl('user', array(), t('On user profile page: !url', array(
'!url' => $this
->getUrl(),
)));
}
/**
* Checks if the current page is the current user edit page.
*
* @param int $uid
* The user ID to check.
*/
protected function isUserProfileEditPage($uid) {
$this
->assertUrl('user' . $uid . '/edit', array(), t('On user profile edit page: !url', array(
'!url' => $this
->getUrl(),
)));
}
/**
* Submits the agreement without checking the "I Agree" checkbox.
*
* @param int $uid
* The user ID of the user that is agreeing. Not used.
*/
protected function submitAgreementFormWithoutAgreeing($uid) {
// Try to submit without checking the I agree box.
$field_values = array();
$this
->drupalPost('agreement', $field_values, $this->agreement['settings']['submit']);
$this
->assertText($this->agreement['settings']['failure'], t('Correct error message'));
}
/**
* Submits the agreement correctly.
*
* @param int $uid
* The user ID of the user that is agreeing.
*/
protected function submitAgreementFormWithAgreeing($uid) {
// Submit with checking the I agree box.
$field_values['agree'] = '1';
$this
->drupalPost('agreement', $field_values, $this->agreement['settings']['submit']);
$this
->assertText($this->agreement['settings']['success'], t('Correct success message'));
$this
->isNotAgreementPage($this->agreement);
}
/**
* {@inheritdoc}
*/
protected function drupalLogin(stdClass $user, $destination = 'user') {
if ($this->loggedInUser) {
$this
->drupalLogout();
}
$edit = array(
'name' => $user->name,
'pass' => $user->pass_raw,
);
$this
->drupalPost($destination, $edit, t('Log in'));
// If a "log out" link appears on the page, it is almost certainly because
// the login was successful.
$pass = $this
->assertLink(t('Log out'), 0, t('User %name successfully logged in.', array(
'%name' => $user->name,
)), t('User login'));
if ($pass) {
$this->loggedInUser = $user;
}
}
}
/**
* Tests agreement functionality with default use case and a privileged user.
*/
class AgreementDefaultsPrivilegedUserTestCase extends AgreementTestCase {
/**
* {@inheritdoc}
*/
public static function getInfo() {
return array(
'name' => '1. Agreement settings',
'description' => 'Ensure that the default settings work properly for the admin user.',
'group' => 'Agreement',
);
}
/**
* Tests the agreement page functionality.
*/
public function testAgreement() {
// After save, re-open agreement settings.
$this
->assertUrl('admin/config/people/agreement/manage/default', array(), t('URL of settings page is correct: !url', array(
'!url' => $this
->getUrl(),
)));
$this
->assertTitle('Manage Agreement: Default agreement' . $this->siteName);
// Go to front page, no agreement.
$this
->drupalGet('node');
$this
->isNotAgreementPage($this->agreement);
// Go anywhere else, open agreement.
$this
->drupalGet('admin');
$this
->isAgreementPage($this->agreement);
// Try to go somewhere without submitting.
$this
->drupalGet('node/add');
$this
->isAgreementPage($this->agreement);
// Try submitting agreement form.
$this
->submitAgreementFormWithoutAgreeing($this->privilegedUser->uid);
$this
->submitAgreementFormWithAgreeing($this->privilegedUser->uid);
$this
->drupalGet('admin');
$this
->isNotAgreementPage($this->agreement);
}
}
/**
* Tests the agreement functionality with an unprivileged user.
*/
class AgreementDefaultsUnprivilegedUserTestCase extends AgreementTestCase {
/**
* {@inheritdoc}
*/
public static function getInfo() {
return array(
'name' => '2. Agreement page - default settings',
'description' => 'Ensure that the default settings work properly for the end user.',
'group' => 'Agreement',
);
}
/**
* Tests the agreement page for every page except the front page.
*/
public function testAgreement() {
$this
->unprivilegedUserLogin();
// Sent to agreement page.
$this
->isAgreementPage($this->agreement);
// Go to front page, no agreement.
$this
->drupalGet('node');
$this
->isNotAgreementPage($this->agreement);
// Go anywhere else, open agreement.
$this
->drupalGet('user/' . $this->unprivilegedUser->uid);
$this
->isAgreementPage($this->agreement);
// Try submitting agreement form.
$this
->submitAgreementFormWithoutAgreeing($this->unprivilegedUser->uid);
$this
->submitAgreementFormWithAgreeing($this->unprivilegedUser->uid);
$this
->drupalGet('admin/config/people/agreement');
$this
->assertResponse(403, t('User is not allowed to access agreement settings page.'));
}
}
/**
* Tests the agreement functionality with custom settings.
*/
class AgreementCustomUnprivilegedUserTestCase extends AgreementTestCase {
/**
* {@inheritdoc}
*/
public static function getInfo() {
return array(
'name' => '3. Agreement page - custom settings',
'description' => 'Ensure that custom settings work properly for the end user.',
'group' => 'Agreement',
);
}
/**
* Tests the agreement page only on the front page.
*/
public function testAgreement() {
$this->agreement['settings']['visibility_settings'] = 1;
$this->agreement['settings']['visibility_pages'] = '<front>';
$this->agreement = agreement_type_save($this->agreement);
$this
->unprivilegedUserLogin();
// Not sent to agreement page.
$this
->isNotAgreementPage($this->agreement);
// Go to front page, open agreement.
$this
->drupalGet('node');
$this
->isAgreementPage($this->agreement);
// Go anywhere else, no agreement.
$this
->drupalGet('user/' . $this->unprivilegedUser->uid);
$this
->isNotAgreementPage($this->agreement);
}
/**
* Tests the agreement frequency setting.
*/
public function testAgreementFrequency() {
// A) Agreement required once.
$this->agreement['settings']['visibility_settings'] = 1;
$this->agreement['settings']['visibility_pages'] = '<front>';
agreement_type_save($this->agreement);
$this->agreement = agreement_type_load($this->agreement['name'], FALSE);
$this
->unprivilegedUserLogin();
// Go to front page, open agreement.
$this
->drupalGet('node');
$this
->isAgreementPage($this->agreement);
$this
->submitAgreementFormWithAgreeing($this->unprivilegedUser->uid);
// Log out, log back in, no agreement.
$this
->drupalLogin($this->unprivilegedUser);
$this
->drupalGet('node');
$this
->isNotAgreementPage($this->agreement);
// B) Agreement required on every login.
$this->agreement['settings']['frequency'] = 0;
agreement_type_save($this->agreement);
$this->agreement = agreement_type_load($this->agreement['name'], FALSE);
$this
->drupalLogin($this->unprivilegedUser);
// Go to front page, open agreement.
$this
->drupalGet('node');
$this
->isAgreementPage($this->agreement);
$this
->submitAgreementFormWithAgreeing($this->unprivilegedUser->uid);
// Log out, log back in, open agreement.
$this
->drupalLogin($this->unprivilegedUser);
$this
->drupalGet('node');
$this
->isAgreementPage($this->agreement);
// Change password, no agreement.
$this
->submitAgreementFormWithAgreeing($this->unprivilegedUser->uid);
$this->agreement['settings']['visibility_pages'] = '';
agreement_type_save($this->agreement);
$this->agreement = agreement_type_load($this->agreement['name'], FALSE);
$edit = array(
'current_pass' => $this->unprivilegedUser->pass_raw,
'pass[pass1]' => $pass = $this
->randomString(),
'pass[pass2]' => $pass,
);
$this
->drupalPost('user/' . $this->unprivilegedUser->uid . '/edit', $edit, t('Save'));
$this
->isNotAgreementPage($this->agreement);
$this
->assertText(t('The changes have been saved.'), t('Password was changed'));
}
/**
* Tests the agreement destination functionality.
*
* 1. Agreement destination = blank.
* - user goes to regular get URL -> redirect to front.
* - user goes to node/1 -> redirect to node/1.
* - user needs to change password -> redirect to user/%/edit
* 2. Agreement destination = node/1.
* - user goes to regular get URL -> redirect to node/1.
* - user goes to user profile -> redirect to node/1.
* - user needs to change password -> redirect to user/%/edit.
*/
public function testAgreementDestination() {
// A) Agreement destination = blank.
$this->agreement['settings']['destination'] = '';
$this->agreement['settings']['visibility_settings'] = 0;
$this->agreement['settings']['visibility_pages'] = '';
agreement_type_save($this->agreement);
$this->agreement = agreement_type_load($this->agreement['name'], FALSE);
// Log in, open agreement, go to front page.
$this
->unprivilegedUserLogin('/node');
$this
->isAgreementPage($this->agreement);
$this
->drupalGet('node/' . $this->node->nid);
$this
->isAgreementPage($this->agreement);
$this
->submitAgreementFormWithAgreeing($this->unprivilegedUser->uid);
$this
->isFrontPage();
// Log in, go somewhere other than front page, open agreement, go to user's
// original destination.
$this
->unprivilegedUserLogin('/node/' . $this->node->nid);
$this
->isAgreementPage($this->agreement);
$this
->drupalGet('/node');
$this
->isAgreementPage($this->agreement);
$this
->submitAgreementFormWithAgreeing($this->unprivilegedUser->uid);
$this
->assertUrl('node/' . $this->node->nid, array(), t('User is redirected to original destination: !url', array(
'!url' => $this
->getUrl(),
)));
// @todo: Log in following password reset link, go somewhere other than
// front page, open agreement, go to user profile.
// B) Agreement destination = node/1.
$this->agreement['settings']['destination'] = 'node/1';
agreement_type_save($this->agreement);
$this->agreement = agreement_type_load($this->agreement['name']);
// Log in, open agreement, go to node/1.
$this
->unprivilegedUserLogin('/node');
$this
->isAgreementPage($this->agreement);
$this
->drupalGet('user/' . $this->unprivilegedUser->uid . '/edit');
$this
->isAgreementPage($this->agreement);
$this
->submitAgreementFormWithAgreeing($this->unprivilegedUser->uid);
$this
->assertUrl('node/1', array(), t('User is redirected to node 1: !url', array(
'!url' => $this
->getUrl(),
)));
// Log in, go somewhere other than front page, open agreement, go to node/1.
$this
->unprivilegedUserLogin('user');
$this
->isAgreementPage($this->agreement);
$this
->drupalGet('user/' . $this->unprivilegedUser->uid . '/edit');
$this
->isAgreementPage($this->agreement);
$this
->submitAgreementFormWithAgreeing($this->unprivilegedUser->uid);
$this
->assertUrl('node/1', array(), t('User is redirected to node 1: !url', array(
'!url' => $this
->getUrl(),
)));
// @todo: Log in following password reset link, go somewhere other than
// front page, open agreement, go to user profile.
}
}
/**
* Tests the agreement functionality for an user with "bypass agreement".
*/
class AgreementBypassUserTestCase extends AgreementTestCase {
/**
* {@inheritdoc}
*/
public static function getInfo() {
return array(
'name' => '4. Agreement page - bypass',
'description' => 'Ensure that user with bypass agreement will not see agreement page.',
'group' => 'Agreement',
);
}
/**
* Tests that the agreement page is not displayed for the user.
*/
public function testAgreement() {
// Sent to agreement page.
$this
->unprivilegedUserLogin();
$this
->isAgreementPage($this->agreement);
// Not sent to agreement page.
$this
->bypassUserLogin();
$this
->assertUrl('user/' . $this->bypassUser->uid);
}
}
/**
* Tests multiple agreement functionality.
*/
class AgreementMultipleTestCase extends AgreementTestCase {
/**
* {@inheritdoc}
*/
public static function getInfo() {
return array(
'name' => '5. Multiple agreements',
'description' => 'Ensure that multiple agreements functional together.',
'group' => 'Agreement',
);
}
/**
* Tests that multiple agreements can function independently.
*/
public function testAgreement() {
$page_title = $this
->randomString(10);
$page_text = $this
->randomString(50);
$settings = array(
'name' => 'new_agreement',
'type' => 'New Agreement',
'path' => 'new-agreement',
'settings[title]' => $page_title,
'agreement[value]' => $page_text,
'settings[visibility_settings]' => 1,
'settings[visibility_pages]' => 'node/' . $this->node->nid,
);
$this
->drupalPost('admin/config/people/agreement/add', $settings, t('Save configuration'));
$this
->assertText('Agreement type ' . $settings['type'] . ' saved successfully.', t('Second agreement created successfully.'));
// The static cache needs to be reset here as this is a different request.
$new_agreement = agreement_type_load($settings['name'], FALSE);
// Go to front page, no agreement.
$this
->drupalGet('node');
$this
->isNotAgreementPage($this->agreement);
$this
->isNotAgreementpage($new_agreement);
// Go anywhere else, open agreement.
$this
->drupalGet('admin');
$this
->isAgreementPage($this->agreement);
$this
->isNotAgreementPage($new_agreement);
// Agreement with visibility settings for all pages displays instead of
// agreement with explicity visibility page settings.
$this
->drupalGet('node/' . $this->node->nid);
$this
->isAgreementPage($this->agreement);
$this
->isNotAgreementPage($new_agreement);
// Accept the agreement.
$edit = array(
'agree' => 1,
);
$this
->drupalPost(NULL, $edit, t('Submit'));
$this
->isNotAgreementPage($this->agreement);
// Go to the node again, which is second agreement page.
$this
->drupalGet('node/' . $this->node->nid);
$this
->isAgreementPage($new_agreement);
$this
->isNotAgreementPage($this->agreement);
// Accept the second agreement.
$this
->drupalPost(NULL, $edit, $new_agreement['settings']['submit']);
$this
->isNotAgreementPage($new_agreement);
$this
->isNotAgreementPage($this->agreement);
// Remove the second agreement.
$this
->drupalLogin($this->privilegedUser);
$this
->drupalPost('admin/config/people/agreement/manage/' . $settings['name'] . '/delete', array(), t('Confirm'));
$agreement_type_delete_success = 'Successfully deleted agreement type, ' . $settings['type'];
$this
->assertText($agreement_type_delete_success, 'Found agreement type deleted message.');
}
}
/**
* Tests revoke agreement functionality.
*/
class AgreementRevokeTestCase extends AgreementTestCase {
protected $revokeUser;
/**
* {@inheritdoc}
*/
public static function getInfo() {
return array(
'name' => '6. Agreement page - revoke',
'description' => 'Tests the "revoke agreement" functionality',
'group' => 'Agreement',
);
}
/**
* Asserts that a user that has agreed can revoke the agreement.
*/
public function testAgreement() {
$this
->revokeUserLogin();
// Try submitting agreement form.
$this
->submitAgreementFormWithAgreeing($this->revokeUser->uid);
// Revokes the agreement.
$this
->submitAgreementFormWithRevoking();
// Assert agreement page after revoking.
$this
->drupalGet('node/' . $this->node->nid);
$this
->isAgreementPage($this->agreement);
// Assert that the user can re-accept the agreement.
$this
->submitAgreementFormWithAgreeing($this->revokeUser->uid);
}
/**
* Submits agreement form using the Revoke button.
*/
protected function submitAgreementFormWithRevoking() {
// Submit with checking the I agree box.
$field_values['agree'] = FALSE;
$this
->drupalPost('agreement', $field_values, $this->agreement['settings']['submit']);
$this
->assertText($this->agreement['settings']['revoked'], t('Correct success message'));
}
/**
* Creates an user with the "revoke own agreement" permission and login.
*
* @param string $destination
* An optional destination to go to on login.
*/
protected function revokeUserLogin($destination = 'user') {
$this->revokeUser = $this
->drupalCreateUser(array(
'access content',
'revoke own agreement',
));
$this
->drupalLogin($this->revokeUser, $destination);
}
}
/**
* Tests multiple roles applying to an agreement.
*/
class AgreementMultipleRoleTestCase extends AgreementTestCase {
/**
* First required role ID.
*
* @var int
*/
protected $requiredRole;
/**
* Second required role ID.
*
* @var int
*/
protected $requiredSecondRole;
/**
* {@inheritdoc}
*/
public static function getInfo() {
return array(
'name' => '7. Agreement page - Multiple roles',
'description' => 'Tests an agreement that applies to multiple roles.',
'group' => 'Agreement',
);
}
/**
* {@inheritdoc}
*/
public function setUp() {
parent::setUp();
// Create the roles.
$this->requiredRole = $this
->drupalCreateRole([
'access content',
], 'first role');
$this->requiredSecondRole = $this
->drupalCreateRole([
'access content',
'create page content',
], 'second role');
// Set the agreement to use the roles from the two required users.
$this
->drupalLogin($this->privilegedUser);
$role_ids = array(
$this->requiredRole,
$this->requiredSecondRole,
);
$settings = array(
'settings[role][]' => $role_ids,
);
$this
->configureAgreementSettings($settings);
$this->agreement = agreement_type_load('default', FALSE);
$this
->assertEqual($this->agreement['settings']['role'], $role_ids, 'Agreement roles saved successfully.');
$this
->drupalLogout();
}
/**
* Asserts that the user with the first role gets the agreement page.
*/
public function testAgreementForFirstRole() {
// Creates user, login and assert agreement page.
$requiredUser = $this
->requiredUserLogin($this->requiredRole, 'first role');
$this
->isAgreementPage($this->agreement);
// Go to front page, no agreement.
$this
->drupalGet('node');
$this
->isNotAgreementPage($this->agreement);
// Go anywhere else, open agreement.
$this
->drupalGet('user/' . $requiredUser->uid);
$this
->isAgreementPage($this->agreement);
// Try submitting agreement form.
$this
->submitAgreementFormWithoutAgreeing($requiredUser->uid);
$this
->submitAgreementFormWithAgreeing($requiredUser->uid);
}
/**
* Asserts that the user with the second role gets the agreement page.
*/
public function testAgreementForSecondRole() {
// Creates user, login and assert agreement page.
$requiredUser = $this
->requiredUserLogin($this->requiredSecondRole, 'second role');
$this
->isAgreementPage($this->agreement);
// Go to front page, no agreement.
$this
->drupalGet('node');
$this
->isNotAgreementPage($this->agreement);
// Go anywhere else, open agreement.
$this
->drupalGet('user/' . $requiredUser->uid);
$this
->isAgreementPage($this->agreement);
// Try submitting agreement form.
$this
->submitAgreementFormWithoutAgreeing($requiredUser->uid);
$this
->submitAgreementFormWithAgreeing($requiredUser->uid);
}
/**
* Asserts that user does not get agreement page without roles.
*/
public function testNoAgreementWithoutRole() {
// Creates user, login and assert no agreement page.
$this
->unprivilegedUserLogin();
$this
->isNotAgreementPage($this->agreement);
// Go to front page, no agreement.
$this
->drupalGet('node');
$this
->isNotAgreementPage($this->agreement);
// Go anywhere else, no agreement.
$this
->drupalGet('user/' . $this->unprivilegedUser->uid);
$this
->isNotAgreementPage($this->agreement);
}
/**
* Creates user with role and login.
*
* @param int $role_id
* The role ID to apply.
* @param string $role_name
* the role name.
*
* @return object
* The required user account.
*/
protected function requiredUserLogin($role_id, $role_name) {
$account = $this
->drupalCreateUser();
$account->roles[$role_id] = $role_name;
try {
user_save($account);
} catch (Exception $e) {
$this
->fail('Exception thrown: ' . $e
->getMessage());
}
$this
->drupalLogin($account);
return $account;
}
}
Classes
Name | Description |
---|---|
AgreementBypassUserTestCase | Tests the agreement functionality for an user with "bypass agreement". |
AgreementCustomUnprivilegedUserTestCase | Tests the agreement functionality with custom settings. |
AgreementDefaultsPrivilegedUserTestCase | Tests agreement functionality with default use case and a privileged user. |
AgreementDefaultsUnprivilegedUserTestCase | Tests the agreement functionality with an unprivileged user. |
AgreementMultipleRoleTestCase | Tests multiple roles applying to an agreement. |
AgreementMultipleTestCase | Tests multiple agreement functionality. |
AgreementRevokeTestCase | Tests revoke agreement functionality. |
AgreementTestCase | Agreement base test class. |