You are here

public function MigrateUserRoleTest::testUserRole in Drupal 9

Same name in this branch
  1. 9 core/modules/user/tests/src/Kernel/Migrate/d6/MigrateUserRoleTest.php \Drupal\Tests\user\Kernel\Migrate\d6\MigrateUserRoleTest::testUserRole()
  2. 9 core/modules/user/tests/src/Kernel/Migrate/d7/MigrateUserRoleTest.php \Drupal\Tests\user\Kernel\Migrate\d7\MigrateUserRoleTest::testUserRole()
Same name and namespace in other branches
  1. 8 core/modules/user/tests/src/Kernel/Migrate/d7/MigrateUserRoleTest.php \Drupal\Tests\user\Kernel\Migrate\d7\MigrateUserRoleTest::testUserRole()

Tests user role migration.

File

core/modules/user/tests/src/Kernel/Migrate/d7/MigrateUserRoleTest.php, line 56

Class

MigrateUserRoleTest
Upgrade user roles to user.role.*.yml.

Namespace

Drupal\Tests\user\Kernel\Migrate\d7

Code

public function testUserRole() {
  $this
    ->assertEntity('anonymous', 'anonymous user', 1);
  $this
    ->assertEntity('authenticated', 'authenticated user', 2);
  $this
    ->assertEntity('administrator', 'administrator', 3);

  // Test there are no duplicated roles.
  $roles = [
    'anonymous1',
    'authenticated1',
    'administrator1',
  ];
  $this
    ->assertEmpty(Role::loadMultiple($roles));

  // Remove the map row for the administrator role and rerun the migration.
  // This will re-import the administrator role again.
  $id_map = $this
    ->getMigration('d7_user_role')
    ->getIdMap();
  $id_map
    ->delete([
    'rid' => 3,
  ]);
  $this->sourceDatabase
    ->insert('role')
    ->fields([
    'rid' => 4,
    'name' => 'test role',
    'weight' => 10,
  ])
    ->execute();
  $this->sourceDatabase
    ->insert('role_permission')
    ->fields([
    'rid' => 4,
    'permission' => 'access content',
    'module' => 'node',
  ])
    ->execute();
  $this
    ->executeMigration('d7_user_role');

  // Test there are no duplicated roles.
  $roles = [
    'anonymous1',
    'authenticated1',
    'administrator1',
  ];
  $this
    ->assertEmpty(Role::loadMultiple($roles));

  // Test that the existing roles have not changed.
  $this
    ->assertEntity('administrator', 'administrator', 3);
  $this
    ->assertEntity('anonymous', 'anonymous user', 1);
  $this
    ->assertEntity('authenticated', 'authenticated user', 2);

  // Test the migration of the new role, test role.
  $this
    ->assertEntity('test_role', 'test role', 4);
}