Skip to content

Commit

Permalink
Fixes #3866 Update AZEnterpriseAttributesMigrationSync, Migrate Tools (
Browse files Browse the repository at this point in the history
…#3868)

Co-authored-by: Troy Dean <[email protected]>
  • Loading branch information
tadean and Troy Dean authored Dec 18, 2024
1 parent 9502a56 commit 9c13f0d
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 10 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@
"drupal/metatag": "2.0.2",
"drupal/migrate_plus": "6.0.5",
"drupal/migrate_queue_importer": "2.1.1",
"drupal/migrate_tools": "6.0.4",
"drupal/migrate_tools": "6.0.5",
"drupal/optional_end_date": "1.4.0",
"drupal/paragraphs": "1.18.0",
"drupal/paragraphs_admin": "1.6.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ services:
decorates: migrate_tools.migration_sync
arguments:
- '@event_dispatcher'
- '@state'
- '@migrate_tools.migrate_tools'
- '@entity_type.manager'
- '@logger.channel.az_enterprise_attributes_import'
logger.channel.az_enterprise_attributes_import:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Logger\LoggerChannel;
use Drupal\Core\State\StateInterface;
use Drupal\migrate\Event\MigrateImportEvent;
use Drupal\migrate_tools\EventSubscriber\MigrationImportSync;
use Drupal\migrate_tools\MigrateTools;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

/**
Expand All @@ -31,17 +31,16 @@ final class AZEnterpriseAttributesMigrationSync extends MigrationImportSync {
*
* @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $dispatcher
* The event dispatcher.
* @param \Drupal\Core\State\StateInterface $state
* The Key/Value Store to use for tracking synced source rows.
* @param \Drupal\migrate_tools\MigrateTools $migrateTools
* The MigrateTools helper for source id tracking.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
* The entity type manager service.
* @param \Drupal\Core\Logger\LoggerChannel $logger
* The logger channel service.
*/
public function __construct(EventDispatcherInterface $dispatcher, StateInterface $state, EntityTypeManagerInterface $entityTypeManager, LoggerChannel $logger) {
public function __construct(EventDispatcherInterface $dispatcher, MigrateTools $migrateTools, EntityTypeManagerInterface $entityTypeManager, LoggerChannel $logger) {
$this->dispatcher = $dispatcher;
$this->state = $state;
$this->state->set('migrate_tools_sync', []);
$this->migrateTools = $migrateTools;
$this->entityTypeManager = $entityTypeManager;
$this->logger = $logger;
}
Expand All @@ -51,12 +50,18 @@ public function __construct(EventDispatcherInterface $dispatcher, StateInterface
*/
public function sync(MigrateImportEvent $event): void {
$migration = $event->getMigration();
$migrationId = $migration->getPluginId();
// If this isn't a migration we're concerned with, use the parent.
if ($migration->id() !== 'az_enterprise_attributes_import') {
if ($migrationId !== 'az_enterprise_attributes_import') {
parent::sync($event);
return;
}
if (!empty($migration->syncSource)) {
// Clear Sync IDs for this migration before starting preparing rows.
$this->migrateTools->clearSyncSourceIds($migrationId);
// Activate the syncing state for this migration, so
// migrate_tools_migrate_prepare_row() can record all IDs.
$this->migrateTools->setMigrationSyncingState($migrationId, TRUE);

// Loop through the source to register existing source ids.
// @see migrate_tools_migrate_prepare_row().
Expand All @@ -68,7 +73,12 @@ public function sync(MigrateImportEvent $event): void {
$source->next();
}

$source_id_values = $this->state->get('migrate_tools_sync', []);
// Deactivate the syncing state for this migration, so
// migrate_tools_migrate_prepare_row() does not record any further IDs
// during the actual migration process.
$this->migrateTools->setMigrationSyncingState($migrationId, FALSE);

$source_id_values = $this->migrateTools->getSyncSourceIds($migrationId);

$id_map = $migration->getIdMap();
$id_map->rewind();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php

namespace Drupal\Tests\az_core\Functional;

use Drupal\Tests\BrowserTestBase;
use Drupal\migrate\MigrateMessage;
use Drupal\migrate_tools\MigrateExecutable;

/**
* Test of attribute import functionality.
*
* @group az_enterprise_attributes_import
*/
class EnterpriseAttributesImportTest extends BrowserTestBase {

/**
* The profile to install as a basis for testing.
*
* @var string
*/
protected $profile = 'az_quickstart';

/**
* @var bool
*/
protected $strictConfigSchema = FALSE;

/**
* @var string
*/
protected $defaultTheme = 'stark';

/**
* Modules to enable.
*
* @var array
*/
protected static $modules = [
'az_core',
'az_enterprise_attributes_import',
'migrate',
'migrate_tools',
];

/**
* Tests that our event subscriber can unpublish terms.
*/
public function testUnpublishAttributes() {
// Get term storage interface.
$term_storage = $this->container->get('entity_type.manager')->getStorage('taxonomy_term');
// Remove initial attributes from enabling az_enterprise_attributes_import.
$manager = $this->container->get('plugin.manager.migration');
$migration = $manager->createInstance('az_enterprise_attributes_import');
$migrate_exec = new MigrateExecutable($migration, new MigrateMessage());
$migrate_exec->rollback();

// Build a test attribute list.
$options = [
'source' => [
'plugin' => 'embedded_data',
'data_rows' => [
[
'key' => 'example_attribute',
'value' => 'Example Attribute',
'type' => 'multi-select picklist',
],
],
],
];
// Run migration.
$migration = $manager->createInstance('az_enterprise_attributes_import', $options);
$migrate_exec = new MigrateExecutable($migration, new MigrateMessage());
$migrate_exec->import();
// Get published terms.
$terms = $term_storage->loadByProperties([
'vid' => 'az_enterprise_attributes',
'status' => 1,
]);
// Assert we have exactly one published term.
$this->assertSame(count($terms), 1);

// Change source to have no terms available.
// We want to check if the imported term can be unpublished.
$options['source']['data_rows'] = [];
// Rerun modified migration.
$migration = $manager->createInstance('az_enterprise_attributes_import', $options);
// Sync the migration to test unpublishing the missing term.
$sync = [
'sync' => 1,
'update' => 1,
];
$migrate_exec = new MigrateExecutable($migration, new MigrateMessage(), $sync);
$migrate_exec->import();
// Get unpublished terms.
$terms = $term_storage->loadByProperties([
'vid' => 'az_enterprise_attributes',
'status' => 0,
]);
// Assert we have exactly one unpublished term.
$this->assertSame(count($terms), 1);
}

}

0 comments on commit 9c13f0d

Please sign in to comment.