View source
<?php
namespace Drupal\Tests\user\Kernel\Migrate\d7;
use Drupal\Core\Database\Database;
use Drupal\Tests\migrate_drupal\Kernel\d7\MigrateDrupal7TestBase;
use Drupal\user\Entity\Role;
use Drupal\user\RoleInterface;
class MigrateUserRoleTest extends MigrateDrupal7TestBase {
protected function setUp() : void {
parent::setUp();
$this
->executeMigration('d7_user_role');
}
protected function assertEntity($id, $label, $original_rid) {
$entity = Role::load($id);
$this
->assertInstanceOf(RoleInterface::class, $entity);
$this
->assertSame($label, $entity
->label());
if (isset($original_rid)) {
$permissions = Database::getConnection('default', 'migrate')
->select('role_permission', 'rp')
->fields('rp', [
'permission',
])
->condition('rid', $original_rid)
->execute()
->fetchCol();
sort($permissions);
$this
->assertSame($permissions, $entity
->getPermissions());
}
}
public function testUserRole() {
$this
->assertEntity('anonymous', 'anonymous user', 1);
$this
->assertEntity('authenticated', 'authenticated user', 2);
$this
->assertEntity('administrator', 'administrator', 3);
$roles = [
'anonymous1',
'authenticated1',
'administrator1',
];
$this
->assertEmpty(Role::loadMultiple($roles));
$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');
$roles = [
'anonymous1',
'authenticated1',
'administrator1',
];
$this
->assertEmpty(Role::loadMultiple($roles));
$this
->assertEntity('administrator', 'administrator', 3);
$this
->assertEntity('anonymous', 'anonymous user', 1);
$this
->assertEntity('authenticated', 'authenticated user', 2);
$this
->assertEntity('test_role', 'test role', 4);
}
}