View source
<?php
namespace Drupal\Tests\migrate\Unit;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\KeyValueStore\KeyValueFactoryInterface;
use Drupal\Core\KeyValueStore\KeyValueStoreInterface;
use Drupal\migrate\MigrateException;
use Drupal\migrate\MigrateExecutable;
use Drupal\migrate\MigrateSkipRowException;
use Drupal\migrate\Plugin\migrate\source\SourcePluginBase;
use Drupal\migrate\Plugin\MigrateIdMapInterface;
use Drupal\migrate\Row;
class MigrateSourceTest extends MigrateTestCase {
protected $defaultMigrationConfiguration = [
'id' => 'test_migration',
'source' => [],
];
protected $row = [
'test_sourceid1' => '1',
'timestamp' => 500,
];
protected $sourceIds = [
'test_sourceid1' => 'test_sourceid1',
];
protected $migration;
protected $executable;
protected function getSource($configuration = [], $migrate_config = [], $status = MigrateIdMapInterface::STATUS_NEEDS_UPDATE, $high_water_value = NULL) {
$container = new ContainerBuilder();
\Drupal::setContainer($container);
$key_value = $this
->createMock(KeyValueStoreInterface::class);
$key_value_factory = $this
->createMock(KeyValueFactoryInterface::class);
$key_value_factory
->method('get')
->with('migrate:high_water')
->willReturn($key_value);
$container
->set('keyvalue', $key_value_factory);
$container
->set('cache.migrate', $this
->createMock(CacheBackendInterface::class));
$this->migrationConfiguration = $this->defaultMigrationConfiguration + $migrate_config;
$this->migration = parent::getMigration();
$this->executable = $this
->getMigrateExecutable($this->migration);
$id_map_array = [
'original_hash' => '',
'hash' => '',
'source_row_status' => $status,
];
$this->idMap
->expects($this
->any())
->method('getRowBySource')
->willReturn($id_map_array);
$constructor_args = [
$configuration,
'd6_action',
[],
$this->migration,
];
$methods = [
'getModuleHandler',
'fields',
'getIds',
'__toString',
'prepareRow',
'initializeIterator',
];
$source_plugin = $this
->getMockBuilder(SourcePluginBase::class)
->setMethods($methods)
->setConstructorArgs($constructor_args)
->getMock();
$source_plugin
->method('fields')
->willReturn([]);
$source_plugin
->method('getIds')
->willReturn([]);
$source_plugin
->method('__toString')
->willReturn('');
$source_plugin
->method('prepareRow')
->willReturn(empty($migrate_config['prepare_row_false']));
$rows = [
$this->row,
];
if (isset($configuration['high_water_property']) && isset($high_water_value)) {
$property = $configuration['high_water_property']['name'];
$rows = array_filter($rows, function (array $row) use ($property, $high_water_value) {
return $row[$property] >= $high_water_value;
});
}
$iterator = new \ArrayIterator($rows);
$source_plugin
->method('initializeIterator')
->willReturn($iterator);
$module_handler = $this
->createMock(ModuleHandlerInterface::class);
$source_plugin
->method('getModuleHandler')
->willReturn($module_handler);
$this->migration
->method('getSourcePlugin')
->willReturn($source_plugin);
return $source_plugin;
}
public function testHighwaterTrackChangesIncompatible() {
$source_config = [
'track_changes' => TRUE,
'high_water_property' => [
'name' => 'something',
],
];
$this
->expectException(MigrateException::class);
$this
->getSource($source_config);
}
public function testCount() {
$container = new ContainerBuilder();
$cache = $this
->createMock(CacheBackendInterface::class);
$cache
->expects($this
->any())
->method('set')
->with($this
->isType('string'), $this
->isType('int'), $this
->isType('int'));
$container
->set('cache.migrate', $cache);
\Drupal::setContainer($container);
$source = $this
->getSource();
$this
->assertEquals(1, $source
->count());
$source = $this
->getSource([
'cache_counts' => TRUE,
]);
$this
->assertEquals(1, $source
->count());
$source = $this
->getSource([
'skip_count' => TRUE,
]);
$this
->assertEquals(-1, $source
->count());
$this->migrationConfiguration['id'] = 'test_migration';
$migration = $this
->getMigration();
$source = new StubSourceGeneratorPlugin([], '', [], $migration);
$this
->assertEquals(-1, $source
->count());
$source = new StubSourceGeneratorPlugin([
'skip_count' => FALSE,
], '', [], $migration);
$this
->assertEquals(3, $source
->count());
}
public function testCountCacheKey() {
$container = new ContainerBuilder();
$cache = $this
->createMock(CacheBackendInterface::class);
$cache
->expects($this
->any())
->method('set')
->with('test_key', $this
->isType('int'), $this
->isType('int'));
$container
->set('cache.migrate', $cache);
\Drupal::setContainer($container);
$source = $this
->getSource([
'cache_counts' => TRUE,
'cache_key' => 'test_key',
]);
$this
->assertEquals(1, $source
->count());
}
public function testPrepareRowFalse() {
$source = $this
->getSource([], [
'prepare_row_false' => TRUE,
]);
$source
->rewind();
$this
->assertNull($source
->current(), 'No row is available when prepareRow() is false.');
}
public function testNextNeedsUpdate() {
$source = $this
->getSource();
$source
->rewind();
$this
->assertTrue(is_a($source
->current(), 'Drupal\\migrate\\Row'), '$row->needsUpdate() is TRUE so we got a row.');
$source = $this
->getSource([], [], MigrateIdMapInterface::STATUS_IMPORTED);
$source
->rewind();
$this
->assertNull($source
->current(), 'Row was already imported, should be NULL');
}
public function testOutdatedHighwater() {
$configuration = [
'high_water_property' => [
'name' => 'timestamp',
],
];
$source = $this
->getSource($configuration, [], MigrateIdMapInterface::STATUS_IMPORTED, $this->row['timestamp'] + 1);
$source
->rewind();
$this
->assertNull($source
->current(), 'Original highwater mark is higher than incoming row timestamp.');
}
public function testNewHighwater() {
$configuration = [
'high_water_property' => [
'name' => 'timestamp',
],
];
$source = $this
->getSource($configuration, [], MigrateIdMapInterface::STATUS_IMPORTED, $this->row['timestamp'] - 1);
$source
->rewind();
$this
->assertInstanceOf(Row::class, $source
->current());
}
public function testPrepareRow() {
$this->migrationConfiguration['id'] = 'test_migration';
$migration = $this
->getMigration();
$source = new StubSourcePlugin([], '', [], $migration);
$row = new Row();
$module_handler = $this
->prophesize(ModuleHandlerInterface::class);
$module_handler
->invokeAll('migrate_prepare_row', [
$row,
$source,
$migration,
])
->willReturn([
TRUE,
TRUE,
])
->shouldBeCalled();
$module_handler
->invokeAll('migrate_' . $migration
->id() . '_prepare_row', [
$row,
$source,
$migration,
])
->willReturn([
TRUE,
TRUE,
])
->shouldBeCalled();
$source
->setModuleHandler($module_handler
->reveal());
$this->idMap
->expects($this
->never())
->method('saveIdMapping');
$this
->assertTrue($source
->prepareRow($row));
$source = new StubSourcePlugin([
'track_changes' => TRUE,
], '', [], $migration);
$row2 = $this
->prophesize(Row::class);
$row2
->rehash()
->shouldBeCalled();
$module_handler
->invokeAll('migrate_prepare_row', [
$row2,
$source,
$migration,
])
->willReturn([
TRUE,
TRUE,
])
->shouldBeCalled();
$module_handler
->invokeAll('migrate_' . $migration
->id() . '_prepare_row', [
$row2,
$source,
$migration,
])
->willReturn([
TRUE,
TRUE,
])
->shouldBeCalled();
$source
->setModuleHandler($module_handler
->reveal());
$this
->assertTrue($source
->prepareRow($row2
->reveal()));
}
public function testPrepareRowGlobalPrepareSkip() {
$this->migrationConfiguration['id'] = 'test_migration';
$migration = $this
->getMigration();
$source = new StubSourcePlugin([], '', [], $migration);
$row = new Row();
$module_handler = $this
->prophesize(ModuleHandlerInterface::class);
$module_handler
->invokeAll('migrate_prepare_row', [
$row,
$source,
$migration,
])
->willReturn([
TRUE,
FALSE,
TRUE,
])
->shouldBeCalled();
$module_handler
->invokeAll('migrate_' . $migration
->id() . '_prepare_row', [
$row,
$source,
$migration,
])
->willReturn([
TRUE,
TRUE,
])
->shouldBeCalled();
$source
->setModuleHandler($module_handler
->reveal());
$this->idMap
->expects($this
->once())
->method('saveIdMapping')
->with($row, [], MigrateIdMapInterface::STATUS_IGNORED);
$this
->assertFalse($source
->prepareRow($row));
}
public function testPrepareRowMigratePrepareSkip() {
$this->migrationConfiguration['id'] = 'test_migration';
$migration = $this
->getMigration();
$source = new StubSourcePlugin([], '', [], $migration);
$row = new Row();
$module_handler = $this
->prophesize(ModuleHandlerInterface::class);
$module_handler
->invokeAll('migrate_prepare_row', [
$row,
$source,
$migration,
])
->willReturn([
TRUE,
TRUE,
])
->shouldBeCalled();
$module_handler
->invokeAll('migrate_' . $migration
->id() . '_prepare_row', [
$row,
$source,
$migration,
])
->willReturn([
TRUE,
FALSE,
TRUE,
])
->shouldBeCalled();
$source
->setModuleHandler($module_handler
->reveal());
$this->idMap
->expects($this
->once())
->method('saveIdMapping')
->with($row, [], MigrateIdMapInterface::STATUS_IGNORED);
$this
->assertFalse($source
->prepareRow($row));
}
public function testPrepareRowPrepareException() {
$this->migrationConfiguration['id'] = 'test_migration';
$migration = $this
->getMigration();
$source = new StubSourcePlugin([], '', [], $migration);
$row = new Row();
$module_handler = $this
->prophesize(ModuleHandlerInterface::class);
$module_handler
->invokeAll('migrate_prepare_row', [
$row,
$source,
$migration,
])
->willReturn([
TRUE,
TRUE,
])
->shouldBeCalled();
$module_handler
->invokeAll('migrate_' . $migration
->id() . '_prepare_row', [
$row,
$source,
$migration,
])
->willThrow(new MigrateSkipRowException())
->shouldBeCalled();
$source
->setModuleHandler($module_handler
->reveal());
$this->idMap
->expects($this
->once())
->method('saveIdMapping')
->with($row, [], MigrateIdMapInterface::STATUS_IGNORED);
$this
->assertFalse($source
->prepareRow($row));
$e = new MigrateSkipRowException('', FALSE);
$module_handler
->invokeAll('migrate_' . $migration
->id() . '_prepare_row', [
$row,
$source,
$migration,
])
->willThrow($e)
->shouldBeCalled();
$this
->assertFalse($source
->prepareRow($row));
}
public function testDefaultPropertiesValues() {
$this->migrationConfiguration['id'] = 'test_migration';
$migration = $this
->getMigration();
$source = new StubSourceGeneratorPlugin([], '', [], $migration);
$this
->assertTrue($source
->getSkipCount());
$this
->assertTrue($source
->getCacheCounts());
$this
->assertTrue($source
->getTrackChanges());
}
protected function getMigrateExecutable($migration) {
$message = $this
->createMock('Drupal\\migrate\\MigrateMessageInterface');
$event_dispatcher = $this
->createMock('Symfony\\Component\\EventDispatcher\\EventDispatcherInterface');
return new MigrateExecutable($migration, $message, $event_dispatcher);
}
}
class StubSourcePlugin extends SourcePluginBase {
public function setModuleHandler(ModuleHandlerInterface $module_handler) {
$this->moduleHandler = $module_handler;
}
public function fields() {
return [];
}
public function __toString() {
return '';
}
public function getIds() {
return [];
}
protected function initializeIterator() {
return [];
}
}
class StubSourceGeneratorPlugin extends StubSourcePlugin {
protected $skipCount = TRUE;
protected $cacheCounts = TRUE;
protected $trackChanges = TRUE;
public function getSkipCount() {
return $this->skipCount;
}
public function getCacheCounts() {
return $this->cacheCounts;
}
public function getTrackChanges() {
return $this->trackChanges;
}
protected function initializeIterator() {
$data = [
[
'title' => 'foo',
],
[
'title' => 'bar',
],
[
'title' => 'iggy',
],
];
foreach ($data as $row) {
(yield $row);
}
}
}