View source
<?php
namespace Drupal\Tests\commerce_migrate_csv_example\Kernel\Migrate;
use Drupal\Tests\commerce_migrate\Kernel\CommerceMigrateTestTrait;
use Drupal\Tests\commerce_migrate\Kernel\CsvTestBase;
use Drupal\taxonomy\Entity\Term;
use Drupal\taxonomy\TermInterface;
class TaxonomyTermTest extends CsvTestBase {
use CommerceMigrateTestTrait;
protected static $modules = [
'commerce_migrate_csv_example',
'taxonomy',
'text',
'user',
];
protected $fixtures = [
__DIR__ . '/../../../fixtures/csv/example-products.csv',
];
protected $treeData = [];
protected function setUp() : void {
parent::setUp();
$this
->installEntitySchema('taxonomy_term');
$this
->createVocabularies([
'Category',
'Season',
]);
$this
->executeMigration('csv_example_taxonomy_term');
}
protected function assertEntity($id, $expected_label, $expected_vid, $expected_description = '', $expected_format = NULL, $expected_weight = 0, array $expected_parents = [], $expected_field_integer_value = NULL, $expected_term_reference_tid = NULL, $expected_container_flag = FALSE) {
$entity = Term::load($id);
$this
->assertInstanceOf(TermInterface::class, $entity);
$this
->assertEquals($expected_label, $entity
->label());
$this
->assertEquals($expected_vid, $entity
->bundle());
$this
->assertEquals($expected_description, $entity
->getDescription());
$this
->assertEquals($expected_format, $entity
->getFormat());
$this
->assertEquals($expected_weight, $entity
->getWeight());
$this
->assertEquals($expected_parents, $this
->getParentIDs($id));
$this
->assertHierarchy($expected_vid, $id, $expected_parents);
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);
}
if ($entity
->hasField('forum_container')) {
$this
->assertEquals($expected_container_flag, $entity->forum_container->value);
}
}
public function testTaxonomyTerms() {
$this
->assertEntity(1, 'Wetsuit', 'category', '', NULL, 0);
$this
->assertEntity(2, 'Summer', 'season', '', NULL, 0);
$this
->assertEntity(3, 'Winter', 'season', '', NULL, 0);
$this
->assertEntity(4, 'Misc', 'category', '', NULL, 0);
$this
->assertEntity(5, 'Camera', 'category', '', NULL, 0);
$this
->assertEntity(6, 'All', 'season', '', NULL, 0);
}
protected function getParentIds($tid) {
return array_keys(\Drupal::entityTypeManager()
->getStorage('taxonomy_term')
->loadParents($tid));
}
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");
}
}