trait StubTestTrait

Same name in other branches
  1. 8.9.x core/modules/migrate_drupal/src/Tests/StubTestTrait.php \Drupal\migrate_drupal\Tests\StubTestTrait
  2. 10 core/modules/migrate_drupal/src/Tests/StubTestTrait.php \Drupal\migrate_drupal\Tests\StubTestTrait
  3. 11.x core/modules/migrate_drupal/src/Tests/StubTestTrait.php \Drupal\migrate_drupal\Tests\StubTestTrait

Provides common functionality for testing stubbing.

Hierarchy

10 files declare their use of StubTestTrait
MigrateAggregatorStubTest.php in core/modules/aggregator/tests/src/Kernel/Migrate/MigrateAggregatorStubTest.php
MigrateBlockContentStubTest.php in core/modules/block_content/tests/src/Kernel/Migrate/MigrateBlockContentStubTest.php
MigrateCommentStubTest.php in core/modules/comment/tests/src/Kernel/Migrate/MigrateCommentStubTest.php
MigrateEntityContentBaseTest.php in core/modules/migrate/tests/src/Kernel/MigrateEntityContentBaseTest.php
MigrateFileStubTest.php in core/modules/file/tests/src/Kernel/Migrate/MigrateFileStubTest.php

... See full list

File

core/modules/migrate_drupal/src/Tests/StubTestTrait.php, line 10

Namespace

Drupal\migrate_drupal\Tests
View source
trait StubTestTrait {
    
    /**
     * Tests that creating a stub of an 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->createEntityStub($entity_type_id);
        $this->assertNotEmpty($entity_id, 'Stub successfully created');
        // When validateStub fails, it will return an array with the violations.
        $this->assertEmpty($this->validateStub($entity_type_id, $entity_id));
    }
    
    /**
     * 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 createEntityStub($entity_type_id) {
        // Create a dummy migration to pass to the destination plugin.
        $definition = [
            'migration_tags' => [
                'Stub test',
            ],
            'source' => [
                'plugin' => 'empty',
            ],
            'process' => [],
            'destination' => [
                'plugin' => 'entity:' . $entity_type_id,
            ],
        ];
        $migration = \Drupal::service('plugin.manager.migration')->createStubMigration($definition);
        $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::entityTypeManager()->getStorage($entity_type_id);
        
        /** @var \Drupal\Core\Entity\ContentEntityInterface $stub_entity */
        $stub_entity = $controller->load($entity_id);
        return $stub_entity->validate();
    }

}

Members

Title Sort descending Modifiers Object type Summary
StubTestTrait::createEntityStub protected function Create a stub of the given entity type.
StubTestTrait::performStubTest protected function Tests that creating a stub of an entity type results in a valid entity.
StubTestTrait::validateStub protected function Perform validation on a stub entity.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.