class DomMigrationLookupTest in Migrate Plus 8.5
Tests the dom_migration_lookup process plugin.
@group migrate @coversDefaultClass \Drupal\migrate_plus\Plugin\migrate\process\DomMigrationLookup
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\migrate\Unit\MigrateTestCase
- class \Drupal\Tests\migrate\Unit\process\MigrateProcessTestCase
- class \Drupal\Tests\migrate_plus\Unit\process\DomMigrationLookupTest
- class \Drupal\Tests\migrate\Unit\process\MigrateProcessTestCase
- class \Drupal\Tests\migrate\Unit\MigrateTestCase
Expanded class hierarchy of DomMigrationLookupTest
File
- tests/
src/ Unit/ process/ DomMigrationLookupTest.php, line 20
Namespace
Drupal\Tests\migrate_plus\Unit\processView source
class DomMigrationLookupTest extends MigrateProcessTestCase {
/**
* Example configuration for the dom_migration_lookup process plugin.
*
* @var array
*/
protected $exampleConfiguration = [
'plugin' => 'dom_migration_lookup',
'mode' => 'attribute',
'xpath' => '//a',
'attribute_options' => [
'name' => 'href',
],
'search' => '@/user/(\\d+)@',
'replace' => '/user/[mapped-id]',
'migrations' => [
'users' => [],
'people' => [
'replace' => '/people/[mapped-id]',
],
],
];
/**
* Mock a migration.
*
* @var \Drupal\migrate\Plugin\MigrationInterface
*/
protected $migration;
/**
* Mock a process plugin manager.
*
* @var \Drupal\migrate\Plugin\MigratePluginManagerInterface
*/
protected $processPluginManager;
/**
* {@inheritdoc}
*/
protected function setUp() : void {
parent::setUp();
// Mock a migration.
$prophecy = $this
->prophesize(MigrationInterface::class);
$this->migration = $prophecy
->reveal();
// Mock two migration lookup plugins.
$prophecy = $this
->prophesize(MigrateProcessInterface::class);
$prophecy
->transform('123', $this->migrateExecutable, $this->row, 'destinationproperty')
->willReturn('321');
$prophecy
->transform('456', $this->migrateExecutable, $this->row, 'destinationproperty')
->willReturn(NULL);
$users_lookup_plugin = $prophecy
->reveal();
$prophecy = $this
->prophesize(MigrateProcessInterface::class);
$prophecy
->transform('123', $this->migrateExecutable, $this->row, 'destinationproperty')
->willReturn('ignored');
$prophecy
->transform('456', $this->migrateExecutable, $this->row, 'destinationproperty')
->willReturn('654');
$people_lookup_plugin = $prophecy
->reveal();
// Mock a process plugin manager.
$prophecy = $this
->prophesize(MigratePluginManager::class);
$users_configuration = [
'migration' => 'users',
'no_stub' => TRUE,
];
$people_configuration = [
'migration' => 'people',
'no_stub' => TRUE,
];
$prophecy
->createInstance('migration_lookup', $users_configuration, $this->migration)
->willReturn($users_lookup_plugin);
$prophecy
->createInstance('migration_lookup', $people_configuration, $this->migration)
->willReturn($people_lookup_plugin);
$this->processPluginManager = $prophecy
->reveal();
}
/**
* @covers ::__construct
*
* @dataProvider providerTestConfigValidation
*/
public function testConfigValidation(array $config_overrides, $message) : void {
$configuration = $config_overrides + $this->exampleConfiguration;
$value = '<p>A simple paragraph.</p>';
$this
->expectException(InvalidPluginDefinitionException::class);
$this
->expectExceptionMessage($message);
(new DomMigrationLookup($configuration, 'dom_migration_lookup', [], $this->migration, $this->processPluginManager))
->transform($value, $this->migrateExecutable, $this->row, 'destinationproperty');
}
/**
* Dataprovider for testConfigValidation().
*/
public function providerTestConfigValidation() : array {
$cases = [
'migrations-empty' => [
[
'migrations' => [],
],
"Configuration option 'migration' is required.",
],
'migrations-invalid' => [
[
'migrations' => 42,
],
"Configuration option 'migration' should be a keyed array.",
],
'replace-null' => [
[
'replace' => NULL,
],
"Please define either a global replace for all migrations, or a specific one for 'migrations.users'.",
],
];
return $cases;
}
/**
* @covers ::transform
*/
public function testTransformInvalidInput() : void {
$value = 'string';
$this
->expectException(MigrateSkipRowException::class);
$this
->expectExceptionMessage('The dom_migration_lookup plugin in the destinationproperty process pipeline requires a \\DOMDocument object. You can use the dom plugin to convert a string to \\DOMDocument.');
(new DomMigrationLookup($this->exampleConfiguration, 'dom_migration_lookup', [], $this->migration, $this->processPluginManager))
->transform($value, $this->migrateExecutable, $this->row, 'destinationproperty');
}
/**
* @covers ::transform
*
* @dataProvider providerTestTransform
*/
public function testTransform($config_overrides, $input_string, $output_string) : void {
$configuration = $config_overrides + $this->exampleConfiguration;
$value = Html::load($input_string);
$document = (new DomMigrationLookup($configuration, 'dom_migration_lookup', [], $this->migration, $this->processPluginManager))
->transform($value, $this->migrateExecutable, $this->row, 'destinationproperty');
$this
->assertTrue($document instanceof \DOMDocument);
$this
->assertEquals($output_string, Html::serialize($document));
}
/**
* Dataprovider for testTransform().
*/
public function providerTestTransform() : array {
$cases = [
'users-migration' => [
[],
'<a href="/user/123">text</a>',
'<a href="/user/321">text</a>',
],
'people-migration' => [
[],
'<a href="https://www.example.com/user/456">text</a>',
'<a href="https://www.example.com/people/654">text</a>',
],
'no-match' => [
[
'search' => '@www\\.mysite\\.com/user/(\\d+)@',
],
'<a href="https://www.example.com/user/456">text</a>',
'<a href="https://www.example.com/user/456">text</a>',
],
];
return $cases;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DomMigrationLookupTest:: |
protected | property | Example configuration for the dom_migration_lookup process plugin. | |
DomMigrationLookupTest:: |
protected | property | Mock a migration. | |
DomMigrationLookupTest:: |
protected | property | Mock a process plugin manager. | |
DomMigrationLookupTest:: |
public | function | Dataprovider for testConfigValidation(). | |
DomMigrationLookupTest:: |
public | function | Dataprovider for testTransform(). | |
DomMigrationLookupTest:: |
protected | function |
Overrides MigrateProcessTestCase:: |
|
DomMigrationLookupTest:: |
public | function | @covers ::__construct | |
DomMigrationLookupTest:: |
public | function | @covers ::transform | |
DomMigrationLookupTest:: |
public | function | @covers ::transform | |
MigrateProcessTestCase:: |
protected | property | ||
MigrateProcessTestCase:: |
protected | property | ||
MigrateProcessTestCase:: |
protected | property | ||
MigrateTestCase:: |
protected | property | The migration ID map. | |
MigrateTestCase:: |
protected | property | An array of migration configuration values. | 16 |
MigrateTestCase:: |
protected | property | Local store for mocking setStatus()/getStatus(). | |
MigrateTestCase:: |
protected | function | Generates a table schema from a row. | |
MigrateTestCase:: |
protected | function | Gets an SQLite database connection object for use in tests. | |
MigrateTestCase:: |
protected | function | Retrieves a mocked migration. | 1 |
MigrateTestCase:: |
protected | function | Gets the value on a row for a given key. | 1 |
MigrateTestCase:: |
public | function | Tests a query. | |
MigrateTestCase:: |
protected | function | Asserts tested values during test retrieval. | |
PhpunitCompatibilityTrait:: |
public | function | Returns a mock object for the specified class using the available method. | |
PhpunitCompatibilityTrait:: |
public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | 1 |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. |