Score:1

How can I load a migration in a KernelTest?

in flag

I'm trying to test a migration with a KernelTest, but I'm running into an issue when I try to load the migration plugin.

My migration is defined in my_module/migrations/my_migration.yml. In my KernelTest, I've listed my_module and migrate in the $modules static property.

In a custom form, I use the following to load a migration plugin:

/** @var \Drupal\migrate\Plugin\MigrationPluginManagerInterface $plugin_manager */
$plugin_manager = \Drupal::service('plugin.manager.migration');
$migration = $plugin_manager
  ->createInstance('MY_MIGRATION');
// Next, create MigrateExecutable, etc...

However, when I use this same code in my KernelTest, $migration is always FALSE. I feel like this is because my migration is not loaded (?) when I enable my module in the test, but I'm not sure what to do about it. What am I missing here? How can I load my migration in the test?

sonfd avatar
in flag
I must've had something strange going on in my test class that caused that to fail.
Score:0
in flag

The migrate module provides a base Kernel test class, Drupal\Tests\migrate\Kernel\MigrateTestBase, which has a getMigration() method baked in.

For example, this works and the test passes:

<?php

namespace Drupal\my_module\tests\Kernel;

use Drupal\migrate\Plugin\MigrationInterface;
use Drupal\Tests\migrate\Kernel\MigrateTestBase;

class MyMigrationTest extends MigrateTestBase {

  protected static $modules = [
    'my_module',
    'migrate',
    // etc...
  ];
  
  public function testMigration(): void {
    $migration = $this->getMigration('MY_MIGRATION');

    $this->assertInstanceOf(MigrationInterface::class, $migration);
  }

}

However, it's not necessary to extend MigrateTestBase, this also works and test passes:

<?php

namespace Drupal\my_module\tests\Kernel;

use Drupal\KernelTests\KernelTestBase;
use Drupal\migrate\Plugin\MigrationInterface;

class MyMigrationTest extends KernelTestBase {

  protected static $modules = [
    'my_module',
    'migrate',
    // etc...
  ];
  
  public function testMigration(): void {
    /** @var \Drupal\migrate\Plugin\MigrationPluginManagerInterface 
    $plugin_manager */
    $plugin_manager = \Drupal::service('plugin.manager.migration');
    $migration = $plugin_manager
      ->createInstance('MY_MIGRATION');

    $this->assertInstanceOf(MigrationInterface::class, $migration);
  }

}
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.