Commit 955edd18 authored by Dan Flanagan's avatar Dan Flanagan Committed by Dan Flanagan
Browse files

Issue #3309873 by danflanagan8: Migrate Sandbox should offer to "Fetch next...

Issue #3309873 by danflanagan8: Migrate Sandbox should offer to "Fetch next row" if populated from real migration
parent e4a355e3
Loading
Loading
Loading
Loading
+59 −1
Original line number Diff line number Diff line
@@ -85,6 +85,29 @@ class MigrateSandboxForm extends ConfigFormBase {
          'callback' => '::updatePopulateFromMigration',
          'wrapper' => 'js-migrate-sandbox-wrapper',
        ],
        '#attributes' => [
          'class' => [
            'button',
            'button--primary',
          ],
        ],
      ],
      'populate_next' => [
        '#type' => 'button',
        '#value' => $this->t('Fetch next row'),
        '#ajax' => [
          'callback' => '::updatePopulateFromMigration',
          'wrapper' => 'js-migrate-sandbox-wrapper',
        ],
        '#attributes' => [
          'title' => $this->t('If populating took a long time, so will fetching the next row. This feature offers convenience but does not improve computational efficiency.'),
        ],
        '#states' => [
          'visible' => [
            'input[name="populate_source_ids"]' => ['value' => 'something to make this invisible'],
            'input[name="populate_migration"]' => ['value' => 'something to make this invisible'],
          ],
        ],
      ],
    ];
    $form['intro'] = [
@@ -523,6 +546,9 @@ class MigrateSandboxForm extends ConfigFormBase {
      $source->rewind();
      if ($form_state->getValue('populate_source_ids') !== '') {
        $ids = explode(",", $form_state->getValue('populate_source_ids'));
        if ($form_state->getTriggeringElement()['#attributes']['data-drupal-selector'] === 'edit-populate-next') {
          $next = TRUE;
        }
        while ($row = $source->current()) {
          // Strict equality below is cumbersome because input is a string, but
          // the true type of the source id may be an integer.
@@ -532,12 +558,23 @@ class MigrateSandboxForm extends ConfigFormBase {
          }
          $source->next();
        }
        if ($next) {
          $data = NULL;
          $source->next();
          if ($row = $source->current()) {
            $data = $row->getSource();
          }
          else {
            throw new \InvalidArgumentException("No more rows in migration $migration_id");
          }
        }
        if (is_null($data)) {
          throw new \InvalidArgumentException("No row found for source ids {$form_state->getValue('populate_source_ids')} in migration $migration_id");
        }
      }
      else {
        $data = $source->current()->getSource();
        $row = $source->current();
        $data = $row->getSource();
      }
    }
    catch (\Throwable $e) {
@@ -548,11 +585,32 @@ class MigrateSandboxForm extends ConfigFormBase {
      array_unshift($form['populate'], $messages);
      \Drupal::messenger()->addError($e->getMessage());
    }
    if ($row) {
      // Always make sure the source ids field is up-to-date.
      $ids = implode(',', $row->getSourceIdValues());
      $form['populate']['populate_source_ids']['#value'] = $ids;
      $form_state->setValue('populate_source_ids', $ids);
      $form['populate']['populate_next']['#states'] = [
        'visible' => [
          'input[name="populate_source_ids"]' => ['value' => $ids],
          'input[name="populate_migration"]' => ['value' => $migration_id],
        ],
      ];
    }
    else {
      $form['populate']['populate_next']['#states'] = [
        'visible' => [
          'input[name="populate_source_ids"]' => ['value' => 'something to make this invisible'],
          'input[name="populate_migration"]' => ['value' => 'something to make this invisible'],
        ],
      ];
    }
    if ($data) {
      // The data also has things like constants and ids that are really part
      // of the source defintion, not the row. Remove those values.
      $data = array_diff_key($data, $source_definition);
      $form['source_wrapper']['data_rows']['#value'] = Yaml::encode($data);
      \Drupal::messenger()->addStatus("Row retrieved for source ids {$form_state->getValue('populate_source_ids')} in migration $migration_id");
    }
    else {
      $form['source_wrapper']['data_rows']['#value'] = '';
+52 −3
Original line number Diff line number Diff line
@@ -148,7 +148,7 @@ class MigrateSandboxTest extends WebDriverTestBase {
    $page->find('css', '[data-drupal-selector="edit-populate"] summary')->click();
    $page->find('css', '[name="populate_migration"]')->setValue('migrate_sandbox_test');
    $page->pressButton('Populate');
    $assert_session->assertWaitOnAjaxRequest();
    $assert_session->statusMessageContainsAfterWait('Row retrieved for source ids 1 in migration migrate_sandbox_test', 'status');
    $expected = [
      'data_rows' => "id: 1\nbody: 'body the first'\ntitle: test_title_1\n",
      'constants' => "loud: '!'\n",
@@ -167,7 +167,7 @@ class MigrateSandboxTest extends WebDriverTestBase {
    $page->find('css', '[name="populate_migration"]')->setValue('migrate_sandbox_test');
    $page->find('css', '[name="populate_source_ids"]')->setValue('2');
    $page->pressButton('Populate');
    $assert_session->assertWaitOnAjaxRequest();
    $assert_session->statusMessageContainsAfterWait('Row retrieved for source ids 2 in migration migrate_sandbox_test', 'status');
    $expected = [
      'data_rows' => "id: 2\nbody: 'body the second'\ntitle: test_title_2\n",
      'constants' => "loud: '!'\n",
@@ -195,7 +195,7 @@ class MigrateSandboxTest extends WebDriverTestBase {
    $page->find('css', '[name="populate_migration"]')->setValue('migrate_sandbox_test');
    $page->find('css', '[name="populate_source_ids"]')->setValue('2');
    $page->pressButton('Populate');
    $assert_session->assertWaitOnAjaxRequest();
    $assert_session->statusMessageContainsAfterWait('Row retrieved for source ids 2 in migration migrate_sandbox_test', 'status');
    $expected = [
      'data_rows' => "id: 2\nbody: 'body the second'\ntitle: test_title_2\n",
      'constants' => "loud: '!'\n",
@@ -205,6 +205,55 @@ class MigrateSandboxTest extends WebDriverTestBase {
    $this->assertSame($expected['constants'], $page->find('css', '[name="constants"]')->getValue());
    $this->assertSame($expected['process'], $page->find('css', '[name="process"]')->getValue());

    // Test the "fetch next row" feature. Start by kind of resetting things.
    $page->find('css', '[data-drupal-selector="edit-populate"] summary')->click();
    $page->find('css', '[name="populate_migration"]')->setValue('migrate_sandbox_test');
    $page->find('css', '[name="populate_source_ids"]')->setValue('');
    $page->find('css', '[name="populate_process"]')->setValue(TRUE);
    $this->assertFalse($page->findButton('Fetch next row')->isVisible());
    $page->pressButton('Populate');
    $assert_session->statusMessageContainsAfterWait('Row retrieved for source ids 1 in migration migrate_sandbox_test', 'status');
    $expected = [
      'data_rows' => "id: 1\nbody: 'body the first'\ntitle: test_title_1\n",
      'constants' => "loud: '!'\n",
      'process' => "title:\n  plugin: concat\n  source:\n    - title\n    - constants/loud\nbody/value: body\n",
    ];
    $this->assertSame($expected['data_rows'], $page->find('css', '[name="data_rows"]')->getValue());
    $this->assertSame($expected['constants'], $page->find('css', '[name="constants"]')->getValue());
    $this->assertSame($expected['process'], $page->find('css', '[name="process"]')->getValue());
    // Now the button should be visible and it should work!
    $page->find('css', '[data-drupal-selector="edit-populate"] summary')->click();
    $this->assertSame('migrate_sandbox_test', $page->find('css', '[name="populate_migration"]')->getValue());
    $this->assertSame('1', $page->find('css', '[name="populate_source_ids"]')->getValue());
    $this->assertTrue($page->findButton('Fetch next row')->isVisible());
    $page->pressButton('Fetch next row');
    $assert_session->statusMessageContainsAfterWait('Row retrieved for source ids 2 in migration migrate_sandbox_test', 'status');
    $expected = [
      'data_rows' => "id: 2\nbody: 'body the second'\ntitle: test_title_2\n",
      'constants' => "loud: '!'\n",
      'process' => "title:\n  plugin: concat\n  source:\n    - title\n    - constants/loud\nbody/value: body\n",
    ];
    $this->assertSame($expected['data_rows'], $page->find('css', '[name="data_rows"]')->getValue());
    $this->assertSame($expected['constants'], $page->find('css', '[name="constants"]')->getValue());
    $this->assertSame($expected['process'], $page->find('css', '[name="process"]')->getValue());
    $page->find('css', '[data-drupal-selector="edit-populate"] summary')->click();
    $this->assertSame('migrate_sandbox_test', $page->find('css', '[name="populate_migration"]')->getValue());
    $this->assertSame('2', $page->find('css', '[name="populate_source_ids"]')->getValue());
    // Confirm the button disappears if we change the migration or source ids.
    $this->assertTrue($page->findButton('Fetch next row')->isVisible());
    $page->find('css', '[name="populate_source_ids"]')->setValue('3');
    $this->assertFalse($page->findButton('Fetch next row')->isVisible());
    $page->find('css', '[name="populate_source_ids"]')->setValue('2');
    $this->assertTrue($page->findButton('Fetch next row')->isVisible());
    $page->find('css', '[name="populate_migration"]')->setValue('uhhh');
    $this->assertFalse($page->findButton('Fetch next row')->isVisible());
    $page->find('css', '[name="populate_migration"]')->setValue('migrate_sandbox_test');
    $this->assertTrue($page->findButton('Fetch next row')->isVisible());
    // Test fetching next when there is no next.
    $page->pressButton('Fetch next row');
    $assert_session->statusMessageContainsAfterWait('No more rows in migration migrate_sandbox_test', 'error');
    $this->assertFalse($page->findButton('Fetch next row')->isVisible());

    // Make sure nothing has been saved in migrate tables.
    $this->assertFalse(\Drupal::database()->schema()->tableExists('migrate_map_migrate_sandbox_test'));
    $this->assertFalse(\Drupal::database()->schema()->tableExists('migrate_message_migrate_sandbox_test'));