You are here

public function DrushCommandsGeneratorTest::testSyncImport in Migrate Tools 8.5

Tests synced import.

File

tests/src/Functional/DrushCommandsGeneratorTest.php, line 69

Class

DrushCommandsGeneratorTest
Execute drush on fully functional website using source generators.

Namespace

Drupal\Tests\migrate_tools\Functional

Code

public function testSyncImport() : void {
  $this
    ->drush('mim', [
    'csv_source_test',
  ]);
  $this
    ->assertStringContainsString('1/4', $this
    ->getErrorOutput());
  $this
    ->assertStringContainsString('4/4', $this
    ->getErrorOutput());
  $this
    ->assertStringContainsString('[notice] Processed 4 items (4 created, 0 updated, 0 failed, 0 ignored) - done with \'csv_source_test\'', $this
    ->getErrorOutput());
  $this
    ->assertStringNotContainsString('5/5', $this
    ->getErrorOutput());
  $vocabulary = \Drupal::entityTypeManager()
    ->getStorage('taxonomy_vocabulary')
    ->load('genre');
  $this
    ->assertEquals('Genre', $vocabulary
    ->label());
  $this
    ->assertEquals(4, \Drupal::entityTypeManager()
    ->getStorage('taxonomy_vocabulary')
    ->getQuery()
    ->count()
    ->execute());

  // Remove one vocab and replace with another.
  $this->sourceData = str_replace('genre,Genre,Genre description,1,0', 'fruit,Fruit,Fruit description,1,0', $this->sourceData);
  file_put_contents('public://test.csv', $this->sourceData);

  // Execute sync migration.
  $this
    ->drush('mim', [
    'csv_source_test',
  ], [
    'sync' => NULL,
  ]);
  $this
    ->assertStringContainsString('1/4', $this
    ->getErrorOutput());
  $this
    ->assertStringContainsString('25% [notice] Rolled back 1 item - done with \'csv_source_test\'', $this
    ->getErrorOutput());
  $this
    ->assertStringContainsString('4/4', $this
    ->getErrorOutput());
  $this
    ->assertStringContainsString('5/5', $this
    ->getErrorOutput());
  $this
    ->assertStringContainsString('100% [notice] Processed 4 items (1 created, 3 updated, 0 failed, 0 ignored) - done with \'csv_source_test\'', $this
    ->getErrorOutput());
  $this
    ->assertEquals(4, \Drupal::entityTypeManager()
    ->getStorage('taxonomy_vocabulary')
    ->getQuery()
    ->count()
    ->execute());

  // Flush cache so recently deleted vocabulary actually goes away.
  drupal_flush_all_caches();
  $this
    ->assertEmpty(\Drupal::entityTypeManager()
    ->getStorage('taxonomy_vocabulary')
    ->load('genre'));

  /** @var \Drupal\migrate\Plugin\MigrateIdMapInterface $id_map */
  $id_map = $this->container
    ->get('plugin.manager.migration')
    ->createInstance('csv_source_test')
    ->getIdMap();
  $this
    ->assertCount(4, $id_map);
}