function MigrateEntityContentValidationTest::testEntityOwnerValidation

Same name in other branches
  1. 9 core/modules/migrate/tests/src/Kernel/MigrateEntityContentValidationTest.php \Drupal\Tests\migrate\Kernel\MigrateEntityContentValidationTest::testEntityOwnerValidation()
  2. 11.x core/modules/migrate/tests/src/Kernel/MigrateEntityContentValidationTest.php \Drupal\Tests\migrate\Kernel\MigrateEntityContentValidationTest::testEntityOwnerValidation()

Tests validation for entities that are instances of EntityOwnerInterface.

File

core/modules/migrate/tests/src/Kernel/MigrateEntityContentValidationTest.php, line 165

Class

MigrateEntityContentValidationTest
Tests validation of an entity during migration.

Namespace

Drupal\Tests\migrate\Kernel

Code

public function testEntityOwnerValidation() : void {
    // Text format access is impacted by user permissions.
    $filter_test_format = FilterFormat::load('filter_test');
    assert($filter_test_format instanceof FilterFormatInterface);
    // Create 2 users, an admin user who has permission to use this text format
    // and another who does not have said access.
    $role = Role::create([
        'id' => 'admin',
        'label' => 'admin',
        'is_admin' => TRUE,
    ]);
    assert($role instanceof RoleInterface);
    $role->grantPermission($filter_test_format->getPermissionName());
    $role->save();
    $admin_user = User::create([
        'name' => 'foobar',
        'mail' => 'foobar@example.com',
    ]);
    $admin_user->addRole($role->id())
        ->save();
    $normal_user = User::create([
        'name' => 'normal user',
        'mail' => 'normal@example.com',
    ]);
    $normal_user->save();
    // Add a "body" field with the text format.
    $field_name = $this->randomMachineName();
    $field_storage = FieldStorageConfig::create([
        'field_name' => $field_name,
        'entity_type' => 'entity_test',
        'type' => 'text',
    ]);
    $field_storage->save();
    FieldConfig::create([
        'field_storage' => $field_storage,
        'bundle' => 'entity_test',
    ])->save();
    // Attempt to migrate entities. The first record is owned by an admin user.
    $definition = [
        'source' => [
            'plugin' => 'embedded_data',
            'data_rows' => [
                [
                    'id' => 1,
                    'uid' => $admin_user->id(),
                    'body' => [
                        'value' => 'foo',
                        'format' => 'filter_test',
                    ],
                ],
                [
                    'id' => 2,
                    'uid' => $normal_user->id(),
                    'body' => [
                        'value' => 'bar',
                        'format' => 'filter_test',
                    ],
                ],
            ],
            'ids' => [
                'id' => [
                    'type' => 'integer',
                ],
            ],
        ],
        'process' => [
            'id' => 'id',
            'user_id' => 'uid',
            "{$field_name}/value" => 'body/value',
            "{$field_name}/format" => 'body/format',
        ],
        'destination' => [
            'plugin' => 'entity:entity_test',
            'validate' => TRUE,
        ],
    ];
    $this->container
        ->get('current_user')
        ->setAccount($normal_user);
    $this->runImport($definition);
    // The second user import should fail validation because they do not have
    // access to use "filter_test" filter.
    $this->assertSame(sprintf('2: [entity_test: 2]: user_id.0.target_id=This entity (<em class="placeholder">user</em>: <em class="placeholder">%s</em>) cannot be referenced.||%s.0.format=The value you selected is not a valid choice.', $normal_user->id(), $field_name), $this->messages[0]);
    $this->assertArrayNotHasKey(1, $this->messages);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.