media_acquiadam-8.x-1.46/tests/src/Functional/AcquiadamMigrationFormTest.php
tests/src/Functional/AcquiadamMigrationFormTest.php
<?php
declare(strict_types=1);
namespace Drupal\Tests\media_acquiadam\Functional;
use Drupal\media\Entity\Media;
use Drupal\Tests\acquia_dam\FunctionalJavascript\AcquiaDamWebDriverTestBase;
/**
* Test Migration from Media acquiadam Acquia DAM.
*
* @group media_acquiadam
*/
class AcquiadamMigrationFormTest extends AcquiaDamWebDriverTestBase {
/**
* {@inheritdoc}
*/
protected static $modules = [
'acquia_dam',
'media_acquiadam',
'media_acquiadam_example',
];
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
// Create user and login.
$user = $this->createUser(['administer media', 'administer site configuration']);
$this->drupalLogin($user);
// Acquia DAM authorization.
$this->grantSiteRegistrationToken();
$this->grantCurrentUserDamToken();
// Create media with asset ID.
$this->createMedia('acquia_dam_image', 'f2a9c03d-3664-477c-8013-e84504ed5adc');
$this->createMedia('acquia_dam_video', 'efb03f75-3c42-497b-baa9-5ec79d1f56af');
}
/**
* Create media with asset ID.
*
* @param string $media_type_id
* Media type ID.
* @param string $asset_id
* Asset ID.
*/
private function createMedia(string $media_type_id, string $asset_id): void {
Media::create([
'bundle' => $media_type_id,
'field_acquiadam_asset_id' => [
'value' => $asset_id,
],
])->save();
}
/**
* Tests the build of the migration form in normal conditions.
*/
public function testMigration(): void {
$this->drupalGet('/admin/config/acquia-dam/migration');
$page = $this->getSession()->getPage();
$assert_session = $this->assertSession();
// Assert that the form exists on first page with the expected text.
$assert_session->pageTextContains('Migration from legacy to modern Acquia DAM');
$assert_session->pageTextContains('This dashboard helps migrate legacy media types to Acquia DAM by providing a mapping between Media: Acquia DAM media types and their corresponding media source in Acquia DAM. This dashboard allows you to track the progress of the migration and its completion.');
// Assert that on migration page, table and its header exist.
$assert_session->elementExists('css', 'table#media_acquiadam_migration_table');
$header_text = [
'Media type label and name', 'Number of media items', '', 'Method to handle media files', 'Target source type',
];
foreach ($header_text as $key => $text) {
$assert_session->elementTextEquals('xpath', '//table[contains(@id,"media_acquiadam_migration_table")]//thead//tr//th[' . ($key + 1) . ']', $text);
}
// Select method as 'embed' to handle media files and press Save & Next.
$page->fillField('legacy_dam_media_types[0][media_type_label]', 'Media: Acquia DAM Image');
$page->selectFieldOption('legacy_dam_media_types[0][sync_method]', 'embed');
$page->selectFieldOption('legacy_dam_media_types[0][target_source_type]', 'acquia_dam_asset:image');
$page->selectFieldOption('legacy_dam_media_types[1][target_source_type]', 'acquia_dam_asset:video');
$assert_session->buttonNotExists('Previous');
$page->pressButton('Save & Next');
// Assert that the form exists second page with the expected text.
$assert_session->pageTextContains('Based on the mapping provided on the previous page, we recommend deleting the mapped media from Acquia DAM module. However, this can be adjusted as needed, and there\'s also the option to update the media label from this page.');
// Assert that on second page table and its header exist.
$assert_session->elementExists('css', 'table#modern_dam_deletion_table');
$assert_session->elementTextEquals('xpath', '//table[contains(@id,"modern_dam_deletion_table")]//thead//tr//th[1]', 'Media type label and name');
$assert_session->elementTextEquals('xpath', '//table[contains(@id,"modern_dam_deletion_table")]//thead//tr//th[2]', 'Delete');
// Assert that media type is checked for deletion, whose mapping is done on the previous page.
$page->fillField('modern_dam_media_types[0][media_type_label]', 'Acquia DAM: Audio update');
$assert_session->checkboxChecked('modern_dam_media_types[3][delete]');
$assert_session->checkboxChecked('modern_dam_media_types[6][delete]');
$assert_session->buttonExists('Previous');
$page->pressButton('Save & Next');
// Assert that the form exists third page with the expected text.
$assert_session->pageTextContains('As part of this migration process, we are updating the existing media type (provided by the legacy module media_acquiadam) by replacing its source with the new one from the acquia_dam module. Also, legacy media types without media items will be automatically deleted after migration. We also recommend deleting the newly created media types from the acquia_dam module that are mapped to replace legacy media, unless they have been explicitly excluded.');
// Asset that key point section appears.
$assert_session->pageTextContains('Key points to consider:');
$assert_session->pageTextContains('Media type label and name: Legacy media types label which are mapped for migration.');
$assert_session->pageTextContains('Method to handle media files: Mechanism to handle media files while rendering media items (sync or embed).');
$assert_session->pageTextContains('Target source type: New source type for the media type being migrated.');
$assert_session->pageTextContains('Number of Media Items: Total number of media items in the media type being migrated.');
// Assert that expected media deletion section appears with correct value.
$assert_session->pageTextContains('Media types marked for deletion:');
$assert_session->elementExists('css', '[data-drupal-selector="edit-media-types"]');
$assert_session->elementTextEquals('css', '[data-drupal-selector="edit-media-types"] li:nth-child(1)', 'Acquia DAM: Image');
$assert_session->elementTextEquals('css', '[data-drupal-selector="edit-media-types"] li:nth-child(2)', 'Acquia DAM: Video');
$assert_session->elementTextEquals('css', '[data-drupal-selector="edit-media-types"] li:nth-child(3)', 'Acquia DAM Asset');
$assert_session->elementTextEquals('css', '[data-drupal-selector="edit-media-types"] li:nth-child(4)', 'Acquia DAM Audio');
$assert_session->elementTextEquals('css', '[data-drupal-selector="edit-media-types"] li:nth-child(5)', 'Acquia DAM Document');
// Assert that expected media label update section appears with correct value.
$assert_session->pageTextContains('Media types label marked for update:');
$assert_session->elementTextEquals('css', '[data-drupal-selector="edit-media-label"] li:nth-child(1)', 'Acquia DAM: Audio → Acquia DAM: Audio update');
$assert_session->elementTextEquals('css', '[data-drupal-selector="edit-media-label"] li:nth-child(2)', 'Acquia DAM Image → Media: Acquia DAM Image');
// Assert that migration table and its header exists.
$assert_session->elementExists('css', 'table#edit-media-bundle-update-info');
$header_text = [
'Media type label and name', 'Method to handle media files', 'Target source type', 'Number of Media Items',
];
foreach ($header_text as $key => $text) {
$assert_session->elementTextEquals('xpath', '//table[contains(@id,"edit-media-bundle-update-info")]//thead//tr//th[' . ($key + 1) . ']', $text);
}
// Assert expected values in migration table for image media type.
$assert_session->elementTextEquals('xpath', '//table[contains(@id,"edit-media-bundle-update-info")]//tbody//tr[1]//td[1]', 'Media: Acquia DAM Image');
$assert_session->elementTextEquals('xpath', '//table[contains(@id,"edit-media-bundle-update-info")]//tbody//tr[1]//td[2]', 'Embed');
$assert_session->elementTextEquals('xpath', '//table[contains(@id,"edit-media-bundle-update-info")]//tbody//tr[1]//td[3]', 'Acquia DAM: Image');
$assert_session->elementTextEquals('xpath', '//table[contains(@id,"edit-media-bundle-update-info")]//tbody//tr[1]//td[4]', '1');
// Assert expected values in migration table for video media type.
$assert_session->elementTextEquals('xpath', '//table[contains(@id,"edit-media-bundle-update-info")]//tbody//tr[2]//td[1]', 'Acquia DAM Video');
$assert_session->elementTextEquals('xpath', '//table[contains(@id,"edit-media-bundle-update-info")]//tbody//tr[2]//td[2]', 'Sync');
$assert_session->elementTextEquals('xpath', '//table[contains(@id,"edit-media-bundle-update-info")]//tbody//tr[2]//td[3]', 'Acquia DAM: Video');
$assert_session->elementTextEquals('xpath', '//table[contains(@id,"edit-media-bundle-update-info")]//tbody//tr[2]//td[4]', '1');
// Assert Previous button exits.
$assert_session->buttonExists('Previous');
// Press save button to save the changes.
$page->pressButton('Save');
$assert_session->waitForText(' Migration preferences have been saved.');
// Press migrate button to start migration.
$page->pressButton('Migrate');
// Assert that the migration is in progress.
$assert_session->waitForText('Acquia DAM migration is in progress…');
// Assert that the migration is completed and get the text.
$assert_session->statusMessageContainsAfterWait('Acquia DAM migration completed successfully.', 'status');
$assert_session->statusMessageContains('The migration process was run earlier 0 seconds ago', 'warning');
// Assert that the warning message is displayed and get its text.
$assert_session->pageTextContains('Migration from legacy to modern Acquia DAM completed');
$assert_session->pageTextContains('This table shows the summary of media items migrated for each media type.');
// Assert that after migration table and its header exists.
$assert_session->elementExists('css', 'table.responsive-enabled');
$header_text = [
'Media type', 'Method for handling media', 'Target source type', 'Number of assets migrated',
];
foreach ($header_text as $key => $text) {
$assert_session->elementTextEquals('xpath', '//table[contains(@class,"responsive-enabled")]//thead//tr//th[' . ($key + 1) . ']', $text);
}
// Assert expected values appears in migration table for image media type.
$assert_session->elementTextEquals('xpath', '//table[contains(@class,"responsive-enabled")]//tbody//tr[1]//td[1]', 'Media: Acquia DAM Image');
$assert_session->elementTextEquals('xpath', '//table[contains(@class,"responsive-enabled")]//tbody//tr[1]//td[2]', 'Embed');
$assert_session->elementTextEquals('xpath', '//table[contains(@class,"responsive-enabled")]//tbody//tr[1]//td[3]', 'Acquia DAM: Image');
$assert_session->elementTextEquals('xpath', '//table[contains(@class,"responsive-enabled")]//tbody//tr[1]//td[4]', '1');
// Assert expected values appears in migration table for video media type.
$assert_session->elementTextEquals('xpath', '//table[contains(@class,"responsive-enabled")]//tbody//tr[2]//td[1]', 'Acquia DAM Video');
$assert_session->elementTextEquals('xpath', '//table[contains(@class,"responsive-enabled")]//tbody//tr[2]//td[2]', 'Sync');
$assert_session->elementTextEquals('xpath', '//table[contains(@class,"responsive-enabled")]//tbody//tr[2]//td[3]', 'Acquia DAM: Video');
$assert_session->elementTextEquals('xpath', '//table[contains(@class,"responsive-enabled")]//tbody//tr[2]//td[4]', '1');
}
}
