View source
<?php
namespace Drupal\Tests\media_migration\Kernel\Plugin\migrate\source\d7;
use Drupal\Core\Database\Database;
use Drupal\migrate\Plugin\MigrateDestinationInterface;
use Drupal\migrate\Plugin\MigrateSourceInterface;
use Drupal\migrate\Row;
use Drupal\Tests\media_migration\Traits\MediaMigrationTestDatabaseTrait;
use Drupal\Tests\migrate\Kernel\MigrateSqlSourceTestBase;
abstract class MediaMigrationSourceTestBase extends MigrateSqlSourceTestBase {
use MediaMigrationTestDatabaseTrait;
public static $modules = [
'media_migration',
'migrate',
'migrate_drupal',
];
protected $sourceDatabase;
protected function setUp() {
parent::setUp();
$destination_plugin = $this
->prophesize(MigrateDestinationInterface::class);
$destination_plugin
->getPluginId()
->willReturn($this
->randomMachineName(16));
$this->migration
->getDestinationPlugin()
->willReturn($destination_plugin
->reveal());
$this
->createSourceMigrationConnection();
$this->sourceDatabase = Database::getConnection('default', 'migrate');
}
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;
$this
->assertNotEmpty($plugin
->getIds());
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 (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 = [];
foreach ($plugin as $row) {
$i++;
$this
->assertInstanceOf(Row::class, $row);
$actual_data[] = $row
->getSource();
}
$this
->assertEquals($expected_data, $actual_data);
if ($expected_count > 0) {
$this
->assertGreaterThan(0, $i);
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');
}
}
}
protected function assertPluginCountCacheability(MigrateSourceInterface $plugin, int $expected_count, ?string $expected_cache_key) {
$cache = \Drupal::cache('migrate');
if (!is_callable([
$cache,
'getCounter',
])) {
return;
}
if ($expected_cache_key) {
$reflector = new \ReflectionObject($plugin);
$property = $reflector
->getProperty('cacheKey');
$property
->setAccessible(TRUE);
$this
->assertSame($expected_cache_key, $property
->getValue($plugin));
$this
->assertFalse($cache
->get($expected_cache_key, 'cache'));
$this
->assertSame([], $cache
->getCounter('set'));
$count = $plugin
->count();
$this
->assertSame($expected_count, $count);
$this
->assertSame([
$expected_cache_key => 1,
], $cache
->getCounter('set'));
$cache_item = $cache
->get($expected_cache_key, 'cache');
$this
->assertNotSame(FALSE, $cache_item, 'This is not a cache hit.');
$this
->assertSame($expected_count, $cache_item->data);
}
else {
$this
->assertSame([], $cache
->getCounter('set'));
$plugin
->count();
$this
->assertSame([], $cache
->getCounter('set'));
}
}
protected function tearDown() {
$this
->cleanupSourceMigrateConnection();
parent::tearDown();
}
}