annotate core/modules/user/tests/src/Functional/UserRoleAdminTest.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents af1871eacc83
children
rev   line source
Chris@14 1 <?php
Chris@14 2
Chris@14 3 namespace Drupal\Tests\user\Functional;
Chris@14 4
Chris@14 5 use Drupal\Tests\BrowserTestBase;
Chris@14 6 use Drupal\user\Entity\Role;
Chris@14 7 use Drupal\user\RoleInterface;
Chris@14 8
Chris@14 9 /**
Chris@14 10 * Tests adding, editing and deleting user roles and changing role weights.
Chris@14 11 *
Chris@14 12 * @group user
Chris@14 13 */
Chris@14 14 class UserRoleAdminTest extends BrowserTestBase {
Chris@14 15
Chris@14 16 /**
Chris@14 17 * User with admin privileges.
Chris@14 18 *
Chris@14 19 * @var \Drupal\user\UserInterface
Chris@14 20 */
Chris@14 21 protected $adminUser;
Chris@14 22
Chris@14 23 /**
Chris@14 24 * Modules to enable.
Chris@14 25 *
Chris@14 26 * @var string[]
Chris@14 27 */
Chris@14 28 public static $modules = ['block'];
Chris@14 29
Chris@14 30 /**
Chris@14 31 * {@inheritdoc}
Chris@14 32 */
Chris@14 33 protected function setUp() {
Chris@14 34 parent::setUp();
Chris@14 35 $this->adminUser = $this->drupalCreateUser(['administer permissions', 'administer users']);
Chris@14 36 $this->drupalPlaceBlock('local_tasks_block');
Chris@14 37 }
Chris@14 38
Chris@14 39 /**
Chris@14 40 * Test adding, renaming and deleting roles.
Chris@14 41 */
Chris@14 42 public function testRoleAdministration() {
Chris@14 43 $this->drupalLogin($this->adminUser);
Chris@14 44 $default_langcode = \Drupal::languageManager()->getDefaultLanguage()->getId();
Chris@14 45 // Test presence of tab.
Chris@14 46 $this->drupalGet('admin/people/permissions');
Chris@14 47 $tabs = $this->xpath('//ul[@class=:classes and //a[contains(., :text)]]', [
Chris@14 48 ':classes' => 'tabs primary',
Chris@14 49 ':text' => 'Roles',
Chris@14 50 ]);
Chris@14 51 $this->assertEqual(count($tabs), 1, 'Found roles tab');
Chris@14 52
Chris@14 53 // Test adding a role. (In doing so, we use a role name that happens to
Chris@14 54 // correspond to an integer, to test that the role administration pages
Chris@14 55 // correctly distinguish between role names and IDs.)
Chris@14 56 $role_name = '123';
Chris@14 57 $edit = ['label' => $role_name, 'id' => $role_name];
Chris@14 58 $this->drupalPostForm('admin/people/roles/add', $edit, t('Save'));
Chris@14 59 $this->assertRaw(t('Role %label has been added.', ['%label' => 123]));
Chris@14 60 $role = Role::load($role_name);
Chris@14 61 $this->assertTrue(is_object($role), 'The role was successfully retrieved from the database.');
Chris@14 62
Chris@14 63 // Check that the role was created in site default language.
Chris@14 64 $this->assertEqual($role->language()->getId(), $default_langcode);
Chris@14 65
Chris@14 66 // Try adding a duplicate role.
Chris@14 67 $this->drupalPostForm('admin/people/roles/add', $edit, t('Save'));
Chris@14 68 $this->assertRaw(t('The machine-readable name is already in use. It must be unique.'), 'Duplicate role warning displayed.');
Chris@14 69
Chris@14 70 // Test renaming a role.
Chris@14 71 $role_name = '456';
Chris@14 72 $edit = ['label' => $role_name];
Chris@14 73 $this->drupalPostForm("admin/people/roles/manage/{$role->id()}", $edit, t('Save'));
Chris@14 74 $this->assertRaw(t('Role %label has been updated.', ['%label' => $role_name]));
Chris@14 75 \Drupal::entityManager()->getStorage('user_role')->resetCache([$role->id()]);
Chris@14 76 $new_role = Role::load($role->id());
Chris@14 77 $this->assertEqual($new_role->label(), $role_name, 'The role name has been successfully changed.');
Chris@14 78
Chris@14 79 // Test deleting a role.
Chris@14 80 $this->drupalGet("admin/people/roles/manage/{$role->id()}");
Chris@14 81 $this->clickLink(t('Delete'));
Chris@14 82 $this->drupalPostForm(NULL, [], t('Delete'));
Chris@14 83 $this->assertRaw(t('The role %label has been deleted.', ['%label' => $role_name]));
Chris@14 84 $this->assertNoLinkByHref("admin/people/roles/manage/{$role->id()}", 'Role edit link removed.');
Chris@14 85 \Drupal::entityManager()->getStorage('user_role')->resetCache([$role->id()]);
Chris@14 86 $this->assertFalse(Role::load($role->id()), 'A deleted role can no longer be loaded.');
Chris@14 87
Chris@14 88 // Make sure that the system-defined roles can be edited via the user
Chris@14 89 // interface.
Chris@14 90 $this->drupalGet('admin/people/roles/manage/' . RoleInterface::ANONYMOUS_ID);
Chris@14 91 $this->assertResponse(200, 'Access granted when trying to edit the built-in anonymous role.');
Chris@14 92 $this->assertNoText(t('Delete role'), 'Delete button for the anonymous role is not present.');
Chris@14 93 $this->drupalGet('admin/people/roles/manage/' . RoleInterface::AUTHENTICATED_ID);
Chris@14 94 $this->assertResponse(200, 'Access granted when trying to edit the built-in authenticated role.');
Chris@14 95 $this->assertNoText(t('Delete role'), 'Delete button for the authenticated role is not present.');
Chris@14 96 }
Chris@14 97
Chris@14 98 /**
Chris@14 99 * Test user role weight change operation and ordering.
Chris@14 100 */
Chris@14 101 public function testRoleWeightOrdering() {
Chris@14 102 $this->drupalLogin($this->adminUser);
Chris@14 103 $roles = user_roles();
Chris@14 104 $weight = count($roles);
Chris@14 105 $new_role_weights = [];
Chris@14 106 $saved_rids = [];
Chris@14 107
Chris@14 108 // Change the role weights to make the roles in reverse order.
Chris@14 109 $edit = [];
Chris@14 110 foreach ($roles as $role) {
Chris@14 111 $edit['entities[' . $role->id() . '][weight]'] = $weight;
Chris@14 112 $new_role_weights[$role->id()] = $weight;
Chris@14 113 $saved_rids[] = $role->id();
Chris@14 114 $weight--;
Chris@14 115 }
Chris@14 116 $this->drupalPostForm('admin/people/roles', $edit, t('Save'));
Chris@14 117 $this->assertText(t('The role settings have been updated.'), 'The role settings form submitted successfully.');
Chris@14 118
Chris@14 119 // Load up the user roles with the new weights.
Chris@14 120 $roles = user_roles();
Chris@14 121 $rids = [];
Chris@14 122 // Test that the role weights have been correctly saved.
Chris@14 123 foreach ($roles as $role) {
Chris@14 124 $this->assertEqual($role->getWeight(), $new_role_weights[$role->id()]);
Chris@14 125 $rids[] = $role->id();
Chris@14 126 }
Chris@14 127 // The order of the roles should be reversed.
Chris@14 128 $this->assertIdentical($rids, array_reverse($saved_rids));
Chris@14 129 }
Chris@14 130
Chris@14 131 }