TaxonomyImportTest.php in YAML Content 8
File
tests/src/Functional/TaxonomyImportTest.php
View source
<?php
namespace Drupal\Tests\yaml_content\Functional;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Tests\BrowserTestBase;
class TaxonomyImportTest extends BrowserTestBase {
protected $defaultTheme = 'stark';
protected $contentDirectory;
protected $contentLoader;
protected static $modules = [
'taxonomy',
'yaml_content',
];
public function setUp() {
parent::setUp();
$this->contentLoader = \Drupal::service('yaml_content.content_loader');
$this->contentLoader
->setContentPath(drupal_get_path('module', 'yaml_content') . '/tests');
}
public function testCanCreateNode() {
$entities = $this->contentLoader
->loadContent('basic_taxonomy.content.yml');
$this
->assertTrue(is_array($entities), 'An array was not returned from loadContent().');
$this
->assertEquals(2, count($entities), 'No entity IDs were returned from loadContent().');
$this
->assertTaxonomy($entities[0], 'Generated content', 'tags');
$this
->assertEquals([
'target_id' => 0,
], $entities[0]->parent
->get(0)
->getValue(), 'Unspecified parent should be root. (0)');
$this
->assertTaxonomy($entities[1], 'Imported demo content', 'tags');
$this
->assertEquals([
'target_id' => $entities[1]
->id() - 1,
], $entities[1]->parent
->get(0)
->getValue(), 'Specified parent reference should be populated with a id.');
}
protected function assertTaxonomy(EntityInterface $entity, $title, $vid) {
$this
->assertEquals('taxonomy_term', $entity
->getEntityTypeId(), 'Entity type should be taxonomy_term');
$this
->assertEquals([
'target_id' => $vid,
], $entity
->get('vid')
->get(0)
->getValue(), 'Vocabulary id is populated');
$this
->assertEquals($title, $entity
->label(), 'Term name is populated.');
}
}