From 9b36ff3c9e258b2928b654fe5946cd54e07ec439 Mon Sep 17 00:00:00 2001 From: Matroskeen <matroskeen@3426249.no-reply.drupal.org> Date: Sat, 29 Jan 2022 15:41:51 +0000 Subject: [PATCH] Issue #3232214 by Matroskeen: Table source plugin - validate the sql table before executing the query --- src/Plugin/migrate/source/Table.php | 11 +++ .../Plugin/migrate/source/TableTest.php | 70 +++++++++++++++++++ 2 files changed, 81 insertions(+) create mode 100644 tests/src/Kernel/Plugin/migrate/source/TableTest.php diff --git a/src/Plugin/migrate/source/Table.php b/src/Plugin/migrate/source/Table.php index c244e0f9..7c25224a 100644 --- a/src/Plugin/migrate/source/Table.php +++ b/src/Plugin/migrate/source/Table.php @@ -3,6 +3,7 @@ namespace Drupal\migrate_plus\Plugin\migrate\source; use Drupal\Core\State\StateInterface; +use Drupal\migrate\Exception\RequirementsException; use Drupal\migrate\MigrateException; use Drupal\migrate\Plugin\migrate\source\SqlBase; use Drupal\migrate\Plugin\MigrationInterface; @@ -141,4 +142,14 @@ class Table extends SqlBase { return $this->idFields; } + /** + * {@inheritdoc} + */ + public function checkRequirements() { + if (!$this->getDatabase()->schema()->tableExists($this->tableName)) { + throw new RequirementsException("Source database table '{$this->tableName}' does not exist", ['source_table' => $this->tableName]); + } + parent::checkRequirements(); + } + } diff --git a/tests/src/Kernel/Plugin/migrate/source/TableTest.php b/tests/src/Kernel/Plugin/migrate/source/TableTest.php new file mode 100644 index 00000000..bb5c6007 --- /dev/null +++ b/tests/src/Kernel/Plugin/migrate/source/TableTest.php @@ -0,0 +1,70 @@ +<?php + +namespace Drupal\Tests\migrate_plus\Kernel\Plugin\migrate\source; + +use Drupal\migrate\Exception\RequirementsException; +use Drupal\Tests\migrate_drupal\Kernel\d7\MigrateDrupal7TestBase; + +/** + * Tests Table source plugin. + * + * @covers \Drupal\migrate_plus\Plugin\migrate\source\Table + * + * @group migrate_plus + */ +class TableTest extends MigrateDrupal7TestBase { + + /** + * {@inheritdoc} + */ + protected static $modules = ['migrate_plus']; + + /** + * The migration plugin manager. + * + * @var \Drupal\migrate\Plugin\MigrationPluginManager + */ + protected $migrationPluginManager; + + /** + * Definition of a test migration. + * + * @var array + */ + protected $migrationDefinition; + + /** + * {@inheritdoc} + */ + protected function setUp(): void { + parent::setUp(); + $this->migrationPluginManager = \Drupal::service('plugin.manager.migration'); + + $this->migrationDefinition = [ + 'id' => 'test', + 'source' => [ + 'plugin' => 'table', + 'table_name' => 'foo', + 'fields' => [], + 'id_fields' => [], + ], + 'process' => [], + 'destination' => [ + 'plugin' => 'null', + ], + ]; + } + + /** + * Tests 'Table' source plugin requirements. + */ + public function testCheckRequirements() { + $this->expectException(RequirementsException::class); + $this->expectExceptionMessage("Source database table 'foo' does not exist"); + + $this->migrationPluginManager->createStubMigration($this->migrationDefinition) + ->getSourcePlugin() + ->checkRequirements(); + } + +} -- GitLab