View source
<?php
namespace Drupal\Tests\migrate_tools\Kernel;
use Drupal\migrate_tools\Commands\MigrateToolsCommands;
use Drupal\migrate_tools\MigrateTools;
use Drupal\Tests\migrate\Kernel\MigrateTestBase;
use Drupal\migrate\Plugin\MigrationInterface;
class DrushTest extends MigrateTestBase {
public static $modules = [
'migrate_tools_test',
'migrate_tools',
'migrate_plus',
'taxonomy',
'text',
'system',
'user',
];
protected $importBaseOptions = [
'all' => NULL,
'group' => NULL,
'tag' => NULL,
'limit' => NULL,
'feedback' => NULL,
'idlist' => NULL,
'idlist-delimiter' => MigrateTools::DEFAULT_ID_LIST_DELIMITER,
'update' => NULL,
'force' => NULL,
'execute-dependencies' => NULL,
'skip-progress-bar' => FALSE,
'continue-on-failure' => FALSE,
'sync' => FALSE,
];
protected $commands;
protected $migrationPluginManager;
protected $logger;
public function setUp() {
parent::setUp();
$this
->installConfig('migrate_plus');
$this
->installConfig('migrate_tools_test');
$this
->installEntitySchema('taxonomy_term');
$this
->installEntitySchema('user');
$this
->installSchema('system', [
'key_value',
'key_value_expire',
]);
$this
->installSchema('user', [
'users_data',
]);
$this->migrationPluginManager = $this->container
->get('plugin.manager.migration');
$this->logger = $this->container
->get('logger.channel.migrate_tools');
$this->commands = new MigrateToolsCommands($this->migrationPluginManager, $this->container
->get('date.formatter'), $this->container
->get('entity_type.manager'), $this->container
->get('keyvalue'));
$this->commands
->setLogger($this->logger);
}
public function testStatus() {
$this
->executeMigration('fruit_terms');
$result = $this->commands
->status('fruit_terms', [
'group' => NULL,
'tag' => NULL,
'names-only' => FALSE,
]);
$rows = $result
->getArrayCopy();
$this
->assertCount(1, $rows);
$row = reset($rows);
$this
->assertSame('fruit_terms', $row['id']);
$this
->assertSame(3, $row['total']);
$this
->assertSame(3, $row['imported']);
$this
->assertSame('Idle', $row['status']);
\Drupal::service('module_installer')
->uninstall([
'migrate_tools_test',
]);
$this
->enableModules([
'migrate_drupal',
]);
\Drupal::configFactory()
->getEditable('migrate_plus.migration.fruit_terms')
->delete();
$rows = $this->commands
->status();
$this
->assertEmpty($rows);
}
public function testFailingStatusThrowsException() {
$this
->expectException(\Exception::class);
$this
->expectExceptionMessage('The "does_not_exist" plugin does not exist.');
$this->commands
->status('invalid_plugin');
}
public function testImport() {
$migration = $this->migrationPluginManager
->createInstance('fruit_terms');
$id_map = $migration
->getIdMap();
$this->commands
->import('fruit_terms', array_merge($this->importBaseOptions, [
'idlist' => 'Apple',
]));
$this
->assertSame(1, $id_map
->importedCount());
$this->commands
->import('fruit_terms', $this->importBaseOptions);
$this
->assertSame(3, $id_map
->importedCount());
$this->commands
->import('fruit_terms', array_merge($this->importBaseOptions, [
'idlist' => 'Apple',
'update' => TRUE,
]));
$this
->assertCount(0, $id_map
->getRowsNeedingUpdate(100));
}
public function testFailingImportThrowsException() {
$this
->expectException(\Exception::class);
$this
->expectExceptionMessage('source_exception migration failed.');
$this->commands
->import('source_exception', $this->importBaseOptions);
}
public function testMessages() {
$this
->executeMigration('fruit_terms');
$message = $this
->getRandomGenerator()
->string(16);
$migration = $this->migrationPluginManager
->createInstance('fruit_terms');
$id_map = $migration
->getIdMap();
$id_map
->saveMessage([
'name' => 'Apple',
], $message);
$result = $this->commands
->messages('fruit_terms', [
'csv' => FALSE,
'idlist' => NULL,
'idlist-delimiter' => MigrateTools::DEFAULT_ID_LIST_DELIMITER,
]);
$rows = $result
->getArrayCopy();
$this
->assertSame($message, $rows[0]['message']);
}
public function testFailingMessagesThrowsException() {
$this
->expectException(\Exception::class);
$this
->expectExceptionMessage('Migration does_not_exist does not exist');
$this->commands
->messages('does_not_exist', [
'csv' => FALSE,
'idlist' => NULL,
'idlist-delimiter' => MigrateTools::DEFAULT_ID_LIST_DELIMITER,
]);
}
public function testRollback() {
$this
->executeMigration('fruit_terms');
$migration = $this->migrationPluginManager
->createInstance('fruit_terms');
$id_map = $migration
->getIdMap();
$this
->assertSame(3, $id_map
->importedCount());
$this->commands
->rollback('fruit_terms', $this->importBaseOptions);
$this
->assertSame(0, $id_map
->importedCount());
}
public function testFailingRollbackThrowsException() {
$this
->expectException(\Exception::class);
$this
->expectExceptionMessage('source_exception migration failed');
$migration = $this->migrationPluginManager
->createInstance('source_exception');
$migration
->setStatus(MigrationInterface::STATUS_IMPORTING);
$this->commands
->rollback('source_exception', $this->importBaseOptions);
}
public function testReset() {
$migration = $this->migrationPluginManager
->createInstance('fruit_terms');
$migration
->setStatus(MigrationInterface::STATUS_IMPORTING);
$status = $this->commands
->status('fruit_terms', [
'group' => NULL,
'tag' => NULL,
'names-only' => FALSE,
])
->getArrayCopy()[0]['status'];
$this
->assertSame('Importing', $status);
$this->commands
->resetStatus('fruit_terms');
$this
->assertSame(MigrationInterface::STATUS_IDLE, $migration
->getStatus());
}
public function testFailingResetStatusThrowsException() {
$this
->expectException(\Exception::class);
$this
->expectExceptionMessage('Migration does_not_exist does not exist');
$this->commands
->resetStatus('does_not_exist');
}
public function testStop() {
$migration = $this->migrationPluginManager
->createInstance('fruit_terms');
$migration
->setStatus(MigrationInterface::STATUS_IMPORTING);
$this->commands
->stop('fruit_terms');
$this
->assertSame(MigrationInterface::STATUS_STOPPING, $migration
->getStatus());
}
public function testFailingStopThrowsException() {
$this
->expectException(\Exception::class);
$this
->expectExceptionMessage('Migration does_not_exist does not exist');
$this->commands
->stop('does_not_exist');
}
public function testFieldsSource() {
$result = $this->commands
->fieldsSource('fruit_terms');
$rows = $result
->getArrayCopy();
$this
->assertCount(1, $rows);
$this
->assertSame('name', $rows[0]['machine_name']);
$this
->assertSame('name', $rows[0]['description']);
}
public function testFailingFieldsSourceThrowsException() {
$this
->expectException(\Exception::class);
$this
->expectExceptionMessage('Migration does_not_exist does not exist');
$this->commands
->fieldsSource('does_not_exist');
}
}
namespace Drupal\migrate_tools\Commands;
function drush_op(callable $callable) {
$args = func_get_args();
array_shift($args);
return call_user_func_array($callable, $args);
}
function dt($text, array $args = []) {
foreach ($args as $before => $after) {
$text = str_replace($before, $after, $text);
}
return $text;
}