You are here

public function DuplicateRoleTest::testDuplicateRoleUi in Duplicate role 8

Tests the "Duplicate" functionality via UI interaction.

File

tests/src/Functional/DuplicateRoleTest.php, line 67

Class

DuplicateRoleTest
Functional tests for Duplicate Role module.

Namespace

Drupal\Tests\duplicate_role\Functional

Code

public function testDuplicateRoleUi() {
  $this
    ->drupalLogin($this
    ->createUser([
    'administer permissions',
    'administer duplicate role',
  ]));
  $this
    ->drupalGet('/admin/people/roles');

  // Verify "Duplicate role" local action.
  $this
    ->clickLink('Duplicate role');
  $this
    ->assertSession()
    ->addressEquals('/admin/people/roles/duplicate');
  $this
    ->assertSession()
    ->elementExists('css', 'select[name="base_role"]');
  $base_role = Role::load('authenticated');
  user_role_grant_permissions($base_role
    ->id(), [
    'administer duplicate role',
  ]);
  $new_role_name = '123';
  $edit = [
    'base_role' => $base_role
      ->id(),
    'label' => $new_role_name,
    'id' => $new_role_name,
  ];
  $this
    ->submitForm($edit, 'Duplicate');
  $this
    ->assertSession()
    ->responseContains(t('Role %label has been added.', [
    '%label' => 123,
  ]));
  $this
    ->assertSession()
    ->addressEquals('/admin/people/roles');

  /** @var \Drupal\user\RoleInterface $new_role */
  $new_role = Role::load($new_role_name);
  $this
    ->assertIsObject($new_role);

  // Verify that new role have the same permissions.
  $this
    ->assertEquals($new_role
    ->getPermissions(), $base_role
    ->getPermissions());
  $new_role
    ->hasPermission('administer duplicate role');

  // Verify "Duplicate" entity operation.
  $this
    ->drupalGet('/admin/people/roles');

  // Click on the first "Duplicate" link, which should be operation for
  // the "Anonymous" user role.
  $this
    ->clickLink('Duplicate');
  $this
    ->assertSession()
    ->addressEquals('/admin/people/roles/duplicate/anonymous');

  // Select list should not be visible.
  $this
    ->assertSession()
    ->elementNotExists('css', 'select[name="base_role"]');
  $new_role_name = 'copy';
  $edit = [
    'label' => $new_role_name,
    'id' => $new_role_name,
  ];
  $this
    ->submitForm($edit, 'Duplicate');
  $this
    ->assertSession()
    ->responseContains(t('Role %label has been added.', [
    '%label' => 'copy',
  ]));
  $new_role = Role::load($new_role_name);
  $this
    ->assertIsObject($new_role);

  // Verify the form validation.
  $this
    ->drupalGet('/admin/people/roles/duplicate');
  $edit = [
    'base_role' => $base_role
      ->id(),
    'label' => $new_role_name,
    'id' => $new_role_name,
  ];
  $this
    ->submitForm($edit, 'Duplicate');
  $this
    ->assertSession()
    ->responseContains('The machine-readable name is already in use. It must be unique.');
}