From 97069ac9c73c052af38f29db63a8e079c1d88a92 Mon Sep 17 00:00:00 2001
From: Pravin Gaikwad <33942-Rajeshreeputra@users.noreply.drupalcode.org>
Date: Sat, 8 Feb 2025 14:21:27 +0000
Subject: [PATCH 1/6] Add service and cron to import assets.
---
.../acquiadam_asset_import.module | 15 ++
.../acquiadam_asset_import.services.yml | 15 ++
.../src/Services/AssetImporterService.php | 207 ++++++++++++++++++
.../AssetImporterServiceInterface.php | 40 ++++
4 files changed, 277 insertions(+)
create mode 100644 modules/acquiadam_asset_import/acquiadam_asset_import.module
create mode 100644 modules/acquiadam_asset_import/acquiadam_asset_import.services.yml
create mode 100644 modules/acquiadam_asset_import/src/Services/AssetImporterService.php
create mode 100644 modules/acquiadam_asset_import/src/Services/AssetImporterServiceInterface.php
diff --git a/modules/acquiadam_asset_import/acquiadam_asset_import.module b/modules/acquiadam_asset_import/acquiadam_asset_import.module
new file mode 100644
index 00000000..7454814e
--- /dev/null
+++ b/modules/acquiadam_asset_import/acquiadam_asset_import.module
@@ -0,0 +1,15 @@
+importAssets();
+}
diff --git a/modules/acquiadam_asset_import/acquiadam_asset_import.services.yml b/modules/acquiadam_asset_import/acquiadam_asset_import.services.yml
new file mode 100644
index 00000000..fbb65eb3
--- /dev/null
+++ b/modules/acquiadam_asset_import/acquiadam_asset_import.services.yml
@@ -0,0 +1,15 @@
+services:
+ acquiadam_asset_import.asset_importer_service:
+ class: Drupal\acquiadam_asset_import\Services\AssetImporterService
+ arguments:
+ - '@acquia_dam.client.factory'
+ - '@queue'
+ - '@database'
+ - '@config.factory'
+ - '@entity_type.manager'
+ - '@logger.channel.acquiadam_asset_import'
+ - '@current_user'
+ - '@acquia_dam.media_type_resolver'
+ logger.channel.acquiadam_asset_import:
+ parent: logger.channel_base
+ arguments: [ 'acquiadam_asset_import' ]
diff --git a/modules/acquiadam_asset_import/src/Services/AssetImporterService.php b/modules/acquiadam_asset_import/src/Services/AssetImporterService.php
new file mode 100644
index 00000000..eba4c20c
--- /dev/null
+++ b/modules/acquiadam_asset_import/src/Services/AssetImporterService.php
@@ -0,0 +1,207 @@
+userClientFactory = $userClientFactory->getSiteClient();
+ $this->assetImportQueue = $queueFactory->get('acquia_dam_asset_import');
+ $this->connection = $connection;
+ $this->config = $config;
+ $this->entityTypeManager = $entityTypeManager;
+ $this->damLoggerChannel = $loggerChannel;
+ $this->currentUser = $currentUser;
+ $this->mediaTypeResolver = $mediaTypeResolver;
+ $this->sourceFieldName = MediaSourceField::SOURCE_FIELD_NAME;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function importAssets() {
+ if ($this->assetImportQueue->numberOfItems() <= 0) {
+ // Get a list of category names from config.
+ $categories = $this->config->get('acquiadam_asset_import.settings')->get('categories');
+
+ // If no categories are configured, return.
+ if (empty($categories)) {
+ return NULL;
+ }
+
+ // Get the media storage.
+ /** @var \Drupal\Core\Entity\EntityStorageInterface $media_storage */
+ $media_storage = $this->entityTypeManager->getStorage('media');
+ // Start processing and queueing media item creation.
+ foreach ($categories as $category_uuid => $bundles) {
+ try {
+ $response = $this->userClientFactory->getAssetsInCategory($category_uuid);
+ }
+ catch (\Exception $exception) {
+ $this->damLoggerChannel->warning('Unable to fetch assets of a category from Widen API. Error: %message', [
+ '%message' => $exception->getMessage(),
+ ]);
+ }
+
+ // If there is no asset in the category, move forward.
+ if (!isset($response['items']) || count($response['items']) < 1 || $response['total_count'] === 0) {
+ continue;
+ }
+
+ // Iterate through the assets in the given category.
+ foreach ($response['items'] as $asset_data) {
+ // Handle cases where Widen might expose unavailable content.
+ if (!$asset_data['released_and_not_expired']) {
+ continue;
+ }
+
+ // Resolve the media type of the asset.
+ $asset_media_type = $this->mediaTypeResolver->resolve($asset_data);
+ // Assets that don't resolve to any media type should be skipped.
+ if ($asset_media_type == NULL) {
+ continue;
+ }
+
+ // Get the media type ID.
+ $asset_media_type_id = $asset_media_type->id();
+ // Check whether if the filtering allows its media type.
+ if (!empty($bundles) && !in_array($asset_media_type_id, $bundles)) {
+ continue;
+ }
+
+ // If the asset is already imported, skip it.
+ if ($this->isMediaExist($media_storage, $asset_data['id'], $asset_data['version_id'], $this->damLoggerChannel, $this->sourceFieldName)) {
+ continue;
+ }
+
+ // Queue the asset for import.
+ $this->assetImportQueue->createItem([
+ 'target_bundle' => $asset_media_type_id,
+ 'file_name' => $asset_data['filename'],
+ 'asset_uuid' => $asset_data['id'],
+ 'version_id' => $asset_data['version_id'],
+ 'queuer_uid' => $this->currentUser->id(),
+ ]);
+ }
+ }
+ }
+
+ return $this->assetImportQueue->numberOfItems();
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function isMediaExist(EntityStorageInterface $media_storage, string $asset_uuid, string $version_id, LoggerChannelInterface $damLoggerChannel, string $source_field_name): bool {
+ // Check whether if the asset is already imported.
+ $existing_media_entities = $media_storage->loadByProperties([$source_field_name => $asset_uuid]);
+ // Check whether if the media entity already exists.
+ if (count($existing_media_entities) > 1) {
+ $damLoggerChannel->error($this->t('Invalid state detected: multiple media items (%media_ids) share the same asset ID: %asset_id. Suggested to delete all affected media items and then create them again.', [
+ '%media_ids' => implode(', ', array_keys($existing_media_entities)),
+ '%asset_id' => $asset_uuid,
+ ]));
+ return TRUE;
+ }
+ elseif (count($existing_media_entities) === 1) {
+ // Check whether if the existing media entity is the same version.
+ $media_entity = array_values($existing_media_entities)[0];
+ assert($media_entity instanceof MediaInterface);
+ $source_field = $media_entity->get($source_field_name);
+ if (!$source_field->isEmpty() && $source_field->first()->version_id === $version_id) {
+ return TRUE;
+ }
+ }
+
+ return FALSE;
+ }
+
+}
diff --git a/modules/acquiadam_asset_import/src/Services/AssetImporterServiceInterface.php b/modules/acquiadam_asset_import/src/Services/AssetImporterServiceInterface.php
new file mode 100644
index 00000000..5b3c85a3
--- /dev/null
+++ b/modules/acquiadam_asset_import/src/Services/AssetImporterServiceInterface.php
@@ -0,0 +1,40 @@
+
Date: Sat, 8 Feb 2025 14:22:31 +0000
Subject: [PATCH 2/6] Update queue worker to use service.
---
.../src/Plugin/QueueWorker/AssetImporter.php | 64 +++++++++----------
1 file changed, 30 insertions(+), 34 deletions(-)
diff --git a/modules/acquiadam_asset_import/src/Plugin/QueueWorker/AssetImporter.php b/modules/acquiadam_asset_import/src/Plugin/QueueWorker/AssetImporter.php
index f50e2d2e..e149b153 100644
--- a/modules/acquiadam_asset_import/src/Plugin/QueueWorker/AssetImporter.php
+++ b/modules/acquiadam_asset_import/src/Plugin/QueueWorker/AssetImporter.php
@@ -6,11 +6,11 @@ namespace Drupal\acquiadam_asset_import\Plugin\QueueWorker;
use Drupal\acquia_dam\Entity\MediaSourceField;
use Drupal\acquia_dam\Plugin\QueueWorker\AssetQueueWorkerBase;
+use Drupal\acquiadam_asset_import\Services\AssetImporterService;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
-use Drupal\media\MediaInterface;
use GuzzleHttp\ClientInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
@@ -56,6 +56,20 @@ class AssetImporter extends AssetQueueWorkerBase implements ContainerFactoryPlug
*/
protected $logger;
+ /**
+ * Store module-wide used constant as class attribute.
+ *
+ * @var string
+ */
+ protected $sourceFieldName;
+
+ /**
+ * The asset importer service.
+ *
+ * @var \Drupal\acquiadam_asset_import\Services\AssetImporterService
+ */
+ protected $assetImporter;
+
/**
* {@inheritdoc}
*
@@ -73,13 +87,17 @@ class AssetImporter extends AssetQueueWorkerBase implements ContainerFactoryPlug
* The messenger service.
* @param \Psr\Log\LoggerInterface $logger
* Acquia DAM specific logger service.
+ * @param \Drupal\acquiadam_asset_import\Services\AssetImporterService $asset_importer
+ * The asset importer service.
*/
- public function __construct(array $configuration, string $plugin_id, array $plugin_definition, EntityStorageInterface $media_storage, ClientInterface $http_client, MessengerInterface $messenger, LoggerInterface $logger) {
+ public function __construct(array $configuration, string $plugin_id, array $plugin_definition, EntityStorageInterface $media_storage, ClientInterface $http_client, MessengerInterface $messenger, LoggerInterface $logger, AssetImporterService $asset_importer) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->mediaStorage = $media_storage;
$this->httpClient = $http_client;
$this->messenger = $messenger;
$this->logger = $logger;
+ $this->assetImporter = $asset_importer;
+ $this->sourceFieldName = MediaSourceField::SOURCE_FIELD_NAME;
}
/**
@@ -93,7 +111,8 @@ class AssetImporter extends AssetQueueWorkerBase implements ContainerFactoryPlug
$container->get('entity_type.manager')->getStorage('media'),
$container->get('http_client'),
$container->get('messenger'),
- $container->get('logger.channel.acquia_dam'),
+ $container->get('logger.channel.acquiadam_asset_import'),
+ $container->get('acquiadam_asset_import.asset_importer_service')
);
}
@@ -101,53 +120,30 @@ class AssetImporter extends AssetQueueWorkerBase implements ContainerFactoryPlug
* {@inheritdoc}
*/
public function processItem($data) {
- $source_field_name = MediaSourceField::SOURCE_FIELD_NAME;
- $existing_media_entities = $this->mediaStorage->loadByProperties([$source_field_name => $data['asset_uuid']]);
-
- // Check whether if the media entity already exists.
- // @todo this duplication checker logic should be centralized. See also: BulkImportConfigForm::submitForm().
- if (count($existing_media_entities) > 1) {
- // @todo Maybe throw new IntegrityConstraintViolationException('message', 500);.
- $this->logger->error('Invalid state detected: multiple media items (%media_ids) share the same asset ID: %asset_id. Suggested to delete all affected media items and then create them again.', [
- '%media_ids' => implode(', ', array_keys($existing_media_entities)),
- '%asset_id' => $data['asset_uuid'],
- ]);
-
+ // If the asset is already imported, skip it.
+ if ($this->assetImporter->isMediaExist($this->mediaStorage, $data['asset_uuid'], $data['version_id'], $this->logger, $this->sourceFieldName)) {
return NULL;
}
- elseif (count($existing_media_entities) === 1) {
- // Check whether if the existing media entity is the same version.
- $media_entity = array_values($existing_media_entities)[0];
- assert($media_entity instanceof MediaInterface);
- $source_field = $media_entity->get($source_field_name);
- if (!$source_field->isEmpty() && $source_field->first()->version_id === $data['version_id']) {
- $this->logger->info('DAM asset %file_name (asset ID: %asset_id) was skipped during import as its corresponding media item with ID: %media_id already exists.', [
- '%file_name' => $data['file_name'],
- '%asset_id' => $data['asset_uuid'],
- '%media_id' => $media_entity->id(),
- 'link' => $media_entity->toLink($this->t('View'))->toString(),
- ]);
-
- return NULL;
- }
- }
+ // Create a media item for the asset.
$media_entity = $this->mediaStorage->create([
'bundle' => $data['target_bundle'],
'name' => $data['file_name'],
'uid' => $data['queuer_uid'],
- MediaSourceField::SOURCE_FIELD_NAME => [
+ $this->sourceFieldName => [
'asset_id' => $data['asset_uuid'],
],
]);
+ // Save the media item.
$media_entity->save();
- $this->logger->info('DAM asset %file_name (asset ID: %asset_id) was created as a media item with ID: %media_id.', [
+ // Log the successful creation of the media item.
+ $this->logger->info($this->t('DAM asset %file_name (asset ID: %asset_id) was created as a media item with ID: %media_id.', [
'%file_name' => $data['file_name'],
'%asset_id' => $data['asset_uuid'],
'%media_id' => $media_entity->id(),
'link' => $media_entity->toLink($this->t('View'))->toString(),
- ]);
+ ]));
}
}
--
GitLab
From 1cd6c4c4e716b79f722cff2099c13fe2acd9186e Mon Sep 17 00:00:00 2001
From: Pravin Gaikwad <33942-Rajeshreeputra@users.noreply.drupalcode.org>
Date: Sat, 8 Feb 2025 14:35:32 +0000
Subject: [PATCH 3/6] Update bulk import form to save selected categories and
filter bundle in config for later importing.
---
.../src/Form/BulkImportConfigForm.php | 92 +------------------
1 file changed, 4 insertions(+), 88 deletions(-)
diff --git a/modules/acquiadam_asset_import/src/Form/BulkImportConfigForm.php b/modules/acquiadam_asset_import/src/Form/BulkImportConfigForm.php
index c0128d09..c250e4a5 100644
--- a/modules/acquiadam_asset_import/src/Form/BulkImportConfigForm.php
+++ b/modules/acquiadam_asset_import/src/Form/BulkImportConfigForm.php
@@ -131,7 +131,7 @@ class BulkImportConfigForm extends ConfigFormBase {
'#type' => 'actions',
'submit' => [
'#type' => 'submit',
- '#value' => $this->t('Save and schedule'),
+ '#value' => $this->t('Save'),
'#button_type' => 'primary',
],
'cancel' => [
@@ -207,7 +207,7 @@ class BulkImportConfigForm extends ConfigFormBase {
'#options' => $categ_opts,
'#sort_options' => TRUE,
'#empty_value' => '',
- '#empty_option' => $this->t('Please choose one…'),
+ '#empty_option' => $this->t('Add category to import'),
];
$form['editor']['activate_filtering'] = [
'#type' => 'checkbox',
@@ -461,92 +461,8 @@ class BulkImportConfigForm extends ConfigFormBase {
// Store received input in module config.
$this->configFactory->getEditable('acquiadam_asset_import.settings')->set('categories', $submitted_data)->save();
- $media_storage = $this->entityTypeManager->getStorage('media');
-
- // Start processing and queueing media item creation.
- $queued_count = 0;
- foreach ($submitted_data as $category_uuid => $bundles) {
- try {
- $response = $this->userClientFactory->getUserClient()->getAssetsInCategory($category_uuid);
- }
- catch (\Exception $exception) {
- $this->logger->warning('Unable to fetch assets of a category from Widen API. Error: %message', [
- '%message' => $exception->getMessage(),
- ]);
- }
-
- // If there is no asset in the category move forward.
- if (!isset($response['items']) || count($response['items']) < 1 || $response['total_count'] === 0) {
- continue;
- }
-
- // Start iterate through the assets in the given category.
- foreach ($response['items'] as $asset_data) {
- // Get prepared to any case Widen might exposing unavailable content.
- if (!$asset_data['released_and_not_expired']) {
- continue;
- }
-
- $asset_media_type = $this->mediaTypeResolver->resolve($asset_data);
- // Assets that don't resolve to any media type should be skipped.
- if ($asset_media_type == NULL) {
- continue;
- }
- $asset_media_type_id = $asset_media_type->id();
-
- // Check whether if its media type is allowed by the filtering.
- if (!empty($bundles) && !in_array($asset_media_type_id, $bundles)) {
- continue;
- }
-
- $existing_media_entities = $media_storage->loadByProperties([$this->sourceFieldName => $asset_data['id']]);
-
- // Check whether if the media entity already exists.
- // @todo this duplication checker logic should be centralized. See also: AssetImporter::processItem().
- if (count($existing_media_entities) > 1) {
- // @todo Consider this instead: throw new IntegrityConstraintViolationException('message', 500);.
- $this->logger->error('Invalid state detected: multiple media items (%media_ids) share the same asset ID: %asset_id. Suggested to delete all affected media items and then create them again.', [
- '%media_ids' => implode(', ', array_keys($existing_media_entities)),
- '%asset_id' => $asset_data['id'],
- ]);
- $this->messenger()->addError($this->t('Invalid state detected: multiple media items share the same asset ID. Please contact the site administrator. See the log for details.'));
- continue;
- }
- elseif (count($existing_media_entities) === 1) {
- // Check whether if the existing media entity is the same version.
- $media_entity = array_values($existing_media_entities)[0];
- assert($media_entity instanceof MediaInterface);
- $source_field = $media_entity->get($this->sourceFieldName);
- if (!$source_field->isEmpty() && $source_field->first()->version_id === $asset_data['version_id']) {
- continue;
- }
- }
-
- $this->assetImportQueue->createItem([
- 'target_bundle' => $asset_media_type_id,
- 'file_name' => $asset_data['filename'],
- 'asset_uuid' => $asset_data['id'],
- 'version_id' => $asset_data['version_id'],
- 'queuer_uid' => $this->currentUser->id(),
- ]);
- $queued_count++;
- }
- }
-
- // Display some informational messages.
- $dam_media_list_url = Url::fromRoute('view.dam_content_overview.page_1')->toString();
- if ($queued_count > 0) {
- $this->messenger->addStatus($this->formatPlural($queued_count,
- '1 asset in total was queued for later importing. Once the scheduled task gets processed, the new media item will appear on the DAM media list.',
- '@count assets in total were queued for later importing. Once the scheduled tasks get processed, the new media items for each of them will appear on the DAM media list.',
- [':url' => $dam_media_list_url])
- );
- }
- else {
- $this->messenger->addStatus($this->t('No asset was queued for later importing which can be normal if all assets from these categories have been imported already earlier. Depending on cron runs, some of their corresponding media items should appear on the DAM media list soon.',
- [':url' => $dam_media_list_url])
- );
- }
+ // Display informational messages.
+ $this->messenger->addStatus($this->t('The configuration settings have been successfully saved.'));
}
}
--
GitLab
From 1e89e6a53580b840aacaf688a67320322cc0f863 Mon Sep 17 00:00:00 2001
From: rajeshreeputra
Date: Sun, 9 Feb 2025 11:29:26 +0530
Subject: [PATCH 4/6] Update test coverage for bulk import form.
---
.../acquiadam_asset_import.services.yml | 1 -
.../src/Services/AssetImporterService.php | 13 ++-
.../BulkImportConfigFormSubmitTest.php | 106 ++++++++++++------
3 files changed, 80 insertions(+), 40 deletions(-)
diff --git a/modules/acquiadam_asset_import/acquiadam_asset_import.services.yml b/modules/acquiadam_asset_import/acquiadam_asset_import.services.yml
index fbb65eb3..8183d14a 100644
--- a/modules/acquiadam_asset_import/acquiadam_asset_import.services.yml
+++ b/modules/acquiadam_asset_import/acquiadam_asset_import.services.yml
@@ -4,7 +4,6 @@ services:
arguments:
- '@acquia_dam.client.factory'
- '@queue'
- - '@database'
- '@config.factory'
- '@entity_type.manager'
- '@logger.channel.acquiadam_asset_import'
diff --git a/modules/acquiadam_asset_import/src/Services/AssetImporterService.php b/modules/acquiadam_asset_import/src/Services/AssetImporterService.php
index eba4c20c..95131b99 100644
--- a/modules/acquiadam_asset_import/src/Services/AssetImporterService.php
+++ b/modules/acquiadam_asset_import/src/Services/AssetImporterService.php
@@ -10,7 +10,6 @@ use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Logger\LoggerChannelInterface;
use Drupal\Core\Queue\QueueFactory;
-use Drupal\Core\Database\Connection;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\media\MediaInterface;
@@ -29,6 +28,13 @@ class AssetImporterService implements AssetImporterServiceInterface {
*/
protected $userClientFactory;
+ /**
+ * The asset import queue.
+ *
+ * @var \Drupal\Core\Queue\QueueInterface
+ */
+ protected $assetImportQueue;
+
/**
* Configuration.
*
@@ -78,8 +84,6 @@ class AssetImporterService implements AssetImporterServiceInterface {
* The Acquia DAM client factory.
* @param \Drupal\Core\Queue\QueueFactory $queueFactory
* The queue factory.
- * @param \Drupal\Core\Database\Connection $connection
- * The database connection.
* @param \Drupal\Core\Config\ConfigFactory $config
* The config factory.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
@@ -92,10 +96,9 @@ class AssetImporterService implements AssetImporterServiceInterface {
* Acquia DAM media type resolver.
*
*/
- public function __construct(AcquiaDamClientFactory $userClientFactory, QueueFactory $queueFactory, Connection $connection, ConfigFactory $config, EntityTypeManagerInterface $entityTypeManager, LoggerChannelInterface $loggerChannel, AccountInterface $currentUser, MediaTypeResolver $mediaTypeResolver) {
+ public function __construct(AcquiaDamClientFactory $userClientFactory, QueueFactory $queueFactory, ConfigFactory $config, EntityTypeManagerInterface $entityTypeManager, LoggerChannelInterface $loggerChannel, AccountInterface $currentUser, MediaTypeResolver $mediaTypeResolver) {
$this->userClientFactory = $userClientFactory->getSiteClient();
$this->assetImportQueue = $queueFactory->get('acquia_dam_asset_import');
- $this->connection = $connection;
$this->config = $config;
$this->entityTypeManager = $entityTypeManager;
$this->damLoggerChannel = $loggerChannel;
diff --git a/modules/acquiadam_asset_import/tests/src/FunctionalJavascript/BulkImportConfigFormSubmitTest.php b/modules/acquiadam_asset_import/tests/src/FunctionalJavascript/BulkImportConfigFormSubmitTest.php
index 34cd18af..1906411b 100644
--- a/modules/acquiadam_asset_import/tests/src/FunctionalJavascript/BulkImportConfigFormSubmitTest.php
+++ b/modules/acquiadam_asset_import/tests/src/FunctionalJavascript/BulkImportConfigFormSubmitTest.php
@@ -50,63 +50,101 @@ class BulkImportConfigFormSubmitTest extends AcquiaDamWebDriverTestBase {
$this->grantCurrentUserDamToken();
$this->drupalGet('/admin/config/acquia-dam/bulk-import');
+ $assert_session = $this->assertSession();
+
// Check the initial state of the form.
- $this->assertSession()->pageTextContains('Acquia DAM bulk import');
- $category_select_list = $this->assertSession()->selectExists('edit-category-uuid');
- $this->assertSession()->pageTextContains('List of categories in the remote DAM system available for the authorized user account. Please choose which of them the media assets should be imported from. When adding the same category multiple times, only the last row of them will be considered and saved.');
- $assignment_table_wrapper = $this->assertSession()->elementExists('css', '#assignment-list-wrapper');
+ $assert_session->pageTextContains('Acquia DAM bulk import');
+ $category_select_list = $assert_session->selectExists('edit-category-uuid');
+ $assert_session->pageTextContains('List of categories in the remote DAM system available for the authorized user account. Please choose which of them the media assets should be imported from.');
+ $assignment_table_wrapper = $assert_session->elementExists('css', '#assignment-list-wrapper');
$assignment_table_wrapper->hasTable('edit-assignment-list');
- $this->assertSession()->pageTextContains('No category has been selected yet.');
+ $assert_session->pageTextContains('No category has been selected yet.');
// Select a category.
$category_select_list->selectOption('Testing');
- $this->assertSession()->pageTextContains('The website automatically detects the file type of each asset in remote DAM and does its best to guess which media type it should be assigned to. In the case of a category containing various types of assets, this filtering option allows you to import only those assets which would be assigned to the given media types.');
- $asset_filtering_checkbox = $this->assertSession()->elementExists('css', '#edit-activate-filtering');
- $category_add_button = $this->assertSession()->buttonExists('edit-add-button');
+ $assert_session->pageTextContains('The website automatically detects the file type of each asset in remote DAM and does its best to guess which media type it should be assigned to. In the case of a category containing various types of assets, this filtering option allows you to import only those assets which would be assigned to the given media types.');
+ $asset_filtering_checkbox = $assert_session->elementExists('css', '#edit-activate-filtering');
+ $category_add_button = $assert_session->buttonExists('edit-add-button');
// Enable filtering.
$asset_filtering_checkbox->check();
- $this->assertSession()->pageTextContains('Import only assets which would be assigned to these media types');
- $this->assertSession()->elementExists('css', '#edit-filter-media-bundles-acquia-dam-image-asset');
- $this->assertSession()->pageTextContains('List of DAM-capable media types currently existing on this site. Set the filtration by specifying those media types that are allowed to receive assets during the import process. Any other assets within the defined category not fitting any allowed media type listed here will be skipped.');
+ $assert_session->pageTextContains('Import only assets which would be assigned to these media types');
+ $assert_session->elementExists('css', '#edit-filter-media-bundles-acquia-dam-image-asset');
+ $assert_session->pageTextContains('List of DAM-capable media types currently existing on this site. Set the filtration by specifying those media types that are allowed to receive assets during the import process. Any other assets within the defined category not fitting any allowed media type listed here will be skipped.');
// Add the current assignment to the list.
$category_add_button->click();
+ $assert_session->assertWaitOnAjaxRequest();
// Check the table content.
- $this->getSession()->wait(1000);
- $this->assertSession()->elementExists('css', 'tr[data-drupal-selector="edit-assignment-list-0"].odd');
- $this->assertSession()->elementTextContains('css', '#assignment-list-wrapper > table > tbody > tr:nth-child(1) > td:nth-child(1)', 'Testing');
- $this->assertSession()->elementTextContains('css', '#assignment-list-wrapper > table > tbody > tr:nth-child(1) > td:nth-child(2)', 'All assets (no filtering)');
-
- // Now the first row is added to the table, let's check resetting the form.
- $category_select_list->selectOption('Please choose one…');
- $this->assertSession()->pageTextNotContains('The website automatically detects the file type of each asset in remote DAM and does its best to guess which media type it should be assigned to. In the case of a category containing various types of assets, this filtering option allows you to import only those assets which would be assigned to the given media types.');
- // $this->assertSession()->elementNotExists('css',
- // '#edit-activate-filtering');
- // $this->assertSession()->buttonNotExists('edit-add-button');
+ $assert_session->elementExists('css', 'tr[data-drupal-selector="edit-assignment-list-0"].odd');
+ $assert_session->elementTextContains('css', '#assignment-list-wrapper > table > tbody > tr:nth-child(1) > td:nth-child(1)', 'Testing');
+ $assert_session->elementTextContains('css', '#assignment-list-wrapper > table > tbody > tr:nth-child(1) > td:nth-child(2)', 'All assets (no filtering)');
+
+ // Submit the form.
+ $assert_session->buttonExists('Save')->click();
+ $assert_session->pageTextContains('The configuration settings have been successfully saved.');
+ $this->drupalGet('/admin/config/acquia-dam/bulk-import');
+
+ // Check if the configuration is updated correctly.
+ $categories = $this->config('acquiadam_asset_import.settings')->get('categories') ?? [];
+ $this->assertNotEmpty($categories, 'Categories configuration should not be empty.');
+
+ // Check the table content.
+ $assert_session->elementExists('css', 'tr[data-drupal-selector="edit-assignment-list-0"].odd');
+ $assert_session->elementTextContains('css', '#assignment-list-wrapper > table > tbody > tr:nth-child(1) > td:nth-child(1)', 'Testing');
+ $assert_session->elementTextContains('css', '#assignment-list-wrapper > table > tbody > tr:nth-child(1) > td:nth-child(2)', 'All assets (no filtering)');
+
// Delete the first row.
- $this->assertSession()->buttonExists('Remove')->click();
- $this->getSession()->wait(1000);
- $this->assertSession()->pageTextContains('No category has been selected yet.');
+ $assert_session->buttonExists('Remove')->click();
+ $assert_session->assertWaitOnAjaxRequest();
+ $assert_session->pageTextContains('No category has been selected yet.');
+
+ // Submit the form.
+ $assert_session->buttonExists('Save')->click();
+ $assert_session->pageTextContains('The configuration settings have been successfully saved.');
+ $this->drupalGet('/admin/config/acquia-dam/bulk-import');
+
+ // Check if the configuration is updated correctly.
+ $categories = $this->config('acquiadam_asset_import.settings')->get('categories') ?? [];
+ $this->assertEmpty($categories, 'Categories configuration should be empty.');
// Select a category again.
$category_select_list->selectOption('Testing');
- $this->assertSession()->elementExists('css', '#edit-activate-filtering')->check();
+ $assert_session->elementExists('css', '#edit-activate-filtering')->check();
// Select all the media types.
- $this->assertSession()->elementExists('css', '#edit-filter-media-bundles-acquia-dam-documents-asset')->check();
- $this->assertSession()->elementExists('css', '#edit-filter-media-bundles-acquia-dam-image-asset')->check();
- $this->assertSession()->elementExists('css', '#edit-filter-media-bundles-acquia-dam-pdf-asset')->check();
- $this->assertSession()->elementExists('css', '#edit-filter-media-bundles-acquia-dam-spinset-asset')->check();
- $this->assertSession()->elementExists('css', '#edit-filter-media-bundles-acquia-dam-video-asset')->check();
+ $assert_session->elementExists('css', '#edit-filter-media-bundles-acquia-dam-documents-asset')->check();
+ $assert_session->elementExists('css', '#edit-filter-media-bundles-acquia-dam-image-asset')->check();
+ $assert_session->elementExists('css', '#edit-filter-media-bundles-acquia-dam-pdf-asset')->check();
+ $assert_session->elementExists('css', '#edit-filter-media-bundles-acquia-dam-video-asset')->check();
// Add the current assignment to the list again.
$category_add_button->click();
- // Finally submit the form.
- $this->assertSession()->buttonExists('edit-submit')->click();
- $this->assertSession()->pageTextContains('No asset was queued for later importing which can be normal if all assets from these categories have been imported already earlier. Depending on cron runs, some of their corresponding media items should appear on the DAM media list soon.');
+ // Submit the form.
+ $assert_session->buttonExists('Save')->click();
+ $assert_session->pageTextContains('The configuration settings have been successfully saved.');
+ $this->drupalGet('/admin/config/acquia-dam/bulk-import');
+
+ // Check the table content.
+ $assert_session->elementExists('css', 'tr[data-drupal-selector="edit-assignment-list-0"].odd');
+ $assert_session->elementTextContains('css', '#assignment-list-wrapper > table > tbody > tr:nth-child(1) > td:nth-child(1)', 'Testing');
+ $assert_session->elementTextContains('css', '#assignment-list-wrapper > table > tbody > tr:nth-child(1) > td:nth-child(2)', 'Acquia DAM: Document');
+ $assert_session->elementTextContains('css', '#assignment-list-wrapper > table > tbody > tr:nth-child(1) > td:nth-child(2)', 'Acquia DAM: Image');
+ $assert_session->elementTextContains('css', '#assignment-list-wrapper > table > tbody > tr:nth-child(1) > td:nth-child(2)', 'Acquia DAM: PDF');
+ $assert_session->elementTextContains('css', '#assignment-list-wrapper > table > tbody > tr:nth-child(1) > td:nth-child(2)', 'Acquia DAM: video');
+ $assert_session->elementTextNotContains('css', '#assignment-list-wrapper > table > tbody > tr:nth-child(1) > td:nth-child(2)', 'Acquia DAM: Spinset');
+
+ // Additional assertions to improve test coverage.
+ // Check if the configuration is updated correctly.
+ $categories = $this->config('acquiadam_asset_import.settings')->get('categories') ?? [];
+ $this->assertNotEmpty($categories, 'Categories configuration should not be empty.');
+
+ // Check if the assets are queued correctly.
+ $queue = $this->container->get('queue')->get('acquia_dam_asset_import');
+ $this->assertEquals(0, $queue->numberOfItems(), 'No items should be in the queue.');
+
}
}
--
GitLab
From 9a0887b323528241e9c5969c168f7e1dc6260ea1 Mon Sep 17 00:00:00 2001
From: rajeshreeputra
Date: Sun, 9 Feb 2025 15:01:05 +0530
Subject: [PATCH 5/6] Add cron service for cron job.
---
.../acquiadam_asset_import.module | 2 +-
.../acquiadam_asset_import.services.yml | 3 ++
modules/acquiadam_asset_import/src/Cron.php | 39 +++++++++++++++++++
3 files changed, 43 insertions(+), 1 deletion(-)
create mode 100644 modules/acquiadam_asset_import/src/Cron.php
diff --git a/modules/acquiadam_asset_import/acquiadam_asset_import.module b/modules/acquiadam_asset_import/acquiadam_asset_import.module
index 7454814e..7443e762 100644
--- a/modules/acquiadam_asset_import/acquiadam_asset_import.module
+++ b/modules/acquiadam_asset_import/acquiadam_asset_import.module
@@ -11,5 +11,5 @@
* Import assets from Acquia DAM.
*/
function acquiadam_asset_import_cron() {
- \Drupal::service('acquiadam_asset_import.asset_importer_service')->importAssets();
+ \Drupal::service('acquiadam_asset_import.cron')->run();
}
diff --git a/modules/acquiadam_asset_import/acquiadam_asset_import.services.yml b/modules/acquiadam_asset_import/acquiadam_asset_import.services.yml
index 8183d14a..fc6f4b01 100644
--- a/modules/acquiadam_asset_import/acquiadam_asset_import.services.yml
+++ b/modules/acquiadam_asset_import/acquiadam_asset_import.services.yml
@@ -12,3 +12,6 @@ services:
logger.channel.acquiadam_asset_import:
parent: logger.channel_base
arguments: [ 'acquiadam_asset_import' ]
+ acquiadam_asset_import.cron:
+ class: Drupal\acquiadam_asset_import\Cron
+ arguments: [ '@acquiadam_asset_import.asset_importer_service' ]
diff --git a/modules/acquiadam_asset_import/src/Cron.php b/modules/acquiadam_asset_import/src/Cron.php
new file mode 100644
index 00000000..2f11db95
--- /dev/null
+++ b/modules/acquiadam_asset_import/src/Cron.php
@@ -0,0 +1,39 @@
+assetImporter = $asset_importer;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function run(): bool {
+ $this->$this->assetImporter->importAssets();
+
+ return TRUE;
+ }
+
+}
--
GitLab
From 10c2a0e62b4469513132887b4e78acf7856d50a3 Mon Sep 17 00:00:00 2001
From: Pravin Gaikwad <33942-Rajeshreeputra@users.noreply.drupalcode.org>
Date: Mon, 10 Feb 2025 07:05:52 +0000
Subject: [PATCH 6/6] Fix typo.
---
modules/acquiadam_asset_import/src/Cron.php | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/modules/acquiadam_asset_import/src/Cron.php b/modules/acquiadam_asset_import/src/Cron.php
index 2f11db95..46d34374 100644
--- a/modules/acquiadam_asset_import/src/Cron.php
+++ b/modules/acquiadam_asset_import/src/Cron.php
@@ -31,7 +31,7 @@ class Cron implements CronInterface {
* {@inheritdoc}
*/
public function run(): bool {
- $this->$this->assetImporter->importAssets();
+ $this->assetImporter->importAssets();
return TRUE;
}
--
GitLab