Drupal 8.0.0 beta 12. More info: https://www.drupal.org/node/2514176

This commit is contained in:
Pantheon Automation 2015-08-17 17:00:26 -07:00 committed by Greg Anderson
commit 9921556621
13277 changed files with 1459781 additions and 0 deletions

View file

@ -0,0 +1,31 @@
<?php
/**
* @file
* Contains \Drupal\Tests\migrate\Unit\Entity\MigrationTest.
*/
namespace Drupal\Tests\migrate\Unit\Entity;
use Drupal\migrate\Entity\Migration;
use Drupal\Tests\UnitTestCase;
/**
* Tests the migrate entity.
*
* @coversDefaultClass \Drupal\migrate\Entity\Migration
* @group migrate
*/
class MigrationTest extends UnitTestCase {
/**
* Tests Migration::getProcessPlugins()
*
* @covers ::getProcessPlugins
*/
public function testGetProcessPlugins() {
$migration = new Migration([], 'migration');
$this->assertEquals([], $migration->getProcessPlugins([]));
}
}

View file

@ -0,0 +1,120 @@
<?php
/**
* @file
* Contains \Drupal\Tests\migrate\Unit\MigrateExecutableMemoryExceededTest.
*/
namespace Drupal\Tests\migrate\Unit;
/**
* Tests the \Drupal\migrate\MigrateExecutable::memoryExceeded() method.
*
* @group migrate
*/
class MigrateExecutableMemoryExceededTest extends MigrateTestCase {
/**
* The mocked migration entity.
*
* @var \Drupal\migrate\Entity\MigrationInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $migration;
/**
* The mocked migrate message.
*
* @var \Drupal\migrate\MigrateMessageInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $message;
/**
* The tested migrate executable.
*
* @var \Drupal\Tests\migrate\Unit\TestMigrateExecutable
*/
protected $executable;
/**
* The migration configuration, initialized to set the ID to test.
*
* @var array
*/
protected $migrationConfiguration = array(
'id' => 'test',
);
/**
* php.init memory_limit value.
*/
protected $memoryLimit = 10000000;
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->migration = $this->getMigration();
$this->message = $this->getMock('Drupal\migrate\MigrateMessageInterface');
$this->executable = new TestMigrateExecutable($this->migration, $this->message);
$this->executable->setStringTranslation($this->getStringTranslationStub());
}
/**
* Runs the actual test.
*
* @param string $message
* The second message to assert.
* @param bool $memory_exceeded
* Whether to test the memory exceeded case.
* @param int $memory_usage_first
* (optional) The first memory usage value.
* @param int $memory_usage_second
* (optional) The fake amount of memory usage reported after memory reclaim.
* @param int $memory_limit
* (optional) The memory limit.
*/
protected function runMemoryExceededTest($message, $memory_exceeded, $memory_usage_first = NULL, $memory_usage_second = NULL, $memory_limit = NULL) {
$this->executable->setMemoryLimit($memory_limit ?: $this->memoryLimit);
$this->executable->setMemoryUsage($memory_usage_first ?: $this->memoryLimit, $memory_usage_second ?: $this->memoryLimit);
$this->executable->setMemoryThreshold(0.85);
if ($message) {
$this->executable->message->expects($this->at(0))
->method('display')
->with($this->stringContains('reclaiming memory'));
$this->executable->message->expects($this->at(1))
->method('display')
->with($this->stringContains($message));
}
else {
$this->executable->message->expects($this->never())
->method($this->anything());
}
$result = $this->executable->memoryExceeded();
$this->assertEquals($memory_exceeded, $result);
}
/**
* Tests memoryExceeded method when a new batch is needed.
*/
public function testMemoryExceededNewBatch() {
// First case try reset and then start new batch.
$this->runMemoryExceededTest('starting new batch', TRUE);
}
/**
* Tests memoryExceeded method when enough is cleared.
*/
public function testMemoryExceededClearedEnough() {
$this->runMemoryExceededTest('reclaimed enough', FALSE, $this->memoryLimit, $this->memoryLimit * 0.75);
}
/**
* Tests memoryExceeded when memory usage is not exceeded.
*/
public function testMemoryNotExceeded() {
$this->runMemoryExceededTest('', FALSE, floor($this->memoryLimit * 0.85) - 1);
}
}

View file

@ -0,0 +1,525 @@
<?php
/**
* @file
* Contains \Drupal\Tests\migrate\Unit\MigrateExecutableTest.
*/
namespace Drupal\Tests\migrate\Unit;
use Drupal\migrate\Entity\MigrationInterface;
use Drupal\migrate\Plugin\MigrateIdMapInterface;
use Drupal\migrate\MigrateException;
use Drupal\migrate\Row;
/**
* @coversDefaultClass \Drupal\Tests\migrate\Unit\MigrateExecutableTest
* @group migrate
*/
class MigrateExecutableTest extends MigrateTestCase {
/**
* The mocked migration entity.
*
* @var \Drupal\migrate\Entity\MigrationInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $migration;
/**
* The mocked migrate message.
*
* @var \Drupal\migrate\MigrateMessageInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $message;
/**
* The tested migrate executable.
*
* @var \Drupal\Tests\migrate\Unit\TestMigrateExecutable
*/
protected $executable;
protected $migrationConfiguration = array(
'id' => 'test',
);
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->migration = $this->getMigration();
$this->message = $this->getMock('Drupal\migrate\MigrateMessageInterface');
$this->executable = new TestMigrateExecutable($this->migration, $this->message);
$this->executable->setStringTranslation($this->getStringTranslationStub());
$this->executable->setTimeThreshold(0.1);
$this->executable->limit = array('unit' => 'second', 'value' => 1);
}
/**
* Tests an import with an incomplete rewinding.
*/
public function testImportWithFailingRewind() {
$exception_message = $this->getRandomGenerator()->string();
$source = $this->getMock('Drupal\migrate\Plugin\MigrateSourceInterface');
$source->expects($this->once())
->method('rewind')
->will($this->throwException(new \Exception($exception_message)));
$this->migration->expects($this->any())
->method('getSourcePlugin')
->will($this->returnValue($source));
// Ensure that a message with the proper message was added.
$this->message->expects($this->once())
->method('display')
->with("Migration failed with source plugin exception: $exception_message");
$result = $this->executable->import();
$this->assertEquals(MigrationInterface::RESULT_FAILED, $result);
}
/**
* Tests the import method with a valid row.
*/
public function testImportWithValidRow() {
$source = $this->getMockSource();
$row = $this->getMockBuilder('Drupal\migrate\Row')
->disableOriginalConstructor()
->getMock();
$row->expects($this->once())
->method('getSourceIdValues')
->will($this->returnValue(array('id' => 'test')));
$this->idMap->expects($this->once())
->method('lookupDestinationId')
->with(array('id' => 'test'))
->will($this->returnValue(array('test')));
$source->expects($this->once())
->method('current')
->will($this->returnValue($row));
$this->executable->setSource($source);
$this->migration->expects($this->once())
->method('getProcessPlugins')
->will($this->returnValue(array()));
$destination = $this->getMock('Drupal\migrate\Plugin\MigrateDestinationInterface');
$destination->expects($this->once())
->method('import')
->with($row, array('test'))
->will($this->returnValue(array('id' => 'test')));
$this->migration->expects($this->once())
->method('getDestinationPlugin')
->will($this->returnValue($destination));
$this->assertSame(MigrationInterface::RESULT_COMPLETED, $this->executable->import());
$this->assertSame(1, $this->executable->getSuccessesSinceFeedback());
$this->assertSame(1, $this->executable->getTotalSuccesses());
$this->assertSame(1, $this->executable->getTotalProcessed());
$this->assertSame(1, $this->executable->getProcessedSinceFeedback());
}
/**
* Tests the import method with a valid row.
*/
public function testImportWithValidRowWithoutDestinationId() {
$source = $this->getMockSource();
$row = $this->getMockBuilder('Drupal\migrate\Row')
->disableOriginalConstructor()
->getMock();
$row->expects($this->once())
->method('getSourceIdValues')
->will($this->returnValue(array('id' => 'test')));
$this->idMap->expects($this->once())
->method('lookupDestinationId')
->with(array('id' => 'test'))
->will($this->returnValue(array('test')));
$source->expects($this->once())
->method('current')
->will($this->returnValue($row));
$this->executable->setSource($source);
$this->migration->expects($this->once())
->method('getProcessPlugins')
->will($this->returnValue(array()));
$destination = $this->getMock('Drupal\migrate\Plugin\MigrateDestinationInterface');
$destination->expects($this->once())
->method('import')
->with($row, array('test'))
->will($this->returnValue(TRUE));
$this->migration->expects($this->once())
->method('getDestinationPlugin')
->will($this->returnValue($destination));
$this->idMap->expects($this->never())
->method('saveIdMapping');
$this->assertSame(MigrationInterface::RESULT_COMPLETED, $this->executable->import());
$this->assertSame(1, $this->executable->getSuccessesSinceFeedback());
$this->assertSame(1, $this->executable->getTotalSuccesses());
$this->assertSame(1, $this->executable->getTotalProcessed());
$this->assertSame(1, $this->executable->getProcessedSinceFeedback());
}
/**
* Tests the import method with a valid row.
*/
public function testImportWithValidRowNoDestinationValues() {
$source = $this->getMockSource();
$row = $this->getMockBuilder('Drupal\migrate\Row')
->disableOriginalConstructor()
->getMock();
$row->expects($this->once())
->method('getSourceIdValues')
->will($this->returnValue(array('id' => 'test')));
$source->expects($this->once())
->method('current')
->will($this->returnValue($row));
$this->executable->setSource($source);
$this->migration->expects($this->once())
->method('getProcessPlugins')
->will($this->returnValue(array()));
$destination = $this->getMock('Drupal\migrate\Plugin\MigrateDestinationInterface');
$destination->expects($this->once())
->method('import')
->with($row, array('test'))
->will($this->returnValue(array()));
$this->migration->expects($this->once())
->method('getDestinationPlugin')
->will($this->returnValue($destination));
$this->idMap->expects($this->once())
->method('delete')
->with(array('id' => 'test'), TRUE);
$this->idMap->expects($this->once())
->method('saveIdMapping')
->with($row, array(), MigrateIdMapInterface::STATUS_FAILED, NULL);
$this->idMap->expects($this->once())
->method('messageCount')
->will($this->returnValue(0));
$this->idMap->expects($this->once())
->method('saveMessage');
$this->idMap->expects($this->once())
->method('lookupDestinationId')
->with(array('id' => 'test'))
->will($this->returnValue(array('test')));
$this->message->expects($this->once())
->method('display')
->with('New object was not saved, no error provided');
$this->assertSame(MigrationInterface::RESULT_COMPLETED, $this->executable->import());
}
/**
* Tests the import method with a MigrateException being thrown.
*/
public function testImportWithValidRowWithMigrateException() {
$exception_message = $this->getRandomGenerator()->string();
$source = $this->getMockSource();
$row = $this->getMockBuilder('Drupal\migrate\Row')
->disableOriginalConstructor()
->getMock();
$row->expects($this->once())
->method('getSourceIdValues')
->will($this->returnValue(array('id' => 'test')));
$source->expects($this->once())
->method('current')
->will($this->returnValue($row));
$this->executable->setSource($source);
$this->migration->expects($this->once())
->method('getProcessPlugins')
->will($this->returnValue(array()));
$destination = $this->getMock('Drupal\migrate\Plugin\MigrateDestinationInterface');
$destination->expects($this->once())
->method('import')
->with($row, array('test'))
->will($this->throwException(new MigrateException($exception_message)));
$this->migration->expects($this->once())
->method('getDestinationPlugin')
->will($this->returnValue($destination));
$this->idMap->expects($this->once())
->method('saveIdMapping')
->with($row, array(), MigrateIdMapInterface::STATUS_FAILED, NULL);
$this->idMap->expects($this->once())
->method('saveMessage');
$this->message->expects($this->once())
->method('display')
->with($exception_message);
$this->idMap->expects($this->once())
->method('lookupDestinationId')
->with(array('id' => 'test'))
->will($this->returnValue(array('test')));
$this->assertSame(MigrationInterface::RESULT_COMPLETED, $this->executable->import());
}
/**
* Tests the import method with a regular Exception being thrown.
*/
public function testImportWithValidRowWithException() {
$exception_message = $this->getRandomGenerator()->string();
$source = $this->getMockSource();
$row = $this->getMockBuilder('Drupal\migrate\Row')
->disableOriginalConstructor()
->getMock();
$row->expects($this->once())
->method('getSourceIdValues')
->will($this->returnValue(array('id' => 'test')));
$source->expects($this->once())
->method('current')
->will($this->returnValue($row));
$this->executable->setSource($source);
$this->migration->expects($this->once())
->method('getProcessPlugins')
->will($this->returnValue(array()));
$destination = $this->getMock('Drupal\migrate\Plugin\MigrateDestinationInterface');
$destination->expects($this->once())
->method('import')
->with($row, array('test'))
->will($this->throwException(new \Exception($exception_message)));
$this->migration->expects($this->once())
->method('getDestinationPlugin')
->will($this->returnValue($destination));
$this->idMap->expects($this->once())
->method('saveIdMapping')
->with($row, array(), MigrateIdMapInterface::STATUS_FAILED, NULL);
$this->idMap->expects($this->once())
->method('saveMessage');
$this->idMap->expects($this->once())
->method('lookupDestinationId')
->with(array('id' => 'test'))
->will($this->returnValue(array('test')));
$this->message->expects($this->once())
->method('display')
->with($exception_message);
$this->assertSame(MigrationInterface::RESULT_COMPLETED, $this->executable->import());
}
/**
* Tests time limit option method.
*/
public function testTimeOptionExceeded() {
// Assert time limit of one second (test configuration default) is exceeded.
$this->executable->setTimeElapsed(1);
$this->assertTrue($this->executable->timeOptionExceeded());
// Assert time limit not exceeded.
$this->executable->limit = array('unit' => 'seconds', 'value' => (int) $_SERVER['REQUEST_TIME'] - 3600);
$this->assertFalse($this->executable->timeOptionExceeded());
// Assert no time limit.
$this->executable->limit = array();
$this->assertFalse($this->executable->timeOptionExceeded());
}
/**
* Tests get time limit method.
*/
public function testGetTimeLimit() {
// Assert time limit has a unit of one second (test configuration default).
$limit = $this->executable->limit;
$this->assertArrayHasKey('unit', $limit);
$this->assertSame('second', $limit['unit']);
$this->assertSame($limit['value'], $this->executable->getTimeLimit());
// Assert time limit has a unit of multiple seconds.
$this->executable->limit = array('unit' => 'seconds', 'value' => 30);
$limit = $this->executable->limit;
$this->assertArrayHasKey('unit', $limit);
$this->assertSame('seconds', $limit['unit']);
$this->assertSame($limit['value'], $this->executable->getTimeLimit());
// Assert no time limit.
$this->executable->limit = array();
$limit = $this->executable->limit;
$this->assertArrayNotHasKey('unit', $limit);
$this->assertArrayNotHasKey('value', $limit);
$this->assertNull($this->executable->getTimeLimit());
}
/**
* Tests saving of queued messages.
*/
public function testSaveQueuedMessages() {
// Assert no queued messages before save.
$this->assertAttributeEquals(array(), 'queuedMessages', $this->executable);
// Set required source_id_values for MigrateIdMapInterface::saveMessage().
$expected_messages[] = array('message' => 'message 1', 'level' => MigrationInterface::MESSAGE_ERROR);
$expected_messages[] = array('message' => 'message 2', 'level' => MigrationInterface::MESSAGE_WARNING);
$expected_messages[] = array('message' => 'message 3', 'level' => MigrationInterface::MESSAGE_INFORMATIONAL);
foreach ($expected_messages as $queued_message) {
$this->executable->queueMessage($queued_message['message'], $queued_message['level']);
}
$this->executable->setSourceIdValues(array());
$this->assertAttributeEquals($expected_messages, 'queuedMessages', $this->executable);
// No asserts of saved messages since coverage exists
// in MigrateSqlIdMapTest::saveMessage().
$this->executable->saveQueuedMessages();
// Assert no queued messages after save.
$this->assertAttributeEquals(array(), 'queuedMessages', $this->executable);
}
/**
* Tests the queuing of messages.
*/
public function testQueueMessage() {
// Assert no queued messages.
$expected_messages = array();
$this->assertAttributeEquals(array(), 'queuedMessages', $this->executable);
// Assert a single (default level) queued message.
$expected_messages[] = array(
'message' => 'message 1',
'level' => MigrationInterface::MESSAGE_ERROR,
);
$this->executable->queueMessage('message 1');
$this->assertAttributeEquals($expected_messages, 'queuedMessages', $this->executable);
// Assert multiple queued messages.
$expected_messages[] = array(
'message' => 'message 2',
'level' => MigrationInterface::MESSAGE_WARNING,
);
$this->executable->queueMessage('message 2', MigrationInterface::MESSAGE_WARNING);
$this->assertAttributeEquals($expected_messages, 'queuedMessages', $this->executable);
$expected_messages[] = array(
'message' => 'message 3',
'level' => MigrationInterface::MESSAGE_INFORMATIONAL,
);
$this->executable->queueMessage('message 3', MigrationInterface::MESSAGE_INFORMATIONAL);
$this->assertAttributeEquals($expected_messages, 'queuedMessages', $this->executable);
}
/**
* Tests maximum execution time (max_execution_time) of an import.
*/
public function testMaxExecTimeExceeded() {
// Assert no max_execution_time value.
$this->executable->setMaxExecTime(0);
$this->assertFalse($this->executable->maxExecTimeExceeded());
// Assert default max_execution_time value does not exceed.
$this->executable->setMaxExecTime(30);
$this->assertFalse($this->executable->maxExecTimeExceeded());
// Assert max_execution_time value is exceeded.
$this->executable->setMaxExecTime(1);
$this->executable->setTimeElapsed(2);
$this->assertTrue($this->executable->maxExecTimeExceeded());
}
/**
* Tests the processRow method.
*/
public function testProcessRow() {
$expected = array(
'test' => 'test destination',
'test1' => 'test1 destination'
);
foreach ($expected as $key => $value) {
$plugins[$key][0] = $this->getMock('Drupal\migrate\Plugin\MigrateProcessInterface');
$plugins[$key][0]->expects($this->once())
->method('getPluginDefinition')
->will($this->returnValue(array()));
$plugins[$key][0]->expects($this->once())
->method('transform')
->will($this->returnValue($value));
}
$this->migration->expects($this->once())
->method('getProcessPlugins')
->with(NULL)
->will($this->returnValue($plugins));
$row = new Row(array(), array());
$this->executable->processRow($row);
foreach ($expected as $key => $value) {
$this->assertSame($row->getDestinationProperty($key), $value);
}
$this->assertSame(count($row->getDestination()), count($expected));
}
/**
* Tests the processRow method with an empty pipeline.
*/
public function testProcessRowEmptyPipeline() {
$this->migration->expects($this->once())
->method('getProcessPlugins')
->with(NULL)
->will($this->returnValue(array('test' => array())));
$row = new Row(array(), array());
$this->executable->processRow($row);
$this->assertSame($row->getDestination(), array());
}
/**
* Returns a mock migration source instance.
*
* @return \Drupal\migrate\Plugin\MigrateSourceInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected function getMockSource() {
$iterator = $this->getMock('\Iterator');
$class = 'Drupal\migrate\Plugin\migrate\source\SourcePluginBase';
$source = $this->getMockBuilder($class)
->disableOriginalConstructor()
->setMethods(get_class_methods($class))
->getMockForAbstractClass();
$source->expects($this->any())
->method('getIterator')
->will($this->returnValue($iterator));
$source->expects($this->once())
->method('rewind')
->will($this->returnValue(TRUE));
$source->expects($this->any())
->method('initializeIterator')
->will($this->returnValue([]));
$source->expects($this->any())
->method('valid')
->will($this->onConsecutiveCalls(TRUE, FALSE));
return $source;
}
}

View file

@ -0,0 +1,245 @@
<?php
/**
* @file
* Contains \Drupal\Tests\migrate\Unit\MigrateSourceTest.
*/
namespace Drupal\Tests\migrate\Unit;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\migrate\MigrateExecutable;
use Drupal\migrate\Plugin\MigrateIdMapInterface;
/**
* @coversDefaultClass \Drupal\migrate\Plugin\migrate\source\SourcePluginBase
* @group migrate
*/
class MigrateSourceTest extends MigrateTestCase {
/**
* Override the migration config.
*
* @var array
*/
protected $defaultMigrationConfiguration = [
'id' => 'test_migration',
'source' => [],
];
/**
* Test row data.
*
* @var array
*/
protected $row = ['test_sourceid1' => '1', 'timestamp' => 500];
/**
* Test source ids.
*
* @var array
*/
protected $sourceIds = ['test_sourceid1' => 'test_sourceid1'];
/**
* The migration entity.
*
* @var \Drupal\migrate\Entity\Migration
*/
protected $migration;
/**
* The migrate executable.
*
* @var \Drupal\migrate\MigrateExecutable
*/
protected $executable;
/**
* Get the source plugin to test.
*
* @param array $configuration
* The source configuration.
* @param array $migrate_config
* The migration configuration to be used in parent::getMigration().
* @param int $status
* The default status for the new rows to be imported.
*
* @return \Drupal\migrate\Plugin\MigrateSourceInterface
* A mocked source plugin.
*/
protected function getSource($configuration = [], $migrate_config = [], $status = MigrateIdMapInterface::STATUS_NEEDS_UPDATE) {
$this->migrationConfiguration = $this->defaultMigrationConfiguration + $migrate_config;
$this->migration = parent::getMigration();
$this->executable = $this->getMigrateExecutable($this->migration);
// Update the idMap for Source so the default is that the row has already
// been imported. This allows us to use the highwater mark to decide on the
// outcome of whether we choose to import the row.
$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', 'getIterator', 'prepareRow', 'initializeIterator', 'calculateDependencies'];
$source_plugin = $this->getMock('\Drupal\migrate\Plugin\migrate\source\SourcePluginBase', $methods, $constructor_args);
$source_plugin
->expects($this->any())
->method('fields')
->willReturn([]);
$source_plugin
->expects($this->any())
->method('getIds')
->willReturn([]);
$source_plugin
->expects($this->any())
->method('__toString')
->willReturn('');
$source_plugin
->expects($this->any())
->method('prepareRow')
->willReturn(empty($migrate_config['prepare_row_false']));
$source_plugin
->expects($this->any())
->method('initializeIterator')
->willReturn([]);
$iterator = new \ArrayIterator([$this->row]);
$source_plugin
->expects($this->any())
->method('getIterator')
->willReturn($iterator);
$module_handler = $this->getMock('\Drupal\Core\Extension\ModuleHandlerInterface');
$source_plugin
->expects($this->any())
->method('getModuleHandler')
->willReturn($module_handler);
$this->migration
->expects($this->any())
->method('getSourcePlugin')
->willReturn($source_plugin);
return $this->migration->getSourcePlugin();
}
/**
* @expectedException \Drupal\migrate\MigrateException
*/
public function testHighwaterTrackChangesIncompatible() {
$source_config = ['track_changes' => TRUE];
$migration_config = ['highWaterProperty' => ['name' => 'something']];
$this->getSource($source_config, $migration_config);
}
/**
* Test that the source count is correct.
*/
public function testCount() {
$container = new ContainerBuilder();
$container->register('cache.migrate', 'Drupal\Core\Cache\NullBackend')
->setArguments(['migrate']);
\Drupal::setContainer($container);
// Test that the basic count works.
$source = $this->getSource();
$this->assertEquals(1, $source->count());
// Test caching the count works.
$source = $this->getSource(['cache_counts' => TRUE]);
$this->assertEquals(1, $source->count());
// Test the skip argument.
$source = $this->getSource(['skip_count' => TRUE]);
$this->assertEquals(-1, $source->count());
}
/**
* Test that we don't get a row if prepareRow() is false.
*/
public function testPrepareRowFalse() {
$source = $this->getSource([], ['prepare_row_false' => TRUE]);
$source->rewind();
$this->assertNull($source->current(), 'No row is available when prepareRow() is false.');
}
/**
* Test that the when a source id is in the idList, we don't get a row.
*/
public function testIdInList() {
$source = $this->getSource([], ['idlist' => ['test_sourceid1']]);
$source->rewind();
$this->assertNull($source->current(), 'No row is available because id was in idList.');
}
/**
* Test that $row->needsUpdate() works as expected.
*/
public function testNextNeedsUpdate() {
$source = $this->getSource();
// $row->needsUpdate() === TRUE so we get a row.
$source->rewind();
$this->assertTrue(is_a($source->current(), 'Drupal\migrate\Row'), '$row->needsUpdate() is TRUE so we got a row.');
// Test that we don't get a row when the incoming row is marked as imported.
$source = $this->getSource([], [], MigrateIdMapInterface::STATUS_IMPORTED);
$source->rewind();
$this->assertNull($source->current(), 'Row was already imported, should be NULL');
}
/**
* Test that an outdated highwater mark does not cause a row to be imported.
*/
public function testOutdatedHighwater() {
$source = $this->getSource([], [], MigrateIdMapInterface::STATUS_IMPORTED);
// Set the originalHighwater to something higher than our timestamp.
$this->migration
->expects($this->any())
->method('getHighwater')
->willReturn($this->row['timestamp'] + 1);
// The current highwater mark is now higher than the row timestamp so no row
// is expected.
$source->rewind();
$this->assertNull($source->current(), 'Original highwater mark is higher than incoming row timestamp.');
}
/**
* Test that a highwater mark newer than our saved one imports a row.
*
* @throws \Exception
*/
public function testNewHighwater() {
// Set a highwater property field for source. Now we should have a row
// because the row timestamp is greater than the current highwater mark.
$source = $this->getSource([], ['highWaterProperty' => ['name' => 'timestamp']], MigrateIdMapInterface::STATUS_IMPORTED);
$source->rewind();
$this->assertTrue(is_a($source->current(), 'Drupal\migrate\Row'), 'Incoming row timestamp is greater than current highwater mark so we have a row.');
}
/**
* Get a mock executable for the test.
*
* @param \Drupal\migrate\Entity\MigrationInterface $migration
* The migration entity.
*
* @return \Drupal\migrate\MigrateExecutable
* The migrate executable.
*/
protected function getMigrateExecutable($migration) {
$message = $this->getMock('Drupal\migrate\MigrateMessageInterface');
return new MigrateExecutable($migration, $message);
}
}

View file

@ -0,0 +1,212 @@
<?php
/**
* @file
* Contains \Drupal\Tests\migrate\Unit\MigrateSqlIdMapEnsureTablesTest.
*/
namespace Drupal\Tests\migrate\Unit;
use Drupal\migrate\Plugin\MigrateIdMapInterface;
/**
* Tests the SQL ID map plugin ensureTables() method.
*
* @group migrate
*/
class MigrateSqlIdMapEnsureTablesTest extends MigrateTestCase {
/**
* The migration configuration, initialized to set the ID and destination IDs.
*
* @var array
*/
protected $migrationConfiguration = array(
'id' => 'sql_idmap_test',
);
/**
* Tests the ensureTables method when the tables do not exist.
*/
public function testEnsureTablesNotExist() {
$fields['source_row_status'] = array(
'type' => 'int',
'size' => 'tiny',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => MigrateIdMapInterface::STATUS_IMPORTED,
'description' => 'Indicates current status of the source row',
);
$fields['rollback_action'] = array(
'type' => 'int',
'size' => 'tiny',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => MigrateIdMapInterface::ROLLBACK_DELETE,
'description' => 'Flag indicating what to do for this item on rollback',
);
$fields['last_imported'] = array(
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
'description' => 'UNIX timestamp of the last time this row was imported',
);
$fields['hash'] = array(
'type' => 'varchar',
'length' => '64',
'not null' => FALSE,
'description' => 'Hash of source row data, for detecting changes',
);
$fields['sourceid1'] = array(
'type' => 'int',
'not null' => TRUE,
);
$fields['destid1'] = array(
'type' => 'varchar',
'length' => 255,
'not null' => FALSE,
);
$map_table_schema = array(
'description' => 'Mappings from source identifier value(s) to destination identifier value(s).',
'fields' => $fields,
'primary key' => array('sourceid1'),
);
$schema = $this->getMockBuilder('Drupal\Core\Database\Schema')
->disableOriginalConstructor()
->getMock();
$schema->expects($this->at(0))
->method('tableExists')
->with('migrate_map_sql_idmap_test')
->will($this->returnValue(FALSE));
$schema->expects($this->at(1))
->method('createTable')
->with('migrate_map_sql_idmap_test', $map_table_schema);
// Now do the message table.
$fields = array();
$fields['msgid'] = array(
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
);
$fields['sourceid1'] = array(
'type' => 'int',
'not null' => TRUE,
);
$fields['level'] = array(
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 1,
);
$fields['message'] = array(
'type' => 'text',
'size' => 'medium',
'not null' => TRUE,
);
$table_schema = array(
'description' => 'Messages generated during a migration process',
'fields' => $fields,
'primary key' => array('msgid'),
);
$table_schema['indexes']['sourcekey'] = array('sourceid1');
$schema->expects($this->at(2))
->method('tableExists')
->with('migrate_message_sql_idmap_test')
->will($this->returnValue(FALSE));
$schema->expects($this->at(3))
->method('createTable')
->with('migrate_message_sql_idmap_test', $table_schema);
$schema->expects($this->any())
->method($this->anything());
$this->runEnsureTablesTest($schema);
}
/**
* Tests the ensureTables method when the tables exist.
*/
public function testEnsureTablesExist() {
$schema = $this->getMockBuilder('Drupal\Core\Database\Schema')
->disableOriginalConstructor()
->getMock();
$schema->expects($this->at(0))
->method('tableExists')
->with('migrate_map_sql_idmap_test')
->will($this->returnValue(TRUE));
$schema->expects($this->at(1))
->method('fieldExists')
->with('migrate_map_sql_idmap_test', 'rollback_action')
->will($this->returnValue(FALSE));
$field_schema = array(
'type' => 'int',
'size' => 'tiny',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
'description' => 'Flag indicating what to do for this item on rollback',
);
$schema->expects($this->at(2))
->method('addField')
->with('migrate_map_sql_idmap_test', 'rollback_action', $field_schema);
$schema->expects($this->at(3))
->method('fieldExists')
->with('migrate_map_sql_idmap_test', 'hash')
->will($this->returnValue(FALSE));
$field_schema = array(
'type' => 'varchar',
'length' => '64',
'not null' => FALSE,
'description' => 'Hash of source row data, for detecting changes',
);
$schema->expects($this->at(4))
->method('addField')
->with('migrate_map_sql_idmap_test', 'hash', $field_schema);
$schema->expects($this->exactly(5))
->method($this->anything());
$this->runEnsureTablesTest($schema);
}
/**
* Actually run the test.
*
* @param array $schema
* The mock schema object with expectations set. The Sql constructor calls
* ensureTables() which in turn calls this object and the expectations on
* it are the actual test and there are no additional asserts added.
*/
protected function runEnsureTablesTest($schema) {
$database = $this->getMockBuilder('Drupal\Core\Database\Connection')
->disableOriginalConstructor()
->getMock();
$database->expects($this->any())
->method('schema')
->will($this->returnValue($schema));
$migration = $this->getMigration();
$plugin = $this->getMock('Drupal\migrate\Plugin\MigrateSourceInterface');
$plugin->expects($this->any())
->method('getIds')
->will($this->returnValue(array(
'source_id_property' => array(
'type' => 'integer',
),
)));
$migration->expects($this->any())
->method('getSourcePlugin')
->will($this->returnValue($plugin));
$plugin = $this->getMock('Drupal\migrate\Plugin\MigrateSourceInterface');
$plugin->expects($this->any())
->method('getIds')
->will($this->returnValue(array(
'destination_id_property' => array(
'type' => 'string',
),
)));
$migration->expects($this->any())
->method('getDestinationPlugin')
->will($this->returnValue($plugin));
$map = new TestSqlIdMap($database, array(), 'sql', array(), $migration);
$map->getDatabase();
}
}

View file

@ -0,0 +1,798 @@
<?php
/**
* @file
* Contains \Drupal\Tests\migrate\Unit\MigrateSqlIdMapTest.
*/
namespace Drupal\Tests\migrate\Unit;
use Drupal\Core\Database\Driver\sqlite\Connection;
use Drupal\migrate\Entity\MigrationInterface;
use Drupal\migrate\MigrateException;
use Drupal\migrate\Plugin\MigrateIdMapInterface;
use Drupal\migrate\Row;
/**
* Tests the SQL ID map plugin.
*
* @group migrate
*/
class MigrateSqlIdMapTest extends MigrateTestCase {
/**
* The migration configuration, initialized to set the ID and destination IDs.
*
* @var array
*/
protected $migrationConfiguration = [
'id' => 'sql_idmap_test',
];
protected $sourceIds = [
'source_id_property' => [
'type' => 'string',
],
];
protected $destinationIds = [
'destination_id_property' => [
'type' => 'string',
],
];
/**
* The database connection.
*
* @var \Drupal\Core\Database\Connection
*/
protected $database;
public function setUp() {
$this->database = $this->getDatabase([]);
}
/**
* Saves a single ID mapping row in the database.
*
* @param array $map
* The row to save.
*/
protected function saveMap(array $map) {
$table = 'migrate_map_sql_idmap_test';
$schema = $this->database->schema();
// If the table already exists, add any columns which are in the map array,
// but don't yet exist in the table. Yay, flexibility!
if ($schema->tableExists($table)) {
foreach (array_keys($map) as $field) {
if (!$schema->fieldExists($table, $field)) {
$schema->addField($table, $field, ['type' => 'text']);
}
}
}
else {
$schema->createTable($table, $this->createSchemaFromRow($map));
}
$this->database->insert($table)->fields($map)->execute();
}
/**
* Creates a test SQL ID map plugin.
*
* @return \Drupal\Tests\migrate\Unit\TestSqlIdMap
* A SQL ID map plugin test instance.
*/
protected function getIdMap() {
$migration = $this->getMigration();
$plugin = $this->getMock('Drupal\migrate\Plugin\MigrateSourceInterface');
$plugin
->method('getIds')
->willReturn($this->sourceIds);
$migration
->method('getSourcePlugin')
->willReturn($plugin);
$plugin = $this->getMock('Drupal\migrate\Plugin\MigrateDestinationInterface');
$plugin
->method('getIds')
->willReturn($this->destinationIds);
$migration
->method('getDestinationPlugin')
->willReturn($plugin);
$id_map = new TestSqlIdMap($this->database, [], 'sql', [], $migration);
$migration
->method('getIdMap')
->willReturn($id_map);
return $id_map;
}
/**
* Sets defaults for SQL ID map plugin tests.
*/
protected function idMapDefaults() {
$defaults = array(
'source_row_status' => MigrateIdMapInterface::STATUS_IMPORTED,
'rollback_action' => MigrateIdMapInterface::ROLLBACK_DELETE,
'hash' => '',
);
// By default, the PDO SQLite driver strongly prefers to return strings
// from SELECT queries. Even for columns that don't store strings. Even
// if the connection's STRINGIFY_FETCHES attribute is FALSE. This can cause
// assertSame() calls to fail, since 0 !== '0'. Casting these values to
// strings isn't the most elegant workaround, but it allows the assertions
// to pass properly.
if ($this->database->driver() == 'sqlite') {
$defaults['source_row_status'] = (string) $defaults['source_row_status'];
$defaults['rollback_action'] = (string) $defaults['rollback_action'];
}
return $defaults;
}
/**
* Tests the ID mapping method.
*
* Create two ID mappings and update the second to verify that:
* - saving new to empty tables work.
* - saving new to nonempty tables work.
* - updating work.
*/
public function testSaveIdMapping() {
$source = array(
'source_id_property' => 'source_value',
);
$row = new Row($source, ['source_id_property' => []]);
$id_map = $this->getIdMap();
$id_map->saveIdMapping($row, ['destination_id_property' => 2]);
$expected_result = [
[
'sourceid1' => 'source_value',
'destid1' => 2,
] + $this->idMapDefaults(),
];
$this->queryResultTest($this->getIdMapContents(), $expected_result);
$source = [
'source_id_property' => 'source_value_1',
];
$row = new Row($source, ['source_id_property' => []]);
$id_map->saveIdMapping($row, ['destination_id_property' => 3]);
$expected_result[] = [
'sourceid1' => 'source_value_1',
'destid1' => 3,
] + $this->idMapDefaults();
$this->queryResultTest($this->getIdMapContents(), $expected_result);
$id_map->saveIdMapping($row, ['destination_id_property' => 4]);
$expected_result[1]['destid1'] = 4;
$this->queryResultTest($this->getIdMapContents(), $expected_result);
}
/**
* Tests the SQL ID map set message method.
*/
public function testSetMessage() {
$message = $this->getMock('Drupal\migrate\MigrateMessageInterface');
$id_map = $this->getIdMap();
$id_map->setMessage($message);
$this->assertAttributeEquals($message, 'message', $id_map);
}
/**
* Tests the clear messages method.
*/
public function testClearMessages() {
$message = 'Hello world.';
$expected_results = [0, 1, 2, 3];
$id_map = $this->getIdMap();
// Insert 4 message for later delete.
foreach ($expected_results as $key => $expected_result) {
$id_map->saveMessage([$key], $message);
}
// Truncate and check that 4 messages were deleted.
$this->assertEquals($id_map->messageCount(), 4);
$id_map->clearMessages();
$count = $id_map->messageCount();
$this->assertEquals($count, 0);
}
/**
* Tests the getRowsNeedingUpdate method for rows that need an update.
*/
public function testGetRowsNeedingUpdate() {
$id_map = $this->getIdMap();
$row_statuses = [
MigrateIdMapInterface::STATUS_IMPORTED,
MigrateIdMapInterface::STATUS_NEEDS_UPDATE,
MigrateIdMapInterface::STATUS_IGNORED,
MigrateIdMapInterface::STATUS_FAILED,
];
// Create a mapping row for each STATUS constant.
foreach ($row_statuses as $status) {
$source = ['source_id_property' => 'source_value_' . $status];
$row = new Row($source, ['source_id_property' => []]);
$destination = ['destination_id_property' => 'destination_value_' . $status];
$id_map->saveIdMapping($row, $destination, $status);
$expected_results[] = [
'sourceid1' => 'source_value_' . $status,
'destid1' => 'destination_value_' . $status,
'source_row_status' => $status,
'rollback_action' => MigrateIdMapInterface::ROLLBACK_DELETE,
'hash' => '',
];
// Assert zero rows need an update.
if ($status == MigrateIdMapInterface::STATUS_IMPORTED) {
$rows_needing_update = $id_map->getRowsNeedingUpdate(1);
$this->assertCount(0, $rows_needing_update);
}
}
// Assert that test values exist.
$this->queryResultTest($this->getIdMapContents(), $expected_results);
// Assert a single row needs an update.
$row_needing_update = $id_map->getRowsNeedingUpdate(1);
$this->assertCount(1, $row_needing_update);
// Assert the row matches its original source.
$source_id = $expected_results[MigrateIdMapInterface::STATUS_NEEDS_UPDATE]['sourceid1'];
$test_row = $id_map->getRowBySource([$source_id]);
// $row_needing_update is an array of objects returned from the database,
// but $test_row is an array, so the cast is necessary.
$this->assertSame($test_row, (array) $row_needing_update[0]);
// Add additional row that needs an update.
$source = ['source_id_property' => 'source_value_multiple'];
$row = new Row($source, ['source_id_property' => []]);
$destination = ['destination_id_property' => 'destination_value_multiple'];
$id_map->saveIdMapping($row, $destination, MigrateIdMapInterface::STATUS_NEEDS_UPDATE);
// Assert multiple rows need an update.
$rows_needing_update = $id_map->getRowsNeedingUpdate(2);
$this->assertCount(2, $rows_needing_update);
}
/**
* Tests the SQL ID map message count method by counting and saving messages.
*/
public function testMessageCount() {
$message = 'Hello world.';
$expected_results = [0, 1, 2, 3];
$id_map = $this->getIdMap();
// Test count message multiple times starting from 0.
foreach ($expected_results as $key => $expected_result) {
$count = $id_map->messageCount();
$this->assertEquals($expected_result, $count);
$id_map->saveMessage([$key], $message);
}
}
/**
* Tests the SQL ID map save message method.
*/
public function testMessageSave() {
$message = 'Hello world.';
$expected_results = [
1 => ['message' => $message, 'level' => MigrationInterface::MESSAGE_ERROR],
2 => ['message' => $message, 'level' => MigrationInterface::MESSAGE_WARNING],
3 => ['message' => $message, 'level' => MigrationInterface::MESSAGE_NOTICE],
4 => ['message' => $message, 'level' => MigrationInterface::MESSAGE_INFORMATIONAL],
];
$id_map = $this->getIdMap();
foreach ($expected_results as $key => $expected_result) {
$id_map->saveMessage([$key], $message, $expected_result['level']);
$message_row = $this->database->select($id_map->messageTableName(), 'message')
->fields('message')
->condition('level', $expected_result['level'])
->condition('message', $expected_result['message'])
->execute()
->fetchAssoc();
$this->assertEquals($expected_result['message'], $message_row['message'], 'Message from database was read.');
}
// Insert with default level.
$message_default = 'Hello world default.';
$id_map->saveMessage([5], $message_default);
$message_row = $this->database->select($id_map->messageTableName(), 'message')
->fields('message')
->condition('level', MigrationInterface::MESSAGE_ERROR)
->condition('message', $message_default)
->execute()
->fetchAssoc();
$this->assertEquals($message_default, $message_row['message'], 'Message from database was read.');
}
/**
* Tests the getRowBySource method.
*/
public function testGetRowBySource() {
$this->getDatabase([]);
$row = [
'sourceid1' => 'source_id_value_1',
'sourceid2' => 'source_id_value_2',
'destid1' => 'destination_id_value_1',
] + $this->idMapDefaults();
$this->saveMap($row);
$row = [
'sourceid1' => 'source_id_value_3',
'sourceid2' => 'source_id_value_4',
'destid1' => 'destination_id_value_2',
] + $this->idMapDefaults();
$this->saveMap($row);
$source_id_values = [$row['sourceid1'], $row['sourceid2']];
$id_map = $this->getIdMap();
$result_row = $id_map->getRowBySource($source_id_values);
$this->assertSame($row, $result_row);
$source_id_values = ['missing_value_1', 'missing_value_2'];
$result_row = $id_map->getRowBySource($source_id_values);
$this->assertFalse($result_row);
}
/**
* Data provider for testLookupDestinationIdMapping().
*
* Scenarios to test (for both hits and misses) are:
* - Single-value source ID to single-value destination ID.
* - Multi-value source ID to multi-value destination ID.
* - Single-value source ID to multi-value destination ID.
* - Multi-value source ID to single-value destination ID.
*/
public function lookupDestinationIdMappingDataProvider() {
return [
[1, 1],
[2, 2],
[1, 2],
[2, 1],
];
}
/**
* Performs destination ID test on source and destination fields.
*
* @param int $num_source_fields
* Number of source fields to test.
* @param int $num_destination_fields
* Number of destination fields to test.
* @dataProvider lookupDestinationIdMappingDataProvider
*/
public function testLookupDestinationIdMapping($num_source_fields, $num_destination_fields) {
// Adjust the migration configuration according to the number of source and
// destination fields.
$this->sourceIds = [];
$this->destinationIds = [];
$source_id_values = [];
$nonexistent_id_values = [];
$row = $this->idMapDefaults();
for ($i = 1; $i <= $num_source_fields; $i++) {
$row["sourceid$i"] = "source_id_value_$i";
$source_id_values[] = "source_id_value_$i";
$nonexistent_id_values[] = "nonexistent_source_id_value_$i";
$this->sourceIds["source_id_property_$i"] = [];
}
$expected_result = [];
for ($i = 1; $i <= $num_destination_fields; $i++) {
$row["destid$i"] = "destination_id_value_$i";
$expected_result[] = "destination_id_value_$i";
$this->destinationIds["destination_id_property_$i"] = [];
}
$this->saveMap($row);
$id_map = $this->getIdMap();
// Test for a valid hit.
$destination_id = $id_map->lookupDestinationId($source_id_values);
$this->assertSame($expected_result, $destination_id);
// Test for a miss.
$destination_id = $id_map->lookupDestinationId($nonexistent_id_values);
$this->assertSame(0, count($destination_id));
}
/**
* Tests the getRowByDestination method.
*/
public function testGetRowByDestination() {
$row = [
'sourceid1' => 'source_id_value_1',
'sourceid2' => 'source_id_value_2',
'destid1' => 'destination_id_value_1',
] + $this->idMapDefaults();
$this->saveMap($row);
$row = [
'sourceid1' => 'source_id_value_3',
'sourceid2' => 'source_id_value_4',
'destid1' => 'destination_id_value_2',
] + $this->idMapDefaults();
$this->saveMap($row);
$dest_id_values = [$row['destid1']];
$id_map = $this->getIdMap();
$result_row = $id_map->getRowByDestination($dest_id_values);
$this->assertSame($row, $result_row);
// This value does not exist.
$dest_id_values = ['invalid_destination_id_property'];
$id_map = $this->getIdMap();
$result_row = $id_map->getRowByDestination($dest_id_values);
$this->assertFalse($result_row);
}
/**
* Data provider for testLookupSourceIDMapping().
*
* Scenarios to test (for both hits and misses) are:
* - Single-value destination ID to single-value source ID.
* - Multi-value destination ID to multi-value source ID.
* - Single-value destination ID to multi-value source ID.
* - Multi-value destination ID to single-value source ID.
*/
public function lookupSourceIDMappingDataProvider() {
return [
[1, 1],
[2, 2],
[1, 2],
[2, 1],
];
}
/**
* Performs the source ID test on source and destination fields.
*
* @param int $num_source_fields
* Number of source fields to test.
* @param int $num_destination_fields
* Number of destination fields to test.
* @dataProvider lookupSourceIDMappingDataProvider
*/
public function testLookupSourceIDMapping($num_source_fields, $num_destination_fields) {
// Adjust the migration configuration according to the number of source and
// destination fields.
$this->sourceIds = [];
$this->destinationIds = [];
$row = $this->idMapDefaults();
$expected_result = [];
for ($i = 1; $i <= $num_source_fields; $i++) {
$row["sourceid$i"] = "source_id_value_$i";
$expected_result[] = "source_id_value_$i";
$this->sourceIds["source_id_property_$i"] = [];
}
$destination_id_values = [];
$nonexistent_id_values = [];
for ($i = 1; $i <= $num_destination_fields; $i++) {
$row["destid$i"] = "destination_id_value_$i";
$destination_id_values[] = "destination_id_value_$i";
$nonexistent_id_values[] = "nonexistent_destination_id_value_$i";
$this->destinationIds["destination_id_property_$i"] = [];
}
$this->saveMap($row);
$id_map = $this->getIdMap();
// Test for a valid hit.
$source_id = $id_map->lookupSourceID($destination_id_values);
$this->assertSame($expected_result, $source_id);
// Test for a miss.
$source_id = $id_map->lookupSourceID($nonexistent_id_values);
$this->assertSame(0, count($source_id));
}
/**
* Tests the imported count method.
*
* Scenarios to test for:
* - No imports.
* - One import.
* - Multiple imports.
*/
public function testImportedCount() {
$id_map = $this->getIdMap();
// Add a single failed row and assert zero imported rows.
$source = ['source_id_property' => 'source_value_failed'];
$row = new Row($source, ['source_id_property' => []]);
$destination = ['destination_id_property' => 'destination_value_failed'];
$id_map->saveIdMapping($row, $destination, MigrateIdMapInterface::STATUS_FAILED);
$this->assertSame(0, (int) $id_map->importedCount());
// Add an imported row and assert single count.
$source = ['source_id_property' => 'source_value_imported'];
$row = new Row($source, ['source_id_property' => []]);
$destination = ['destination_id_property' => 'destination_value_imported'];
$id_map->saveIdMapping($row, $destination, MigrateIdMapInterface::STATUS_IMPORTED);
$this->assertSame(1, (int) $id_map->importedCount());
// Add a row needing update and assert multiple imported rows.
$source = ['source_id_property' => 'source_value_update'];
$row = new Row($source, ['source_id_property' => []]);
$destination = ['destination_id_property' => 'destination_value_update'];
$id_map->saveIdMapping($row, $destination, MigrateIdMapInterface::STATUS_NEEDS_UPDATE);
$this->assertSame(2, (int) $id_map->importedCount());
}
/**
* Tests the number of processed source rows.
*
* Scenarios to test for:
* - No processed rows.
* - One processed row.
* - Multiple processed rows.
*/
public function testProcessedCount() {
$id_map = $this->getIdMap();
// Assert zero rows have been processed before adding rows.
$this->assertSame(0, (int) $id_map->processedCount());
$row_statuses = [
MigrateIdMapInterface::STATUS_IMPORTED,
MigrateIdMapInterface::STATUS_NEEDS_UPDATE,
MigrateIdMapInterface::STATUS_IGNORED,
MigrateIdMapInterface::STATUS_FAILED,
];
// Create a mapping row for each STATUS constant.
foreach ($row_statuses as $status) {
$source = ['source_id_property' => 'source_value_' . $status];
$row = new Row($source, ['source_id_property' => []]);
$destination = ['destination_id_property' => 'destination_value_' . $status];
$id_map->saveIdMapping($row, $destination, $status);
if ($status == MigrateIdMapInterface::STATUS_IMPORTED) {
// Assert a single row has been processed.
$this->assertSame(1, (int) $id_map->processedCount());
}
}
// Assert multiple rows have been processed.
$this->assertSame(count($row_statuses), (int) $id_map->processedCount());
}
/**
* Data provider for testUpdateCount().
*
* Scenarios to test for:
* - No updates.
* - One update.
* - Multiple updates.
*/
public function updateCountDataProvider() {
return [
[0],
[1],
[3],
];
}
/**
* Performs the update count test with a given number of update rows.
*
* @param int $num_update_rows
* The number of update rows to test.
* @dataProvider updateCountDataProvider
*/
public function testUpdateCount($num_update_rows) {
for ($i = 0; $i < 5; $i++) {
$row = $this->idMapDefaults();
$row['sourceid1'] = "source_id_value_$i";
$row['destid1'] = "destination_id_value_$i";
$row['source_row_status'] = MigrateIdMapInterface::STATUS_IMPORTED;
$this->saveMap($row);
}
for (; $i < 5 + $num_update_rows; $i++) {
$row = $this->idMapDefaults();
$row['sourceid1'] = "source_id_value_$i";
$row['destid1'] = "destination_id_value_$i";
$row['source_row_status'] = MigrateIdMapInterface::STATUS_NEEDS_UPDATE;
$this->saveMap($row);
}
$id_map = $this->getIdMap();
$this->assertSame($num_update_rows, (int) $id_map->updateCount());
}
/**
* Data provider for testErrorCount().
*
* Scenarios to test for:
* - No errors.
* - One error.
* - Multiple errors.
*/
public function errorCountDataProvider() {
return [
[0],
[1],
[3],
];
}
/**
* Performs error count test with a given number of error rows.
*
* @param int $num_error_rows
* Number of error rows to test.
* @dataProvider errorCountDataProvider
*/
public function testErrorCount($num_error_rows) {
for ($i = 0; $i < 5; $i++) {
$row = $this->idMapDefaults();
$row['sourceid1'] = "source_id_value_$i";
$row['destid1'] = "destination_id_value_$i";
$row['source_row_status'] = MigrateIdMapInterface::STATUS_IMPORTED;
$this->saveMap($row);
}
for (; $i < 5 + $num_error_rows; $i++) {
$row = $this->idMapDefaults();
$row['sourceid1'] = "source_id_value_$i";
$row['destid1'] = "destination_id_value_$i";
$row['source_row_status'] = MigrateIdMapInterface::STATUS_FAILED;
$this->saveMap($row);
}
$this->assertSame($num_error_rows, (int) $this->getIdMap()->errorCount());
}
/**
* Tests setting a row source_row_status to STATUS_NEEDS_UPDATE.
*/
public function testSetUpdate() {
$id_map = $this->getIdMap();
$row_statuses = [
MigrateIdMapInterface::STATUS_IMPORTED,
MigrateIdMapInterface::STATUS_NEEDS_UPDATE,
MigrateIdMapInterface::STATUS_IGNORED,
MigrateIdMapInterface::STATUS_FAILED,
];
// Create a mapping row for each STATUS constant.
foreach ($row_statuses as $status) {
$source = ['source_id_property' => 'source_value_' . $status];
$row = new Row($source, ['source_id_property' => []]);
$destination = ['destination_id_property' => 'destination_value_' . $status];
$id_map->saveIdMapping($row, $destination, $status);
$expected_results[] = [
'sourceid1' => 'source_value_' . $status,
'destid1' => 'destination_value_' . $status,
'source_row_status' => $status,
'rollback_action' => MigrateIdMapInterface::ROLLBACK_DELETE,
'hash' => '',
];
}
// Assert that test values exist.
$this->queryResultTest($this->getIdMapContents(), $expected_results);
// Mark each row as STATUS_NEEDS_UPDATE.
foreach ($row_statuses as $status) {
$id_map->setUpdate(['source_value_' . $status]);
}
// Update expected results.
foreach ($expected_results as $key => $value) {
$expected_results[$key]['source_row_status'] = MigrateIdMapInterface::STATUS_NEEDS_UPDATE;
}
// Assert that updated expected values match.
$this->queryResultTest($this->getIdMapContents(), $expected_results);
// Assert an exception is thrown when source identifiers are not provided.
try {
$id_map->setUpdate([]);
$this->assertFalse(FALSE, 'MigrateException not thrown, when source identifiers were provided to update.');
}
catch (MigrateException $e) {
$this->assertTrue(TRUE, "MigrateException thrown, when source identifiers were not provided to update.");
}
}
/**
* Tests prepareUpdate().
*/
public function testPrepareUpdate() {
$id_map = $this->getIdMap();
$row_statuses = [
MigrateIdMapInterface::STATUS_IMPORTED,
MigrateIdMapInterface::STATUS_NEEDS_UPDATE,
MigrateIdMapInterface::STATUS_IGNORED,
MigrateIdMapInterface::STATUS_FAILED,
];
// Create a mapping row for each STATUS constant.
foreach ($row_statuses as $status) {
$source = ['source_id_property' => 'source_value_' . $status];
$row = new Row($source, ['source_id_property' => []]);
$destination = ['destination_id_property' => 'destination_value_' . $status];
$id_map->saveIdMapping($row, $destination, $status);
$expected_results[] = [
'sourceid1' => 'source_value_' . $status,
'destid1' => 'destination_value_' . $status,
'source_row_status' => $status,
'rollback_action' => MigrateIdMapInterface::ROLLBACK_DELETE,
'hash' => '',
];
}
// Assert that test values exist.
$this->queryResultTest($this->getIdMapContents(), $expected_results);
// Mark all rows as STATUS_NEEDS_UPDATE.
$id_map->prepareUpdate();
// Update expected results.
foreach ($expected_results as $key => $value) {
$expected_results[$key]['source_row_status'] = MigrateIdMapInterface::STATUS_NEEDS_UPDATE;
}
// Assert that updated expected values match.
$this->queryResultTest($this->getIdMapContents(), $expected_results);
}
/**
* Tests the destroy method.
*
* Scenarios to test for:
* - No errors.
* - One error.
* - Multiple errors.
*/
public function testDestroy() {
$id_map = $this->getIdMap();
// Initialize the id map.
$id_map->getDatabase();
$map_table_name = $id_map->mapTableName();
$message_table_name = $id_map->messageTableName();
$row = new Row(['source_id_property' => 'source_value'], ['source_id_property' => []]);
$id_map->saveIdMapping($row, ['destination_id_property' => 2]);
$id_map->saveMessage(['source_value'], 'A message');
$this->assertTrue($this->database->schema()->tableExists($map_table_name),
"$map_table_name exists");
$this->assertTrue($this->database->schema()->tableExists($message_table_name),
"$message_table_name exists");
$id_map->destroy();
$this->assertFalse($this->database->schema()->tableExists($map_table_name),
"$map_table_name does not exist");
$this->assertFalse($this->database->schema()->tableExists($message_table_name),
"$message_table_name does not exist");
}
/**
* Tests the getQualifiedMapTable method with a prefixed database.
*/
public function testGetQualifiedMapTablePrefix() {
$connection_options = [
'database' => ':memory:',
'prefix' => 'prefix',
];
$pdo = Connection::open($connection_options);
$this->database = new Connection($pdo, $connection_options);
$qualified_map_table = $this->getIdMap()->getQualifiedMapTableName();
// The SQLite driver is a special flower. It will prefix tables with
// PREFIX.TABLE, instead of the standard PREFIXTABLE.
// @see \Drupal\Core\Database\Driver\sqlite\Connection::__construct()
$this->assertEquals('prefix.migrate_map_sql_idmap_test', $qualified_map_table);
}
/**
* Tests all the iterator methods in one swing.
*
* The iterator methods are:
* - Sql::rewind()
* - Sql::next()
* - Sql::valid()
* - Sql::key()
* - Sql::current()
*/
public function testIterators() {
for ($i = 0; $i < 3; $i++) {
$row = $this->idMapDefaults();
$row['sourceid1'] = "source_id_value_$i";
$row['destid1'] = "destination_id_value_$i";
$row['source_row_status'] = MigrateIdMapInterface::STATUS_IMPORTED;
$expected_results[serialize(['sourceid1' => $row['sourceid1']])] = ['destid1' => $row['destid1']];
$this->saveMap($row);
}
$this->assertSame(iterator_to_array($this->getIdMap()), $expected_results);
}
private function getIdMapContents() {
$result = $this->database
->select('migrate_map_sql_idmap_test', 't')
->fields('t')
->execute();
// The return value needs to be countable, or it will fail certain
// assertions. iterator_to_array() will not suffice because it won't
// respect the PDO fetch mode, if specified.
$contents = [];
foreach ($result as $row) {
$contents[] = (array) $row;
}
return $contents;
}
}

View file

@ -0,0 +1,118 @@
<?php
/**
* @file
* Contains \Drupal\Tests\migrate\Unit\MigrateSqlSourceTestCase.
*/
namespace Drupal\Tests\migrate\Unit;
/**
* Base class for Migrate module source unit tests.
*/
abstract class MigrateSqlSourceTestCase extends MigrateTestCase {
/**
* The tested source plugin.
*
* @var \Drupal\migrate_drupal\Plugin\migrate\source\DrupalSqlBase.
*/
protected $source;
/**
* The database contents.
*
* Database contents represents a mocked database. It should contain an
* associative array with the table name as key, and as many nested arrays as
* the number of mocked rows. Each of those faked rows must be another array
* with the column name as the key and the value as the cell.
*
* @var array
*/
protected $databaseContents = array();
/**
* The plugin class under test.
*
* The plugin system is not working during unit testing so the source plugin
* class needs to be manually specified.
*
* @var string
*/
const PLUGIN_CLASS = '';
/**
* The high water mark at the beginning of the import operation.
*
* Once the migration is run, we save a mark of the migrated sources, so the
* migration can run again and update only new sources or changed sources.
*
* @var string
*/
const ORIGINAL_HIGH_WATER = '';
/**
* Expected results after the source parsing.
*
* @var array
*/
protected $expectedResults = array();
/**
* The source plugin instance under test.
*
* @var \Drupal\migrate\Plugin\MigrateSourceInterface
*/
protected $plugin;
/**
* {@inheritdoc}
*/
protected function setUp() {
$module_handler = $this->getMock('Drupal\Core\Extension\ModuleHandlerInterface');
$entity_manager = $this->getMock('Drupal\Core\Entity\EntityManagerInterface');
$migration = $this->getMigration();
$migration->expects($this->any())
->method('getHighWater')
->will($this->returnValue(static::ORIGINAL_HIGH_WATER));
// Setup the plugin.
$plugin_class = static::PLUGIN_CLASS;
$plugin = new $plugin_class($this->migrationConfiguration['source'], $this->migrationConfiguration['source']['plugin'], array(), $migration, $entity_manager);
// Do some reflection to set the database and moduleHandler.
$plugin_reflection = new \ReflectionClass($plugin);
$database_property = $plugin_reflection->getProperty('database');
$database_property->setAccessible(TRUE);
$module_handler_property = $plugin_reflection->getProperty('moduleHandler');
$module_handler_property->setAccessible(TRUE);
// Set the database and the module handler onto our plugin.
$database_property->setValue($plugin, $this->getDatabase($this->databaseContents + array('test_map' => array())));
$module_handler_property->setValue($plugin, $module_handler);
$plugin->setStringTranslation($this->getStringTranslationStub());
$migration->expects($this->any())
->method('getSourcePlugin')
->will($this->returnValue($plugin));
$this->source = $plugin;
}
/**
* Test the source returns the same rows as expected.
*/
public function testRetrieval() {
$this->queryResultTest($this->source, $this->expectedResults);
}
/**
* @param \Drupal\migrate\Row $row
* @param string $key
* @return mixed
*/
protected function getValue($row, $key) {
return $row->getSourceProperty($key);
}
}

View file

@ -0,0 +1,162 @@
<?php
/**
* @file
* Contains \Drupal\Tests\migrate\Unit\MigrateTestCase.
*/
namespace Drupal\Tests\migrate\Unit;
use Drupal\Core\Database\Driver\sqlite\Connection;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Tests\UnitTestCase;
/**
* Provides setup and helper methods for Migrate module tests.
*/
abstract class MigrateTestCase extends UnitTestCase {
protected $migrationConfiguration = [];
/**
* Retrieve a mocked migration.
*
* @return \Drupal\migrate\Entity\MigrationInterface|\PHPUnit_Framework_MockObject_MockObject
* The mocked migration.
*/
protected function getMigration() {
$this->migrationConfiguration += ['migrationClass' => 'Drupal\migrate\Entity\Migration'];
$this->idMap = $this->getMock('Drupal\migrate\Plugin\MigrateIdMapInterface');
$this->idMap->expects($this->any())
->method('getQualifiedMapTableName')
->will($this->returnValue('test_map'));
$migration = $this->getMockBuilder($this->migrationConfiguration['migrationClass'])
->disableOriginalConstructor()
->getMock();
$migration->expects($this->any())
->method('checkRequirements')
->will($this->returnValue(TRUE));
$migration->expects($this->any())
->method('getIdMap')
->will($this->returnValue($this->idMap));
$configuration = &$this->migrationConfiguration;
$migration->expects($this->any())->method('get')->will($this->returnCallback(function ($argument) use (&$configuration) {
return isset($configuration[$argument]) ? $configuration[$argument] : '';
}));
$migration->expects($this->any())->method('set')->will($this->returnCallback(function ($argument, $value) use (&$configuration) {
$configuration[$argument] = $value;
}));
$migration->expects($this->any())
->method('id')
->will($this->returnValue($configuration['id']));
return $migration;
}
/**
* Get an SQLite database connection object for use in tests.
*
* @param array $database_contents
* The database contents faked as an array. Each key is a table name, each
* value is a list of table rows, an associative array of field => value.
* @param array $connection_options
* (optional) Options for the database connection.
*
* @return \Drupal\Core\Database\Driver\sqlite\Connection
* The database connection.
*/
protected function getDatabase(array $database_contents, $connection_options = []) {
if (extension_loaded('pdo_sqlite')) {
$connection_options['database'] = ':memory:';
$pdo = Connection::open($connection_options);
$connection = new Connection($pdo, $connection_options);
}
else {
$this->markTestSkipped('The pdo_sqlite extension is not available.');
}
// Initialize the DIC with a fake module handler for alterable queries.
$container = new ContainerBuilder();
$container->set('module_handler', $this->getMock('\Drupal\Core\Extension\ModuleHandlerInterface'));
\Drupal::setContainer($container);
// Create the tables and load them up with data, skipping empty ones.
foreach (array_filter($database_contents) as $table => $rows) {
$pilot_row = reset($rows);
$connection->schema()->createTable($table, $this->createSchemaFromRow($pilot_row));
$insert = $connection->insert($table)->fields(array_keys($pilot_row));
array_walk($rows, [$insert, 'values']);
$insert->execute();
}
return $connection;
}
/**
* Generates a table schema from a row.
*
* @param array $row
* The reference row on which to base the schema.
*
* @return array
* The Schema API-ready table schema.
*/
protected function createSchemaFromRow(array $row) {
// SQLite uses loose ("affinity") typing, so it's OK for every column
// to be a text field.
$fields = array_map(function() { return ['type' => 'text']; }, $row);
return ['fields' => $fields];
}
/**
* Tests a query
*
* @param array|\Traversable
* The countable. foreach-able actual results if a query is being run.
*/
public function queryResultTest($iter, $expected_results) {
$this->assertSame(count($expected_results), count($iter), 'Number of results match');
$count = 0;
foreach ($iter as $data_row) {
$expected_row = $expected_results[$count];
$count++;
foreach ($expected_row as $key => $expected_value) {
$this->retrievalAssertHelper($expected_value, $this->getValue($data_row, $key), sprintf('Value matches for key "%s"', $key));
}
}
$this->assertSame(count($expected_results), $count);
}
/**
* @param array $row
* @param string $key
* @return mixed
*/
protected function getValue($row, $key) {
return $row[$key];
}
/**
* Asserts tested values during test retrieval.
*
* @param mixed $expected_value
* The incoming expected value to test.
* @param mixed $actual_value
* The incoming value itself.
* @param string $message
* The tested result as a formatted string.
*/
protected function retrievalAssertHelper($expected_value, $actual_value, $message) {
if (is_array($expected_value)) {
foreach ($expected_value as $k => $v) {
$this->retrievalAssertHelper($v, $actual_value[$k], $message . '[' . $k . ']');
}
}
else {
$this->assertSame((string) $expected_value, (string) $actual_value, $message);
}
}
}

View file

@ -0,0 +1,148 @@
<?php
/**
* @file
* Contains \Drupal\Tests\migrate\Unit\MigrationTest.
*/
namespace Drupal\Tests\migrate\Unit;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\migrate\Entity\Migration;
use Drupal\migrate\Exception\RequirementsException;
use Drupal\migrate\Plugin\MigrateDestinationInterface;
use Drupal\migrate\Plugin\MigrateSourceInterface;
use Drupal\migrate\Plugin\RequirementsInterface;
use Drupal\Tests\UnitTestCase;
/**
* @coversDefaultClass \Drupal\migrate\Entity\Migration
* @group Migration
*/
class MigrationTest extends UnitTestCase {
/**
* Tests checking requirements for source plugins.
*
* @covers ::checkRequirements
*
* @expectedException \Drupal\migrate\Exception\RequirementsException
* @expectedExceptionMessage Missing source requirement
*/
public function testRequirementsForSourcePlugin() {
$migration = new TestMigration();
$source_plugin = $this->getMock('Drupal\Tests\migrate\Unit\RequirementsAwareSourceInterface');
$source_plugin->expects($this->once())
->method('checkRequirements')
->willThrowException(new RequirementsException('Missing source requirement', ['key' => 'value']));
$destination_plugin = $this->getMock('Drupal\Tests\migrate\Unit\RequirementsAwareDestinationInterface');
$migration->setSourcePlugin($source_plugin);
$migration->setDestinationPlugin($destination_plugin);
$migration->checkRequirements();
}
/**
* Tests checking requirements for destination plugins.
*
* @covers ::checkRequirements
*
* @expectedException \Drupal\migrate\Exception\RequirementsException
* @expectedExceptionMessage Missing destination requirement
*/
public function testRequirementsForDestinationPlugin() {
$migration = new TestMigration();
$source_plugin = $this->getMock('Drupal\migrate\Plugin\MigrateSourceInterface');
$destination_plugin = $this->getMock('Drupal\Tests\migrate\Unit\RequirementsAwareDestinationInterface');
$destination_plugin->expects($this->once())
->method('checkRequirements')
->willThrowException(new RequirementsException('Missing destination requirement', ['key' => 'value']));
$migration->setSourcePlugin($source_plugin);
$migration->setDestinationPlugin($destination_plugin);
$migration->checkRequirements();
}
/**
* Tests checking requirements for destination plugins.
*
* @covers ::checkRequirements
*
* @expectedException \Drupal\migrate\Exception\RequirementsException
* @expectedExceptionMessage Missing migrations test_a, test_c
*/
public function testRequirementsForMigrations() {
$migration = new TestMigration();
// Setup source and destination plugins without any requirements.
$source_plugin = $this->getMock('Drupal\migrate\Plugin\MigrateSourceInterface');
$destination_plugin = $this->getMock('Drupal\migrate\Plugin\MigrateDestinationInterface');
$migration->setSourcePlugin($source_plugin);
$migration->setDestinationPlugin($destination_plugin);
$entity_manager = $this->getMock('Drupal\Core\Entity\EntityManagerInterface');
$migration->setEntityManager($entity_manager);
// We setup the requirements that test_a doesn't exist and test_c is not
// completed yet.
$migration->setRequirements(['test_a', 'test_b', 'test_c', 'test_d']);
$migration_b = $this->getMock('Drupal\migrate\Entity\MigrationInterface');
$migration_c = $this->getMock('Drupal\migrate\Entity\MigrationInterface');
$migration_d = $this->getMock('Drupal\migrate\Entity\MigrationInterface');
$migration_b->expects($this->once())
->method('isComplete')
->willReturn(TRUE);
$migration_c->expects($this->once())
->method('isComplete')
->willReturn(FALSE);
$migration_d->expects($this->once())
->method('isComplete')
->willReturn(TRUE);
$migration_storage = $this->getMock('Drupal\Core\Entity\EntityStorageInterface');
$migration_storage->expects($this->once())
->method('loadMultiple')
->with(['test_a', 'test_b', 'test_c', 'test_d'])
->willReturn(['test_b' => $migration_b, 'test_c' => $migration_c, 'test_d' => $migration_d]);
$entity_manager->expects($this->once())
->method('getStorage')
->with('migration')
->willReturn($migration_storage);
$migration->checkRequirements();
}
}
class TestMigration extends Migration {
public function __construct() {
}
public function setRequirements(array $requirements) {
$this->requirements = $requirements;
}
public function setSourcePlugin(MigrateSourceInterface $source_plugin) {
$this->sourcePlugin = $source_plugin;
}
public function setDestinationPlugin(MigrateDestinationInterface $destination_plugin) {
$this->destinationPlugin = $destination_plugin;
}
public function setEntityManager(EntityManagerInterface $entity_manager) {
$this->entityManager = $entity_manager;
}
}
interface RequirementsAwareSourceInterface extends MigrateSourceInterface, RequirementsInterface {}
interface RequirementsAwareDestinationInterface extends MigrateDestinationInterface, RequirementsInterface {}

View file

@ -0,0 +1,250 @@
<?php
/**
* @file
* Contains \Drupal\Tests\migrate\Unit\RowTest.
*/
namespace Drupal\Tests\migrate\Unit;
use Drupal\migrate\Plugin\MigrateIdMapInterface;
use Drupal\migrate\Row;
use Drupal\Tests\UnitTestCase;
/**
* @coversDefaultClass \Drupal\migrate\Row
* @group migrate
*/
class RowTest extends UnitTestCase {
/**
* The source IDs.
*
* @var array
*/
protected $testSourceIds = array(
'nid' => 'Node ID',
);
/**
* The test values.
*
* @var array
*/
protected $testValues = array(
'nid' => 1,
'title' => 'node 1',
);
/**
* The test hash.
*
* @var string
*/
protected $testHash = '85795d4cde4a2425868b812cc88052ecd14fc912e7b9b4de45780f66750e8b1e';
/**
* The test hash after changing title value to 'new title'.
*
* @var string
*/
protected $testHashMod = '9476aab0b62b3f47342cc6530441432e5612dcba7ca84115bbab5cceaca1ecb3';
/**
* Tests object creation: empty.
*/
public function testRowWithoutData() {
$row = new Row(array(), array());
$this->assertSame(array(), $row->getSource(), 'Empty row');
}
/**
* Tests object creation: basic.
*/
public function testRowWithBasicData() {
$row = new Row($this->testValues, $this->testSourceIds);
$this->assertSame($this->testValues, $row->getSource(), 'Row with data, simple id.');
}
/**
* Tests object creation: multiple source IDs.
*/
public function testRowWithMultipleSourceIds() {
$multi_source_ids = $this->testSourceIds + array('vid' => 'Node revision');
$multi_source_ids_values = $this->testValues + array('vid' => 1);
$row = new Row($multi_source_ids_values, $multi_source_ids);
$this->assertSame($multi_source_ids_values, $row->getSource(), 'Row with data, multifield id.');
}
/**
* Tests object creation: invalid values.
*
* @expectedException \Exception
*/
public function testRowWithInvalidData() {
$invalid_values = array(
'title' => 'node X',
);
$row = new Row($invalid_values, $this->testSourceIds);
}
/**
* Tests source immutability after freeze.
*
* @expectedException \Exception
*/
public function testSourceFreeze() {
$row = new Row($this->testValues, $this->testSourceIds);
$row->rehash();
$this->assertSame($this->testHash, $row->getHash(), 'Correct hash.');
$row->setSourceProperty('title', 'new title');
$row->rehash();
$this->assertSame($this->testHashMod, $row->getHash(), 'Hash changed correctly.');
$row->freezeSource();
$row->setSourceProperty('title', 'new title');
}
/**
* Tests setting on a frozen row.
*
* @expectedException \Exception
* @expectedExceptionMessage The source is frozen and can't be changed any more
*/
public function testSetFrozenRow() {
$row = new Row($this->testValues, $this->testSourceIds);
$row->freezeSource();
$row->setSourceProperty('title', 'new title');
}
/**
* Tests hashing.
*/
public function testHashing() {
$row = new Row($this->testValues, $this->testSourceIds);
$this->assertSame('', $row->getHash(), 'No hash at creation');
$row->rehash();
$this->assertSame($this->testHash, $row->getHash(), 'Correct hash.');
$row->rehash();
$this->assertSame($this->testHash, $row->getHash(), 'Correct hash even doing it twice.');
// Set the map to needs update.
$test_id_map = array(
'original_hash' => '',
'hash' => '',
'source_row_status' => MigrateIdMapInterface::STATUS_NEEDS_UPDATE,
);
$row->setIdMap($test_id_map);
$this->assertTrue($row->needsUpdate());
$row->rehash();
$this->assertSame($this->testHash, $row->getHash(), 'Correct hash even if id_mpa have changed.');
$row->setSourceProperty('title', 'new title');
$row->rehash();
$this->assertSame($this->testHashMod, $row->getHash(), 'Hash changed correctly.');
// Check hash calculation algorithm.
$hash = hash('sha256', serialize($row->getSource()));
$this->assertSame($hash, $row->getHash());
// Check length of generated hash used for mapping schema.
$this->assertSame(64, strlen($row->getHash()));
// Set the map to successfully imported.
$test_id_map = array(
'original_hash' => '',
'hash' => '',
'source_row_status' => MigrateIdMapInterface::STATUS_IMPORTED,
);
$row->setIdMap($test_id_map);
$this->assertFalse($row->needsUpdate());
// Set the same hash value and ensure it was not changed.
$random = $this->randomMachineName();
$test_id_map = array(
'original_hash' => $random,
'hash' => $random,
'source_row_status' => MigrateIdMapInterface::STATUS_NEEDS_UPDATE,
);
$row->setIdMap($test_id_map);
$this->assertFalse($row->changed());
// Set different has values to ensure it is marked as changed.
$test_id_map = array(
'original_hash' => $this->randomMachineName(),
'hash' => $this->randomMachineName(),
'source_row_status' => MigrateIdMapInterface::STATUS_NEEDS_UPDATE,
);
$row->setIdMap($test_id_map);
$this->assertTrue($row->changed());
}
/**
* Tests getting/setting the ID Map.
*
* @covers ::setIdMap
* @covers ::getIdMap
*/
public function testGetSetIdMap() {
$row = new Row($this->testValues, $this->testSourceIds);
$test_id_map = array(
'original_hash' => '',
'hash' => '',
'source_row_status' => MigrateIdMapInterface::STATUS_NEEDS_UPDATE,
);
$row->setIdMap($test_id_map);
$this->assertEquals($test_id_map, $row->getIdMap());
}
/**
* Tests the source ID.
*/
public function testSourceIdValues() {
$row = new Row($this->testValues, $this->testSourceIds);
$this->assertSame(array('nid' => $this->testValues['nid']), $row->getSourceIdValues());
}
/**
* Tests getting the source property.
*
* @covers ::getSourceProperty
*/
public function testGetSourceProperty() {
$row = new Row($this->testValues, $this->testSourceIds);
$this->assertSame($this->testValues['nid'], $row->getSourceProperty('nid'));
$this->assertSame($this->testValues['title'], $row->getSourceProperty('title'));
$this->assertNull($row->getSourceProperty('non_existing'));
}
/**
* Tests setting and getting the destination.
*/
public function testDestination() {
$row = new Row($this->testValues, $this->testSourceIds);
$this->assertEmpty($row->getDestination());
$this->assertFalse($row->hasDestinationProperty('nid'));
// Set a destination.
$row->setDestinationProperty('nid', 2);
$this->assertTrue($row->hasDestinationProperty('nid'));
$this->assertEquals(array('nid' => 2), $row->getDestination());
}
/**
* Tests setting/getting multiple destination IDs.
*/
public function testMultipleDestination() {
$row = new Row($this->testValues, $this->testSourceIds);
// Set some deep nested values.
$row->setDestinationProperty('image/alt', 'alt text');
$row->setDestinationProperty('image/fid', 3);
$this->assertTrue($row->hasDestinationProperty('image'));
$this->assertFalse($row->hasDestinationProperty('alt'));
$this->assertFalse($row->hasDestinationProperty('fid'));
$destination = $row->getDestination();
$this->assertEquals('alt text', $destination['image']['alt']);
$this->assertEquals(3, $destination['image']['fid']);
$this->assertEquals('alt text', $row->getDestinationProperty('image/alt'));
$this->assertEquals(3, $row->getDestinationProperty('image/fid'));
}
}

View file

@ -0,0 +1,177 @@
<?php
/**
* @file
* Contains \Drupal\Tests\migrate\Unit\SqlBaseTest.
*/
namespace Drupal\Tests\migrate\Unit;
use Drupal\migrate\Plugin\migrate\source\SqlBase;
use Drupal\Tests\UnitTestCase;
/**
* Tests the SqlBase class.
*
* @group migrate
*/
class SqlBaseTest extends UnitTestCase {
/**
* @param bool $expected_result
* The expected result.
* @param bool $id_map_is_sql
* TRUE if we want getIdMap() to return an instance of Sql.
* @param bool $with_id_map
* TRUE if we want the id map to have a valid map of ids.
* @param array $source_options
* An array of connection options for the source connection.
* @param array $idmap_options
* An array of connection options for the id map connection.
*
* @dataProvider sqlBaseTestProvider
*/
public function testMapJoinable($expected_result, $id_map_is_sql, $with_id_map, $source_options = [], $idmap_options = []) {
// Setup a connection object.
$source_connection = $this->getMockBuilder('Drupal\Core\Database\Connection')
->disableOriginalConstructor()
->getMock();
$source_connection->expects($id_map_is_sql && $with_id_map ? $this->once() : $this->never())
->method('getConnectionOptions')
->willReturn($source_options);
// Setup the id map connection.
$idmap_connection = $this->getMockBuilder('Drupal\Core\Database\Connection')
->disableOriginalConstructor()
->getMock();
$idmap_connection->expects($id_map_is_sql && $with_id_map ? $this->once() : $this->never())
->method('getConnectionOptions')
->willReturn($idmap_options);
// Setup the Sql object.
$sql = $this->getMockBuilder('Drupal\migrate\Plugin\migrate\id_map\Sql')
->disableOriginalConstructor()
->getMock();
$sql->expects($id_map_is_sql && $with_id_map ? $this->once() : $this->never())
->method('getDatabase')
->willReturn($idmap_connection);
// Setup a migration entity.
$migration = $this->getMock('Drupal\migrate\Entity\MigrationInterface');
$migration->expects($with_id_map ? $this->once() : $this->never())
->method('getIdMap')
->willReturn($id_map_is_sql ? $sql : NULL);
// Create our SqlBase test class.
$sql_base = new TestSqlBase();
$sql_base->setMigration($migration);
$sql_base->setDatabase($source_connection);
// Configure the idMap to make the check in mapJoinable() pass.
if ($with_id_map) {
$sql_base->setIds([
'uid' => ['type' => 'integer', 'alias' => 'u'],
]);
}
$this->assertEquals($expected_result, $sql_base->mapJoinable());
}
/**
* The data provider for SqlBase.
*
* @return array
* An array of data per test run.
*/
public function sqlBaseTestProvider() {
return [
// Source ids are empty so mapJoinable() is false.
[FALSE, FALSE, FALSE],
// Still false because getIdMap() is not a subclass of Sql.
[FALSE, FALSE, TRUE],
// Test mapJoinable() returns false when source and id connection options
// differ.
[FALSE, TRUE, TRUE, ['username' => 'different_from_map', 'password' => 'different_from_map'], ['username' => 'different_from_source', 'password' => 'different_from_source']],
// Returns true because source and id map connection options are the same.
[TRUE, TRUE, TRUE, ['username' => 'same_value', 'password' => 'same_value'], ['username' => 'same_value', 'password' => 'same_value']],
];
}
}
class TestSqlBase extends SqlBase {
protected $database;
protected $ids;
/**
* Override the constructor so we can create one easily.
*/
public function __construct() {}
/**
* Allows us to set the database during tests.
*
* @param mixed $database
* The database mock object.
*/
public function setDatabase($database) {
$this->database = $database;
}
/**
* {@inheritdoc}
*/
public function getDatabase() {
return $this->database;
}
/**
* Allows us to set the migration during the test.
*
* @param mixed $migration
* The migration mock.
*/
public function setMigration($migration) {
$this->migration = $migration;
}
/**
* {@inheritdoc}
*/
public function mapJoinable() {
return parent::mapJoinable();
}
/**
* {@inheritdoc}
*/
public function getIds() {
return $this->ids;
}
/**
* Allows us to set the ids during a test.
*/
public function setIds($ids) {
$this->ids = $ids;
}
/**
* {@inheritdoc}
*/
public function fields() {}
/**
* {@inheritdoc}
*/
public function query() {}
/**
* {@inheritdoc}
*/
public function calculateDependencies() {
return [];
}
}

View file

@ -0,0 +1,251 @@
<?php
/**
* @file
* Contains \Drupal\Tests\migrate\Unit\TestMigrateExecutable.
*/
namespace Drupal\Tests\migrate\Unit;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\migrate\MigrateExecutable;
/**
* Tests MigrateExecutable.
*/
class TestMigrateExecutable extends MigrateExecutable {
/**
* The (fake) number of seconds elapsed since the start of the test.
*
* @var int
*/
protected $timeElapsed;
/**
* The fake memory usage in bytes.
*
* @var int
*/
protected $memoryUsage;
/**
* The cleared memory usage.
*
* @var int
*/
protected $clearedMemoryUsage;
/**
* Sets the string translation service.
*
* @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
* The translation manager.
*/
public function setStringTranslation(TranslationInterface $string_translation) {
$this->stringTranslation = $string_translation;
}
/**
* Allows access to protected timeOptionExceeded method.
*
* @return bool
* A threshold exceeded value.
*/
public function timeOptionExceeded() {
return parent::timeOptionExceeded();
}
/**
* Allows access to set protected maxExecTime property.
*
* @param int $max_exec_time
* The value to set.
*/
public function setMaxExecTime($max_exec_time) {
$this->maxExecTime = $max_exec_time;
}
/**
* Allows access to protected maxExecTime property.
*
* @return int
* The value of the protected property.
*/
public function getMaxExecTime() {
return $this->maxExecTime;
}
/**
* Allows access to protected successesSinceFeedback property.
*
* @return int
* The value of the protected property.
*/
public function getSuccessesSinceFeedback() {
return $this->successesSinceFeedback;
}
/**
* Allows access to protected totalSuccesses property.
*
* @return int
* The value of the protected property.
*/
public function getTotalSuccesses() {
return $this->totalSuccesses;
}
/**
* Allows access to protected totalProcessed property.
*
* @return int
* The value of the protected property.
*/
public function getTotalProcessed() {
return $this->totalProcessed;
}
/**
* Allows access to protected processedSinceFeedback property.
*
* @return int
* The value of the protected property.
*/
public function getProcessedSinceFeedback() {
return $this->processedSinceFeedback;
}
/**
* Allows access to protected maxExecTimeExceeded method.
*
* @return bool
* The threshold exceeded value.
*/
public function maxExecTimeExceeded() {
return parent::maxExecTimeExceeded();
}
/**
* Allows access to set protected source property.
*
* @param \Drupal\migrate\Plugin\MigrateSourceInterface $source
* The value to set.
*/
public function setSource($source) {
$this->source = $source;
}
/**
* Allows access to protected sourceIdValues property.
*
* @param array $source_id_values
* The value to set.
*/
public function setSourceIdValues($source_id_values) {
$this->sourceIdValues = $source_id_values;
}
/**
* Allows setting a fake elapsed time.
*
* @param int $time
* The time in seconds.
*/
public function setTimeElapsed($time) {
$this->timeElapsed = $time;
}
/**
* {@inheritdoc}
*/
public function getTimeElapsed() {
return $this->timeElapsed;
}
/**
* {@inheritdoc}
*/
public function handleException(\Exception $exception, $save = TRUE) {
$message = $exception->getMessage();
if ($save) {
$this->saveMessage($message);
}
$this->message->display($message);
}
/**
* Allows access to the protected memoryExceeded method.
*
* @return bool
* The memoryExceeded value.
*/
public function memoryExceeded() {
return parent::memoryExceeded();
}
/**
* {@inheritdoc}
*/
protected function attemptMemoryReclaim() {
return $this->clearedMemoryUsage;
}
/**
* {@inheritdoc}
*/
protected function getMemoryUsage() {
return $this->memoryUsage;
}
/**
* Sets the fake memory usage.
*
* @param int $memory_usage
* The fake memory usage value.
* @param int $cleared_memory_usage
* (optional) The fake cleared memory value.
*/
public function setMemoryUsage($memory_usage, $cleared_memory_usage = NULL) {
$this->memoryUsage = $memory_usage;
$this->clearedMemoryUsage = $cleared_memory_usage;
}
/**
* Sets the memory limit.
*
* @param int $memory_limit
* The memory limit.
*/
public function setMemoryLimit($memory_limit) {
$this->memoryLimit = $memory_limit;
}
/**
* Sets the memory threshold.
*
* @param float $threshold
* The new threshold.
*/
public function setMemoryThreshold($threshold) {
$this->memoryThreshold = $threshold;
}
/**
* Sets the time threshold.
*
* @param float $threshold
* The new threshold.
*/
public function setTimeThreshold($threshold) {
$this->timeThreshold = $threshold;
}
/**
* {@inheritdoc}
*/
protected function formatSize($size) {
return $size;
}
}

View file

@ -0,0 +1,67 @@
<?php
/**
* @file
* Contains \Drupal\Tests\migrate\Unit\TestSqlIdMap.
*/
namespace Drupal\Tests\migrate\Unit;
use Drupal\Component\Utility\SafeMarkup;
use Drupal\Core\Database\Connection;
use Drupal\migrate\Entity\MigrationInterface;
use Drupal\migrate\MigrateException;
use Drupal\migrate\Plugin\migrate\id_map\Sql;
/**
* Defines a SQL ID map for use in tests.
*/
class TestSqlIdMap extends Sql implements \Iterator {
/**
* Constructs a TestSqlIdMap object.
*
* @param \Drupal\Core\Database\Connection $database
* The database.
* @param array $configuration
* The configuration.
* @param string $plugin_id
* The plugin ID for the migration process to do.
* @param mixed $plugin_definition
* The configuration for the plugin.
* @param \Drupal\migrate\Entity\MigrationInterface $migration
* The migration to do.
*/
public function __construct(Connection $database, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration) {
$this->database = $database;
parent::__construct($configuration, $plugin_id, $plugin_definition, $migration);
}
/**
* {@inheritdoc}
*/
public function getDatabase() {
return parent::getDatabase();
}
protected function getFieldSchema(array $id_definition) {
if (!isset($id_definition['type'])) {
return array();
}
switch ($id_definition['type']) {
case 'integer':
return array(
'type' => 'int',
'not null' => TRUE,
);
case 'string':
return array(
'type' => 'varchar',
'length' => 255,
'not null' => FALSE,
);
default:
throw new MigrateException(SafeMarkup::format('@type not supported', array('@type' => $id_definition['type'])));
}
}
}

View file

@ -0,0 +1,55 @@
<?php
/**
* @file
* Contains \Drupal\Tests\migrate\Unit\destination\ConfigTest.
*/
namespace Drupal\Tests\migrate\Unit\destination;
use Drupal\migrate\Plugin\migrate\destination\Config;
use Drupal\Tests\UnitTestCase;
/**
* @coversDefaultClass \Drupal\migrate\Plugin\migrate\destination\Config
* @group migrate
*/
class ConfigTest extends UnitTestCase {
/**
* Test the import method.
*/
public function testImport() {
$source = array(
'test' => 'x',
);
$migration = $this->getMockBuilder('Drupal\migrate\Entity\Migration')
->disableOriginalConstructor()
->getMock();
$config = $this->getMockBuilder('Drupal\Core\Config\Config')
->disableOriginalConstructor()
->getMock();
foreach ($source as $key => $val) {
$config->expects($this->once())
->method('set')
->with($this->equalTo($key), $this->equalTo($val))
->will($this->returnValue($config));
}
$config->expects($this->once())
->method('save');
$config_factory = $this->getMock('Drupal\Core\Config\ConfigFactoryInterface');
$config_factory->expects($this->once())
->method('getEditable')
->with('d8_config')
->will($this->returnValue($config));
$row = $this->getMockBuilder('Drupal\migrate\Row')
->disableOriginalConstructor()
->getMock();
$row->expects($this->once())
->method('getRawDestination')
->will($this->returnValue($source));
$destination = new Config(array('config_name' => 'd8_config'), 'd8_config', array('pluginId' => 'd8_config'), $migration, $config_factory);
$destination->import($row);
}
}

View file

@ -0,0 +1,66 @@
<?php
/**
* @file
* Contains \Drupal\Tests\migrate\Unit\destination\PerComponentEntityDisplayTest.
*/
namespace Drupal\Tests\migrate\Unit\destination;
use Drupal\migrate\Plugin\migrate\destination\ComponentEntityDisplayBase;
use Drupal\migrate\Row;
use Drupal\Tests\migrate\Unit\MigrateTestCase;
/**
* Tests the entity display destination plugin.
*
* @group migrate
*/
class PerComponentEntityDisplayTest extends MigrateTestCase {
/**
* Tests the entity display import method.
*/
public function testImport() {
$values = array(
'entity_type' => 'entity_type_test',
'bundle' => 'bundle_test',
'view_mode' => 'view_mode_test',
'field_name' => 'field_name_test',
'options' => array('test setting'),
);
$row = new Row(array(), array());
foreach ($values as $key => $value) {
$row->setDestinationProperty($key, $value);
}
$entity = $this->getMockBuilder('Drupal\Core\Entity\Entity\EntityViewDisplay')
->disableOriginalConstructor()
->getMock();
$entity->expects($this->once())
->method('setComponent')
->with('field_name_test', array('test setting'))
->will($this->returnSelf());
$entity->expects($this->once())
->method('save')
->with();
$plugin = new TestPerComponentEntityDisplay($entity);
$this->assertSame($plugin->import($row), array('entity_type_test', 'bundle_test', 'view_mode_test', 'field_name_test'));
$this->assertSame($plugin->getTestValues(), array('entity_type_test', 'bundle_test', 'view_mode_test'));
}
}
class TestPerComponentEntityDisplay extends ComponentEntityDisplayBase {
const MODE_NAME = 'view_mode';
protected $testValues;
public function __construct($entity) {
$this->entity = $entity;
}
protected function getEntity($entity_type, $bundle, $view_mode) {
$this->testValues = func_get_args();
return $this->entity;
}
public function getTestValues() {
return $this->testValues;
}
}

View file

@ -0,0 +1,66 @@
<?php
/**
* @file
* Contains \Drupal\Tests\migrate\Unit\destination\PerComponentEntityFormDisplayTest.
*/
namespace Drupal\Tests\migrate\Unit\destination;
use Drupal\migrate\Plugin\migrate\destination\PerComponentEntityFormDisplay;
use Drupal\migrate\Row;
use Drupal\Tests\migrate\Unit\MigrateTestCase;
/**
* Tests the entity display destination plugin.
*
* @group migrate
*/
class PerComponentEntityFormDisplayTest extends MigrateTestCase {
/**
* Tests the entity display import method.
*/
public function testImport() {
$values = array(
'entity_type' => 'entity_type_test',
'bundle' => 'bundle_test',
'form_mode' => 'form_mode_test',
'field_name' => 'field_name_test',
'options' => array('test setting'),
);
$row = new Row(array(), array());
foreach ($values as $key => $value) {
$row->setDestinationProperty($key, $value);
}
$entity = $this->getMockBuilder('Drupal\Core\Entity\Entity\EntityFormDisplay')
->disableOriginalConstructor()
->getMock();
$entity->expects($this->once())
->method('setComponent')
->with('field_name_test', array('test setting'))
->will($this->returnSelf());
$entity->expects($this->once())
->method('save')
->with();
$plugin = new TestPerComponentEntityFormDisplay($entity);
$this->assertSame($plugin->import($row), array('entity_type_test', 'bundle_test', 'form_mode_test', 'field_name_test'));
$this->assertSame($plugin->getTestValues(), array('entity_type_test', 'bundle_test', 'form_mode_test'));
}
}
class TestPerComponentEntityFormDisplay extends PerComponentEntityFormDisplay {
const MODE_NAME = 'form_mode';
protected $testValues;
public function __construct($entity) {
$this->entity = $entity;
}
protected function getEntity($entity_type, $bundle, $form_mode) {
$this->testValues = func_get_args();
return $this->entity;
}
public function getTestValues() {
return $this->testValues;
}
}

View file

@ -0,0 +1,55 @@
<?php
/**
* @file
* Contains \Drupal\Tests\migrate\Unit\process\CallbackTest.
*/
namespace Drupal\Tests\migrate\Unit\process;
use Drupal\migrate\Plugin\migrate\process\Callback;
/**
* Tests the callback process plugin.
*
* @group migrate
*/
class CallbackTest extends MigrateProcessTestCase {
/**
* {@inheritdoc}
*/
protected function setUp() {
$this->plugin = new TestCallback();
parent::setUp();
}
/**
* Test callback with a function as callable.
*/
public function testCallbackWithFunction() {
$this->plugin->setCallable('strtolower');
$value = $this->plugin->transform('FooBar', $this->migrateExecutable, $this->row, 'destinationproperty');
$this->assertSame($value, 'foobar');
}
/**
* Test callback with a class method as callable.
*/
public function testCallbackWithClassMethod() {
$this->plugin->setCallable(array('\Drupal\Component\Utility\Unicode', 'strtolower'));
$value = $this->plugin->transform('FooBar', $this->migrateExecutable, $this->row, 'destinationproperty');
$this->assertSame($value, 'foobar');
}
}
class TestCallback extends Callback {
public function __construct() {
}
public function setCallable($callable) {
$this->configuration['callable'] = $callable;
}
}

View file

@ -0,0 +1,68 @@
<?php
/**
* @file
* Contains \Drupal\Tests\migrate\Unit\process\ConcatTest.
*/
namespace Drupal\Tests\migrate\Unit\process;
use Drupal\migrate\Plugin\migrate\process\Concat;
/**
* Tests the concat process plugin.
*
* @group migrate
*/
class ConcatTest extends MigrateProcessTestCase {
/**
* {@inheritdoc}
*/
protected function setUp() {
$this->plugin = new TestConcat();
parent::setUp();
}
/**
* Test concat works without a delimiter.
*/
public function testConcatWithoutDelimiter() {
$value = $this->plugin->transform(array('foo', 'bar'), $this->migrateExecutable, $this->row, 'destinationproperty');
$this->assertSame($value, 'foobar');
}
/**
* Test concat fails properly on non-arrays.
*
* @expectedException \Drupal\migrate\MigrateException
*/
public function testConcatWithNonArray() {
$this->plugin->transform('foo', $this->migrateExecutable, $this->row, 'destinationproperty');
}
/**
* Test concat works without a delimiter.
*/
public function testConcatWithDelimiter() {
$this->plugin->setDelimiter('_');
$value = $this->plugin->transform(array('foo', 'bar'), $this->migrateExecutable, $this->row, 'destinationproperty');
$this->assertSame($value, 'foo_bar');
}
}
class TestConcat extends Concat {
public function __construct() {
}
/**
* Set the delimiter.
*
* @param string $delimiter
* The new delimiter.
*/
public function setDelimiter($delimiter) {
$this->configuration['delimiter'] = $delimiter;
}
}

View file

@ -0,0 +1,167 @@
<?php
/**
* @file
* Contains \Drupal\Tests\migrate\Unit\process\DedupeEntityTest.
*/
namespace Drupal\Tests\migrate\Unit\process;
use Drupal\migrate\Plugin\migrate\process\DedupeEntity;
use Drupal\Component\Utility\Unicode;
/**
* @coversDefaultClass \Drupal\migrate\Plugin\migrate\process\DedupeEntity
* @group migrate
*/
class DedupeEntityTest extends MigrateProcessTestCase {
/**
* The mock entity query.
*
* @var \Drupal\Core\Entity\Query\QueryInterface
* @var \Drupal\Core\Entity\Query\QueryFactory
*/
protected $entityQuery;
/**
* The mock entity query factory.
*
* @var \Drupal\Core\Entity\Query\QueryFactory|\PHPUnit_Framework_MockObject_MockObject
*/
protected $entityQueryFactory;
/**
* The migration configuration, initialized to set the ID to test.
*
* @var array
*/
protected $migrationConfiguration = array(
'id' => 'test',
);
/**
* {@inheritdoc}
*/
protected function setUp() {
$this->entityQuery = $this->getMockBuilder('Drupal\Core\Entity\Query\QueryInterface')
->disableOriginalConstructor()
->getMock();
$this->entityQueryFactory = $this->getMockBuilder('Drupal\Core\Entity\Query\QueryFactory')
->disableOriginalConstructor()
->getMock();
$this->entityQueryFactory->expects($this->any())
->method('get')
->will($this->returnValue($this->entityQuery));
parent::setUp();
}
/**
* Tests entity based deduplication based on providerTestDedupe() values.
*
* @dataProvider providerTestDedupe
*/
public function testDedupe($count, $postfix = '', $start = NULL, $length = NULL) {
$configuration = array(
'entity_type' => 'test_entity_type',
'field' => 'test_field',
);
if ($postfix) {
$configuration['postfix'] = $postfix;
}
$configuration['start'] = isset($start) ? $start : NULL;
$configuration['length'] = isset($length) ? $length : NULL;
$plugin = new DedupeEntity($configuration, 'dedupe_entity', array(), $this->getMigration(), $this->entityQueryFactory);
$this->entityQueryExpects($count);
$value = $this->randomMachineName(32);
$actual = $plugin->transform($value, $this->migrateExecutable, $this->row, 'testproperty');
$expected = Unicode::substr($value, $start, $length);
$expected .= $count ? $postfix . $count : '';
$this->assertSame($expected, $actual);
}
/**
* Tests that invalid start position throws an exception.
*/
public function testDedupeEntityInvalidStart() {
$configuration = array(
'entity_type' => 'test_entity_type',
'field' => 'test_field',
'start' => 'foobar',
);
$plugin = new DedupeEntity($configuration, 'dedupe_entity', array(), $this->getMigration(), $this->entityQueryFactory);
$this->setExpectedException('Drupal\migrate\MigrateException', 'The start position configuration key should be an integer. Omit this key to capture from the beginning of the string.');
$plugin->transform('test_start', $this->migrateExecutable, $this->row, 'testproperty');
}
/**
* Tests that invalid length option throws an exception.
*/
public function testDedupeEntityInvalidLength() {
$configuration = array(
'entity_type' => 'test_entity_type',
'field' => 'test_field',
'length' => 'foobar',
);
$plugin = new DedupeEntity($configuration, 'dedupe_entity', array(), $this->getMigration(), $this->entityQueryFactory);
$this->setExpectedException('Drupal\migrate\MigrateException', 'The character length configuration key should be an integer. Omit this key to capture the entire string.');
$plugin->transform('test_length', $this->migrateExecutable, $this->row, 'testproperty');
}
/**
* Data provider for testDedupe().
*/
public function providerTestDedupe() {
return array(
// Tests no duplication.
array(0),
// Tests no duplication and start position.
array(0, NULL, 10),
// Tests no duplication, start position, and length.
array(0, NULL, 5, 10),
// Tests no duplication and length.
array(0, NULL, NULL, 10),
// Tests duplication.
array(3),
// Tests duplication and start position.
array(3, NULL, 10),
// Tests duplication, start position, and length.
array(3, NULL, 5, 10),
// Tests duplication and length.
array(3, NULL, NULL, 10),
// Tests no duplication and postfix.
array(0, '_'),
// Tests no duplication, postfix, and start position.
array(0, '_', 5),
// Tests no duplication, postfix, start position, and length.
array(0, '_', 5, 10),
// Tests no duplication, postfix, and length.
array(0, '_', NULL, 10),
// Tests duplication and postfix.
array(2, '_'),
// Tests duplication, postfix, and start position.
array(2, '_', 5),
// Tests duplication, postfix, start position, and length.
array(2, '_', 5, 10),
// Tests duplication, postfix, and length.
array(2, '_', NULL, 10),
);
}
/**
* Helper function to add expectations to the mock entity query object.
*
* @param int $count
* The number of deduplications to be set up.
*/
protected function entityQueryExpects($count) {
$this->entityQuery->expects($this->exactly($count + 1))
->method('condition')
->will($this->returnValue($this->entityQuery));
$this->entityQuery->expects($this->exactly($count + 1))
->method('count')
->will($this->returnValue($this->entityQuery));
$this->entityQuery->expects($this->exactly($count + 1))
->method('execute')
->will($this->returnCallback(function () use (&$count) { return $count--;}));
}
}

View file

@ -0,0 +1,54 @@
<?php
/**
* @file
* Contains \Drupal\Tests\migrate\Unit\process\ExtractTest.
*/
namespace Drupal\Tests\migrate\Unit\process;
use Drupal\migrate\Plugin\migrate\process\Extract;
/**
* @coversDefaultClass \Drupal\migrate\Plugin\migrate\process\Extract
* @group migrate
*/
class ExtractTest extends MigrateProcessTestCase {
/**
* {@inheritdoc}
*/
protected function setUp() {
$configuration['index'] = array('foo');
$this->plugin = new Extract($configuration, 'map', array());
parent::setUp();
}
/**
* Tests successful extraction.
*/
public function testExtract() {
$value = $this->plugin->transform(array('foo' => 'bar'), $this->migrateExecutable, $this->row, 'destinationproperty');
$this->assertSame($value, 'bar');
}
/**
* Tests invalid input.
*
* @expectedException \Drupal\migrate\MigrateException
* @expectedExceptionMessage Input should be an array.
*/
public function testExtractFromString() {
$this->plugin->transform('bar', $this->migrateExecutable, $this->row, 'destinationproperty');
}
/**
* Tests unsuccessful extraction.
*
* @expectedException \Drupal\migrate\MigrateException
* @expectedExceptionMessage Array index missing, extraction failed.
*/
public function testExtractFail() {
$this->plugin->transform(array('bar' => 'foo'), $this->migrateExecutable, $this->row, 'destinationproperty');
}
}

View file

@ -0,0 +1,27 @@
<?php
/**
* @file
* Contains \Drupal\Tests\migrate\Unit\process\FlattenTest.
*/
namespace Drupal\Tests\migrate\Unit\process;
use Drupal\migrate\Plugin\migrate\process\Flatten;
/**
* Tests the flatten plugin.
*
* @group migrate
*/
class FlattenTest extends MigrateProcessTestCase {
/**
* Test that various array flatten operations work properly.
*/
public function testFlatten() {
$plugin = new Flatten(array(), 'flatten', array());
$flattened = $plugin->transform(array(1, 2, array(3, 4, array(5)), array(), array(7, 8)), $this->migrateExecutable, $this->row, 'destinationproperty');
$this->assertSame($flattened, array(1, 2, 3, 4, 5, 7, 8));
}
}

View file

@ -0,0 +1,96 @@
<?php
/**
* @file
* Contains \Drupal\Tests\migrate\Unit\process\GetTest.
*/
namespace Drupal\Tests\migrate\Unit\process;
use Drupal\migrate\Plugin\migrate\process\TestGet;
use Drupal\migrate\Row;
/**
* Tests the get process plugin.
*
* @group migrate
*/
class GetTest extends MigrateProcessTestCase {
/**
* {@inheritdoc}
*/
protected function setUp() {
$this->plugin = new TestGet();
parent::setUp();
}
/**
* Tests the Get plugin when source is a string.
*/
public function testTransformSourceString() {
$this->row->expects($this->once())
->method('getSourceProperty')
->with('test')
->will($this->returnValue('source_value'));
$this->plugin->setSource('test');
$value = $this->plugin->transform(NULL, $this->migrateExecutable, $this->row, 'destinationproperty');
$this->assertSame($value, 'source_value');
}
/**
* Tests the Get plugin when source is an array.
*/
public function testTransformSourceArray() {
$map = array(
'test1' => 'source_value1',
'test2' => 'source_value2',
);
$this->plugin->setSource(array('test1', 'test2'));
$this->row->expects($this->exactly(2))
->method('getSourceProperty')
->will($this->returnCallback(function ($argument) use ($map) { return $map[$argument]; } ));
$value = $this->plugin->transform(NULL, $this->migrateExecutable, $this->row, 'destinationproperty');
$this->assertSame($value, array('source_value1', 'source_value2'));
}
/**
* Tests the Get plugin when source is a string pointing to destination.
*/
public function testTransformSourceStringAt() {
$this->row->expects($this->once())
->method('getSourceProperty')
->with('@test')
->will($this->returnValue('source_value'));
$this->plugin->setSource('@@test');
$value = $this->plugin->transform(NULL, $this->migrateExecutable, $this->row, 'destinationproperty');
$this->assertSame($value, 'source_value');
}
/**
* Tests the Get plugin when source is an array pointing to destination.
*/
public function testTransformSourceArrayAt() {
$map = array(
'test1' => 'source_value1',
'@test2' => 'source_value2',
'@test3' => 'source_value3',
'test4' => 'source_value4',
);
$this->plugin->setSource(array('test1', '@@test2', '@@test3', 'test4'));
$this->row->expects($this->exactly(4))
->method('getSourceProperty')
->will($this->returnCallback(function ($argument) use ($map) { return $map[$argument]; } ));
$value = $this->plugin->transform(NULL, $this->migrateExecutable, $this->row, 'destinationproperty');
$this->assertSame($value, array('source_value1', 'source_value2', 'source_value3', 'source_value4'));
}
}
namespace Drupal\migrate\Plugin\migrate\process;
class TestGet extends Get {
public function __construct() {
}
public function setSource($source) {
$this->configuration['source'] = $source;
}
}

View file

@ -0,0 +1,85 @@
<?php
/**
* @file
* Contains \Drupal\Tests\migrate\Unit\process\IteratorTest.
*/
namespace Drupal\Tests\migrate\Unit\process;
use Drupal\migrate\MigrateExecutable;
use Drupal\migrate\Plugin\migrate\process\Get;
use Drupal\migrate\Plugin\migrate\process\Iterator;
use Drupal\migrate\Plugin\migrate\process\StaticMap;
use Drupal\migrate\Row;
use Drupal\Tests\migrate\Unit\MigrateTestCase;
/**
* Tests the iterator process plugin.
*
* @group migrate
*/
class IteratorTest extends MigrateTestCase {
/**
* The iterator plugin being tested.
*
* @var \Drupal\migrate\Plugin\migrate\process\TestIterator
*/
protected $plugin;
/**
* @var array
*/
protected $migrationConfiguration = array(
'id' => 'test',
);
/**
* Tests the iterator process plugin.
*/
public function testIterator() {
$migration = $this->getMigration();
// Set up the properties for the iterator.
$configuration = array(
'process' => array(
'foo' => 'source_foo',
'id' => 'source_id',
),
'key' => '@id',
);
$plugin = new Iterator($configuration, 'iterator', array());
// Manually create the plugins. Migration::getProcessPlugins does this
// normally but the plugin system is not available.
foreach ($configuration['process'] as $destination => $source) {
$iterator_plugins[$destination][] = new Get(array('source' => $source), 'get', array());
}
$migration->expects($this->at(1))
->method('getProcessPlugins')
->will($this->returnValue($iterator_plugins));
// Set up the key plugins.
$key_plugin['key'][] = new Get(array('source' => '@id'), 'get', array());
$migration->expects($this->at(2))
->method('getProcessPlugins')
->will($this->returnValue($key_plugin));
$migrate_executable = new MigrateExecutable($migration, $this->getMock('Drupal\migrate\MigrateMessageInterface'));
// The current value of the pipeline.
$current_value = array(
array(
'source_foo' => 'test',
'source_id' => 42,
),
);
// This is not used but the interface requires it, so create an empty row.
$row = new Row(array(), array());
// After transformation, check to make sure that source_foo and source_id's
// values ended up in the proper destinations, and that the value of the
// key (@id) is the same as the destination ID (42).
$new_value = $plugin->transform($current_value, $migrate_executable, $row, 'test');
$this->assertSame(count($new_value), 1);
$this->assertSame(count($new_value[42]), 2);
$this->assertSame($new_value[42]['foo'], 'test');
$this->assertSame($new_value[42]['id'], 42);
}
}

View file

@ -0,0 +1,65 @@
<?php
/**
* @file
* Contains \Drupal\Tests\migrate\Unit\process\MachineNameTest.
*/
namespace Drupal\Tests\migrate\Unit\process;
use Drupal\migrate\Plugin\migrate\process\MachineName;
/**
* Tests the machine name process plugin.
*
* @group migrate
*/
class MachineNameTest extends MigrateProcessTestCase {
/**
* The mock transliteration.
*
* @var \Drupal\Component\Transliteration\TransliterationInterface
*/
protected $transliteration;
/**
* {@inheritdoc}
*/
protected function setUp() {
$this->transliteration = $this->getMockBuilder('Drupal\Component\Transliteration\TransliterationInterface')
->disableOriginalConstructor()
->getMock();
$this->row = $this->getMockBuilder('Drupal\migrate\Row')
->disableOriginalConstructor()
->getMock();
$this->migrateExecutable = $this->getMockBuilder('Drupal\migrate\MigrateExecutable')
->disableOriginalConstructor()
->getMock();
parent::setUp();
}
/**
* Tests machine name transformation of non-alphanumeric characters.
*/
public function testMachineNames() {
// Tests the following transformations:
// - non-alphanumeric character (including spaces) -> underscore,
// - Uppercase -> lowercase,
// - Multiple consecutive underscore -> single underscore.
$human_name_ascii = 'foo2, the.bar;2*&the%baz!YEE____HaW ';
$human_name = $human_name_ascii .'áéő';
$expected_result = 'foo2_the_bar_2_the_baz_yee_haw_aeo';
// Test for calling transliterate on mock object.
$this->transliteration
->expects($this->once())
->method('transliterate')
->with($human_name)
->will($this->returnValue($human_name_ascii . 'aeo'));
$plugin = new MachineName(array(), 'machine_name', array(), $this->transliteration);
$value = $plugin->transform($human_name, $this->migrateExecutable, $this->row, 'destinationproperty');
$this->assertEquals($expected_result, $value);
}
}

View file

@ -0,0 +1,43 @@
<?php
/**
* @file
* Contains \Drupal\Tests\migrate\Unit\process\MigrateProcessTestCase.
*/
namespace Drupal\Tests\migrate\Unit\process;
use Drupal\Tests\migrate\Unit\MigrateTestCase;
abstract class MigrateProcessTestCase extends MigrateTestCase {
/**
* @var \Drupal\migrate\Plugin\MigrateProcessInterface
*/
protected $plugin;
/**
* @var \Drupal\migrate\Row
*/
protected $row;
/**
* @var \Drupal\migrate\MigrateExecutable|\PHPUnit_Framework_MockObject_MockObject
*/
protected $migrateExecutable;
/**
* {@inheritdoc}
*/
protected function setUp() {
$this->row = $this->getMockBuilder('Drupal\migrate\Row')
->disableOriginalConstructor()
->getMock();
$this->migrateExecutable = $this->getMockBuilder('Drupal\migrate\MigrateExecutable')
->disableOriginalConstructor()
->getMock();
parent::setUp();
}
}

View file

@ -0,0 +1,85 @@
<?php
/**
* @file
* Contains \Drupal\Tests\migrate\Unit\process\MigrationTest.
*/
namespace Drupal\Tests\migrate\Unit\process;
use Drupal\migrate\MigrateException;
use Drupal\migrate\Plugin\migrate\process\Migration;
/**
* Tests the migration process plugin.
*
* @coversDefaultClass \Drupal\migrate\Plugin\migrate\process\Migration
* @group migrate
*/
class MigrationTest extends MigrateProcessTestCase {
/**
* {@inheritdoc}
*/
protected function setUp() {
$this->migrationConfiguration = [
'id' => 'test',
'process' => [],
'source' => [],
];
parent::setUp();
}
/**
* Assert that exceptions during import are logged.
* @expectedException \Drupal\migrate\MigrateSkipRowException
* @covers ::transform
*/
public function testSaveOnException() {
// A bunch of mock objects to get thing working
$migration = $this->getMigration();
$migration_source = $this->getMock('\Drupal\migrate\Plugin\MigrateSourceInterface');
$migration_source->expects($this->once())
->method('getIds')
->willReturn([]);
$migration->expects($this->once())
->method('getSourcePlugin')
->willReturn($migration_source);
$migration_destination = $this->getMock('\Drupal\migrate\Plugin\MigrateDestinationInterface');
$migration->expects($this->once())
->method('getDestinationPlugin')
->willReturn($migration_destination);
$storage = $this->getMock('\Drupal\Core\Entity\EntityStorageInterface');
$storage->expects($this->once())
->method('loadMultiple')
->willReturn([
'id' => $migration
]);
$manager = $this->getMockBuilder('\Drupal\migrate\Plugin\MigratePluginManager')
->disableOriginalConstructor()
->getMock();
// Throw an exception during import so we can log it.
$migration_destination->expects($this->once())
->method('import')
->willThrowException(new MigrateException());
// Build our migration plugin.
$plugin = new Migration(['migration' => []],
'migration', // ?
[],
$migration,
$storage,
$manager);
// Assert that we log exceptions thrown during the import.
$this->migrateExecutable->expects($this->once())
->method('saveMessage');
$plugin->transform('value', $this->migrateExecutable, $this->row, 'prop');
}
}

View file

@ -0,0 +1,60 @@
<?php
/**
* @file
* Contains \Drupal\Tests\migrate\Unit\process\SkipOnEmptyTest.
*/
namespace Drupal\Tests\migrate\Unit\process;
use Drupal\migrate\Plugin\migrate\process\SkipOnEmpty;
/**
* Tests the skip on empty process plugin.
*
* @group migrate
* @coversDefaultClass \Drupal\migrate\Plugin\migrate\process\SkipOnEmpty
*/
class SkipOnEmptyTest extends MigrateProcessTestCase {
/**
* @covers ::process
* @expectedException \Drupal\migrate\MigrateSkipProcessException
*/
public function testProcessSkipsOnEmpty() {
$configuration['method'] = 'process';
(new SkipOnEmpty($configuration, 'skip_on_empty', []))
->transform('', $this->migrateExecutable, $this->row, 'destinationproperty');
}
/**
* @covers ::process
*/
public function testProcessBypassesOnNonEmpty() {
$configuration['method'] = 'process';
$value = (new SkipOnEmpty($configuration, 'skip_on_empty', []))
->transform(' ', $this->migrateExecutable, $this->row, 'destinationproperty');
$this->assertSame($value, ' ');
}
/**
* @covers ::row
* @expectedException \Drupal\migrate\MigrateSkipRowException
*/
public function testRowSkipsOnEmpty() {
$configuration['method'] = 'row';
(new SkipOnEmpty($configuration, 'skip_on_empty', []))
->transform('', $this->migrateExecutable, $this->row, 'destinationproperty');
}
/**
* @covers ::row
*/
public function testRowBypassesOnNonEmpty() {
$configuration['method'] = 'row';
$value = (new SkipOnEmpty($configuration, 'skip_on_empty', []))
->transform(' ', $this->migrateExecutable, $this->row, 'destinationproperty');
$this->assertSame($value, ' ');
}
}

View file

@ -0,0 +1,92 @@
<?php
/**
* @file
* Contains \Drupal\Tests\migrate\Unit\process\StaticMapTest.
*/
namespace Drupal\Tests\migrate\Unit\process;
use Drupal\migrate\Plugin\migrate\process\StaticMap;
/**
* Tests the static map process plugin.
*
* @group migrate
*/
class StaticMapTest extends MigrateProcessTestCase {
/**
* {@inheritdoc}
*/
protected function setUp() {
$this->row = $this->getMockBuilder('Drupal\migrate\Row')
->disableOriginalConstructor()
->getMock();
$this->migrateExecutable = $this->getMockBuilder('Drupal\migrate\MigrateExecutable')
->disableOriginalConstructor()
->getMock();
$configuration['map']['foo']['bar'] = 'baz';
$this->plugin = new StaticMap($configuration, 'map', array());
parent::setUp();
}
/**
* Tests map when the source is a string.
*/
public function testMapWithSourceString() {
$value = $this->plugin->transform('foo', $this->migrateExecutable, $this->row, 'destinationproperty');
$this->assertSame($value, array('bar' => 'baz'));
}
/**
* Tests map when the source is a list.
*/
public function testMapWithSourceList() {
$value = $this->plugin->transform(array('foo', 'bar'), $this->migrateExecutable, $this->row, 'destinationproperty');
$this->assertSame($value, 'baz');
}
/**
* Tests when the source is empty.
*
* @expectedException \Drupal\migrate\MigrateException
*/
public function testMapwithEmptySource() {
$this->plugin->transform(array(), $this->migrateExecutable, $this->row, 'destinationproperty');
}
/**
* Tests when the source is invalid.
*
* @expectedException \Drupal\migrate\MigrateSkipRowException
*/
public function testMapwithInvalidSource() {
$this->plugin->transform(array('bar'), $this->migrateExecutable, $this->row, 'destinationproperty');
}
/**
* Tests when the source is invalid but there's a default.
*/
public function testMapWithInvalidSourceWithADefaultValue() {
$configuration['map']['foo']['bar'] = 'baz';
$configuration['default_value'] = 'test';
$this->plugin = new StaticMap($configuration, 'map', array());
$value = $this->plugin->transform(array('bar'), $this->migrateExecutable, $this->row, 'destinationproperty');
$this->assertSame($value, 'test');
}
/**
* Tests when the source is invalid and bypass is enabled.
*
* @expectedException \Drupal\migrate\MigrateException
* @expectedExceptionMessage Setting both default_value and bypass is invalid.
*/
public function testMapWithInvalidSourceAndBypass() {
$configuration['map']['foo']['bar'] = 'baz';
$configuration['default_value'] = 'test';
$configuration['bypass'] = TRUE;
$this->plugin = new StaticMap($configuration, 'map', array());
$this->plugin->transform(array('bar'), $this->migrateExecutable, $this->row, 'destinationproperty');
}
}