Unverified Commit dd855179 authored by Alex Pott's avatar Alex Pott
Browse files

Issue #3187386 by quietone, anmolgoyal74, benjifisher, alexpott, catch: Use a...

Issue #3187386 by quietone, anmolgoyal74, benjifisher, alexpott, catch: Use a custom error message for PluginNotFoundException in the migratelookup service

(cherry picked from commit c0904f72)
parent 4ccf68da
Loading
Loading
Loading
Loading
+7 −0
Original line number Diff line number Diff line
@@ -2,6 +2,7 @@

namespace Drupal\migrate;

use Drupal\Component\Plugin\Exception\PluginException;
use Drupal\Component\Plugin\Exception\PluginNotFoundException;
use Drupal\migrate\Plugin\MigrationInterface;
use Drupal\migrate\Plugin\MigrationPluginManagerInterface;
@@ -35,6 +36,12 @@ public function lookup($migration_id, array $source_id_values) {
    $results = [];
    $migrations = $this->migrationPluginManager->createInstances($migration_id);
    if (!$migrations) {
      if (is_array($migration_id)) {
        if (count($migration_id) != 1) {
          throw new PluginException("Plugin IDs '" . implode("', '", $migration_id) . "' were not found.");
        }
        $migration_id = reset($migration_id);
      }
      throw new PluginNotFoundException($migration_id);
    }
    foreach ($migrations as $migration) {
+54 −5
Original line number Diff line number Diff line
@@ -2,6 +2,7 @@

namespace Drupal\Tests\migrate\Unit;

use Drupal\Component\Plugin\Exception\PluginException;
use Drupal\Component\Plugin\Exception\PluginNotFoundException;
use Drupal\migrate\MigrateLookup;
use Drupal\migrate\Plugin\MigrateDestinationInterface;
@@ -47,15 +48,63 @@ public function testLookup() {
  }

  /**
   * Tests that an appropriate message is logged if a PluginException is thrown.
   * Tests message logged when a single migration is not found.
   *
   * @dataProvider providerExceptionOnMigrationNotFound
   */
  public function testExceptionOnMigrationNotFound() {
  public function testExceptionOnMigrationNotFound($migrations, $message) {
    $migration_plugin_manager = $this->prophesize(MigrationPluginManagerInterface::class);
    $migration_plugin_manager->createInstances('bad_plugin')->willReturn([]);
    $migration_plugin_manager->createInstances($migrations)->willReturn([]);
    $this->expectException(PluginNotFoundException::class);
    $this->expectExceptionMessage("Plugin ID 'bad_plugin' was not found.");
    $this->expectExceptionMessage($message);
    $lookup = new MigrateLookup($migration_plugin_manager->reveal());
    $lookup->lookup($migrations, [1]);
  }

  /**
   * Provides data for testExceptionOnMigrationNotFound.
   */
  public function providerExceptionOnMigrationNotFound() {
    return [
      'string' => [
        'bad_plugin',
        "Plugin ID 'bad_plugin' was not found.",
      ],
      'array one item' => [
        ['bad_plugin'],
        "Plugin ID 'bad_plugin' was not found.",
      ],
    ];
  }

  /**
   * Tests message logged when multiple migrations are not found.
   *
   * @dataProvider providerExceptionOnMultipleMigrationsNotFound
   */
  public function testExceptionOnMultipleMigrationsNotFound($migrations, $message) {
    $migration_plugin_manager = $this->prophesize(MigrationPluginManagerInterface::class);
    $migration_plugin_manager->createInstances($migrations)->willReturn([]);
    $this->expectException(PluginException::class);
    $this->expectExceptionMessage($message);
    $lookup = new MigrateLookup($migration_plugin_manager->reveal());
    $lookup->lookup('bad_plugin', [1]);
    $lookup->lookup($migrations, [1]);
  }

  /**
   * Provides data for testExceptionOnMultipleMigrationsNotFound.
   */
  public function providerExceptionOnMultipleMigrationsNotFound() {
    return [
      'array two items' => [
        ['foo', 'bar'],
        "Plugin IDs 'foo', 'bar' were not found.",
      ],
      'empty array' => [
        [],
        "Plugin IDs '' were not found.",
      ],
    ];
  }

}