View source  
  <?php
namespace Drupal\Tests\migrate_drupal\Kernel;
use Drupal\Component\Plugin\Exception\PluginNotFoundException;
class MigrateFieldPluginManagerTest extends MigrateDrupalTestBase {
  
  protected $pluginManager;
  
  protected static $modules = [
    'datetime',
    'system',
    'user',
    'field',
    'migrate_drupal',
    'options',
    'file',
    'image',
    'text',
    'link',
    'migrate_field_plugin_manager_test',
  ];
  
  public function setUp() : void {
    parent::setUp();
    $this->pluginManager = $this->container
      ->get('plugin.manager.migrate.field');
  }
  
  public function testPluginSelection() {
    $this
      ->assertSame('link', $this->pluginManager
      ->getPluginIdFromFieldType('link', [
      'core' => 6,
    ]));
    $this
      ->assertSame('link_field', $this->pluginManager
      ->getPluginIdFromFieldType('link_field', [
      'core' => 7,
    ]));
    $this
      ->assertSame('image', $this->pluginManager
      ->getPluginIdFromFieldType('image', [
      'core' => 7,
    ]));
    $this
      ->assertSame('file', $this->pluginManager
      ->getPluginIdFromFieldType('file', [
      'core' => 7,
    ]));
    $this
      ->assertSame('d6_file', $this->pluginManager
      ->getPluginIdFromFieldType('file', [
      'core' => 6,
    ]));
    $this
      ->assertSame('d6_text', $this->pluginManager
      ->getPluginIdFromFieldType('text', [
      'core' => 6,
    ]));
    $this
      ->assertSame('d7_text', $this->pluginManager
      ->getPluginIdFromFieldType('text', [
      'core' => 7,
    ]));
    
    $this
      ->assertSame('datetime', $this->pluginManager
      ->getPluginIdFromFieldType('date', [
      'core' => 6,
    ]));
    
    $this
      ->assertSame('d6_no_core_version_specified', $this->pluginManager
      ->getPluginIdFromFieldType('d6_no_core_version_specified', [
      'core' => 6,
    ]));
  }
  
  public function testNonExistentPluginExceptions($core, $field_type) {
    $this
      ->expectException(PluginNotFoundException::class);
    $this
      ->expectExceptionMessage(sprintf("Plugin ID '%s' was not found.", $field_type));
    $this->pluginManager
      ->getPluginIdFromFieldType($field_type, [
      'core' => $core,
    ]);
  }
  
  public function nonExistentPluginExceptionsData() {
    return [
      'D7 Filefield' => [
        'core' => 7,
        'field_type' => 'filefield',
      ],
      'D6 linkfield' => [
        'core' => 6,
        'field_type' => 'link_field',
      ],
      'D7 link' => [
        'core' => 7,
        'field_type' => 'link',
      ],
      'D7 no core version' => [
        'core' => 7,
        'field_type' => 'd6_no_core_version_specified',
      ],
    ];
  }
  
  public function testDefaultWeight() {
    $definitions = $this->pluginManager
      ->getDefinitions();
    $deprecated_plugins = [
      'date',
    ];
    foreach ($definitions as $id => $definition) {
      $this
        ->assertArrayHasKey('weight', $definition);
      if (in_array($id, $deprecated_plugins, TRUE)) {
        $this
          ->assertSame(9999999, $definition['weight']);
      }
      else {
        $this
          ->assertSame(0, $definition['weight']);
      }
    }
  }
}