diff --git a/src/Plugin/migrate/source/d7/MetatagFieldInstance.php b/src/Plugin/migrate/source/d7/MetatagFieldInstance.php index b41305a..b375dbd 100644 --- a/src/Plugin/migrate/source/d7/MetatagFieldInstance.php +++ b/src/Plugin/migrate/source/d7/MetatagFieldInstance.php @@ -8,7 +8,10 @@ namespace Drupal\metatag\Plugin\migrate\source\d7; use Drupal\Core\Entity\Entity; +use Drupal\Core\Entity\EntityTypeBundleInfo; +use Drupal\migrate\Plugin\MigrationInterface; use Drupal\migrate_drupal\Plugin\migrate\source\DrupalSqlBase; +use Symfony\Component\DependencyInjection\ContainerInterface; /** * Drupal 7 metatag field instances. @@ -20,6 +23,31 @@ class MetatagFieldInstance extends DrupalSqlBase { /** + * @var EntityTypeBundleInfo + */ + protected $entityTypeBundleInfo; + + public function __construct($configuration, $plugin_id, $plugin_definition, $migration, $state, $entity_manager, $entity_type_bundle_info){ + parent::__construct($configuration, $plugin_id, $plugin_definition, $migration, $state, $entity_manager); + $this->entityTypeBundleInfo = $entity_type_bundle_info; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) { + return new static( + $configuration, + $plugin_id, + $plugin_definition, + $migration, + $container->get('state'), + $container->get('entity.manager'), + $container->get('entity_type.bundle.info') + ); + } + + /** * {@inheritdoc} */ public function query() { @@ -44,8 +72,7 @@ public function fields() { public function initializeIterator() { $bundles = []; foreach (parent::initializeIterator() as $instance) { - $bundle_info = \Drupal::getContainer() - ->get('entity_type.bundle.info') + $bundle_info = $this->entityTypeBundleInfo ->getBundleInfo($instance['entity_type']); foreach (array_keys($bundle_info) as $bundle) { $bundles[] = [ diff --git a/tests/src/Kernel/Migrate/d7/MigrateMetatagTest.php b/tests/src/Kernel/Migrate/d7/MigrateMetatagTest.php index bb1e924..2b9fe3b 100644 --- a/tests/src/Kernel/Migrate/d7/MigrateMetatagTest.php +++ b/tests/src/Kernel/Migrate/d7/MigrateMetatagTest.php @@ -35,7 +35,7 @@ class MigrateMetatagTest extends MigrateDrupal7TestBase { */ protected function setUp() { parent::setUp(); - $this->loadFixture(__DIR__ . '/../../../../../tests/fixtures/drupal7.php'); + $this->loadFixture(__DIR__ . '/../../../../fixtures/drupal7.php'); $this->installEntitySchema('node'); $this->installEntitySchema('comment'); diff --git a/tests/src/Unit/Migrate/d7/MetatagFieldInstanceTest.php b/tests/src/Unit/Migrate/d7/MetatagFieldInstanceTest.php new file mode 100644 index 0000000..530508d --- /dev/null +++ b/tests/src/Unit/Migrate/d7/MetatagFieldInstanceTest.php @@ -0,0 +1,86 @@ + 'test', + 'source' => array( + 'plugin' => 'd7_metatag_field_instance', + ), + ); + + protected $expectedResults = array( + array( + 'entity_type' => 'node', + 'bundle' => 'test_content_type', + ), + array( + 'entity_type' => 'taxonomy_term', + 'bundle' => 'test_vocabulary', + ), + array( + 'entity_type' => 'user', + 'bundle' => 'user', + ), + ); + + /** + * {@inheritdoc} + */ + protected function setUp() { + $this->databaseContents['metatag'] = $this->expectedResults; + + $module_handler = $this->getMock('Drupal\Core\Extension\ModuleHandlerInterface'); + $state = $this->getMock('Drupal\Core\State\StateInterface'); + $entity_manager = $this->getMock('Drupal\Core\Entity\EntityManagerInterface'); + $entity_type_bundle_info = $this->getMockBuilder('Drupal\Core\Entity\EntityTypeBundleInfo') + ->disableOriginalConstructor() + ->getMock(); + $entity_type_bundle_info->expects($this->any()) + ->method('getBundleInfo') + ->willReturnMap([ + ['node', ['test_content_type' => 'test_content_type']], + ['taxonomy_term', ['test_vocabulary' => 'test_vocabulary']], + ['user', ['user' => 'user']], + ]); + + $migration = $this->getMigration(); + $migration->expects($this->any()) + ->method('getHighWater') + ->will($this->returnValue(static::ORIGINAL_HIGH_WATER)); + + // Setup the plugin. + $plugin_class = static::PLUGIN_CLASS; + $plugin = new $plugin_class($this->migrationConfiguration['source'], $this->migrationConfiguration['source']['plugin'], array(), $migration, $state, $entity_manager, $entity_type_bundle_info); + + // Do some reflection to set the database and moduleHandler. + $plugin_reflection = new \ReflectionClass($plugin); + $database_property = $plugin_reflection->getProperty('database'); + $database_property->setAccessible(TRUE); + $module_handler_property = $plugin_reflection->getProperty('moduleHandler'); + $module_handler_property->setAccessible(TRUE); + + // Set the database and the module handler onto our plugin. + $database_property->setValue($plugin, $this->getDatabase($this->databaseContents + array('test_map' => array()))); + $module_handler_property->setValue($plugin, $module_handler); + + $plugin->setStringTranslation($this->getStringTranslationStub()); + $migration->expects($this->any()) + ->method('getSourcePlugin') + ->will($this->returnValue($plugin)); + $this->source = $plugin; + $this->expectedCount = count($this->expectedResults); + } + +}