View source
<?php
namespace Drupal\Tests\migrate_drupal\Unit\source;
use Drupal\Core\Database\Connection;
use Drupal\Tests\migrate\Unit\MigrateTestCase;
use Drupal\migrate\Exception\RequirementsException;
use Drupal\migrate_drupal\Plugin\migrate\source\DrupalSqlBase;
class DrupalSqlBaseTest extends MigrateTestCase {
protected $migrationConfiguration = [
'id' => 'DrupalSqlBase',
];
protected $base;
protected $pluginDefinition = [];
protected $state;
protected $entityTypeManager;
protected $databaseContents = [
'system' => [
[
'filename' => 'sites/all/modules/module1',
'name' => 'module1',
'type' => 'module',
'status' => 0,
'schema_version' => -1,
],
],
];
protected function setUp() : void {
parent::setUp();
$this->pluginDefinition['requirements_met'] = TRUE;
$this->pluginDefinition['source_module'] = 'module1';
$this->state = $this
->createMock('Drupal\\Core\\State\\StateInterface');
$this->entityTypeManager = $this
->createMock('Drupal\\Core\\Entity\\EntityTypeManagerInterface');
}
public function testSourceProviderNotActive() {
$plugin = new TestDrupalSqlBase([], 'placeholder_id', $this->pluginDefinition, $this
->getMigration(), $this->state, $this->entityTypeManager);
$plugin
->setDatabase($this
->getDatabase($this->databaseContents));
$this
->expectException(RequirementsException::class);
$this
->expectExceptionMessage('The module module1 is not enabled in the source site.');
try {
$plugin
->checkRequirements();
} catch (RequirementsException $e) {
$this
->assertEquals([
'source_module' => 'module1',
], $e
->getRequirements());
throw $e;
}
}
public function testSourceDatabaseError() {
$plugin = new TestDrupalSqlBase([], 'test', $this->pluginDefinition, $this
->getMigration(), $this->state, $this->entityTypeManager);
$this
->expectException(RequirementsException::class);
$this
->expectExceptionMessage('No database connection configured for source plugin test');
$plugin
->checkRequirements();
}
public function testMinimumVersion($success, $minimum_version, $schema_version) {
$this->pluginDefinition['minimum_version'] = $minimum_version;
$this->databaseContents['system'][0]['status'] = 1;
$this->databaseContents['system'][0]['schema_version'] = $schema_version;
$plugin = new TestDrupalSqlBase([], 'test', $this->pluginDefinition, $this
->getMigration(), $this->state, $this->entityTypeManager);
$plugin
->setDatabase($this
->getDatabase($this->databaseContents));
if (!$success) {
$this
->expectException(RequirementsException::class);
$this
->expectExceptionMessage("Required minimum version {$minimum_version}");
}
$plugin
->checkRequirements();
}
public function providerMinimumVersion() {
return [
'minimum less than schema' => [
TRUE,
'7000',
'7001',
],
'same version' => [
TRUE,
'7001',
'7001',
],
'minimum greater than schema' => [
FALSE,
'7005',
'7001',
],
'schema version 0' => [
FALSE,
'7000',
'0',
],
'schema version -1' => [
FALSE,
'7000',
'-1',
],
'minimum not set' => [
TRUE,
NULL,
'-1',
],
];
}
}
class TestDrupalSqlBase extends DrupalSqlBase {
public function fields() {
return [];
}
public function query() {
throw new \RuntimeException(__METHOD__ . " not implemented for " . __CLASS__);
}
public function setDatabase(Connection $database) {
$this->database = $database;
}
public function getIds() {
return [];
}
}