You are here

class CommerceFieldNameTest in Commerce Migrate 3.0.x

Same name and namespace in other branches
  1. 8.2 modules/commerce/tests/src/Unit/Plugin/migrate/process/commerce1/CommerceFieldNameTest.php \Drupal\Tests\commerce_migrate_commerce\Unit\Plugin\migrate\process\commerce1\CommerceFieldNameTest
  2. 3.1.x modules/commerce/tests/src/Unit/Plugin/migrate/process/commerce1/CommerceFieldNameTest.php \Drupal\Tests\commerce_migrate_commerce\Unit\Plugin\migrate\process\commerce1\CommerceFieldNameTest

Tests the CommerceFieldName plugin.

@coversDefaultClass \Drupal\commerce_migrate_commerce\Plugin\migrate\process\commerce1\CommerceFieldName

@group commerce_migrate_commerce

Hierarchy

Expanded class hierarchy of CommerceFieldNameTest

File

modules/commerce/tests/src/Unit/Plugin/migrate/process/commerce1/CommerceFieldNameTest.php, line 15

Namespace

Drupal\Tests\commerce_migrate_commerce\Unit\Plugin\migrate\process\commerce1
View source
class CommerceFieldNameTest extends MigrateProcessTestCase {

  /**
   * Process plugin make_entity_unique.
   *
   * @var \Drupal\migrate\Plugin\migrate\process\MakeUniqueEntityField
   */
  protected $makeEntityUnique;

  /**
   * Process plugin manager.
   *
   * @var \Drupal\migrate\Plugin\MigratePluginManagerInterface
   */
  protected $processPluginManager;

  /**
   * {@inheritdoc}
   */
  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);
  }

  /**
   * Tests CommerceFieldName plugin for an attribute.
   *
   * @dataProvider providerCommerceFieldName
   */
  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);
  }

  /**
   * Data provider for testCommerceFieldName().
   */
  public function providerCommerceFieldName() {

    // Tests attribute field name is stripped of leading 'field_'.
    $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;
  }

  /**
   * Tests CommerceFieldName plugin for address field input.
   *
   * @dataProvider providerCommerceFieldNameAddress
   */
  public function testCommerceFieldNameAddress($source_properties = NULL, $expected = NULL) {
    $this->makeEntityUnique
      ->expects($this
      ->never())
      ->method('transform');
    $this->row
      ->expects($this
      ->never())
      ->method('getDestinationProperty');

    // Put the input values onto the Row.
    $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);
  }

  /**
   * Data provider for testCommerceFieldNameAddress().
   */
  public function providerCommerceFieldNameAddress() {
    $tests = [];

    // Tests address field name is changed.
    $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';

    // Tests address field name not on commerce_customer_profile is not changed.
    $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';

    // Tests the field name for field that is not an address is not changed.
    $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;
  }

  /**
   * Tests CommerceFieldName plugin for other entity types.
   *
   * @dataProvider providerCommerceFieldNameOther
   */
  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);
  }

  /**
   * Data provider for testCommerceFieldNameOther().
   */
  public function providerCommerceFieldNameOther() {
    $tests = [];

    // Tests address field name is changed.
    $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;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CommerceFieldNameTest::$makeEntityUnique protected property Process plugin make_entity_unique.
CommerceFieldNameTest::$processPluginManager protected property Process plugin manager.
CommerceFieldNameTest::providerCommerceFieldName public function Data provider for testCommerceFieldName().
CommerceFieldNameTest::providerCommerceFieldNameAddress public function Data provider for testCommerceFieldNameAddress().
CommerceFieldNameTest::providerCommerceFieldNameOther public function Data provider for testCommerceFieldNameOther().
CommerceFieldNameTest::setUp protected function Overrides MigrateProcessTestCase::setUp
CommerceFieldNameTest::testCommerceFieldName public function Tests CommerceFieldName plugin for an attribute.
CommerceFieldNameTest::testCommerceFieldNameAddress public function Tests CommerceFieldName plugin for address field input.
CommerceFieldNameTest::testCommerceFieldNameOther public function Tests CommerceFieldName plugin for other entity types.
MigrateProcessTestCase::$migrateExecutable protected property
MigrateProcessTestCase::$plugin protected property
MigrateProcessTestCase::$row protected property
MigrateTestCase::$idMap protected property The migration ID map.
MigrateTestCase::$migrationConfiguration protected property An array of migration configuration values. 7
MigrateTestCase::$migrationStatus protected property Local store for mocking setStatus()/getStatus().
MigrateTestCase::createSchemaFromRow protected function Generates a table schema from a row.
MigrateTestCase::getDatabase protected function Gets an SQLite database connection object for use in tests.
MigrateTestCase::getMigration protected function Retrieves a mocked migration.
MigrateTestCase::getValue protected function Gets the value on a row for a given key.
MigrateTestCase::queryResultTest public function Tests a query.
MigrateTestCase::retrievalAssertHelper protected function Asserts tested values during test retrieval.
PhpUnitWarnings::$deprecationWarnings private static property Deprecation warnings from PHPUnit to raise with @trigger_error().
PhpUnitWarnings::addWarning public function Converts PHPUnit deprecation warnings to E_USER_DEPRECATED.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals Deprecated protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.
UnitTestCase::setUpBeforeClass public static function