View source
<?php
namespace Drupal\Tests\acquia_contenthub\Kernel;
class PreExistingTermMultipleParentImportExportTest extends ImportExportTestBase {
protected $fixtures = [
[
'cdf' => 'taxonomy_term/taxonomy_term-multiple_parent.json',
'expectations' => 'expectations/node/node_term_page.php',
],
];
public static $modules = [
'system',
'taxonomy',
'user',
'node',
'field',
'depcalc',
'acquia_contenthub',
'acquia_contenthub_subscriber',
];
protected $termValues;
protected $terms;
protected function setUp() {
parent::setUp();
$this
->installEntitySchema('user');
$this
->installEntitySchema('node');
$this
->installEntitySchema('taxonomy_term');
$this
->installSchema('acquia_contenthub_subscriber', 'acquia_contenthub_subscriber_import_tracking');
$values00 = [
'langcode' => 'en',
'status' => TRUE,
'name' => 'Category',
'vid' => 'category',
'description' => 'Category',
'hierarchy' => 1,
'weight' => 0,
];
$vocab = $this->entityTypeManager
->getStorage('taxonomy_vocabulary')
->create($values00);
$vocab
->save();
$values0 = [
'name' => 'Category 1',
'vid' => $vocab
->id(),
'parent' => [],
];
$values1 = [
'name' => 'Category 1 - 1',
'vid' => $vocab
->id(),
'parent' => [
0,
],
];
$values2 = [
'name' => 'Category 2',
'vid' => $vocab
->id(),
'parent' => [],
];
$values3 = [
'name' => 'Category 2 - 1',
'vid' => $vocab
->id(),
'parent' => [
2,
],
];
$values4 = [
'name' => 'Category Mixed',
'vid' => $vocab
->id(),
'parent' => [
1,
3,
],
];
$this->termValues = [
0 => $values0,
1 => $values1,
2 => $values2,
3 => $values3,
4 => $values4,
];
}
public function createTerms(array $keys) {
foreach ($keys as $key) {
$values = $this->termValues[$key];
$parents = $values['parent'];
if (!empty($parents)) {
$term_parents = [];
foreach ($parents as $parent) {
$parent_name = $this->termValues[$parent]['name'];
$term_parent = $this->entityTypeManager
->getStorage('taxonomy_term')
->loadByProperties([
'name' => $parent_name,
]);
$term_parent = reset($term_parent);
if (!empty($term_parent)) {
$term_parents[] = $term_parent
->id();
}
}
$values['parent'] = $term_parents;
}
$term = $this->entityTypeManager
->getStorage('taxonomy_term')
->create($values);
$term
->save();
$this->terms[$key] = $term;
}
}
public function verifyTaxonomyTerm($key, array $imported_uuids, $imported = FALSE) {
$name = $this->termValues[$key]['name'];
$terms = $this->entityTypeManager
->getStorage('taxonomy_term')
->loadByProperties([
'name' => $name,
]);
$this
->assertEquals(1, count($terms));
$term = reset($terms);
if ($imported) {
$this
->assertTrue(in_array($term
->uuid(), $imported_uuids), "Failed to assert that UUID for term '{$term->label()}' ({$term->uuid()}) was found in list of imported UUIDs.");
}
else {
$this
->assertFalse(in_array($term
->uuid(), $imported_uuids), "Failed to assert that UUID for term '{$term->label()}' ({$term->uuid()}) was not found in list of imported UUIDs.");
}
$expected_parents = [];
$parent_keys = $this->termValues[$key]['parent'];
foreach ($parent_keys as $parent_key) {
$expected_parents[] = $this->termValues[$parent_key]['name'];
}
$parents = $this->entityTypeManager
->getStorage('taxonomy_term')
->loadParents($term
->id());
$actual_parents = [];
foreach ($parents as $parent) {
$actual_parents[] = $parent
->label();
}
$this
->assertEquals($expected_parents, $actual_parents);
}
public function testTermImportExport1() {
$terms = [
0,
1,
4,
];
$this
->createTerms($terms);
$this
->assertEquals(3, count($this->terms), 'Created 3 local taxonomy terms.');
$this
->importFixture(0);
$document = $this
->createCdfDocumentFromFixture(0);
$imported_uuids = array_keys($document
->getEntities());
$this
->verifyTaxonomyTerm(0, $imported_uuids, FALSE);
$this
->verifyTaxonomyTerm(1, $imported_uuids, FALSE);
$this
->verifyTaxonomyTerm(2, $imported_uuids, TRUE);
$this
->verifyTaxonomyTerm(3, $imported_uuids, TRUE);
$this
->verifyTaxonomyTerm(4, $imported_uuids, FALSE);
}
public function testTermImportExport2() {
$terms = [
0,
1,
2,
3,
4,
];
$this
->createTerms($terms);
$this
->assertEquals(5, count($this->terms), 'Created 5 local taxonomy terms.');
$this
->importFixture(0);
$document = $this
->createCdfDocumentFromFixture(0);
$imported_uuids = array_keys($document
->getEntities());
$this
->verifyTaxonomyTerm(0, $imported_uuids, FALSE);
$this
->verifyTaxonomyTerm(1, $imported_uuids, FALSE);
$this
->verifyTaxonomyTerm(2, $imported_uuids, FALSE);
$this
->verifyTaxonomyTerm(3, $imported_uuids, FALSE);
$this
->verifyTaxonomyTerm(4, $imported_uuids, FALSE);
}
}