You are here

public function EntityLookupAccessTest::testEntityLookupAccessCheck in Migrate Plus 8.5

Tests that access is honored for entity lookups.

File

tests/src/Kernel/EntityLookupAccessTest.php, line 67

Class

EntityLookupAccessTest
Tests entity lookup access check.

Namespace

Drupal\Tests\migrate_plus\Kernel

Code

public function testEntityLookupAccessCheck() : void {
  $definition = [
    'source' => [
      'plugin' => 'embedded_data',
      'data_rows' => [
        [
          'id' => 1,
        ],
      ],
      'ids' => [
        'id' => [
          'type' => 'integer',
        ],
      ],
    ],
    'process' => [],
    'destination' => [
      'plugin' => 'entity:entity_test',
    ],
  ];
  $migration = \Drupal::service('plugin.manager.migration')
    ->createStubMigration($definition);
  $executable = new MigrateExecutable($migration);
  $row = new Row();
  $configuration_base = [
    'entity_type' => 'entity_test',
    'value_key' => 'id',
  ];

  // Set access_check true.
  $configuration = $configuration_base + [
    'access_check' => TRUE,
  ];

  // Test as anonymous.
  $anonymous = User::getAnonymousUser();
  $this
    ->setCurrentUser($anonymous);
  $plugin = \Drupal::service('plugin.manager.migrate.process')
    ->createInstance('entity_lookup', $configuration, $migration);

  // Check the entity is not found.
  $value = $plugin
    ->transform($this->entity
    ->id(), $executable, $row, 'id');
  $this
    ->assertNull($value);

  // Test as authenticated user.
  $this
    ->setCurrentUser($this->user);
  $plugin = \Drupal::service('plugin.manager.migrate.process')
    ->createInstance('entity_lookup', $configuration, $migration);

  // Check the entity is found.
  $value = $plugin
    ->transform($this->entity
    ->id(), $executable, $row, 'id');
  $this
    ->assertSame($this->entity
    ->id(), $value);

  // Retest with access check false.
  $configuration = $configuration_base + [
    'access_check' => FALSE,
  ];
  $plugin = \Drupal::service('plugin.manager.migrate.process')
    ->createInstance('entity_lookup', $configuration, $migration);

  // Check the entity is found.
  $value = $plugin
    ->transform($this->entity
    ->id(), $executable, $row, 'id');
  $this
    ->assertSame($this->entity
    ->id(), $value);
}