Core and composer updates

This commit is contained in:
Rob Davies 2017-07-03 16:47:07 +01:00
parent a82634bb98
commit 62cac30480
1118 changed files with 21770 additions and 6306 deletions

View file

@ -7,7 +7,7 @@ source:
type: external_test
process:
nid:
plugin: migration
plugin: migration_lookup
source: name
migration: external_translated_test_node
type: constants/type

View file

@ -0,0 +1,7 @@
type: module
name: Migration High Water Test
description: 'Provides test fixtures for testing high water marks.'
package: Testing
core: 8.x
dependencies:
- migrate

View file

@ -1,7 +0,0 @@
type: module
name: Migrate SQL Source test
description: 'Provides a database table and records for SQL import testing.'
package: Testing
core: 8.x
dependencies:
- migrate

View file

@ -1,6 +1,6 @@
<?php
namespace Drupal\migrate_sql_test\Plugin\migrate\source;
namespace Drupal\migrate_high_water_test\Plugin\migrate\source;
use Drupal\migrate\Plugin\migrate\source\SqlBase;
@ -17,9 +17,13 @@ class HighWaterTest extends SqlBase {
* {@inheritdoc}
*/
public function query() {
$field_names = array_keys($this->fields());
$query = $this
->select('high_water_node', 'm')
->fields('m', array_keys($this->fields()));
->fields('m', $field_names);
foreach ($field_names as $field_name) {
$query->groupBy($field_name);
}
return $query;
}

View file

@ -5,7 +5,7 @@ namespace Drupal\Tests\migrate\Kernel;
/**
* Tests the high water handling.
*
* @covers \Drupal\migrate_sql_test\Plugin\migrate\source\HighWaterTest
* @covers \Drupal\migrate_high_water_test\Plugin\migrate\source\HighWaterTest
* @group migrate
*/
class HighWaterNotJoinableTest extends MigrateSqlSourceTestBase {
@ -13,15 +13,15 @@ class HighWaterNotJoinableTest extends MigrateSqlSourceTestBase {
/**
* {@inheritdoc}
*/
public static $modules = ['migrate', 'migrate_drupal', 'migrate_sql_test'];
public static $modules = ['migrate', 'migrate_drupal', 'migrate_high_water_test'];
/**
* {@inheritdoc}
*/
public function providerSource() {
$tests = [];
// Test high water when the map is not joinable.
// The source data.
$tests[0]['source_data']['high_water_node'] = [
[
@ -55,13 +55,62 @@ class HighWaterNotJoinableTest extends MigrateSqlSourceTestBase {
],
];
$tests[0]['expected_count'] = NULL;
// The expected count is the count returned by the query before the query
// is modified by SqlBase::initializeIterator().
$tests[0]['expected_count'] = 3;
$tests[0]['configuration'] = [
'high_water_property' => [
'name' => 'changed',
],
];
$tests[0]['high_water'] = $tests[0]['source_data']['high_water_node'][0]['changed'];
// Test high water initialized to NULL.
$tests[1]['source_data'] = $tests[0]['source_data'];
$tests[1]['expected_data'] = [
[
'id' => 1,
'title' => 'Item 1',
'changed' => 1,
],
[
'id' => 2,
'title' => 'Item 2',
'changed' => 2,
],
[
'id' => 3,
'title' => 'Item 3',
'changed' => 3,
],
];
$tests[1]['expected_count'] = $tests[0]['expected_count'];
$tests[1]['configuration'] = $tests[0]['configuration'];
$tests[1]['high_water'] = NULL;
// Test high water initialized to an empty string.
$tests[2]['source_data'] = $tests[0]['source_data'];
$tests[2]['expected_data'] = [
[
'id' => 1,
'title' => 'Item 1',
'changed' => 1,
],
[
'id' => 2,
'title' => 'Item 2',
'changed' => 2,
],
[
'id' => 3,
'title' => 'Item 3',
'changed' => 3,
],
];
$tests[2]['expected_count'] = $tests[0]['expected_count'];
$tests[2]['configuration'] = $tests[0]['configuration'];
$tests[2]['high_water'] = '';
return $tests;
}

View file

@ -17,7 +17,7 @@ class HighWaterTest extends MigrateTestBase {
'user',
'node',
'migrate',
'migrate_sql_test',
'migrate_high_water_test',
'field',
];

View file

@ -160,8 +160,8 @@ abstract class MigrateSourceTestBase extends KernelTestBase {
// If an expected count was given, assert it only if the plugin is
// countable.
if (is_numeric($expected_count)) {
$this->assertInstanceOf('\Iterator', $plugin);
$this->assertSame($expected_count, iterator_count($plugin));
$this->assertInstanceOf('\Countable', $plugin);
$this->assertCount($expected_count, $plugin);
}
$i = 0;
@ -185,6 +185,11 @@ abstract class MigrateSourceTestBase extends KernelTestBase {
}
}
}
// False positives occur if the foreach is not entered. So, confirm the
// foreach loop was entered if the expected count is greater than 0.
if ($expected_count > 0) {
$this->assertGreaterThan(0, $i);
}
}
}

View file

@ -0,0 +1,61 @@
<?php
namespace Drupal\Tests\migrate\Kernel\Plugin;
use Drupal\KernelTests\KernelTestBase;
use Drupal\migrate\MigrateExecutableInterface;
use Drupal\migrate\Row;
use Drupal\user\Entity\User;
/**
* Tests the EntityExists process plugin.
*
* @group migrate
*/
class EntityExistsTest extends KernelTestBase {
/**
* {@inheritdoc}
*/
public static $modules = ['migrate', 'system', 'user'];
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->installSchema('system', ['sequences']);
$this->installEntitySchema('user');
}
/**
* Test the EntityExists plugin.
*/
public function testEntityExists() {
$user = User::create([
'name' => $this->randomString(),
]);
$user->save();
$uid = $user->id();
$plugin = \Drupal::service('plugin.manager.migrate.process')
->createInstance('entity_exists', [
'entity_type' => 'user',
]);
$executable = $this->prophesize(MigrateExecutableInterface::class)->reveal();
$row = new Row();
// Ensure that the entity ID is returned if it really exists.
$value = $plugin->transform($uid, $executable, $row, 'buffalo');
$this->assertSame($uid, $value);
// Ensure that the plugin returns FALSE if the entity doesn't exist.
$value = $plugin->transform(420, $executable, $row, 'buffalo');
$this->assertFalse($value);
// Make sure the plugin can gracefully handle an array as input.
$value = $plugin->transform([$uid, 420], $executable, $row, 'buffalo');
$this->assertSame($uid, $value);
}
}

View file

@ -0,0 +1,36 @@
<?php
namespace Drupal\Tests\migrate\Kernel\Plugin;
use Drupal\KernelTests\KernelTestBase;
use Drupal\migrate\MigrateExecutableInterface;
use Drupal\migrate\Row;
/**
* Tests the Log process plugin.
*
* @group migrate
*/
class LogTest extends KernelTestBase {
/**
* {@inheritdoc}
*/
public static $modules = ['migrate'];
/**
* Test the Log plugin.
*/
public function testLog() {
$plugin = \Drupal::service('plugin.manager.migrate.process')
->createInstance('log');
$executable = $this->prophesize(MigrateExecutableInterface::class)->reveal();
$row = new Row();
$log_message = "Testing the log message";
// Ensure the log is getting saved.
$saved_message = $plugin->transform($log_message, $executable, $row, 'buffalo');
$this->assertSame($log_message, $saved_message);
}
}

View file

@ -192,6 +192,39 @@ class RowTest extends UnitTestCase {
$this->assertSame(['nid' => $this->testValues['nid']], $row->getSourceIdValues());
}
/**
* Tests the multiple source IDs.
*/
public function testMultipleSourceIdValues() {
// Set values in same order as ids.
$multi_source_ids = $this->testSourceIds + [
'vid' => 'Node revision',
'type' => 'Node type',
'langcode' => 'Node language',
];
$multi_source_ids_values = $this->testValues + [
'vid' => 1,
'type' => 'page',
'langcode' => 'en',
];
$row = new Row($multi_source_ids_values, $multi_source_ids);
$this->assertSame(array_keys($multi_source_ids), array_keys($row->getSourceIdValues()));
// Set values in different order.
$multi_source_ids = $this->testSourceIds + [
'vid' => 'Node revision',
'type' => 'Node type',
'langcode' => 'Node language',
];
$multi_source_ids_values = $this->testValues + [
'langcode' => 'en',
'type' => 'page',
'vid' => 1,
];
$row = new Row($multi_source_ids_values, $multi_source_ids);
$this->assertSame(array_keys($multi_source_ids), array_keys($row->getSourceIdValues()));
}
/**
* Tests getting the source property.
*

View file

@ -111,13 +111,14 @@ class SqlBaseTest extends UnitTestCase {
['driver' => 'mysql', 'username' => 'different_from_map', 'password' => 'different_from_map'],
['driver' => 'mysql', 'username' => 'different_from_source', 'password' => 'different_from_source'],
],
// Returns true because source and id map connection options are the same.
// Returns false because driver is pgsql and the databases are not the
// same.
[
FALSE,
TRUE,
TRUE,
TRUE,
['driver' => 'pgsql', 'username' => 'same_value', 'password' => 'same_value'],
['driver' => 'pgsql', 'username' => 'same_value', 'password' => 'same_value'],
['driver' => 'pgsql', 'database' => '1.pgsql', 'username' => 'same_value', 'password' => 'same_value'],
['driver' => 'pgsql', 'database' => '2.pgsql', 'username' => 'same_value', 'password' => 'same_value'],
],
// Returns false because driver is sqlite and the databases are not the
// same.

View file

@ -2,6 +2,9 @@
namespace Drupal\Tests\migrate\Unit\process;
@trigger_error('The ' . __NAMESPACE__ . '\DedupeEntityTest is deprecated in
Drupal 8.4.0 and will be removed before Drupal 9.0.0. Instead, use ' . __NAMESPACE__ . '\MakeUniqueEntityFieldTest', E_USER_DEPRECATED);
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\Query\QueryInterface;

View file

@ -0,0 +1,124 @@
<?php
namespace Drupal\Tests\migrate\Unit\process;
use Drupal\migrate\MigrateException;
use Drupal\migrate\Plugin\migrate\process\FormatDate;
/**
* Tests the format date process plugin.
*
* @group migrate
*
* @coversDefaultClass Drupal\migrate\Plugin\migrate\process\FormatDate
*/
class FormatDateTest extends MigrateProcessTestCase {
/**
* Tests that missing configuration will throw an exception.
*/
public function testMigrateExceptionMissingFromFormat() {
$configuration = [
'from_format' => '',
'to_format' => 'Y-m-d',
];
$this->setExpectedException(MigrateException::class, 'Format date plugin is missing from_format configuration.');
$this->plugin = new FormatDate($configuration, 'test_format_date', []);
$this->plugin->transform('01/05/1955', $this->migrateExecutable, $this->row, 'field_date');
}
/**
* Tests that missing configuration will throw an exception.
*/
public function testMigrateExceptionMissingToFormat() {
$configuration = [
'from_format' => 'm/d/Y',
'to_format' => '',
];
$this->setExpectedException(MigrateException::class, 'Format date plugin is missing to_format configuration.');
$this->plugin = new FormatDate($configuration, 'test_format_date', []);
$this->plugin->transform('01/05/1955', $this->migrateExecutable, $this->row, 'field_date');
}
/**
* Tests that date format mismatches will throw an exception.
*/
public function testMigrateExceptionBadFormat() {
$configuration = [
'from_format' => 'm/d/Y',
'to_format' => 'Y-m-d',
];
$this->setExpectedException(MigrateException::class, 'Format date plugin could not transform "January 5, 1955" using the format "m/d/Y". Error: The date cannot be created from a format.');
$this->plugin = new FormatDate($configuration, 'test_format_date', []);
$this->plugin->transform('January 5, 1955', $this->migrateExecutable, $this->row, 'field_date');
}
/**
* Tests transformation.
*
* @covers ::transform
*
* @dataProvider datesDataProvider
*
* @param $configuration
* The configuration of the migration process plugin.
* @param $value
* The source value for the migration process plugin.
* @param $expected
* The expected value of the migration process plugin.
*/
public function testTransform($configuration, $value, $expected) {
$this->plugin = new FormatDate($configuration, 'test_format_date', []);
$actual = $this->plugin->transform($value, $this->migrateExecutable, $this->row, 'field_date');
$this->assertEquals($expected, $actual);
}
/**
* Data provider of test dates.
*
* @return array
* Array of date formats and actual/expected values.
*/
public function datesDataProvider() {
return [
'datetime_date' => [
'configuration' => [
'from_format' => 'm/d/Y',
'to_format' => 'Y-m-d',
],
'value' => '01/05/1955',
'expected' => '1955-01-05',
],
'datetime_datetime' => [
'configuration' => [
'from_format' => 'm/d/Y H:i:s',
'to_format' => 'Y-m-d\TH:i:s',
],
'value' => '01/05/1955 10:43:22',
'expected' => '1955-01-05T10:43:22',
],
'empty_values' => [
'configuration' => [
'from_format' => 'm/d/Y',
'to_format' => 'Y-m-d',
],
'value' => '',
'expected' => '',
],
'timezone' => [
'configuration' => [
'from_format' => 'Y-m-d\TH:i:sO',
'to_format' => 'Y-m-d\TH:i:s',
'timezone' => 'America/Managua',
],
'value' => '2004-12-19T10:19:42-0600',
'expected' => '2004-12-19T10:19:42',
],
];
}
}

View file

@ -0,0 +1,214 @@
<?php
namespace Drupal\Tests\migrate\Unit\process;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\Query\QueryInterface;
use Drupal\migrate\Plugin\migrate\process\MakeUniqueEntityField;
use Drupal\Component\Utility\Unicode;
/**
* @coversDefaultClass \Drupal\migrate\Plugin\migrate\process\MakeUniqueEntityField
* @group migrate
*/
class MakeUniqueEntityFieldTest extends MigrateProcessTestCase {
/**
* The mock entity query.
*
* @var \Drupal\Core\Entity\Query\QueryInterface
* @var \Drupal\Core\Entity\Query\QueryFactory
*/
protected $entityQuery;
/**
* The mocked entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $entityTypeManager;
/**
* The migration configuration, initialized to set the ID to test.
*
* @var array
*/
protected $migrationConfiguration = [
'id' => 'test',
];
/**
* {@inheritdoc}
*/
protected function setUp() {
$this->entityQuery = $this->getMockBuilder('Drupal\Core\Entity\Query\QueryInterface')
->disableOriginalConstructor()
->getMock();
$this->entityTypeManager = $this->getMock(EntityTypeManagerInterface::class);
$storage = $this->getMock(EntityStorageInterface::class);
$storage->expects($this->any())
->method('getQuery')
->willReturn($this->entityQuery);
$this->entityTypeManager->expects($this->any())
->method('getStorage')
->with('test_entity_type')
->willReturn($storage);
parent::setUp();
}
/**
* Tests making an entity field value unique.
*
* @dataProvider providerTestMakeUniqueEntityField
*/
public function testMakeUniqueEntityField($count, $postfix = '', $start = NULL, $length = NULL) {
$configuration = [
'entity_type' => 'test_entity_type',
'field' => 'test_field',
];
if ($postfix) {
$configuration['postfix'] = $postfix;
}
$configuration['start'] = isset($start) ? $start : NULL;
$configuration['length'] = isset($length) ? $length : NULL;
$plugin = new MakeUniqueEntityField($configuration, 'make_unique', [], $this->getMigration(), $this->entityTypeManager);
$this->entityQueryExpects($count);
$value = $this->randomMachineName(32);
$actual = $plugin->transform($value, $this->migrateExecutable, $this->row, 'testproperty');
$expected = Unicode::substr($value, $start, $length);
$expected .= $count ? $postfix . $count : '';
$this->assertSame($expected, $actual);
}
/**
* Tests that invalid start position throws an exception.
*/
public function testMakeUniqueEntityFieldEntityInvalidStart() {
$configuration = [
'entity_type' => 'test_entity_type',
'field' => 'test_field',
'start' => 'foobar',
];
$plugin = new MakeUniqueEntityField($configuration, 'make_unique', [], $this->getMigration(), $this->entityTypeManager);
$this->setExpectedException('Drupal\migrate\MigrateException', 'The start position configuration key should be an integer. Omit this key to capture from the beginning of the string.');
$plugin->transform('test_start', $this->migrateExecutable, $this->row, 'testproperty');
}
/**
* Tests that invalid length option throws an exception.
*/
public function testMakeUniqueEntityFieldEntityInvalidLength() {
$configuration = [
'entity_type' => 'test_entity_type',
'field' => 'test_field',
'length' => 'foobar',
];
$plugin = new MakeUniqueEntityField($configuration, 'make_unique', [], $this->getMigration(), $this->entityTypeManager);
$this->setExpectedException('Drupal\migrate\MigrateException', 'The character length configuration key should be an integer. Omit this key to capture the entire string.');
$plugin->transform('test_length', $this->migrateExecutable, $this->row, 'testproperty');
}
/**
* Data provider for testMakeUniqueEntityField().
*/
public function providerTestMakeUniqueEntityField() {
return [
// Tests no duplication.
[0],
// Tests no duplication and start position.
[0, NULL, 10],
// Tests no duplication, start position, and length.
[0, NULL, 5, 10],
// Tests no duplication and length.
[0, NULL, NULL, 10],
// Tests duplication.
[3],
// Tests duplication and start position.
[3, NULL, 10],
// Tests duplication, start position, and length.
[3, NULL, 5, 10],
// Tests duplication and length.
[3, NULL, NULL, 10],
// Tests no duplication and postfix.
[0, '_'],
// Tests no duplication, postfix, and start position.
[0, '_', 5],
// Tests no duplication, postfix, start position, and length.
[0, '_', 5, 10],
// Tests no duplication, postfix, and length.
[0, '_', NULL, 10],
// Tests duplication and postfix.
[2, '_'],
// Tests duplication, postfix, and start position.
[2, '_', 5],
// Tests duplication, postfix, start position, and length.
[2, '_', 5, 10],
// Tests duplication, postfix, and length.
[2, '_', NULL, 10],
];
}
/**
* Helper function to add expectations to the mock entity query object.
*
* @param int $count
* The number of unique values to be set up.
*/
protected function entityQueryExpects($count) {
$this->entityQuery->expects($this->exactly($count + 1))
->method('condition')
->will($this->returnValue($this->entityQuery));
$this->entityQuery->expects($this->exactly($count + 1))
->method('count')
->will($this->returnValue($this->entityQuery));
$this->entityQuery->expects($this->exactly($count + 1))
->method('execute')
->will($this->returnCallback(function () use (&$count) { return $count--;}));
}
/**
* Tests making an entity field value unique only for migrated entities.
*/
public function testMakeUniqueEntityFieldMigrated() {
$configuration = [
'entity_type' => 'test_entity_type',
'field' => 'test_field',
'migrated' => TRUE,
];
$plugin = new MakeUniqueEntityField($configuration, 'make_unique', [], $this->getMigration(), $this->entityTypeManager);
// Setup the entityQuery used in MakeUniqueEntityFieldEntity::exists. The
// map, $map, is an array consisting of the four input parameters to the
// query condition method and then the query to return. Both 'forum' and
// 'test_vocab' are existing entities. There is no 'test_vocab1'.
$map = [];
foreach (['forums', 'test_vocab', 'test_vocab1'] as $id) {
$query = $this->prophesize(QueryInterface::class);
$query->willBeConstructedWith([]);
$query->execute()->willReturn($id === 'test_vocab1' ? [] : [$id]);
$map[] = ['test_field', $id, NULL, NULL, $query->reveal()];
}
$this->entityQuery
->method('condition')
->will($this->returnValueMap($map));
// Entity 'forums' is pre-existing, entity 'test_vocab' was migrated.
$this->idMap
->method('lookupSourceID')
->will($this->returnValueMap([
[['test_field' => 'forums'], FALSE],
[['test_field' => 'test_vocab'], ['source_id' => 42]],
]));
// Existing entity 'forums' was not migrated, value should not be unique.
$actual = $plugin->transform('forums', $this->migrateExecutable, $this->row, 'testproperty');
$this->assertEquals('forums', $actual, 'Pre-existing name is re-used');
// Entity 'test_vocab' was migrated, value should be unique.
$actual = $plugin->transform('test_vocab', $this->migrateExecutable, $this->row, 'testproperty');
$this->assertEquals('test_vocab1', $actual, 'Migrated name is deduplicated');
}
}

View file

@ -0,0 +1,202 @@
<?php
namespace Drupal\Tests\migrate\Unit\process;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\migrate\MigrateSkipProcessException;
use Drupal\migrate\Plugin\MigrationInterface;
use Drupal\migrate\Plugin\migrate\process\MigrationLookup;
use Drupal\migrate\Plugin\MigrateDestinationInterface;
use Drupal\migrate\Plugin\MigrateIdMapInterface;
use Drupal\migrate\Plugin\MigratePluginManager;
use Drupal\migrate\Plugin\MigrateSourceInterface;
use Drupal\migrate\Plugin\MigrationPluginManagerInterface;
use Prophecy\Argument;
/**
* @coversDefaultClass \Drupal\migrate\Plugin\migrate\process\MigrationLookup
* @group migrate
*/
class MigrationLookupTest extends MigrateProcessTestCase {
/**
* @covers ::transform
*/
public function testTransformWithStubSkipping() {
$migration_plugin = $this->prophesize(MigrationInterface::class);
$migration_plugin_manager = $this->prophesize(MigrationPluginManagerInterface::class);
$process_plugin_manager = $this->prophesize(MigratePluginManager::class);
$destination_id_map = $this->prophesize(MigrateIdMapInterface::class);
$destination_migration = $this->prophesize(MigrationInterface::class);
$destination_migration->getIdMap()->willReturn($destination_id_map->reveal());
$destination_id_map->lookupDestinationId([1])->willReturn(NULL);
// Ensure the migration plugin manager returns our migration.
$migration_plugin_manager->createInstances(Argument::exact(['destination_migration']))
->willReturn(['destination_migration' => $destination_migration->reveal()]);
$configuration = [
'no_stub' => TRUE,
'migration' => 'destination_migration',
];
$migration_plugin->id()->willReturn('actual_migration');
$destination_migration->getDestinationPlugin(TRUE)->shouldNotBeCalled();
$migration = new MigrationLookup($configuration, '', [], $migration_plugin->reveal(), $migration_plugin_manager->reveal(), $process_plugin_manager->reveal());
$result = $migration->transform(1, $this->migrateExecutable, $this->row, '');
$this->assertNull($result);
}
/**
* @covers ::transform
*/
public function testTransformWithStubbing() {
$migration_plugin = $this->prophesize(MigrationInterface::class);
$migration_plugin_manager = $this->prophesize(MigrationPluginManagerInterface::class);
$process_plugin_manager = $this->prophesize(MigratePluginManager::class);
$destination_id_map = $this->prophesize(MigrateIdMapInterface::class);
$destination_migration = $this->prophesize('Drupal\migrate\Plugin\Migration');
$destination_migration->getIdMap()->willReturn($destination_id_map->reveal());
$migration_plugin_manager->createInstances(['destination_migration'])
->willReturn(['destination_migration' => $destination_migration->reveal()]);
$destination_id_map->lookupDestinationId([1])->willReturn(NULL);
$destination_id_map->saveIdMapping(Argument::any(), Argument::any(), MigrateIdMapInterface::STATUS_NEEDS_UPDATE)->willReturn(NULL);
$configuration = [
'no_stub' => FALSE,
'migration' => 'destination_migration',
];
$migration_plugin->id()->willReturn('actual_migration');
$destination_migration->id()->willReturn('destination_migration');
$destination_migration->getDestinationPlugin(TRUE)->shouldBeCalled();
$destination_migration->getProcess()->willReturn([]);
$destination_migration->getSourceConfiguration()->willReturn([]);
$source_plugin = $this->prophesize(MigrateSourceInterface::class);
$source_plugin->getIds()->willReturn(['nid']);
$destination_migration->getSourcePlugin()->willReturn($source_plugin->reveal());
$destination_plugin = $this->prophesize(MigrateDestinationInterface::class);
$destination_plugin->import(Argument::any())->willReturn([2]);
$destination_migration->getDestinationPlugin(TRUE)->willReturn($destination_plugin->reveal());
$migration = new MigrationLookup($configuration, '', [], $migration_plugin->reveal(), $migration_plugin_manager->reveal(), $process_plugin_manager->reveal());
$result = $migration->transform(1, $this->migrateExecutable, $this->row, '');
$this->assertEquals(2, $result);
}
/**
* Tests that processing is skipped when the input value is empty.
*/
public function testSkipOnEmpty() {
$migration_plugin = $this->prophesize(MigrationInterface::class);
$migration_plugin_manager = $this->prophesize(MigrationPluginManagerInterface::class);
$process_plugin_manager = $this->prophesize(MigratePluginManager::class);
$configuration = [
'migration' => 'foobaz',
];
$migration_plugin->id()->willReturn(uniqid());
$migration = new MigrationLookup($configuration, 'migration_lookup', [], $migration_plugin->reveal(), $migration_plugin_manager->reveal(), $process_plugin_manager->reveal());
$this->setExpectedException(MigrateSkipProcessException::class);
$migration->transform(0, $this->migrateExecutable, $this->row, 'foo');
}
/**
* Tests a successful lookup.
*
* @dataProvider successfulLookupDataProvider
*
* @param array $source_id_values
* The source id(s) of the migration map.
* @param array $destination_id_values
* The destination id(s) of the migration map.
* @param string|array $source_value
* The source value(s) for the migration process plugin.
* @param string|array $expected_value
* The expected value(s) of the migration process plugin.
*/
public function testSuccessfulLookup($source_id_values, $destination_id_values, $source_value, $expected_value) {
$migration_plugin = $this->prophesize(MigrationInterface::class);
$migration_plugin_manager = $this->prophesize(MigrationPluginManagerInterface::class);
$process_plugin_manager = $this->prophesize(MigratePluginManager::class);
$configuration = [
'migration' => 'foobaz',
];
$migration_plugin->id()->willReturn(uniqid());
$id_map = $this->prophesize(MigrateIdMapInterface::class);
$id_map->lookupDestinationId($source_id_values)->willReturn($destination_id_values);
$migration_plugin->getIdMap()->willReturn($id_map->reveal());
$migration_plugin_manager->createInstances(['foobaz'])
->willReturn(['foobaz' => $migration_plugin->reveal()]);
$migrationStorage = $this->prophesize(EntityStorageInterface::class);
$migrationStorage
->loadMultiple(['foobaz'])
->willReturn([$migration_plugin->reveal()]);
$migration = new MigrationLookup($configuration, 'migration_lookup', [], $migration_plugin->reveal(), $migration_plugin_manager->reveal(), $process_plugin_manager->reveal());
$this->assertSame($expected_value, $migration->transform($source_value, $this->migrateExecutable, $this->row, 'foo'));
}
/**
* Provides data for the successful lookup test.
*
* @return array
*/
public function successfulLookupDataProvider() {
return [
// Test data for scalar to scalar.
[
// Source ID of the migration map.
[1],
// Destination ID of the migration map.
[3],
// Input value for the migration plugin.
1,
// Expected output value of the migration plugin.
3,
],
// Test data for scalar to array.
[
// Source ID of the migration map.
[1],
// Destination IDs of the migration map.
[3, 'foo'],
// Input value for the migration plugin.
1,
// Expected output values of the migration plugin.
[3, 'foo'],
],
// Test data for array to scalar.
[
// Source IDs of the migration map.
[1, 3],
// Destination ID of the migration map.
['foo'],
// Input values for the migration plugin.
[1, 3],
// Expected output value of the migration plugin.
'foo',
],
// Test data for array to array.
[
// Source IDs of the migration map.
[1, 3],
// Destination IDs of the migration map.
[3, 'foo'],
// Input values for the migration plugin.
[1, 3],
// Expected output values of the migration plugin.
[3, 'foo'],
],
];
}
}

View file

@ -2,6 +2,9 @@
namespace Drupal\Tests\migrate\Unit\process;
@trigger_error('The ' . __NAMESPACE__ . '\MigrationTest is deprecated in
Drupal 8.4.0 and will be removed before Drupal 9.0.0. Instead, use ' . __NAMESPACE__ . '\MigrationLookupTest', E_USER_DEPRECATED);
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\migrate\MigrateSkipProcessException;
use Drupal\migrate\Plugin\MigrationInterface;
@ -14,26 +17,49 @@ use Drupal\migrate\Plugin\MigrationPluginManagerInterface;
use Prophecy\Argument;
/**
* @deprecated in Drupal 8.4.x, to be removed before Drupal 9.0.x. Use
* \Drupal\Tests\migrate\Unit\process\MigrationLookupTest instead.
*
* @coversDefaultClass \Drupal\migrate\Plugin\migrate\process\Migration
* @group migrate
*/
class MigrationTest extends MigrateProcessTestCase {
/**
* @var \Drupal\migrate\Plugin\MigrationInterface
*/
protected $migration_plugin;
/**
* @var \Drupal\migrate\Plugin\MigrationPluginManagerInterface
*/
protected $migration_plugin_manager;
/**
* @var \Drupal\migrate\Plugin\MigratePluginManager
*/
protected $process_plugin_manager;
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->migration_plugin = $this->prophesize(MigrationInterface::class);
$this->migration_plugin_manager = $this->prophesize(MigrationPluginManagerInterface::class);
$this->process_plugin_manager = $this->prophesize(MigratePluginManager::class);
}
/**
* @covers ::transform
*/
public function testTransformWithStubSkipping() {
$migration_plugin = $this->prophesize(MigrationInterface::class);
$migration_plugin_manager = $this->prophesize(MigrationPluginManagerInterface::class);
$process_plugin_manager = $this->prophesize(MigratePluginManager::class);
$destination_id_map = $this->prophesize(MigrateIdMapInterface::class);
$destination_migration = $this->prophesize(MigrationInterface::class);
$destination_migration->getIdMap()->willReturn($destination_id_map->reveal());
$destination_id_map->lookupDestinationId([1])->willReturn(NULL);
$destination_migration = $this->getMigration();
$destination_migration->getDestinationPlugin(TRUE)->shouldNotBeCalled();
// Ensure the migration plugin manager returns our migration.
$migration_plugin_manager->createInstances(Argument::exact(['destination_migration']))
$this->migration_plugin_manager->createInstances(Argument::exact(['destination_migration']))
->willReturn(['destination_migration' => $destination_migration->reveal()]);
$configuration = [
@ -41,10 +67,9 @@ class MigrationTest extends MigrateProcessTestCase {
'migration' => 'destination_migration',
];
$migration_plugin->id()->willReturn('actual_migration');
$destination_migration->getDestinationPlugin(TRUE)->shouldNotBeCalled();
$this->migration_plugin->id()->willReturn('actual_migration');
$migration = new Migration($configuration, '', [], $migration_plugin->reveal(), $migration_plugin_manager->reveal(), $process_plugin_manager->reveal());
$migration = new Migration($configuration, '', [], $this->migration_plugin->reveal(), $this->migration_plugin_manager->reveal(), $this->process_plugin_manager->reveal());
$result = $migration->transform(1, $this->migrateExecutable, $this->row, '');
$this->assertNull($result);
}
@ -53,24 +78,16 @@ class MigrationTest extends MigrateProcessTestCase {
* @covers ::transform
*/
public function testTransformWithStubbing() {
$migration_plugin = $this->prophesize(MigrationInterface::class);
$migration_plugin_manager = $this->prophesize(MigrationPluginManagerInterface::class);
$process_plugin_manager = $this->prophesize(MigratePluginManager::class);
$destination_id_map = $this->prophesize(MigrateIdMapInterface::class);
$destination_migration = $this->prophesize('Drupal\migrate\Plugin\Migration');
$destination_migration->getIdMap()->willReturn($destination_id_map->reveal());
$migration_plugin_manager->createInstances(['destination_migration'])
$destination_migration = $this->getMigration();
$this->migration_plugin_manager->createInstances(['destination_migration'])
->willReturn(['destination_migration' => $destination_migration->reveal()]);
$destination_id_map->lookupDestinationId([1])->willReturn(NULL);
$destination_id_map->saveIdMapping(Argument::any(), Argument::any(), MigrateIdMapInterface::STATUS_NEEDS_UPDATE)->willReturn(NULL);
$configuration = [
'no_stub' => FALSE,
'migration' => 'destination_migration',
];
$migration_plugin->id()->willReturn('actual_migration');
$this->migration_plugin->id()->willReturn('actual_migration');
$destination_migration->id()->willReturn('destination_migration');
$destination_migration->getDestinationPlugin(TRUE)->shouldBeCalled();
$destination_migration->getProcess()->willReturn([]);
@ -83,24 +100,36 @@ class MigrationTest extends MigrateProcessTestCase {
$destination_plugin->import(Argument::any())->willReturn([2]);
$destination_migration->getDestinationPlugin(TRUE)->willReturn($destination_plugin->reveal());
$migration = new Migration($configuration, '', [], $migration_plugin->reveal(), $migration_plugin_manager->reveal(), $process_plugin_manager->reveal());
$migration = new Migration($configuration, '', [], $this->migration_plugin->reveal(), $this->migration_plugin_manager->reveal(), $this->process_plugin_manager->reveal());
$result = $migration->transform(1, $this->migrateExecutable, $this->row, '');
$this->assertEquals(2, $result);
}
/**
* Creates a mock Migration instance.
*
* @return \Prophecy\Prophecy\ObjectProphecy
* A mock Migration instance.
*/
protected function getMigration() {
$id_map = $this->prophesize(MigrateIdMapInterface::class);
$id_map->lookupDestinationId([1])->willReturn(NULL);
$id_map->saveIdMapping(Argument::any(), Argument::any(), MigrateIdMapInterface::STATUS_NEEDS_UPDATE)->willReturn(NULL);
$migration = $this->prophesize(MigrationInterface::class);
$migration->getIdMap()->willReturn($id_map->reveal());
return $migration;
}
/**
* Tests that processing is skipped when the input value is empty.
*/
public function testSkipOnEmpty() {
$migration_plugin = $this->prophesize(MigrationInterface::class);
$migration_plugin_manager = $this->prophesize(MigrationPluginManagerInterface::class);
$process_plugin_manager = $this->prophesize(MigratePluginManager::class);
$configuration = [
'migration' => 'foobaz',
];
$migration_plugin->id()->willReturn(uniqid());
$migration = new Migration($configuration, 'migration', [], $migration_plugin->reveal(), $migration_plugin_manager->reveal(), $process_plugin_manager->reveal());
$this->migration_plugin->id()->willReturn(uniqid());
$migration = new Migration($configuration, 'migration', [], $this->migration_plugin->reveal(), $this->migration_plugin_manager->reveal(), $this->process_plugin_manager->reveal());
$this->setExpectedException(MigrateSkipProcessException::class);
$migration->transform(0, $this->migrateExecutable, $this->row, 'foo');
}
@ -120,28 +149,24 @@ class MigrationTest extends MigrateProcessTestCase {
* The expected value(s) of the migration process plugin.
*/
public function testSuccessfulLookup($source_id_values, $destination_id_values, $source_value, $expected_value) {
$migration_plugin = $this->prophesize(MigrationInterface::class);
$migration_plugin_manager = $this->prophesize(MigrationPluginManagerInterface::class);
$process_plugin_manager = $this->prophesize(MigratePluginManager::class);
$configuration = [
'migration' => 'foobaz',
];
$migration_plugin->id()->willReturn(uniqid());
$this->migration_plugin->id()->willReturn(uniqid());
$id_map = $this->prophesize(MigrateIdMapInterface::class);
$id_map->lookupDestinationId($source_id_values)->willReturn($destination_id_values);
$migration_plugin->getIdMap()->willReturn($id_map->reveal());
$this->migration_plugin->getIdMap()->willReturn($id_map->reveal());
$migration_plugin_manager->createInstances(['foobaz'])
->willReturn(['foobaz' => $migration_plugin->reveal()]);
$this->migration_plugin_manager->createInstances(['foobaz'])
->willReturn(['foobaz' => $this->migration_plugin->reveal()]);
$migrationStorage = $this->prophesize(EntityStorageInterface::class);
$migrationStorage
->loadMultiple(['foobaz'])
->willReturn([$migration_plugin->reveal()]);
->willReturn([$this->migration_plugin->reveal()]);
$migration = new Migration($configuration, 'migration', [], $migration_plugin->reveal(), $migration_plugin_manager->reveal(), $process_plugin_manager->reveal());
$migration = new Migration($configuration, 'migration', [], $this->migration_plugin->reveal(), $this->migration_plugin_manager->reveal(), $this->process_plugin_manager->reveal());
$this->assertSame($expected_value, $migration->transform($source_value, $this->migrateExecutable, $this->row, 'foo'));
}
@ -152,49 +177,29 @@ class MigrationTest extends MigrateProcessTestCase {
*/
public function successfulLookupDataProvider() {
return [
// Test data for scalar to scalar.
[
// Source ID of the migration map.
[1],
// Destination ID of the migration map.
[3],
// Input value for the migration plugin.
1,
// Expected output value of the migration plugin.
3,
'scalar_to_scalar' => [
'source_ids' => [1],
'destination_ids' => [3],
'input_value' => 1,
'expected_value' => 3,
],
// Test data for scalar to array.
[
// Source ID of the migration map.
[1],
// Destination IDs of the migration map.
[3, 'foo'],
// Input value for the migration plugin.
1,
// Expected output values of the migration plugin.
[3, 'foo'],
'scalar_to_array' => [
'source_ids' => [1],
'destination_ids' => [3, 'foo'],
'input_value' => 1,
'expected_value' => [3, 'foo'],
],
// Test data for array to scalar.
[
// Source IDs of the migration map.
[1, 3],
// Destination ID of the migration map.
['foo'],
// Input values for the migration plugin.
[1, 3],
// Expected output value of the migration plugin.
'foo',
'array_to_scalar' => [
'source_ids' => [1, 3],
'destination_ids' => ['foo'],
'input_value' => [1, 3],
'expected_value' => 'foo',
],
// Test data for array to array.
[
// Source IDs of the migration map.
[1, 3],
// Destination IDs of the migration map.
[3, 'foo'],
// Input values for the migration plugin.
[1, 3],
// Expected output values of the migration plugin.
[3, 'foo'],
'array_to_array' => [
'source_ids' => [1, 3],
'destination_ids' => [3, 'foo'],
'input_value' => [1, 3],
'expected_value' => [3, 'foo'],
],
];
}

View file

@ -1,6 +1,7 @@
<?php
namespace Drupal\Tests\migrate\Unit\process;
use Drupal\migrate\MigrateSkipProcessException;
use Drupal\migrate\MigrateSkipRowException;
use Drupal\migrate\Plugin\migrate\process\SkipOnEmpty;
@ -53,4 +54,33 @@ class SkipOnEmptyTest extends MigrateProcessTestCase {
$this->assertSame($value, ' ');
}
/**
* Tests that a skip row exception without a message is raised.
*
* @covers ::row
*/
public function testRowSkipWithoutMessage() {
$configuration = [
'method' => 'row',
];
$process = new SkipOnEmpty($configuration, 'skip_on_empty', []);
$this->setExpectedException(MigrateSkipRowException::class);
$process->transform('', $this->migrateExecutable, $this->row, 'destinationproperty');
}
/**
* Tests that a skip row exception with a message is raised.
*
* @covers ::row
*/
public function testRowSkipWithMessage() {
$configuration = [
'method' => 'row',
'message' => 'The value is empty',
];
$process = new SkipOnEmpty($configuration, 'skip_on_empty', []);
$this->setExpectedException(MigrateSkipRowException::class, 'The value is empty');
$process->transform('', $this->migrateExecutable, $this->row, 'destinationproperty');
}
}

View file

@ -0,0 +1,45 @@
<?php
namespace Drupal\Tests\migrate\Unit\process;
use Drupal\migrate\MigrateSkipRowException;
use Drupal\migrate\Plugin\migrate\process\SkipRowIfNotSet;
/**
* Tests the skip row if not set process plugin.
*
* @group migrate
* @coversDefaultClass \Drupal\migrate\Plugin\migrate\process\SkipRowIfNotSet
*/
class SkipRowIfNotSetTest extends MigrateProcessTestCase {
/**
* Tests that a skip row exception without a message is raised.
*
* @covers ::transform
*/
public function testRowSkipWithoutMessage() {
$configuration = [
'index' => 'some_key',
];
$process = new SkipRowIfNotSet($configuration, 'skip_row_if_not_set', []);
$this->setExpectedException(MigrateSkipRowException::class);
$process->transform('', $this->migrateExecutable, $this->row, 'destinationproperty');
}
/**
* Tests that a skip row exception with a message is raised.
*
* @covers ::transform
*/
public function testRowSkipWithMessage() {
$configuration = [
'index' => 'some_key',
'message' => "The 'some_key' key is not set",
];
$process = new SkipRowIfNotSet($configuration, 'skip_row_if_not_set', []);
$this->setExpectedException(MigrateSkipRowException::class, "The 'some_key' key is not set");
$process->transform('', $this->migrateExecutable, $this->row, 'destinationproperty');
}
}