class MigrateTaxonomyTermStubTest

Same name and namespace in other branches
  1. 11.x core/modules/taxonomy/tests/src/Kernel/Migrate/MigrateTaxonomyTermStubTest.php \Drupal\Tests\taxonomy\Kernel\Migrate\MigrateTaxonomyTermStubTest
  2. 10 core/modules/taxonomy/tests/src/Kernel/Migrate/MigrateTaxonomyTermStubTest.php \Drupal\Tests\taxonomy\Kernel\Migrate\MigrateTaxonomyTermStubTest
  3. 8.9.x core/modules/taxonomy/tests/src/Kernel/Migrate/MigrateTaxonomyTermStubTest.php \Drupal\Tests\taxonomy\Kernel\Migrate\MigrateTaxonomyTermStubTest

Test stub creation for taxonomy terms.

@group taxonomy

Hierarchy

Expanded class hierarchy of MigrateTaxonomyTermStubTest

File

core/modules/taxonomy/tests/src/Kernel/Migrate/MigrateTaxonomyTermStubTest.php, line 16

Namespace

Drupal\Tests\taxonomy\Kernel\Migrate
View source
class MigrateTaxonomyTermStubTest extends MigrateDrupalTestBase {
  use StubTestTrait;
  
  /**
   * {@inheritdoc}
   */
  protected static $modules = [
    'taxonomy',
    'text',
    'taxonomy_term_stub_test',
  ];
  
  /**
   * {@inheritdoc}
   */
  protected function setUp() : void {
    parent::setUp();
    $this->installEntitySchema('taxonomy_term');
  }
  
  /**
   * Tests creation of taxonomy term stubs.
   */
  public function testStub() {
    Vocabulary::create([
      'vid' => 'test_vocabulary',
      'name' => 'Test vocabulary',
    ])->save();
    $this->performStubTest('taxonomy_term');
  }
  
  /**
   * Tests creation of stubs when weight is mapped.
   */
  public function testStubWithWeightMapping() {
    // Create a vocabulary via migration for the terms to reference.
    $vocabulary_data_rows = [
      [
        'id' => '1',
        'name' => 'tags',
      ],
    ];
    $ids = [
      'id' => [
        'type' => 'integer',
      ],
    ];
    $definition = [
      'migration_tags' => [
        'Stub test',
      ],
      'source' => [
        'plugin' => 'embedded_data',
        'data_rows' => $vocabulary_data_rows,
        'ids' => $ids,
      ],
      'process' => [
        'vid' => 'id',
        'name' => 'name',
      ],
      'destination' => [
        'plugin' => 'entity:taxonomy_vocabulary',
      ],
    ];
    $vocabulary_migration = \Drupal::service('plugin.manager.migration')->createStubMigration($definition);
    $vocabulary_executable = new MigrateExecutable($vocabulary_migration, $this);
    $vocabulary_executable->import();
    // We have a term referencing an unmigrated parent, forcing a stub to be
    // created.
    $migration = $this->getMigration('taxonomy_term_stub_test');
    $term_executable = new MigrateExecutable($migration, $this);
    $term_executable->import();
    $this->assertNotEmpty($migration->getIdMap()
      ->getRowBySource([
      '2',
    ]), 'Stub row exists in the ID map table');
    // Load the referenced term, which should exist as a stub.
    /** @var \Drupal\Core\Entity\ContentEntityBase $stub_entity */
    $stub_entity = Term::load(2);
    $this->assertNotEmpty($stub_entity, 'Stub successfully created');
    if ($stub_entity) {
      $this->assertCount(0, $stub_entity->validate(), 'Stub is a valid entity');
    }
  }

}

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