You are here

trait StubTestTrait in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 core/modules/migrate_drupal/src/Tests/StubTestTrait.php \Drupal\migrate_drupal\Tests\StubTestTrait

Provides common functionality for testing stubbing.

Hierarchy

9 files declare their use of StubTestTrait
MigrateAggregatorStubTest.php in core/modules/aggregator/src/Tests/Migrate/MigrateAggregatorStubTest.php
Contains \Drupal\aggregator\Tests\Migrate\MigrateAggregatorStubTest.
MigrateBlockContentStubTest.php in core/modules/block_content/src/Tests/Migrate/MigrateBlockContentStubTest.php
Contains \Drupal\block_content\Tests\Migrate\MigrateBlockContentStubTest.
MigrateCommentStubTest.php in core/modules/comment/src/Tests/Migrate/MigrateCommentStubTest.php
Contains \Drupal\comment\Tests\Migrate\MigrateCommentStubTest.
MigrateFileStubTest.php in core/modules/file/src/Tests/Migrate/MigrateFileStubTest.php
Contains \Drupal\file\Tests\Migrate\MigrateFileStubTest.
MigrateMenuLinkContentStubTest.php in core/modules/menu_link_content/src/Tests/Migrate/MigrateMenuLinkContentStubTest.php
Contains \Drupal\menu_link_content\Tests\Migrate\MigrateMenuLinkContentStubTest.

... See full list

File

core/modules/migrate_drupal/src/Tests/StubTestTrait.php, line 15
Contains \Drupal\migrate_drupal\Tests\StubTestTrait.

Namespace

Drupal\migrate_drupal\Tests
View source
trait StubTestTrait {

  /**
   * Test that creating a stub of the given entity type results in a valid
   * entity.
   *
   * @param string $entity_type_id
   *   The entity type we are stubbing.
   */
  protected function performStubTest($entity_type_id) {
    $entity_id = $this
      ->createStub($entity_type_id);
    $this
      ->assertTrue($entity_id, 'Stub successfully created');
    if ($entity_id) {
      $violations = $this
        ->validateStub($entity_type_id, $entity_id);
      if (!$this
        ->assertIdentical(count($violations), 0, 'Stub is a valid entity')) {
        foreach ($violations as $violation) {
          $this
            ->fail((string) $violation
            ->getMessage());
        }
      }
    }
  }

  /**
   * Create a stub of the given entity type.
   *
   * @param string $entity_type_id
   *   The entity type we are stubbing.
   *
   * @return int
   *   ID of the created entity.
   */
  protected function createStub($entity_type_id) {

    // Create a dummy migration to pass to the destination plugin.
    $config = [
      'id' => 'dummy',
      'migration_tags' => [
        'Stub test',
      ],
      'source' => [
        'plugin' => 'empty',
      ],
      'process' => [],
      'destination' => [
        'plugin' => 'entity:' . $entity_type_id,
      ],
    ];
    $migration = Migration::create($config);
    $destination_plugin = $migration
      ->getDestinationPlugin(TRUE);
    $stub_row = new Row([], [], TRUE);
    $destination_ids = $destination_plugin
      ->import($stub_row);
    return reset($destination_ids);
  }

  /**
   * Perform validation on a stub entity.
   *
   * @param string $entity_type_id
   *   The entity type we are stubbing.
   * @param string $entity_id
   *   ID of the stubbed entity to validate.
   *
   * @return \Drupal\Core\Entity\EntityConstraintViolationListInterface
   *   List of constraint violations identified.
   */
  protected function validateStub($entity_type_id, $entity_id) {
    $controller = \Drupal::entityManager()
      ->getStorage($entity_type_id);

    /** @var \Drupal\Core\Entity\ContentEntityInterface $stub_entity */
    $stub_entity = $controller
      ->load($entity_id);
    return $stub_entity
      ->validate();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
StubTestTrait::createStub protected function Create a stub of the given entity type.
StubTestTrait::performStubTest protected function Test that creating a stub of the given entity type results in a valid entity.
StubTestTrait::validateStub protected function Perform validation on a stub entity.