Commit 9b36ff3c authored by Ivan Doroshenko's avatar Ivan Doroshenko
Browse files

Issue #3232214 by Matroskeen: Table source plugin - validate the sql table...

Issue #3232214 by Matroskeen: Table source plugin - validate the sql table before executing the query
parent ef77138d
Loading
Loading
Loading
Loading
+11 −0
Changes for src/Plugin/migrate/source/Table.php: 11 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -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();
  }

}
+70 −0
Changes for tests/src/Kernel/Plugin/migrate/source/TableTest.php: 70 added lines, 0 removed lines.
Original line number Diff line number Diff line
<?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();
  }

}