annotate core/modules/user/src/Tests/UserRoleAdminTest.php @ 0:4c8ae668cc8c

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