simplenews_roles.test in Simplenews Roles 7
Same filename and directory in other branches
SimplenewsRoles test functions.
File
tests/simplenews_roles.testView source
<?php
/**
* @file
* SimplenewsRoles test functions.
*
* @ingroup simplenews_roles
*/
class SimplenewsRolesTestCase extends SimplenewsTestCase {
public function setUp() {
parent::setUp(array(
'simplenews_roles',
));
}
}
class SimplenewsRolesUserRegisterTestCase extends SimplenewsRolesTestCase {
public static function getInfo() {
return array(
'name' => t('User Register'),
'description' => t('Check subscription after registration.'),
'group' => t('Simplenews Roles'),
);
}
/**
* Check if the user subscribes to all newsletters from his role.
*
* Steps performed:
* 1. Create an admin user,
* 2. Create a newsletter category,
* 3. Find $tid based on the category name,
* 4. Create a newsletter,
* 5. Add roles to the newsletter,
* 6. Logout as admin user,
* 7. Create a user,
* 8. Check if the user has subscribed to the appropriate newsletters.
*/
function testUserRegister() {
// Create an admin user.
$permissions = array(
'administer content types',
'administer nodes',
'access administration pages',
'administer newsletters',
'administer simplenews settings',
'administer simplenews subscriptions',
'create simplenews content',
'edit own simplenews content',
);
$admin_user = $this
->drupalCreateUser($permissions);
$this
->drupalLogin($admin_user);
// Create a newsletter category.
$this
->drupalGet('admin/config/services/simplenews');
$this
->clickLink(t('Add newsletter category'));
$name = $this
->randomName();
$edit = array(
'name' => $name,
'description' => $this
->randomString(20),
);
$this
->drupalPost(NULL, $edit, t('Save'));
// Find $tid based on the category name.
drupal_static_reset('simplenews_categories_load_multiple');
$categories = simplenews_categories_load_multiple();
$tid = NULL;
foreach ($categories as $category) {
if ($category->name == $name) {
$tid = $category->tid;
}
}
// Create a newsletter.
$this
->drupalGet('node/add/simplenews');
$this
->assertText(t('Newsletter category'));
$edit = array(
'title' => $this
->randomName(),
'field_simplenews_term[und]' => $tid,
);
$this
->drupalPost(NULL, $edit, 'Save');
// Add roles to the newsletter.
$roles = array_keys(user_roles(TRUE));
$edit = array();
foreach ($roles as $role) {
$edit['roles[' . $role . ']'] = $role;
}
$this
->drupalPost('admin/config/services/simplenews/categories/' . $tid . '/edit', $edit, t('Save'));
// Logout as admin user.
$this
->drupalLogout();
// Create a user.
$user = $this
->drupalCreateUser();
// Check if the user has subscribed to the appropriate newsletters.
$this
->assertTrue(simplenews_user_is_subscribed($user->mail, $tid), t('Subscription is successful.'));
}
}
class SimplenewsRolesUpdateProfileTestCase extends SimplenewsRolesTestCase {
public static function getInfo() {
return array(
'name' => t('Update User Profile'),
'description' => t('Check subscription after editing profile (assign/unassign role).'),
'group' => t('Simplenews Roles'),
);
}
/**
* Check that the user has subscribed to appropriate newsletters
* after the new role assigned or existing unassigned.
*
* Steps performed:
* 1. Create an admin user,
* 2. Create a newsletter category,
* 3. Find $tid based on the category name,
* 4. Create a newsletter,
* 5. Create a new role,
* 6. Add role to the newsletter,
* 7. Logout as admin user,
* 8. Create a new user,
* 9. Load real user object,
* 5. Check if the new user subscribed to newsletter, it should not be,
* 6. Assign a "newsletter" role to user,
* 7. Check again if the user has subscribed, it should be,
* 8. Unassign role,
* 9. Check if the user has unsubscribed.
*/
function testUpdateProfileRole() {
// Create an admin user.
$permissions = array(
'administer content types',
'administer nodes',
'access administration pages',
'administer newsletters',
'administer simplenews settings',
'administer simplenews subscriptions',
'create simplenews content',
'edit own simplenews content',
);
$admin_user = $this
->drupalCreateUser($permissions);
$this
->drupalLogin($admin_user);
// Create a newsletter category.
$this
->drupalGet('admin/config/services/simplenews');
$this
->clickLink(t('Add newsletter category'));
$name = $this
->randomName();
$edit = array(
'name' => $name,
'description' => $this
->randomString(20),
);
$this
->drupalPost(NULL, $edit, t('Save'));
// Find $tid based on the category name.
drupal_static_reset('simplenews_categories_load_multiple');
$categories = simplenews_categories_load_multiple();
$tid = NULL;
foreach ($categories as $category) {
if ($category->name == $name) {
$tid = $category->tid;
}
}
// Create a newsletter.
$this
->drupalGet('node/add/simplenews');
$this
->assertText(t('Newsletter category'));
$edit = array(
'title' => $this
->randomName(),
'field_simplenews_term[und]' => $tid,
);
$this
->drupalPost(NULL, $edit, 'Save');
// Create a new role.
$role = new stdClass();
$role->name = 'newsletter';
$role->weight = 3;
user_role_save($role);
$permissions = array(
'administer newsletters',
'administer simplenews settings',
'administer simplenews subscriptions',
);
$role = user_role_load_by_name($role->name);
user_role_grant_permissions($role->rid, $permissions);
// Add role to the newsletter.
$edit = array();
$edit['roles[' . $role->rid . ']'] = $role->rid;
$this
->drupalPost('admin/config/services/simplenews/categories/' . $tid . '/edit', $edit, t('Save'));
// Logout as admin user.
$this
->drupalLogout();
// Create a new user.
$user = $this
->drupalCreateUser(array(
'administer permissions',
'administer users',
));
$this
->drupalLogin($user);
// Load real user object.
$user = user_load($user->uid, TRUE);
// Let's check if the new user subscribed to "$tid" newsletter.
// It should not be.
$this
->assertFalse(simplenews_user_is_subscribed($user->mail, $tid), t('User does not have a subscription.'));
// Assign role.
$edit = array();
$edit['roles[' . $role->rid . ']'] = $role->rid;
$this
->drupalPost('user/' . $user->uid . '/edit', $edit, t('Save'));
// Check if the user has subscribed (should be).
drupal_static_reset('simplenews_user_is_subscribed');
$this
->assertTrue(simplenews_user_is_subscribed($user->mail, $tid), t('Successfully subscribed.'));
// Unassign role.
$edit = array();
$edit['roles[' . $role->rid . ']'] = FALSE;
$this
->drupalPost('user/' . $user->uid . '/edit', $edit, t('Save'));
// Check if the user has unsubscribed (should be).
drupal_static_reset('simplenews_user_is_subscribed');
$this
->assertFalse(simplenews_user_is_subscribed($user->mail, $tid), t('Successfully unsubscribed.'));
}
}
class SimplenewsRolesUserDeleteTestCase extends SimplenewsRolesTestCase {
public static function getInfo() {
return array(
'name' => t('User Delete'),
'description' => t('Check that the subscription is deleted after the user is deleted.'),
'group' => t('Simplenews Roles'),
);
}
/*
* Check that the subscription is deleted after the user is deleted.
*
* @todo - do we need this test case, becase this functionality should be
* handled by simplenews module and not simplenews_roles module?
*
* Steps performed:
* 1. Set cancel method,
* 2. Create an admin user,
* 3. Create a newsletter category,
* 4. Find $tid based on the category name,
* 5. Create a newsletter,
* 6. Add roles to the newsletter,
* 7. Logout as admin user,
* 8. Create a new user,
* 9. Load real user object,
* 10. Check if the user has subscribed to the appropriate newsletters,
* 11. Cancel account,
* 12. Check subscription again.
*/
function testUserDelete() {
// Set cancel method.
variable_set('user_cancel_method', 'user_cancel_delete');
// Create an admin user.
$permissions = array(
'administer content types',
'administer nodes',
'access administration pages',
'administer newsletters',
'administer simplenews settings',
'administer simplenews subscriptions',
'create simplenews content',
'edit own simplenews content',
);
$admin_user = $this
->drupalCreateUser($permissions);
$this
->drupalLogin($admin_user);
// Create a newsletter category.
$this
->drupalGet('admin/config/services/simplenews');
$this
->clickLink(t('Add newsletter category'));
$name = $this
->randomName();
$edit = array(
'name' => $name,
'description' => $this
->randomString(20),
);
$this
->drupalPost(NULL, $edit, t('Save'));
// Find $tid based on the category name.
drupal_static_reset('simplenews_categories_load_multiple');
$categories = simplenews_categories_load_multiple();
$tid = NULL;
foreach ($categories as $category) {
if ($category->name == $name) {
$tid = $category->tid;
}
}
// Create a newsletter.
$this
->drupalGet('node/add/simplenews');
$this
->assertText(t('Newsletter category'));
$edit = array(
'title' => $this
->randomName(),
'field_simplenews_term[und]' => $tid,
);
$this
->drupalPost(NULL, $edit, 'Save');
// Add roles to the newsletter.
$roles = array_keys(user_roles(TRUE));
$edit = array();
foreach ($roles as $role) {
$edit['roles[' . $role . ']'] = $role;
}
$this
->drupalPost('admin/config/services/simplenews/categories/' . $tid . '/edit', $edit, t('Save'));
// Logout as admin user.
$this
->drupalLogout();
// Create a new user.
$user = $this
->drupalCreateUser(array(
'cancel account',
));
$this
->drupalLogin($user);
// Load real user object.
$user = user_load($user->uid, TRUE);
// Check if the user has subscribed to the appropriate newsletters.
$this
->assertTrue(simplenews_user_is_subscribed($user->mail, $tid), t('Subscription is successful.'));
// Then we have to cancel account and check if the subscription is deleted.
$this
->drupalGet('user/' . $user->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.'));
// 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/{$user->uid}/cancel/confirm/{$timestamp}/" . user_pass_rehash($user->pass, $timestamp, $user->login));
$this
->assertFalse(user_load($user->uid, TRUE), t('User is not found in the database.'));
// Check subscription again.
drupal_static_reset('simplenews_user_is_subscribed');
$this
->assertFalse(simplenews_user_is_subscribed($user->mail, $tid), t('Subscription is successfully deleted.'));
}
}
class SimplenewsRolesResyncTestCase extends SimplenewsRolesTestCase {
public static function getInfo() {
return array(
'name' => t('Resync'),
'description' => t('Test to check the general behavior of simplenews_roles module and simplenews_roles_update_subscriptions() method.'),
'group' => t('Simplenews Roles'),
);
}
/*
* Test regular subscription is still working and no accidental unsubscribes happen.
*
* Steps performed:
* 1. Create an admin user,
* 2. Create a test user and subscribe to default newsletter. Subscribe random mail.
* 3. Update newsletter settings. Run Cron.
* 4. Check user is still subscribed.
*/
function testRegular() {
// Find $tid based on the category name.
$tid = $this
->getRandomNewsletter();
// 1. Create an admin user,
$permissions = array(
'administer content types',
'administer nodes',
'access administration pages',
'administer newsletters',
'administer simplenews settings',
'administer simplenews subscriptions',
'create simplenews content',
'edit own simplenews content',
);
$admin_user = $this
->drupalCreateUser($permissions);
$this
->drupalLogin($admin_user);
$this
->drupalLogout();
// 2. Create a test user and subscribe to default newsletter. Subscribe random mail.
$user_1 = $this
->drupalCreateUser(array(
'administer permissions',
'administer users',
));
$this
->drupalLogin($user_1);
$edit = array();
$this
->drupalPost('user/' . $user_1->uid . '/edit', $edit, t('Save'));
simplenews_subscribe_user($user_1->mail, $tid, FALSE, 'test');
$this
->drupalLogout();
$this
->drupalLogin($admin_user);
$mail_1 = $this
->randomEmail();
simplenews_subscribe_user($mail_1, $tid, FALSE, 'test');
// Check user is already subscribed.
drupal_static_reset('simplenews_user_is_subscribed');
drupal_static_reset('simplenews_categories_load_multiple');
$this
->assertTrue(simplenews_user_is_subscribed($user_1->mail, $tid), t('User (1) has subscribed.'));
$this
->assertTrue(simplenews_user_is_subscribed($mail_1, $tid), t('Mail (1) has subscribed.'));
// 3. Update newsletter settings. Run Cron.
$this
->drupalGet('admin/config/services/simplenews');
$this
->clickLink(t('edit newsletter category'));
// Trigger Save
$edit = array(
'from_address' => 'example@example.com',
);
$this
->drupalPost(NULL, $edit, t('Save'));
// Make sure to run cron to allow processing everything (accidentally).
simplenews_roles_cron();
// 4. Check user is still subscribed.
drupal_static_reset('simplenews_user_is_subscribed');
drupal_static_reset('simplenews_categories_load_multiple');
$this
->assertTrue(simplenews_user_is_subscribed($user_1->mail, $tid), t('User (1) has still subscribed.'));
$this
->assertTrue(simplenews_user_is_subscribed($mail_1, $tid), t('Mail (1) has still subscribed.'));
}
/*
* Test to check the general behavior of simplenews_roles module
* and simplenews_roles_update_subscriptions() method.
*
* Steps performed:
* 1. Create an admin user,
* 2. Create role "subscriber to list x",
* 3. Create three users and assign them "subscriber to list x" role,
* 4. Create newsletter category,
* 5. Add user 4 to list, add anonymous subscribers also,
* 6. Setup sync (list => role), trigger sync,
* 7. Make sure user 4 got removed and users 1..3 are in list,
* 8. Also anonymous subscribers need to be removed.
*/
function testResync() {
// Create an admin user.
$permissions = array(
'administer content types',
'administer nodes',
'access administration pages',
'administer newsletters',
'administer simplenews settings',
'administer simplenews subscriptions',
'create simplenews content',
'edit own simplenews content',
);
$admin_user = $this
->drupalCreateUser($permissions);
$this
->drupalLogin($admin_user);
// Create role "subscriber to list x".
$role = new stdClass();
$role->name = 'subscriber to list x';
$role->weight = 3;
user_role_save($role);
$permissions = array(
'administer newsletters',
'administer simplenews settings',
'administer simplenews subscriptions',
);
$role = user_role_load_by_name($role->name);
user_role_grant_permissions($role->rid, $permissions);
$this
->drupalLogout();
// Create three users and assign them "subscriber to list x" role.
$user_1 = $this
->drupalCreateUser(array(
'administer permissions',
'administer users',
));
$this
->drupalLogin($user_1);
$edit = array();
$edit['roles[' . $role->rid . ']'] = $role->rid;
$this
->drupalPost('user/' . $user_1->uid . '/edit', $edit, t('Save'));
$this
->drupalLogout();
$user_2 = $this
->drupalCreateUser(array(
'administer permissions',
'administer users',
));
$this
->drupalLogin($user_2);
$edit = array();
$edit['roles[' . $role->rid . ']'] = $role->rid;
$this
->drupalPost('user/' . $user_2->uid . '/edit', $edit, t('Save'));
$this
->drupalLogout();
$user_3 = $this
->drupalCreateUser(array(
'administer permissions',
'administer users',
));
$this
->drupalLogin($user_3);
$edit = array();
$edit['roles[' . $role->rid . ']'] = $role->rid;
$this
->drupalPost('user/' . $user_3->uid . '/edit', $edit, t('Save'));
$this
->drupalLogout();
$user_4 = $this
->drupalCreateUser(array(
'administer permissions',
'administer users',
));
// Create newsletter category.
$this
->drupalLogin($admin_user);
$this
->drupalGet('admin/config/services/simplenews');
$this
->clickLink(t('Add newsletter category'));
$name = $this
->randomName();
$edit = array(
'name' => $name,
'description' => $this
->randomString(20),
);
$this
->drupalPost(NULL, $edit, t('Save'));
// Find $tid based on the category name.
drupal_static_reset('simplenews_categories_load_multiple');
$categories = simplenews_categories_load_multiple();
$tid = NULL;
foreach ($categories as $category) {
if ($category->name == $name) {
$tid = $category->tid;
}
}
// Add user 4 to list, add anonymous subscribers also.
$this
->drupalGet('admin/people/simplenews');
$this
->clickLink(t('Mass subscribe'));
$anonymous = array();
$anonymous[] = $this
->randomName() . '@' . $this
->randomName() . '.com';
$anonymous[] = $this
->randomName() . '@' . $this
->randomName() . '.com';
$anonymous[] = $this
->randomName() . '@' . $this
->randomName() . '.com';
$edit = array(
'emails' => $user_4->mail . ',' . rtrim(implode(',', $anonymous), ','),
"newsletters[{$tid}]" => $tid,
);
$this
->drupalPost(NULL, $edit, t('Subscribe'));
// Setup sync (list => role), trigger sync.
$edit = array();
$edit['roles[' . $role->rid . ']'] = $role->rid;
$this
->drupalPost('admin/config/services/simplenews/categories/' . $tid . '/edit', $edit, t('Save'));
drupal_static_reset('simplenews_user_is_subscribed');
// Make sure user 4 got removed and users 1..3 are in list.
$this
->assertFalse(simplenews_user_is_subscribed($user_4->mail, $tid), t('User (4) has been deleted.'));
$this
->assertTrue(simplenews_user_is_subscribed($user_1->mail, $tid), t('User (1) has subscribed.'));
$this
->assertTrue(simplenews_user_is_subscribed($user_2->mail, $tid), t('User (2) has subscribed.'));
$this
->assertTrue(simplenews_user_is_subscribed($user_3->mail, $tid), t('User (3) has subscribed.'));
// Also anonymous subscribers need to be removed.
$this
->assertFalse(simplenews_user_is_subscribed($anonymous[0], $tid), t('Anonymous (1) has been deleted.'));
$this
->assertFalse(simplenews_user_is_subscribed($anonymous[1], $tid), t('Anonymous (2) has been deleted.'));
$this
->assertFalse(simplenews_user_is_subscribed($anonymous[2], $tid), t('Anonymous (3) has been deleted.'));
}
}
Classes
Name | Description |
---|---|
SimplenewsRolesResyncTestCase | |
SimplenewsRolesTestCase | @file SimplenewsRoles test functions. |
SimplenewsRolesUpdateProfileTestCase | |
SimplenewsRolesUserDeleteTestCase | |
SimplenewsRolesUserRegisterTestCase |