MigrateTaxonomyTermTest.php 8.94 KB
Newer Older
1 2
<?php

3
namespace Drupal\Tests\taxonomy\Kernel\Migrate\d7;
4 5

use Drupal\taxonomy\Entity\Term;
6
use Drupal\Tests\migrate_drupal\Kernel\d7\MigrateDrupal7TestBase;
7 8 9 10 11 12 13 14 15
use Drupal\taxonomy\TermInterface;

/**
 * Upgrade taxonomy terms.
 *
 * @group taxonomy
 */
class MigrateTaxonomyTermTest extends MigrateDrupal7TestBase {

16
  public static $modules = [
17
    'comment',
18
    'content_translation',
19
    'datetime',
20
    'forum',
21
    'image',
22
    'language',
23
    'link',
24
    'menu_ui',
25 26
    // Required for translation migrations.
    'migrate_drupal_multilingual',
27 28 29 30 31
    'node',
    'taxonomy',
    'telephone',
    'text',
  ];
32

33 34 35 36 37 38 39
  /**
   * The cached taxonomy tree items, keyed by vid and tid.
   *
   * @var array
   */
  protected $treeData = [];

40 41 42 43 44
  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();
45 46
    $this->installEntitySchema('comment');
    $this->installEntitySchema('node');
47
    $this->installEntitySchema('taxonomy_term');
48 49 50
    $this->installConfig(static::$modules);

    $this->executeMigrations([
51 52 53
      'language',
      'd7_user_role',
      'd7_user',
54 55 56 57 58
      'd7_node_type',
      'd7_comment_type',
      'd7_field',
      'd7_taxonomy_vocabulary',
      'd7_field_instance',
59
      'd7_taxonomy_term',
60 61
      'd7_entity_translation_settings',
      'd7_taxonomy_term_entity_translation',
62
    ]);
63 64 65 66 67 68 69 70 71 72 73 74 75
  }

  /**
   * Validate a migrated term contains the expected values.
   *
   * @param $id
   *   Entity ID to load and check.
   * @param $expected_label
   *   The label the migrated entity should have.
   * @param $expected_vid
   *   The parent vocabulary the migrated entity should have.
   * @param string $expected_description
   *   The description the migrated entity should have.
76 77
   * @param string $expected_format
   *   The format the migrated entity should have.
78 79 80 81
   * @param int $expected_weight
   *   The weight the migrated entity should have.
   * @param array $expected_parents
   *   The parent terms the migrated entity should have.
82 83 84 85
   * @param int $expected_field_integer_value
   *   The value the migrated entity field should have.
   * @param int $expected_term_reference_tid
   *   The term reference id the migrated entity field should have.
86 87
   * @param bool $expected_container_flag
   *   The term should be a container entity.
88
   */
89
  protected function assertEntity($id, $expected_label, $expected_vid, $expected_description = '', $expected_format = NULL, $expected_weight = 0, $expected_parents = [], $expected_field_integer_value = NULL, $expected_term_reference_tid = NULL, $expected_container_flag = 0) {
90 91
    /** @var \Drupal\taxonomy\TermInterface $entity */
    $entity = Term::load($id);
92 93 94 95
    $this->assertInstanceOf(TermInterface::class, $entity);
    $this->assertEquals($expected_label, $entity->label());
    $this->assertEquals($expected_vid, $entity->bundle());
    $this->assertEquals($expected_description, $entity->getDescription());
96
    $this->assertEquals($expected_format, $entity->getFormat());
97 98
    $this->assertEquals($expected_weight, $entity->getWeight());
    $this->assertEquals($expected_parents, $this->getParentIDs($id));
99
    $this->assertHierarchy($expected_vid, $id, $expected_parents);
100 101 102 103 104 105 106 107
    if (!is_null($expected_field_integer_value)) {
      $this->assertTrue($entity->hasField('field_integer'));
      $this->assertEquals($expected_field_integer_value, $entity->field_integer->value);
    }
    if (!is_null($expected_term_reference_tid)) {
      $this->assertTrue($entity->hasField('field_integer'));
      $this->assertEquals($expected_term_reference_tid, $entity->field_term_reference->target_id);
    }
108 109 110
    if ($entity->hasField('forum_container')) {
      $this->assertEquals($expected_container_flag, $entity->forum_container->value);
    }
111 112 113 114 115 116
  }

  /**
   * Tests the Drupal 7 taxonomy term to Drupal 8 migration.
   */
  public function testTaxonomyTerms() {
117
    $this->assertEntity(1, 'General discussion', 'forums', '', NULL, 2);
118 119 120 121 122

    // Tests that terms that used the Drupal 7 Title module and that have their
    // name and description replaced by real fields are correctly migrated.
    $this->assertEntity(2, 'Term1 (This is a real field!)', 'test_vocabulary', 'The first term. (This is a real field!)', 'filtered_html', 0, [], NULL, 3);

123
    $this->assertEntity(3, 'Term2', 'test_vocabulary', 'The second term.', 'filtered_html');
124
    $this->assertEntity(4, 'Term3 in plain old English', 'test_vocabulary', 'The third term in plain old English.', 'full_html', 0, [3], 6);
125
    $this->assertEntity(5, 'Custom Forum', 'forums', 'Where the cool kids are.', NULL, 3);
126
    $this->assertEntity(6, 'Games', 'forums', '', NULL, 4, [], NULL, NULL, 1);
127 128
    $this->assertEntity(7, 'Minecraft', 'forums', '', NULL, 1, [6]);
    $this->assertEntity(8, 'Half Life 3', 'forums', '', NULL, 0, [6]);
129 130 131 132 133 134 135 136

    // Verify that we still can create forum containers after the migration.
    $term = Term::create(['vid' => 'forums', 'name' => 'Forum Container', 'forum_container' => 1]);
    $term->save();

    // Reset the forums tree data so this new term is included in the tree.
    unset($this->treeData['forums']);
    $this->assertEntity(19, 'Forum Container', 'forums', '', NULL, 0, [], NULL, NULL, 1);
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
  }

  /**
   * Retrieves the parent term IDs for a given term.
   *
   * @param $tid
   *   ID of the term to check.
   *
   * @return array
   *   List of parent term IDs.
   */
  protected function getParentIDs($tid) {
    return array_keys(\Drupal::entityManager()->getStorage('taxonomy_term')->loadParents($tid));
  }

152 153 154 155
  /**
   * Assert that a term is present in the tree storage, with the right parents.
   *
   * @param string $vid
156
   *   Vocabulary ID.
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
   * @param int $tid
   *   ID of the term to check.
   * @param array $parent_ids
   *   The expected parent term IDs.
   */
  protected function assertHierarchy($vid, $tid, array $parent_ids) {
    if (!isset($this->treeData[$vid])) {
      $tree = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadTree($vid);
      $this->treeData[$vid] = [];
      foreach ($tree as $item) {
        $this->treeData[$vid][$item->tid] = $item;
      }
    }

    $this->assertArrayHasKey($tid, $this->treeData[$vid], "Term $tid exists in taxonomy tree");
    $term = $this->treeData[$vid][$tid];
    $this->assertEquals($parent_ids, array_filter($term->parents), "Term $tid has correct parents in taxonomy tree");
  }

176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
  /**
   * Tests the migration of taxonomy term entity translations.
   */
  public function testTaxonomyTermEntityTranslations() {
    $manager = $this->container->get('content_translation.manager');

    // Get the term and its translations.
    $term = Term::load(4);
    $term_fr = $term->getTranslation('fr');
    $term_is = $term->getTranslation('is');

    // Test that fields translated with Entity Translation are migrated.
    $this->assertSame('Term3 in plain old English', $term->getName());
    $this->assertSame('Term3 en français s\'il vous plaît', $term_fr->getName());
    $this->assertSame('Term3 á íslensku', $term_is->getName());
    $this->assertSame('The third term in plain old English.', $term->getDescription());
    $this->assertSame('The third term en français s\'il vous plaît.', $term_fr->getDescription());
    $this->assertSame('The third term á íslensku.', $term_is->getDescription());
    $this->assertSame('full_html', $term->getFormat());
    $this->assertSame('filtered_html', $term_fr->getFormat());
    $this->assertSame('plain_text', $term_is->getFormat());
    $this->assertSame('6', $term->field_integer->value);
    $this->assertSame('5', $term_fr->field_integer->value);
    $this->assertSame('4', $term_is->field_integer->value);

    // Test that the French translation metadata is correctly migrated.
    $metadata_fr = $manager->getTranslationMetadata($term_fr);
    $this->assertTrue($metadata_fr->isPublished());
    $this->assertSame('en', $metadata_fr->getSource());
    $this->assertSame('2', $metadata_fr->getAuthor()->uid->value);
    $this->assertSame('1531922267', $metadata_fr->getCreatedTime());
    $this->assertSame('1531922268', $metadata_fr->getChangedTime());
    $this->assertTrue($metadata_fr->isOutdated());

    // Test that the Icelandic translation metadata is correctly migrated.
    $metadata_is = $manager->getTranslationMetadata($term_is);
    $this->assertFalse($metadata_is->isPublished());
    $this->assertSame('en', $metadata_is->getSource());
    $this->assertSame('1', $metadata_is->getAuthor()->uid->value);
    $this->assertSame('1531922278', $metadata_is->getCreatedTime());
    $this->assertSame('1531922279', $metadata_is->getChangedTime());
    $this->assertFalse($metadata_is->isOutdated());

    // Test that untranslatable properties are the same as the source language.
    $this->assertSame($term->bundle(), $term_fr->bundle());
    $this->assertSame($term->bundle(), $term_is->bundle());
    $this->assertSame($term->getWeight(), $term_fr->getWeight());
    $this->assertSame($term->getWeight(), $term_is->getWeight());
    $this->assertSame($term->parent->terget_id, $term_fr->parent->terget_id);
    $this->assertSame($term->parent->terget_id, $term_is->parent->terget_id);
  }

228
}