Commit ef77138d authored by Nazar Velychenko's avatar Nazar Velychenko Committed by Ivan Doroshenko
Browse files

Issue #3261273 by dinazaur, Matroskeen: Throw consistent exceptions on...

Issue #3261273 by dinazaur, Matroskeen: Throw consistent exceptions on \Drupal\migrate_plus\Plugin\migrate\process\Gate process plugin 
parent 4fb94a8f
Loading
Loading
Loading
Loading
+16 −9
Changes for src/Plugin/migrate/process/Gate.php: 16 added lines, 9 removed lines.
Original line number Diff line number Diff line
@@ -84,20 +84,27 @@ class Gate extends ProcessPluginBase {
  /**
   * {@inheritdoc}
   */
  public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
    if (!array_key_exists('valid_keys', $this->configuration)) {
      throw new MigrateException('Gate plugin is missing valid_keys configuration.');
  public function __construct(array $configuration, $plugin_id, $plugin_definition) {
    if (!array_key_exists('valid_keys', $configuration)) {
      throw new \InvalidArgumentException('Gate plugin is missing valid_keys configuration.');
    }
    if (!array_key_exists('use_as_key', $configuration)) {
      throw new \InvalidArgumentException('Gate plugin is missing use_as_key configuration.');
    }
    if (!array_key_exists('use_as_key', $this->configuration)) {
      throw new MigrateException('Gate plugin is missing use_as_key configuration.');
    if (!array_key_exists('key_direction', $configuration)) {
      throw new \InvalidArgumentException('Gate plugin is missing key_direction configuration.');
    }
    if (!array_key_exists('key_direction', $this->configuration)) {
      throw new MigrateException('Gate plugin is missing key_direction configuration.');
    if (!in_array($configuration['key_direction'], ['lock', 'unlock'], TRUE)) {
      throw new \InvalidArgumentException('Gate plugin only accepts the following values for key_direction: lock and unlock.');
    }
    if (!in_array($this->configuration['key_direction'], ['lock', 'unlock'], TRUE)) {
      throw new MigrateException('Gate plugin only accepts the following values for key_direction: lock and unlock.');

    parent::__construct($configuration, $plugin_id, $plugin_definition);
  }

  /**
   * {@inheritdoc}
   */
  public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
    $valid_keys = (array) $this->configuration['valid_keys'];
    $key = $row->get($this->configuration['use_as_key']);
    $key_is_valid = in_array($key, $valid_keys, TRUE);
+2 −3
Changes for tests/src/Unit/process/GateTest.php: 2 added lines, 3 removed lines.
Original line number Diff line number Diff line
@@ -156,10 +156,9 @@ class GateTest extends MigrateProcessTestCase {
   * @dataProvider badConfigurationProvider
   */
  public function testGateBadConfigration($configuration, $message): void {
    $this->expectException(MigrateException::class);
    $this->expectException(\InvalidArgumentException::class);
    $this->expectExceptionMessage($message);
    $plugin = new Gate($configuration, 'gate', []);
    $plugin->transform(NULL, $this->migrateExecutable, $this->row, 'destinationproperty');
    new Gate($configuration, 'gate', []);
  }

  /**