You are here

class MigrateFieldPluginManagerTest in Drupal 8

Same name in this branch
  1. 8 core/modules/migrate_drupal/tests/src/Unit/MigrateFieldPluginManagerTest.php \Drupal\Tests\migrate_drupal\Unit\MigrateFieldPluginManagerTest
  2. 8 core/modules/migrate_drupal/tests/src/Kernel/MigrateFieldPluginManagerTest.php \Drupal\Tests\migrate_drupal\Kernel\MigrateFieldPluginManagerTest
Same name and namespace in other branches
  1. 9 core/modules/migrate_drupal/tests/src/Unit/MigrateFieldPluginManagerTest.php \Drupal\Tests\migrate_drupal\Unit\MigrateFieldPluginManagerTest
  2. 10 core/modules/migrate_drupal/tests/src/Unit/MigrateFieldPluginManagerTest.php \Drupal\Tests\migrate_drupal\Unit\MigrateFieldPluginManagerTest

Tests the MigrateFieldPluginManager class.

@group migrate_drupal @coversDefaultClass \Drupal\migrate_drupal\Plugin\MigrateFieldPluginManager

Hierarchy

Expanded class hierarchy of MigrateFieldPluginManagerTest

File

core/modules/migrate_drupal/tests/src/Unit/MigrateFieldPluginManagerTest.php, line 19

Namespace

Drupal\Tests\migrate_drupal\Unit
View source
class MigrateFieldPluginManagerTest extends UnitTestCase {

  /**
   * Tests the plugin weighting system.
   *
   * @covers ::getPluginIdFromFieldType
   * @covers ::sortDefinitions
   * @covers ::findDefinitions
   * @dataProvider weightsData
   */
  public function testWeights($field_type, $core, $expected_plugin_id) {

    /* @var \Drupal\Core\Cache\CacheBackendInterface $cache */
    $cache = $this
      ->prophesize(CacheBackendInterface::class)
      ->reveal();

    /* @var \Drupal\Core\Extension\ModuleHandlerInterfaceModuleHandlerInterface $module_handler */
    $module_handler = $this
      ->prophesize(ModuleHandlerInterface::class)
      ->reveal();
    $discovery = $this
      ->prophesize(AnnotatedClassDiscovery::class);
    $discovery
      ->getDefinitions()
      ->willReturn($this
      ->pluginFixtureData());
    $manager = new MigrateFieldPluginManagerTestClass('field', new \ArrayObject(), $cache, $module_handler, MigrateField::class, $discovery
      ->reveal());
    if (!$expected_plugin_id) {
      $this
        ->expectException(PluginNotFoundException::class);
      $this
        ->expectExceptionMessage(sprintf("Plugin ID '%s' was not found.", $field_type));
    }
    $actual_plugin_id = $manager
      ->getPluginIdFromFieldType($field_type, [
      'core' => $core,
    ]);
    $this
      ->assertSame($expected_plugin_id, $actual_plugin_id);
  }

  /**
   * Provides data for testWeights().
   *
   * @return array
   *   The data.
   */
  public function weightsData() {
    return [
      'Field 1, D6' => [
        'field_type' => 'field_1',
        'core' => 6,
        'expected_plugin_id' => 'core_replacement_plugin',
      ],
      'Field 2, D6' => [
        'field_type' => 'field_2',
        'core' => 6,
        'expected_plugin_id' => 'field_1',
      ],
      'Field 3, D6' => [
        'field_type' => 'field_3',
        'core' => 6,
        'expected_plugin_id' => 'field_3',
      ],
      'Field 4, D6' => [
        'field_type' => 'field_4',
        'core' => 6,
        'expected_plugin_id' => 'field_4',
      ],
      'Field 5, D6' => [
        'field_type' => 'field_5',
        'core' => 6,
        'expected_plugin_id' => 'alphabetically_second',
      ],
      'Field 1, D7' => [
        'field_type' => 'field_1',
        'core' => 7,
        'expected_plugin_id' => 'core_replacement_plugin',
      ],
      'Field 2, D7' => [
        'field_type' => 'field_2',
        'core' => 7,
        'expected_plugin_id' => FALSE,
      ],
      'Field 3, D7' => [
        'field_type' => 'field_3',
        'core' => 7,
        'expected_plugin_id' => 'field_3',
      ],
      'Field 4, D7' => [
        'field_type' => 'field_4',
        'core' => 7,
        'expected_plugin_id' => 'contrib_override_plugin',
      ],
      'Field 5, D7' => [
        'field_type' => 'field_5',
        'core' => 7,
        'expected_plugin_id' => 'alphabetically_first',
      ],
    ];
  }

  /**
   * Returns test plugin data for the test class to use.
   *
   * @return array
   *   The test plugin data.
   */
  protected function pluginFixtureData() {
    return [
      // Represents a deprecated core field plugin that applied to field_1
      // and field_2 for Drupal 6.
      'field_1' => [
        'weight' => 99999999,
        'core' => [
          6,
        ],
        'type_map' => [
          'field_1' => 'field_1',
          'field_2' => 'field_2',
        ],
        'source_module' => 'system',
        'destination_module' => 'system',
      ],
      // Replacement for deprecated plugin for field_1 in Drupal 6 and 7.
      // Does not provide replacement for field_2.
      'core_replacement_plugin' => [
        'weight' => 0,
        'core' => [
          6,
          7,
        ],
        'type_map' => [
          'field_1' => 'field_1',
        ],
        'source_module' => 'system',
        'destination_module' => 'system',
      ],
      // Represents a core plugin with no type_map, applies to field_3 due to
      // plugin id.
      'field_3' => [
        'core' => [
          6,
          7,
        ],
        'type_map' => [],
        'weight' => 0,
        'source_module' => 'system',
        'destination_module' => 'system',
      ],
      // Represents a core plugin with no type_map, applies to field_4 due to
      // plugin id.
      'field_4' => [
        'core' => [
          6,
          7,
        ],
        'type_map' => [],
        'weight' => 0,
        'source_module' => 'system',
        'destination_module' => 'system',
      ],
      // Represents a contrib plugin overriding field_4 for Drupal 7 only.
      'contrib_override_plugin' => [
        'weight' => -100,
        'core' => [
          7,
        ],
        'type_map' => [
          'field_4' => 'field_4',
        ],
        'source_module' => 'system',
        'destination_module' => 'system',
      ],
      // field_5 is served by alphabetically_second in Drupal 6 and
      // alphabetically_first and alphabetically_second in Drupal 7.  It should
      // be served by the alphabetically_first in Drupal 7 regardless of the
      // order they appear here.
      'alphabetically_second' => [
        'weight' => 0,
        'core' => [
          6,
          7,
        ],
        'type_map' => [
          'field_5' => 'field_5',
        ],
        'source_module' => 'system',
        'destination_module' => 'system',
      ],
      'alphabetically_first' => [
        'weight' => 0,
        'core' => [
          7,
        ],
        'type_map' => [
          'field_5' => 'field_5',
        ],
        'source_module' => 'system',
        'destination_module' => 'system',
      ],
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MigrateFieldPluginManagerTest::pluginFixtureData protected function Returns test plugin data for the test class to use.
MigrateFieldPluginManagerTest::testWeights public function Tests the plugin weighting system.
MigrateFieldPluginManagerTest::weightsData public function Provides data for testWeights().
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
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::setUp protected function 340