View source
<?php
namespace Drupal\Tests\user\Kernel\Migrate\d6;
use Drupal\user\Entity\Role;
use Drupal\user\RoleInterface;
use Drupal\Tests\migrate_drupal\Kernel\d6\MigrateDrupal6TestBase;
use Drupal\migrate\Plugin\MigrateIdMapInterface;
class MigrateUserRoleTest extends MigrateDrupal6TestBase {
protected function setUp() {
parent::setUp();
$this
->executeMigrations([
'd6_filter_format',
'd6_user_role',
]);
}
protected function assertRole($id, array $permissions, $lookupId, MigrateIdMapInterface $id_map) {
$role = Role::load($id);
$this
->assertInstanceOf(RoleInterface::class, $role);
sort($permissions);
$this
->assertSame($permissions, $role
->getPermissions());
$this
->assertSame([
[
$id,
],
], $id_map
->lookupDestinationIds([
'rid' => $lookupId,
]));
}
protected function assertRoles(MigrateIdMapInterface $id_map) {
$permissions = [
'access content',
'migrate test anonymous permission',
'use text format filtered_html',
];
$this
->assertRole('anonymous', $permissions, 1, $id_map);
$permissions = [
'access comments',
'access content',
'migrate test authenticated permission',
'post comments',
'skip comment approval',
'use text format filtered_html',
];
$this
->assertRole('authenticated', $permissions, 2, $id_map);
$permissions = [
'migrate test role 1 test permission',
'use text format full_html',
'use text format php_code',
];
$this
->assertRole('migrate_test_role_1', $permissions, 3, $id_map);
$permissions = [
'migrate test role 2 test permission',
'use PHP for settings',
'administer contact forms',
'skip comment approval',
'edit own blog content',
'edit any blog content',
'delete own blog content',
'delete any blog content',
'create forum content',
'delete any forum content',
'delete own forum content',
'edit any forum content',
'edit own forum content',
'administer nodes',
'access content overview',
'use text format php_code',
];
$this
->assertRole('migrate_test_role_2', $permissions, 4, $id_map);
$permissions = [
'use text format php_code',
];
$this
->assertRole('migrate_test_role_3_that_is_longer_than_thirty_two_characters', $permissions, 5, $id_map);
}
public function testUserRole() {
$id_map = $this
->getMigration('d6_user_role')
->getIdMap();
$this
->assertRoles($id_map);
$roles = [
'anonymous1',
'authenticated1',
'administrator1',
'migrate_test_role_11',
'migrate_test_role_21',
'migrate_test_role_3_that_is_longer_than_thirty_two_characters1',
];
$this
->assertEmpty(Role::loadMultiple($roles));
$this->sourceDatabase
->insert('role')
->fields([
'rid' => 6,
'name' => 'migrate test role 4',
])
->execute();
$this->sourceDatabase
->insert('permission')
->fields([
'pid' => 7,
'rid' => 6,
'perm' => 'access content',
'tid' => 0,
])
->execute();
$id_map
->delete([
'rid' => 3,
]);
$this
->executeMigration('d6_user_role');
$roles[] = 'migrate_test_role_41';
$this
->assertEmpty(Role::loadMultiple($roles));
$this
->assertRoles($id_map);
$permissions = [
'access content',
];
$this
->assertRole('migrate_test_role_4', $permissions, 6, $id_map);
}
}