Issue #1479454 by Hugo Wetterberg, galooph, dawehner, andypost, marcingy, heyrocker...
[project/drupal.git] / core / modules / user / lib / Drupal / user / Tests / UserRoleAdminTest.php
1 <?php
2
3 /**
4 * @file
5 * Definition of Drupal\user\Tests\UserRoleAdminTest.
6 */
7
8 namespace Drupal\user\Tests;
9
10 use Drupal\simpletest\WebTestBase;
11
12 /**
13 * Test case to test adding, editing and deleting roles.
14 */
15 class UserRoleAdminTest extends WebTestBase {
16
17 public static function getInfo() {
18 return array(
19 'name' => 'User role administration',
20 'description' => 'Test adding, editing and deleting user roles and changing role weights.',
21 'group' => 'User',
22 );
23 }
24
25 function setUp() {
26 parent::setUp();
27 $this->admin_user = $this->drupalCreateUser(array('administer permissions', 'administer users'));
28 }
29
30 /**
31 * Test adding, renaming and deleting roles.
32 */
33 function testRoleAdministration() {
34 $this->drupalLogin($this->admin_user);
35
36 // Test adding a role. (In doing so, we use a role name that happens to
37 // correspond to an integer, to test that the role administration pages
38 // correctly distinguish between role names and IDs.)
39 $role_name = '123';
40 $edit = array('role[label]' => $role_name, 'role[id]' => $role_name);
41 $this->drupalPost('admin/people/roles', $edit, t('Add role'));
42 $this->assertText(t('The role has been added.'), 'The role has been added.');
43 $role = entity_load('user_role', $role_name);
44 $this->assertTrue(is_object($role), 'The role was successfully retrieved from the database.');
45
46 // Try adding a duplicate role.
47 $this->drupalPost(NULL, $edit, t('Add role'));
48 $this->assertRaw(t('The machine-readable name is already in use. It must be unique.'), 'Duplicate role warning displayed.');
49
50 // Test renaming a role.
51 $old_name = $role_name;
52 $role_name = '456';
53 $edit = array('role[label]' => $role_name);
54 $this->drupalPost("admin/people/roles/edit/{$role->id()}", $edit, t('Save role'));
55 $this->assertText(t('The role has been renamed.'), 'The role has been renamed.');
56 $new_role = entity_load('user_role', $old_name);
57 $this->assertEqual($new_role->label(), $role_name, 'The role name has been successfully changed.');
58
59 // Test deleting a role.
60 $this->drupalPost("admin/people/roles/edit/{$role->id()}", NULL, t('Delete role'));
61 $this->drupalPost(NULL, NULL, t('Delete'));
62 $this->assertText(t('The role has been deleted.'), 'The role has been deleted');
63 $this->assertNoLinkByHref("admin/people/roles/edit/{$role->id()}", 'Role edit link removed.');
64 $this->assertFalse(entity_load('user_role', $role_name), 'A deleted role can no longer be loaded.');
65
66 // Make sure that the system-defined roles can be edited via the user
67 // interface.
68 $this->drupalGet('admin/people/roles/edit/' . DRUPAL_ANONYMOUS_RID);
69 $this->assertResponse(200, 'Access granted when trying to edit the built-in anonymous role.');
70 $this->assertNoText(t('Delete role'), 'Delete button for the anonymous role is not present.');
71 $this->drupalGet('admin/people/roles/edit/' . DRUPAL_AUTHENTICATED_RID);
72 $this->assertResponse(200, 'Access granted when trying to edit the built-in authenticated role.');
73 $this->assertNoText(t('Delete role'), 'Delete button for the authenticated role is not present.');
74 }
75
76 /**
77 * Test user role weight change operation and ordering.
78 */
79 function testRoleWeightOrdering() {
80 $this->drupalLogin($this->admin_user);
81 $roles = user_roles();
82 $weight = count($roles);
83 $new_role_weights = array();
84 $saved_rids = array();
85
86 // Change the role weights to make the roles in reverse order.
87 $edit = array();
88 foreach ($roles as $role) {
89 $edit['roles['. $role->id() .'][weight]'] = $weight;
90 $new_role_weights[$role->id()] = $weight;
91 $saved_rids[] = $role->id;
92 $weight--;
93 }
94 $this->drupalPost('admin/people/roles', $edit, t('Save order'));
95 $this->assertText(t('The role settings have been updated.'), 'The role settings form submitted successfully.');
96
97 // Load up the user roles with the new weights.
98 drupal_static_reset('user_roles');
99 $roles = user_roles();
100 $rids = array();
101 // Test that the role weights have been correctly saved.
102 foreach ($roles as $role) {
103 $this->assertEqual($role->weight, $new_role_weights[$role->id()]);
104 $rids[] = $role->id;
105 }
106 // The order of the roles should be reversed.
107 $this->assertIdentical($rids, array_reverse($saved_rids));
108 }
109 }