You are here

public function MediaMigrationSourceTestBase::testSource in Media Migration 8

@dataProvider providerSource

Overrides MigrateSqlSourceTestBase::testSource

File

tests/src/Kernel/Plugin/migrate/source/d7/MediaMigrationSourceTestBase.php, line 65

Class

MediaMigrationSourceTestBase
Base class for testing media migrate source plugins with native databases.

Namespace

Drupal\Tests\media_migration\Kernel\Plugin\migrate\source\d7

Code

public function testSource(array $source_data, array $expected_data, $expected_count = NULL, array $configuration = [], $high_water = NULL, $expected_cache_key = NULL) {
  $this
    ->importSourceDatabase($source_data);
  $plugin = $this
    ->getPlugin($configuration);
  $clone_plugin = clone $plugin;

  // All source plugins must define IDs.
  $this
    ->assertNotEmpty($plugin
    ->getIds());

  // If there is a high water mark, set it in the high water storage.
  if (isset($high_water)) {
    $this->container
      ->get('keyvalue')
      ->get('migrate:high_water')
      ->set($this->migration
      ->reveal()
      ->id(), $high_water);
  }
  if (is_null($expected_count)) {
    $expected_count = count($expected_data);
  }

  // If an expected count was given, assert it only if the plugin is
  // countable.
  if (is_numeric($expected_count)) {
    $this
      ->assertInstanceOf('\\Countable', $plugin);
    $this
      ->assertCount($expected_count, $plugin);
  }
  $this
    ->assertPluginCountCacheability($plugin, $expected_count, $expected_cache_key);
  $i = 0;
  $actual_data = [];

  /** @var \Drupal\migrate\Row $row */
  foreach ($plugin as $row) {
    $i++;
    $this
      ->assertInstanceOf(Row::class, $row);
    $actual_data[] = $row
      ->getSource();
  }
  $this
    ->assertEquals($expected_data, $actual_data);

  // False positives occur if the foreach is not entered. So, confirm the
  // foreach loop was entered if the expected count is greater than 0.
  if ($expected_count > 0) {
    $this
      ->assertGreaterThan(0, $i);

    // Test that we can skip all rows.
    // The 'migrate_skip_all_rows_test' test module exists and installed only
    // in Drupal core 9.1+.
    if (\Drupal::moduleHandler()
      ->moduleExists('migrate_skip_all_rows_test')) {
      \Drupal::state()
        ->set('migrate_skip_all_rows_test_migrate_prepare_row', TRUE);
      $iterator_items = iterator_to_array($clone_plugin, FALSE);
      $this
        ->assertEmpty($iterator_items, 'Row not skipped');
    }
  }
}