Skip to content
Snippets Groups Projects
Commit 84cdd8e2 authored by Lucas Hedding's avatar Lucas Hedding Committed by Lucas Hedding
Browse files

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
No related branches found
No related tags found
No related merge requests found
...@@ -5,3 +5,6 @@ package: Migration ...@@ -5,3 +5,6 @@ package: Migration
core: 8.x core: 8.x
dependencies: dependencies:
- drupal:migrate (>=8.3) - drupal:migrate (>=8.3)
test_dependencies:
- entity
- profile
...@@ -16,15 +16,14 @@ use Symfony\Component\DependencyInjection\ContainerInterface; ...@@ -16,15 +16,14 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
/** /**
* This plugin looks for existing entities. * 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 * In its most simple form, this plugin needs no configuration. However, if the
* lookup properties cannot be determined through introspection, define them via * lookup properties cannot be determined through introspection, define them via
* configuration. * 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: * Example usage with minimal configuration:
* @code * @code
* destination: * destination:
...@@ -35,9 +34,12 @@ use Symfony\Component\DependencyInjection\ContainerInterface; ...@@ -35,9 +34,12 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
* default_value: page * default_value: page
* field_tags: * field_tags:
* plugin: entity_lookup * plugin: entity_lookup
* access_check: false
* source: tags * source: tags
* @endcode * @endcode
* *
* In this example the access check is disabled.
*
* Example usage with full configuration: * Example usage with full configuration:
* @code * @code
* field_tags: * field_tags:
...@@ -49,6 +51,11 @@ use Symfony\Component\DependencyInjection\ContainerInterface; ...@@ -49,6 +51,11 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
* entity_type: taxonomy_term * entity_type: taxonomy_term
* ignore_case: true * ignore_case: true
* @endcode * @endcode
*
* @MigrateProcessPlugin(
* id = "entity_lookup",
* handle_multiples = TRUE
* )
*/ */
class EntityLookup extends ProcessPluginBase implements ContainerFactoryPluginInterface { class EntityLookup extends ProcessPluginBase implements ContainerFactoryPluginInterface {
...@@ -122,6 +129,13 @@ class EntityLookup extends ProcessPluginBase implements ContainerFactoryPluginIn ...@@ -122,6 +129,13 @@ class EntityLookup extends ProcessPluginBase implements ContainerFactoryPluginIn
*/ */
protected $destinationProperty; protected $destinationProperty;
/**
* The access check flag.
*
* @var string
*/
protected $accessCheck = TRUE;
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
...@@ -177,6 +191,9 @@ class EntityLookup extends ProcessPluginBase implements ContainerFactoryPluginIn ...@@ -177,6 +191,9 @@ class EntityLookup extends ProcessPluginBase implements ContainerFactoryPluginIn
* with the $row above. * with the $row above.
*/ */
protected function determineLookupProperties($destinationProperty) { protected function determineLookupProperties($destinationProperty) {
if (isset($this->configuration['access_check'])) {
$this->accessCheck = $this->configuration['access_check'];
}
if (!empty($this->configuration['value_key'])) { if (!empty($this->configuration['value_key'])) {
$this->lookupValueKey = $this->configuration['value_key']; $this->lookupValueKey = $this->configuration['value_key'];
} }
...@@ -260,6 +277,7 @@ class EntityLookup extends ProcessPluginBase implements ContainerFactoryPluginIn ...@@ -260,6 +277,7 @@ class EntityLookup extends ProcessPluginBase implements ContainerFactoryPluginIn
$query = $this->entityManager->getStorage($this->lookupEntityType) $query = $this->entityManager->getStorage($this->lookupEntityType)
->getQuery() ->getQuery()
->accessCheck($this->accessCheck)
->condition($this->lookupValueKey, $value, $multiple ? 'IN' : NULL); ->condition($this->lookupValueKey, $value, $multiple ? 'IN' : NULL);
// Sqlite and possibly others returns data in a non-deterministic order. // Sqlite and possibly others returns data in a non-deterministic order.
// Make it deterministic. // 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);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment