diff --git a/migrate_plus.info.yml b/migrate_plus.info.yml index 05b5477b44a953601d0799c238f9cd0575270177..38c4be8d9df1ef33317ce8ea2fe328686b8e40b3 100644 --- a/migrate_plus.info.yml +++ b/migrate_plus.info.yml @@ -5,3 +5,6 @@ package: Migration core: 8.x dependencies: - drupal:migrate (>=8.3) +test_dependencies: + - entity + - profile diff --git a/src/Plugin/migrate/process/EntityLookup.php b/src/Plugin/migrate/process/EntityLookup.php index 7ed4e84d27c0a090c356c0210a04d09ab7481c32..ec11a28d3dafb34fddfc122a4db9454011bb09c5 100644 --- a/src/Plugin/migrate/process/EntityLookup.php +++ b/src/Plugin/migrate/process/EntityLookup.php @@ -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. diff --git a/tests/src/Kernel/Plugin/migrate/process/EntityLookupAccessTest.php b/tests/src/Kernel/Plugin/migrate/process/EntityLookupAccessTest.php new file mode 100644 index 0000000000000000000000000000000000000000..29662a656e456842ab0adde572b2448495742027 --- /dev/null +++ b/tests/src/Kernel/Plugin/migrate/process/EntityLookupAccessTest.php @@ -0,0 +1,129 @@ +<?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); + } + +}