View source
<?php
namespace Drupal\Tests\commerce_migrate_commerce\Unit\Plugin\migrate\process\commerce1;
use Drupal\commerce_migrate_commerce\Plugin\migrate\process\commerce1\CommerceFieldName;
use Drupal\Tests\migrate\Unit\process\MigrateProcessTestCase;
class CommerceFieldNameTest extends MigrateProcessTestCase {
protected $makeEntityUnique;
protected $processPluginManager;
protected function setUp() {
parent::setUp();
$migration_plugin_manager = $this
->getMockBuilder('Drupal\\migrate\\Plugin\\MigrationPluginManagerInterface')
->disableOriginalConstructor()
->getMock();
$this->processPluginManager = $this
->getMockBuilder('Drupal\\migrate\\Plugin\\MigratePluginManagerInterface')
->disableOriginalConstructor()
->getMock();
$this->makeEntityUnique = $this
->getMockBuilder('Drupal\\migrate\\Plugin\\migrate\\process\\MakeUniqueEntityField')
->disableOriginalConstructor()
->getMock();
$this->migrateExecutable = $this
->getMockBuilder('Drupal\\migrate\\MigrateExecutable')
->disableOriginalConstructor()
->getMock();
$this->plugin = new CommerceFieldName([], 'test', [], $migration_plugin_manager, $this->processPluginManager);
}
public function testCommerceFieldName($destination_property = NULL, $source_properties = NULL, $expected = NULL) {
$this->makeEntityUnique
->expects($this
->once())
->method('transform')
->willReturn('color');
$this->processPluginManager
->method('createInstance')
->willReturn($this->makeEntityUnique);
$this->row
->expects($this
->any())
->method('getDestinationProperty')
->willReturn($destination_property);
$this->row
->method('getSourceProperty')
->will($this
->onConsecutiveCalls($source_properties[0], $source_properties[1], $source_properties[2], $source_properties[3]));
$value = $this->plugin
->transform('', $this->migrateExecutable, $this->row, 'destination_property');
$this
->assertSame($expected, $value);
}
public function providerCommerceFieldName() {
$tests[0]['destination_property'] = NULL;
$field_name = 'field_color';
$entity_type = 'commerce_product';
$type = 'taxonomy_term_reference';
$instances = [
[
'data' => serialize([
'widget' => [
'type' => 'options_select',
],
]),
],
[
'data' => serialize([
'widget' => [
'type' => 'text',
],
]),
],
];
$tests[0]['source_properties'] = [
$field_name,
$entity_type,
$type,
$instances,
];
$tests[0]['expected'] = 'color';
return $tests;
}
public function testCommerceFieldNameAddress($source_properties = NULL, $expected = NULL) {
$this->makeEntityUnique
->expects($this
->never())
->method('transform');
$this->row
->expects($this
->never())
->method('getDestinationProperty');
$this->row
->method('getSourceProperty')
->will($this
->onConsecutiveCalls($source_properties[0], $source_properties[1], $source_properties[2], $source_properties[3]));
$value = $this->plugin
->transform('', $this->migrateExecutable, $this->row, 'destination_property');
$this
->assertSame($expected, $value);
}
public function providerCommerceFieldNameAddress() {
$tests = [];
$field_name = 'color';
$entity_type = 'commerce_customer_profile';
$type = 'addressfield';
$instances = [
[
'data' => serialize([
'widget' => [
'type' => 'options_select',
],
]),
],
[
'data' => serialize([
'widget' => [
'type' => 'text',
],
]),
],
];
$tests[0]['source_properties'] = [
$field_name,
$entity_type,
$type,
$instances,
];
$tests[0]['expected'] = 'address';
$field_name = 'field_address';
$entity_type = 'node';
$type = 'addressfield';
$instances = [
[
'data' => serialize([
'widget' => [
'type' => 'options_select',
],
]),
],
[
'data' => serialize([
'widget' => [
'type' => 'text',
],
]),
],
];
$tests[1]['source_properties'] = [
$field_name,
$entity_type,
$type,
$instances,
];
$tests[1]['expected'] = 'field_address';
$field_name = 'field_text';
$entity_type = 'node';
$type = 'text';
$instances = [
[
'data' => serialize([
'widget' => [
'type' => 'options_select',
],
]),
],
[
'data' => serialize([
'widget' => [
'type' => 'text',
],
]),
],
];
$tests[1]['source_properties'] = [
$field_name,
$entity_type,
$type,
$instances,
];
$tests[1]['expected'] = 'field_text';
return $tests;
}
public function testCommerceFieldNameOther($source_properties = NULL, $expected = NULL) {
$this->makeEntityUnique
->expects($this
->never())
->method('transform');
$this->row
->expects($this
->never())
->method('getDestinationProperty');
$this->row
->method('getSourceProperty')
->will($this
->onConsecutiveCalls($source_properties[0], $source_properties[1], $source_properties[2], $source_properties[3]));
$value = $this->plugin
->transform('', $this->migrateExecutable, $this->row, 'destination_property');
$this
->assertSame($expected, $value);
}
public function providerCommerceFieldNameOther() {
$tests = [];
$field_name = 'field_image';
$entity_type = 'node';
$type = 'text';
$instances = [
[
'data' => serialize([
'widget' => [
'type' => 'options_select',
],
]),
],
[
'data' => serialize([
'widget' => [
'type' => 'text',
],
]),
],
];
$tests[0]['source_properties'] = [
$field_name,
$entity_type,
$type,
$instances,
];
$tests[0]['expected'] = 'field_image';
return $tests;
}
}