View source
<?php
namespace Drupal\Tests\yaml_content\Functional;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Tests\field\Traits\EntityReferenceTestTrait;
use Drupal\Tests\BrowserTestBase;
class NodeImportTest extends BrowserTestBase {
use EntityReferenceTestTrait;
protected $defaultTheme = 'stark';
protected $contentDirectory;
protected $contentLoader;
protected static $modules = [
'taxonomy',
'node',
'field',
'user',
'filter',
'text',
'yaml_content',
];
public function setUp() {
parent::setUp();
$this
->createContentType([
'type' => 'article',
'name' => 'Article',
]);
$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_node.content.yml');
$this
->assertTrue(is_array($entities), 'An array was not returned from loadContent().');
$this
->assertEquals(1, count($entities), 'No entity IDs were returned from loadContent().');
$entity = reset($entities);
$this
->assertEquals('node', $entity
->getEntityTypeId(), 'The entity type created was not a Node.');
$this
->assertEquals('article', $entity
->bundle(), 'An article Node was not correctly created.');
$this
->assertEquals('Basic Article', $entity
->label(), 'An article\'s title was not correctly assigned.');
$body_value = $entity
->get('body')
->get(0)
->getValue();
$this
->assertEquals('full_html', $body_value['format'], 'Body field format was not correctly assigned to "full_html".');
$expected_content = <<<END_OF_VALUE
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed vobis
voluptatum perceptarum recordatio vitam beatam facit, et quidem corpore
perceptarum. Tum Quintus: Est plane, Piso, ut dicis, inquit.</p>
<p>Primum cur ista res digna odio est, nisi quod est turpis? Duo Reges:
constructio interrete. Rhetorice igitur, inquam, nos mavis quam
dialectice disputare?</p>
END_OF_VALUE;
$this
->assertEquals($expected_content, $body_value['value'], 'Body field content was not correctly assigned.');
return $entity;
}
public function testFancyNode() {
$this
->setupTaxonomyField();
$entities = $this->contentLoader
->loadContent('fancy_node.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().');
$tag = $entities[0];
$node = $entities[1];
$tags = $node
->get('field_tags');
$this
->assertEquals([
'target_id' => $tag
->id(),
], $tags
->get(0)
->getValue(), 'Existing tag is connected.');
$this
->assertNull($tags
->get(1), 'Missing tag is not created.');
}
protected function setupTaxonomyField() {
$field_name = 'field_tags';
$handler_settings = [
'target_bundles' => [
'tags' => 'tags',
],
'auto_create' => TRUE,
];
$this
->createEntityReferenceField('node', 'article', $field_name, 'Tags', 'taxonomy_term', 'default', $handler_settings, FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
$entity_type_manager = $this->container
->get('entity_type.manager');
$entity_type_manager
->getStorage('entity_form_display')
->load('node.article.default')
->setComponent($field_name, [
'type' => 'entity_reference_autocomplete_tags',
'weight' => -4,
])
->save();
}
}