Commit 92965c5b authored by alexpott's avatar alexpott

Issue #2522652 by neclimdul, phenaproxima, prajaankit, benjy: Add...

Issue #2522652 by neclimdul, phenaproxima, prajaankit, benjy: Add getTemplateByName method to TemplateStorage
parent 2873e817
......@@ -14,10 +14,10 @@
* Storage to access migration template configuration in enabled extensions.
*/
class MigrateTemplateStorage {
/**
* Extension sub-directory containing default configuration for migrations.
*/
const MIGRATION_TEMPLATE_DIRECTORY = 'migration_templates';
/**
......@@ -62,6 +62,20 @@ public function findTemplatesByTag($tag) {
return $matched_templates;
}
/**
* Retrieve a template given a specific name.
*
* @param string $name
* A migration template name.
*
* @return NULL|array
* A parsed migration template, or NULL if it doesn't exist.
*/
public function getTemplateByName($name) {
$templates = $this->getAllTemplates();
return isset($templates[$name]) ? $templates[$name] : NULL;
}
/**
* Retrieves all migration templates belonging to enabled extensions.
*
......@@ -69,7 +83,6 @@ public function findTemplatesByTag($tag) {
* Array of parsed templates, keyed by the fully-qualified id.
*/
public function getAllTemplates() {
$templates = [];
foreach ($this->moduleHandler->getModuleDirectories() as $directory) {
$full_directory = $directory . '/' . $this->directory;
......
......@@ -7,8 +7,6 @@
namespace Drupal\migrate\Tests;
use Drupal\migrate\MigrationStorage;
/**
* Test the migration template functionality.
*
......@@ -29,24 +27,52 @@ class TemplateTest extends MigrateTestBase {
public function testTemplates() {
$migration_templates = \Drupal::service('migrate.template_storage')->findTemplatesByTag("Template Test");
$expected_url = [
'id' => 'url_template',
'label' => 'Template test - url',
'migration_tags' => ['Template Test'],
'source' => ['plugin' => 'empty'],
'process' => ['src' => 'foobar'],
'destination' => ['plugin' => 'url_alias'],
'id' => 'url_template',
'label' => 'Template test - url',
'migration_tags' => ['Template Test'],
'source' => ['plugin' => 'empty'],
'process' => ['src' => 'foobar'],
'destination' => ['plugin' => 'url_alias'],
];
$expected_node = [
'id' => 'node_template',
'label' => 'Template test - node',
'migration_tags' => ['Template Test'],
'source' => ['plugin' => 'empty'],
'process' => ['src' => 'barfoo'],
'destination' => ['plugin' => 'entity:node'],
'id' => 'node_template',
'label' => 'Template test - node',
'migration_tags' => ['Template Test'],
'source' => ['plugin' => 'empty'],
'process' => ['src' => 'barfoo'],
'destination' => ['plugin' => 'entity:node'],
];
$this->assertIdentical($migration_templates['migrate.migration.url_template'], $expected_url);
$this->assertIdentical($migration_templates['migrate.migration.node_template'], $expected_node);
$this->assertFalse(isset($migration_templates['migrate.migration.other_template']));
}
/**
* Tests retrieving a template by name.
*/
public function testGetTemplateByName() {
/** @var \Drupal\migrate\MigrateTemplateStorage $template_storage */
$template_storage = \Drupal::service('migrate.template_storage');
$expected_url = [
'id' => 'url_template',
'label' => 'Template test - url',
'migration_tags' => ['Template Test'],
'source' => ['plugin' => 'empty'],
'process' => ['src' => 'foobar'],
'destination' => ['plugin' => 'url_alias'],
];
$expected_node = [
'id' => 'node_template',
'label' => 'Template test - node',
'migration_tags' => ['Template Test'],
'source' => ['plugin' => 'empty'],
'process' => ['src' => 'barfoo'],
'destination' => ['plugin' => 'entity:node'],
];
$this->assertIdentical($template_storage->getTemplateByName('migrate.migration.url_template'), $expected_url);
$this->assertIdentical($template_storage->getTemplateByName('migrate.migration.node_template'), $expected_node);
$this->assertNull($template_storage->getTemplateByName('migrate.migration.dne'));
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment