Commit 84cdd8e2 authored by heddn's avatar heddn Committed by heddn

Issue #3002407 by quietone, heddn, grahl: entity_lookup doesn't work when run...

Issue #3002407 by quietone, heddn, grahl: entity_lookup doesn't work when run with drush (anonymous user.)
parent 23fefb11
......@@ -5,3 +5,6 @@ package: Migration
core: 8.x
dependencies:
- drupal:migrate (>=8.3)
test_dependencies:
- entity
- profile
......@@ -16,15 +16,14 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* This plugin looks for existing entities.
*
* @MigrateProcessPlugin(
* id = "entity_lookup",
* handle_multiples = TRUE
* )
*
* In its most simple form, this plugin needs no configuration. However, if the
* lookup properties cannot be determined through introspection, define them via
* configuration.
*
* Available configuration keys:
* - access_check: (optional) Indicates if access to the entity for this user
* will be checked. Default is true.
*
* Example usage with minimal configuration:
* @code
* destination:
......@@ -35,9 +34,12 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
* default_value: page
* field_tags:
* plugin: entity_lookup
* access_check: false
* source: tags
* @endcode
*
* In this example the access check is disabled.
*
* Example usage with full configuration:
* @code
* field_tags:
......@@ -49,6 +51,11 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
* entity_type: taxonomy_term
* ignore_case: true
* @endcode
*
* @MigrateProcessPlugin(
* id = "entity_lookup",
* handle_multiples = TRUE
* )
*/
class EntityLookup extends ProcessPluginBase implements ContainerFactoryPluginInterface {
......@@ -122,6 +129,13 @@ class EntityLookup extends ProcessPluginBase implements ContainerFactoryPluginIn
*/
protected $destinationProperty;
/**
* The access check flag.
*
* @var string
*/
protected $accessCheck = TRUE;
/**
* {@inheritdoc}
*/
......@@ -177,6 +191,9 @@ class EntityLookup extends ProcessPluginBase implements ContainerFactoryPluginIn
* with the $row above.
*/
protected function determineLookupProperties($destinationProperty) {
if (isset($this->configuration['access_check'])) {
$this->accessCheck = $this->configuration['access_check'];
}
if (!empty($this->configuration['value_key'])) {
$this->lookupValueKey = $this->configuration['value_key'];
}
......@@ -260,6 +277,7 @@ class EntityLookup extends ProcessPluginBase implements ContainerFactoryPluginIn
$query = $this->entityManager->getStorage($this->lookupEntityType)
->getQuery()
->accessCheck($this->accessCheck)
->condition($this->lookupValueKey, $value, $multiple ? 'IN' : NULL);
// Sqlite and possibly others returns data in a non-deterministic order.
// Make it deterministic.
......
<?php
namespace Drupal\Tests\migrate_plus\Kernel\Plugin\migrate\process;
use Drupal\KernelTests\KernelTestBase;
use Drupal\Tests\user\Traits\UserCreationTrait;
use Drupal\migrate\MigrateExecutableInterface;
use Drupal\migrate\Plugin\MigrateDestinationInterface;
use Drupal\migrate\Plugin\MigrationInterface;
use Drupal\migrate\Row;
use Drupal\profile\Entity\Profile;
use Drupal\profile\Entity\ProfileType;
/**
* Tests Entity Lookup access check.
*
* @group migrate_plus
*
* @requires entity
* @requires profile
*/
class EntityLookupAccessTest extends KernelTestBase {
use UserCreationTrait;
/**
* {@inheritdoc}
*/
public static $modules = [
'entity',
'field',
'migrate',
'migrate_plus',
'node',
'profile',
'system',
'user',
'views',
'text',
];
/**
* The mocked migration.
*
* @var \Drupal\migrate\Plugin\MigrationInterface|\Prophecy\Prophecy\ObjectProphecy
*/
protected $migration;
/**
* The mocked migrate executable.
*
* @var \Drupal\migrate\MigrateExecutable|\Prophecy\Prophecy\ObjectProphecy
*/
protected $executable;
/**
* The migrate row.
*
* @var \Drupal\migrate\Row
*/
protected $row;
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->installSchema('system', 'sequences');
$this->installEntitySchema('profile');
$this->installEntitySchema('user');
$this->installConfig(['profile', 'system']);
$known_user = $this->createUser([], 'lucuma');
// Create a profile entity.
ProfileType::create(['id' => 'default']);
Profile::create([
'uid' => $known_user->id(),
'type' => 'default',
])->save();
$migration_prophecy = $this->prophesize(MigrationInterface::class);
$migrate_destination_prophecy = $this->prophesize(MigrateDestinationInterface::class);
$migrate_destination_prophecy->getPluginId()->willReturn('profile');
$migrate_destination = $migrate_destination_prophecy->reveal();
$migration_prophecy->getDestinationPlugin()
->willReturn($migrate_destination);
$migration_prophecy->getProcess()->willReturn([]);
$this->migration = $migration_prophecy->reveal();
$this->executable = $this->prophesize(MigrateExecutableInterface::class)
->reveal();
$this->row = new Row();
}
/**
* Tests entity_lookup access_check configuration key.
*/
public function testEntityLookupAccessCheck() {
$configuration_base = [
'entity_type' => 'profile',
'value_key' => 'profile_id',
];
// Set access_check true.
$configuration = $configuration_base +
[
'access_check' => TRUE,
];
$plugin = \Drupal::service('plugin.manager.migrate.process')
->createInstance('entity_lookup', $configuration, $this->migration);
// Check the profile is not found.
$value = $plugin->transform('1', $this->executable, $this->row, 'profile_id');
$this->assertNull($value);
// Retest with access check false.
$configuration = $configuration_base +
[
'access_check' => FALSE,
];
$plugin = \Drupal::service('plugin.manager.migrate.process')
->createInstance('entity_lookup', $configuration, $this->migration);
// Check the profile is found.
$value = $plugin->transform('1', $this->executable, $this->row, 'profile_id');
$this->assertSame('1', $value);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment