View source
<?php
namespace Drupal\Tests\migrate\Kernel;
use Drupal\migrate\MigrateException;
use Drupal\Tests\node\Traits\ContentTypeCreationTrait;
class MigrateLookupTest extends MigrateTestBase {
use ContentTypeCreationTrait;
protected static $modules = [
'system',
'node',
'field',
'user',
'text',
'migrate_lookup_test',
];
protected $migrateLookup;
protected function setUp() : void {
parent::setUp();
$this
->setTestLogger();
$this->migrateLookup = $this->container
->get('migrate.lookup');
$this
->installEntitySchema('node');
$this
->installEntitySchema('user');
$this
->installConfig([
'node',
'user',
]);
$this
->createContentType([
'type' => 'node_lookup',
]);
}
public function testSingleLookup() {
$this
->executeMigration('sample_lookup_migration');
$result = $this->migrateLookup
->lookup('sample_lookup_migration', [
17,
]);
$this
->assertSame('1', $result[0]['nid']);
$result = $this->migrateLookup
->lookup('sample_lookup_migration', [
'id' => 25,
]);
$this
->assertSame('2', $result[0]['nid']);
$result = $this->migrateLookup
->lookup('sample_lookup_migration', [
1337,
]);
$this
->assertSame([], $result);
}
public function testInvalidIdLookup() {
$this
->executeMigration('sample_lookup_migration');
$this
->expectException(MigrateException::class);
$this
->expectExceptionMessage("Extra unknown items for map migrate_map_sample_lookup_migration in source IDs: array (\n 'invalid_id' => 25,\n)");
$this->migrateLookup
->lookup('sample_lookup_migration', [
'invalid_id' => 25,
]);
}
public function testMultipleSourceIds() {
$this
->executeMigration('sample_lookup_migration_multiple_source_ids');
$result = $this->migrateLookup
->lookup('sample_lookup_migration_multiple_source_ids', [
25,
26,
]);
$this
->assertCount(1, $result);
$this
->assertSame('3', $result[0]['nid']);
$result = $this->migrateLookup
->lookup('sample_lookup_migration_multiple_source_ids', [
'id' => 17,
'version_id' => 17,
]);
$this
->assertCount(1, $result);
$this
->assertSame('1', $result[0]['nid']);
$result = $this->migrateLookup
->lookup('sample_lookup_migration_multiple_source_ids', [
'version_id' => 26,
'id' => 25,
]);
$this
->assertCount(1, $result);
$this
->assertSame('3', $result[0]['nid']);
$result = $this->migrateLookup
->lookup('sample_lookup_migration_multiple_source_ids', [
25,
]);
$this
->assertCount(2, $result);
$this
->assertSame('2', $result[0]['nid']);
$this
->assertSame('3', $result[1]['nid']);
$result = $this->migrateLookup
->lookup('sample_lookup_migration_multiple_source_ids', [
'version_id' => 25,
]);
$this
->assertCount(1, $result);
$this
->assertSame('2', $result[0]['nid']);
}
public function testMultipleMigrationLookup() {
$migrations = [
'sample_lookup_migration',
'sample_lookup_migration_2',
];
foreach ($migrations as $migration) {
$this
->executeMigration($migration);
}
$result = $this->migrateLookup
->lookup($migrations, [
17,
]);
$this
->assertSame('1', $result[0]['nid']);
$result = $this->migrateLookup
->lookup($migrations, [
'id' => 35,
]);
$this
->assertSame('4', $result[0]['nid']);
$result = $this->migrateLookup
->lookup($migrations, [
1337,
]);
$this
->assertSame([], $result);
}
public function testLookupWithStringIds() {
$this
->executeMigration('sample_lookup_migration_string_ids');
$result = $this->migrateLookup
->lookup('sample_lookup_migration_string_ids', [
'node1',
]);
$this
->assertSame('10', $result[0]['nid']);
$result = $this->migrateLookup
->lookup('sample_lookup_migration_string_ids', [
'id' => 'node2',
]);
$this
->assertSame('11', $result[0]['nid']);
$result = $this->migrateLookup
->lookup('sample_lookup_migration_string_ids', [
'node1337',
]);
$this
->assertSame([], $result);
}
}