annotate core/modules/migrate_drupal/src/Tests/StubTestTrait.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 4c8ae668cc8c
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\migrate_drupal\Tests;
Chris@0 4
Chris@0 5 use Drupal\migrate\Row;
Chris@0 6
Chris@0 7 /**
Chris@0 8 * Provides common functionality for testing stubbing.
Chris@0 9 */
Chris@0 10 trait StubTestTrait {
Chris@0 11
Chris@0 12 /**
Chris@0 13 * Test that creating a stub of the given entity type results in a valid
Chris@0 14 * entity.
Chris@0 15 *
Chris@0 16 * @param string $entity_type_id
Chris@0 17 * The entity type we are stubbing.
Chris@0 18 */
Chris@0 19 protected function performStubTest($entity_type_id) {
Chris@0 20 $entity_id = $this->createStub($entity_type_id);
Chris@0 21 $this->assertTrue($entity_id, 'Stub successfully created');
Chris@0 22 if ($entity_id) {
Chris@0 23 $violations = $this->validateStub($entity_type_id, $entity_id);
Chris@0 24 if (!$this->assertIdentical(count($violations), 0, 'Stub is a valid entity')) {
Chris@0 25 foreach ($violations as $violation) {
Chris@0 26 $this->fail((string) $violation->getMessage());
Chris@0 27 }
Chris@0 28 }
Chris@0 29 }
Chris@0 30 }
Chris@0 31
Chris@0 32 /**
Chris@0 33 * Create a stub of the given entity type.
Chris@0 34 *
Chris@0 35 * @param string $entity_type_id
Chris@0 36 * The entity type we are stubbing.
Chris@0 37 *
Chris@0 38 * @return int
Chris@0 39 * ID of the created entity.
Chris@0 40 */
Chris@0 41 protected function createStub($entity_type_id) {
Chris@0 42 // Create a dummy migration to pass to the destination plugin.
Chris@0 43 $definition = [
Chris@0 44 'migration_tags' => ['Stub test'],
Chris@0 45 'source' => ['plugin' => 'empty'],
Chris@0 46 'process' => [],
Chris@0 47 'destination' => ['plugin' => 'entity:' . $entity_type_id],
Chris@0 48 ];
Chris@0 49 $migration = \Drupal::service('plugin.manager.migration')->createStubMigration($definition);
Chris@0 50 $destination_plugin = $migration->getDestinationPlugin(TRUE);
Chris@0 51 $stub_row = new Row([], [], TRUE);
Chris@0 52 $destination_ids = $destination_plugin->import($stub_row);
Chris@0 53 return reset($destination_ids);
Chris@0 54 }
Chris@0 55
Chris@0 56 /**
Chris@0 57 * Perform validation on a stub entity.
Chris@0 58 *
Chris@0 59 * @param string $entity_type_id
Chris@0 60 * The entity type we are stubbing.
Chris@0 61 * @param string $entity_id
Chris@0 62 * ID of the stubbed entity to validate.
Chris@0 63 *
Chris@0 64 * @return \Drupal\Core\Entity\EntityConstraintViolationListInterface
Chris@0 65 * List of constraint violations identified.
Chris@0 66 */
Chris@0 67 protected function validateStub($entity_type_id, $entity_id) {
Chris@0 68 $controller = \Drupal::entityManager()->getStorage($entity_type_id);
Chris@0 69 /** @var \Drupal\Core\Entity\ContentEntityInterface $stub_entity */
Chris@0 70 $stub_entity = $controller->load($entity_id);
Chris@0 71 return $stub_entity->validate();
Chris@0 72 }
Chris@0 73
Chris@0 74 }