View source
<?php
namespace Drupal\Tests\migrate\Kernel;
use Drupal\field\Entity\FieldConfig;
use Drupal\Tests\node\Traits\ContentTypeCreationTrait;
class MigrateStubTest extends MigrateTestBase {
use ContentTypeCreationTrait;
protected static $modules = [
'system',
'node',
'field',
'user',
'text',
'filter',
'migrate_stub_test',
];
protected $migrateStub;
protected $migrateLookup;
protected $migrationPluginManager;
protected function setUp() : void {
parent::setUp();
$this
->setTestLogger();
$this->migrateStub = $this->container
->get('migrate.stub');
$this->migrateLookup = $this->container
->get('migrate.lookup');
$this->migrationPluginManager = $this->container
->get('plugin.manager.migration');
$this
->installEntitySchema('node');
$this
->installEntitySchema('user');
$this
->installSchema('node', 'node_access');
$this
->installConfig([
'node',
'user',
]);
$this
->createContentType([
'type' => 'node_lookup',
]);
}
public function testCreateStub() {
$this
->assertSame([], $this->migrateLookup
->lookup('sample_stubbing_migration', [
17,
]));
$ids = $this->migrateStub
->createStub('sample_stubbing_migration', [
17,
]);
$this
->assertSame([
$ids,
], $this->migrateLookup
->lookup('sample_stubbing_migration', [
17,
]));
$this
->assertNotNull(\Drupal::entityTypeManager()
->getStorage('node')
->load($ids['nid']));
}
public function testCreateStubRawReturn() {
$this
->assertSame([], $this->migrateLookup
->lookup('sample_stubbing_migration', [
17,
]));
$ids = $this->migrateStub
->createStub('sample_stubbing_migration', [
17,
], [], FALSE);
$this
->assertSame($ids, [
$this->migrateLookup
->lookup('sample_stubbing_migration', [
17,
])[0]['nid'],
]);
$this
->assertNotNull(\Drupal::entityTypeManager()
->getStorage('node')
->load($ids[0]));
}
public function testStubWithDefaultValues() {
$this
->assertSame([], $this->migrateLookup
->lookup('sample_stubbing_migration', [
17,
]));
$ids = $this->migrateStub
->createStub('sample_stubbing_migration', [
17,
], [
'title' => "Placeholder for source id 17",
]);
$this
->assertSame([
$ids,
], $this->migrateLookup
->lookup('sample_stubbing_migration', [
17,
]));
$node = \Drupal::entityTypeManager()
->getStorage('node')
->load($ids['nid']);
$this
->assertNotNull($node);
$this
->assertSame("Placeholder for source id 17", $node
->label());
$this
->executeMigration('sample_stubbing_migration');
$node = \Drupal::entityTypeManager()
->getStorage('node')
->loadUnchanged($ids['nid']);
$this
->assertSame("Sample 1", $node
->label());
}
public function testStubWithBundleFields() {
$this
->createContentType([
'type' => 'node_stub',
]);
$body_field = FieldConfig::loadByName('node', 'node_stub', 'body');
$body_field
->setRequired(TRUE)
->save();
$this
->assertSame([], $this->migrateLookup
->lookup('sample_stubbing_migration', [
33,
]));
$ids = $this->migrateStub
->createStub('sample_stubbing_migration', [
33,
], []);
$this
->assertSame([
$ids,
], $this->migrateLookup
->lookup('sample_stubbing_migration', [
33,
]));
$node = \Drupal::entityTypeManager()
->getStorage('node')
->load($ids['nid']);
$this
->assertNotNull($node);
$this
->assertNotEmpty($node
->get('body')->value);
}
public function testInvalidSourceIdCount() {
$this
->expectException(\InvalidArgumentException::class);
$this
->expectExceptionMessage('Expected and provided source id counts do not match.');
$this->migrateStub
->createStub('sample_stubbing_migration_with_multiple_source_ids', [
17,
]);
}
public function testInvalidSourceIdKeys() {
$this
->expectException(\InvalidArgumentException::class);
$this
->expectExceptionMessage("'version_id' is defined as a source ID but has no value.");
$this->migrateStub
->createStub('sample_stubbing_migration_with_multiple_source_ids', [
'id' => 17,
'not_a_key' => 17,
]);
}
}