Upgraded drupal core with security updates
[yaffs-website] / web / core / modules / taxonomy / tests / src / Kernel / Migrate / MigrateTaxonomyTermStubTest.php
1 <?php
2
3 namespace Drupal\Tests\taxonomy\Kernel\Migrate;
4
5 use Drupal\migrate\MigrateExecutable;
6 use Drupal\Tests\migrate_drupal\Kernel\MigrateDrupalTestBase;
7 use Drupal\migrate_drupal\Tests\StubTestTrait;
8 use Drupal\taxonomy\Entity\Term;
9 use Drupal\taxonomy\Entity\Vocabulary;
10
11 /**
12  * Test stub creation for taxonomy terms.
13  *
14  * @group taxonomy
15  */
16 class MigrateTaxonomyTermStubTest extends MigrateDrupalTestBase {
17
18   use StubTestTrait;
19
20   /**
21    * {@inheritdoc}
22    */
23   public static $modules = ['taxonomy', 'text', 'taxonomy_term_stub_test'];
24
25   /**
26    * {@inheritdoc}
27    */
28   protected function setUp() {
29     parent::setUp();
30     $this->installEntitySchema('taxonomy_term');
31   }
32
33   /**
34    * Tests creation of taxonomy term stubs.
35    */
36   public function testStub() {
37     Vocabulary::create([
38       'vid' => 'test_vocabulary',
39       'name' => 'Test vocabulary',
40     ])->save();
41     $this->performStubTest('taxonomy_term');
42   }
43
44   /**
45    * Tests creation of stubs when weight is mapped.
46    */
47   public function testStubWithWeightMapping() {
48     // Create a vocabulary via migration for the terms to reference.
49     $vocabulary_data_rows = [
50       ['id' => '1', 'name' => 'tags'],
51     ];
52     $ids = ['id' => ['type' => 'integer']];
53     $definition = [
54       'migration_tags' => ['Stub test'],
55       'source' => [
56         'plugin' => 'embedded_data',
57         'data_rows' => $vocabulary_data_rows,
58         'ids' => $ids,
59       ],
60       'process' => [
61         'vid' => 'id',
62         'name' => 'name',
63       ],
64       'destination' => ['plugin' => 'entity:taxonomy_vocabulary'],
65     ];
66     $vocabulary_migration = \Drupal::service('plugin.manager.migration')->createStubMigration($definition);
67     $vocabulary_executable = new MigrateExecutable($vocabulary_migration, $this);
68     $vocabulary_executable->import();
69
70     // We have a term referencing an unmigrated parent, forcing a stub to be
71     // created.
72     $migration = $this->getMigration('taxonomy_term_stub_test');
73     $term_executable = new MigrateExecutable($migration, $this);
74     $term_executable->import();
75     $this->assertTrue($migration->getIdMap()->getRowBySource(['2']), 'Stub row exists in the ID map table');
76
77     // Load the referenced term, which should exist as a stub.
78     /** @var \Drupal\Core\Entity\ContentEntityBase $stub_entity */
79     $stub_entity = Term::load(2);
80     $this->assertTrue($stub_entity, 'Stub successfully created');
81     if ($stub_entity) {
82       $this->assertIdentical(count($stub_entity->validate()), 0, 'Stub is a valid entity');
83     }
84   }
85
86 }