You are here

public function SourceCsvFormTest::testSourceCsvForm in Migrate Tools 8.4

Tests the form to edit CSV column aliases.

Throws

\Behat\Mink\Exception\ExpectationException

File

tests/src/Functional/SourceCsvFormTest.php, line 100

Class

SourceCsvFormTest
Test the CSV column alias edit form.

Namespace

Drupal\Tests\migrate_tools\Functional

Code

public function testSourceCsvForm() {

  // Define the paths to be used.
  $executeUrlPath = "/admin/structure/migrate/manage/{$this->group}/migrations/{$this->migration}/execute";
  $editUrlPath = "/admin/structure/migrate/manage/{$this->group}/migrations/{$this->migration}/source/edit";

  // Assert the test migration is listed.
  $this
    ->drupalGet("/admin/structure/migrate/manage/{$this->group}/migrations");
  $session = $this
    ->assertSession();
  $session
    ->responseContains('Test edit of column aliases for CSV source plugin');

  // Proceed to the edit page.
  $this
    ->drupalGet($editUrlPath);
  $session
    ->responseContains('You can change the columns to be used by this migration for each source property.');

  // Test that there are 3 select fields available which match the number of
  // properties in the process pipeline.
  $this
    ->assertTrue($session
    ->optionExists('edit-vid', 'vid')
    ->isSelected());
  $this
    ->assertTrue($session
    ->optionExists('edit-name', 'name')
    ->isSelected());
  $this
    ->assertTrue($session
    ->optionExists('edit-description', 'description')
    ->isSelected());
  $session
    ->responseNotContains('edit-hierarchy');
  $session
    ->responseNotContains('edit-weight');

  // Test that all 5 columns in the CSV source are available as options on
  // one of the select fields.
  $this
    ->assertTrue($session
    ->optionExists('edit-description', 'vid'));
  $this
    ->assertTrue($session
    ->optionExists('edit-description', 'name'));
  $this
    ->assertTrue($session
    ->optionExists('edit-description', 'description'));
  $this
    ->assertTrue($session
    ->optionExists('edit-description', 'hierarchy'));
  $this
    ->assertTrue($session
    ->optionExists('edit-description', 'weight'));

  // Test that two aliases can not be the same.
  $edit = [
    'edit-vid' => 2,
    'edit-name' => 1,
    'edit-description' => 1,
  ];
  $this
    ->drupalPostForm($editUrlPath, $edit, $this
    ->t('Submit'));
  $session
    ->responseContains('Source properties can not share the same source column.');
  $this
    ->assertTrue($session
    ->optionExists('edit-vid', 'description')
    ->isSelected());
  $this
    ->assertTrue($session
    ->optionExists('edit-name', 'name')
    ->isSelected());
  $this
    ->assertTrue($session
    ->optionExists('edit-description', 'name')
    ->isSelected());

  // Test that changes to all the column aliases are saved.
  $edit = [
    'edit-vid' => 4,
    'edit-name' => 0,
    'edit-description' => 1,
  ];
  $this
    ->drupalPostForm($editUrlPath, $edit, $this
    ->t('Submit'));
  $this
    ->assertTrue($session
    ->optionExists('edit-vid', 'weight')
    ->isSelected());
  $this
    ->assertTrue($session
    ->optionExists('edit-name', 'vid')
    ->isSelected());
  $this
    ->assertTrue($session
    ->optionExists('edit-description', 'name')
    ->isSelected());

  // Test that the changes are saved to store.
  $columnConfiguration = $this->store
    ->get('csv_source_test');
  $migrationsChanged = $this->store
    ->get('migrations_changed');
  $this
    ->assertSame([
    'csv_source_test',
  ], $migrationsChanged);
  $expected = [
    'original' => [
      0 => [
        'vid' => 'Vocabulary Id',
      ],
      1 => [
        'name' => 'Name',
      ],
      2 => [
        'description' => 'Description',
      ],
    ],
    'changed' => [
      4 => [
        'vid' => 'weight',
      ],
      0 => [
        'name' => 'vid',
      ],
      1 => [
        'description' => 'name',
      ],
    ],
  ];
  $this
    ->assertSame($expected, $columnConfiguration);

  // Test the migration with incorrect column aliases. Flush the cache to
  // ensure the plugin alter is run.
  drupal_flush_all_caches();
  $edit = [
    'operation' => 'import',
  ];
  $this
    ->drupalPostForm($executeUrlPath, $edit, $this
    ->t('Execute'));
  $session
    ->responseContains("Processed 1 item (1 created, 0 updated, 0 failed, 0 ignored) - done with 'csv_source_test'");

  // Rollback.
  $edit = [
    'operation' => 'rollback',
  ];
  $this
    ->drupalPostForm($executeUrlPath, $edit, $this
    ->t('Execute'));

  // Restore to an order that will successfully migrate.
  $edit = [
    'edit-vid' => 0,
    'edit-name' => 1,
    'edit-description' => 2,
  ];
  $this
    ->drupalPostForm($editUrlPath, $edit, $this
    ->t('Submit'));
  $this
    ->assertTrue($session
    ->optionExists('edit-vid', 'vid')
    ->isSelected());
  $this
    ->assertTrue($session
    ->optionExists('edit-name', 'name')
    ->isSelected());
  $this
    ->assertTrue($session
    ->optionExists('edit-description', 'description')
    ->isSelected());

  // Test the vocabulary migration.
  $edit = [
    'operation' => 'import',
  ];
  drupal_flush_all_caches();
  $this
    ->drupalPostForm($executeUrlPath, $edit, $this
    ->t('Execute'));
  $session
    ->responseContains("Processed 4 items (4 created, 0 updated, 0 failed, 0 ignored) - done with 'csv_source_test'");
  $this
    ->assertEntity('tags', 'Tags', 'Use tags to group articles');
  $this
    ->assertEntity('forums', 'Sujet de discussion', 'Forum navigation vocabulary');
  $this
    ->assertEntity('test_vocabulary', 'Test Vocabulary', 'This is the vocabulary description');
  $this
    ->assertEntity('genre', 'Genre', 'Genre description');
}