RequirementsException example

$target = $database_info['target'] ?? 'default';
    if (isset($database_info['database'])) {
      Database::addConnectionInfo($key$target$database_info['database']);
    }
    try {
      $connection = Database::getConnection($target$key);
    }
    catch (ConnectionNotDefinedException $e) {
      // If we fell back to the magic 'migrate' connection and it doesn't exist,       // treat the lack of the connection as a RequirementsException.       if ($key == 'migrate') {
        throw new RequirementsException("No database connection configured for source plugin " . $this->pluginId, [], 0, $e);
      }
      else {
        throw $e;
      }
    }
    return $connection;
  }

  /** * {@inheritdoc} */
  

  public function rollbackAction() {
    return $this->rollbackAction;
  }

  /** * {@inheritdoc} */
  public function checkRequirements() {
    if (empty($this->pluginDefinition['requirements_met'])) {
      throw new RequirementsException(sprintf("Destination plugin '%s' did not meet the requirements", $this->pluginId));
    }
  }

  /** * {@inheritdoc} */
  public function rollback(array $destination_identifier) {
    // By default we do nothing.   }

  /** * {@inheritdoc} */
$ids['fid']['type'] = 'integer';
    return $ids;
  }

  /** * {@inheritdoc} */
  public function checkRequirements() {
    $this->setTableNames();
    if (!$this->getDatabase()->schema()->tableExists($this->fieldTable)) {
      // If we make it to here, the profile module isn't installed.       throw new RequirementsException('Profile module not enabled on source site');
    }
    parent::checkRequirements();
  }

  /** * Helper to set the profile field table names. */
  protected function setTableNames() {
    if (empty($this->fieldTable) || empty($this->valueTable)) {
      if ($this->getModuleSchemaVersion('system') >= 7000) {
        $this->fieldTable = 'profile_field';
        
/** @var \Drupal\migrate\Plugin\MigrationInterface[] $required_migrations */
    $required_migrations = $this->getMigrationPluginManager()->createInstances($this->requirements);

    $missing_migrations = array_diff($this->requirements, array_keys($required_migrations));
    // Check if the dependencies are in good shape.     foreach ($required_migrations as $migration_id => $required_migration) {
      if (!$required_migration->allRowsProcessed()) {
        $missing_migrations[] = $migration_id;
      }
    }
    if ($missing_migrations) {
      throw new RequirementsException('Missing migrations ' . implode(', ', $missing_migrations) . '.', ['requirements' => $missing_migrations]);
    }
  }

  /** * Gets the migration plugin manager. * * @return \Drupal\migrate\Plugin\MigrationPluginManagerInterface * The migration plugin manager. */
  protected function getMigrationPluginManager() {
    return $this->migrationPluginManager;
  }
/** * Tests checking requirements for source plugins. * * @covers ::checkRequirements */
  public function testRequirementsForSourcePlugin() {
    $migration = new TestMigration();

    $source_plugin = $this->createMock('Drupal\Tests\migrate\Unit\RequirementsAwareSourceInterface');
    $source_plugin->expects($this->once())
      ->method('checkRequirements')
      ->willThrowException(new RequirementsException('Missing source requirement', ['key' => 'value']));
    $destination_plugin = $this->createMock('Drupal\Tests\migrate\Unit\RequirementsAwareDestinationInterface');

    $migration->setSourcePlugin($source_plugin);
    $migration->setDestinationPlugin($destination_plugin);

    $this->expectException(RequirementsException::class);
    $this->expectExceptionMessage('Missing source requirement');
    $migration->checkRequirements();
  }

  /** * Tests checking requirements for destination plugins. * * @covers ::checkRequirements */

  }

  /** * {@inheritdoc} */
  public function checkRequirements() {
    parent::checkRequirements();

    if (!$this->moduleExists('comment')) {
      // If we make it to here, the comment module isn't installed.       throw new RequirementsException('The module comment is not enabled in the source site');
    }
    if (!$this->moduleExists('node')) {
      // Node module is also a requirement.       throw new RequirementsException('The module node is not enabled in the source site');
    }
  }

}

  public function getIds() {
    $ids['language']['type'] = 'string';
    return $ids;
  }

  /** * {@inheritdoc} */
  public function checkRequirements() {
    if (!$this->getDatabase()->schema()->tableExists('i18n_variable')) {
      throw new RequirementsException("Source database table 'i18n_variable' does not exist");
    }
    parent::checkRequirements();
  }

}

  public function checkRequirements() {
    parent::checkRequirements();
    if ($this->pluginDefinition['requirements_met'] === TRUE) {
      if ($source_module = $this->getSourceModule()) {
        if ($this->moduleExists($source_module)) {
          if (isset($this->pluginDefinition['minimum_version'])) {
            $minimum_version = (int) $this->pluginDefinition['minimum_version'];
            $installed_version = (int) $this->getModuleSchemaVersion($source_module);
            if ($minimum_version > $installed_version) {
              throw new RequirementsException('Required minimum version ' . $this->pluginDefinition['minimum_version']['minimum_version' => $this->pluginDefinition['minimum_version']]);
            }
          }
        }
        else {
          throw new RequirementsException('The module ' . $source_module . ' is not enabled in the source site.', ['source_module' => $source_module]);
        }
      }
    }
  }

  /** * Retrieves a module schema_version from the source Drupal database. * * @param string $module * Name of module. * * @return mixed * The current module schema version on the origin system table or FALSE if * not found. */

class RequirementsExceptionTest extends UnitTestCase {

  protected const MISSING_REQUIREMENTS = ['random_jackson_pivot', '51_Eridani_b'];

  /** * @covers ::getRequirements */
  public function testGetRequirements() {
    $exception = new RequirementsException('Missing requirements ', ['requirements' => static::MISSING_REQUIREMENTS]);
    $this->assertEquals(['requirements' => static::MISSING_REQUIREMENTS]$exception->getRequirements());
  }

  /** * @covers ::getRequirementsString * @dataProvider getRequirementsProvider */
  public function testGetExceptionString($expected$message$requirements) {
    $exception = new RequirementsException($message$requirements);
    $this->assertEquals($expected$exception->getRequirementsString());
  }

  
return $ids;
  }

  /** * {@inheritdoc} */
  public function checkRequirements() {
    parent::checkRequirements();
    if (!$this->moduleExists('node')) {
      // Drupal 6 and Drupal 7 comment configuration migrations migrate comment       // types and comment fields for node comments only.       throw new RequirementsException('The node module is not enabled in the source site.', [
        'source_module_additional' => 'node',
      ]);
    }
  }

}
Home | Imprint | This part of the site doesn't use cookies.