3 namespace Drupal\migrate_drupal\Tests;
5 use Drupal\migrate\Row;
8 * Provides common functionality for testing stubbing.
13 * Test that creating a stub of the given entity type results in a valid
16 * @param string $entity_type_id
17 * The entity type we are stubbing.
19 protected function performStubTest($entity_type_id) {
20 $entity_id = $this->createStub($entity_type_id);
21 $this->assertTrue($entity_id, 'Stub successfully created');
23 $violations = $this->validateStub($entity_type_id, $entity_id);
24 if (!$this->assertIdentical(count($violations), 0, 'Stub is a valid entity')) {
25 foreach ($violations as $violation) {
26 $this->fail((string) $violation->getMessage());
33 * Create a stub of the given entity type.
35 * @param string $entity_type_id
36 * The entity type we are stubbing.
39 * ID of the created entity.
41 protected function createStub($entity_type_id) {
42 // Create a dummy migration to pass to the destination plugin.
44 'migration_tags' => ['Stub test'],
45 'source' => ['plugin' => 'empty'],
47 'destination' => ['plugin' => 'entity:' . $entity_type_id],
49 $migration = \Drupal::service('plugin.manager.migration')->createStubMigration($definition);
50 $destination_plugin = $migration->getDestinationPlugin(TRUE);
51 $stub_row = new Row([], [], TRUE);
52 $destination_ids = $destination_plugin->import($stub_row);
53 return reset($destination_ids);
57 * Perform validation on a stub entity.
59 * @param string $entity_type_id
60 * The entity type we are stubbing.
61 * @param string $entity_id
62 * ID of the stubbed entity to validate.
64 * @return \Drupal\Core\Entity\EntityConstraintViolationListInterface
65 * List of constraint violations identified.
67 protected function validateStub($entity_type_id, $entity_id) {
68 $controller = \Drupal::entityManager()->getStorage($entity_type_id);
69 /** @var \Drupal\Core\Entity\ContentEntityInterface $stub_entity */
70 $stub_entity = $controller->load($entity_id);
71 return $stub_entity->validate();