View source
<?php
namespace Drupal\sitemap\Tests;
use Drupal\taxonomy\Entity\Vocabulary;
use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Tests\field\Traits\EntityReferenceTestTrait;
use Drupal\Tests\taxonomy\Traits\TaxonomyTestTrait;
abstract class SitemapTaxonomyTestBase extends SitemapBrowserTestBase {
use TaxonomyTestTrait;
use EntityReferenceTestTrait;
protected $defaultTheme = 'stark';
public static $modules = [
'sitemap',
'node',
'taxonomy',
'views',
];
protected $vocabulary;
protected $fieldTagsName;
protected $terms;
public $user;
protected function setUp() {
parent::setUp();
if ($this->profile != 'standard') {
$this
->drupalCreateContentType([
'type' => 'article',
'name' => 'Article',
]);
}
$this->vocabulary = $this
->createVocabulary();
$this->user = $this
->drupalCreateUser([
'administer sitemap',
'access sitemap',
'administer nodes',
'create article content',
'administer taxonomy',
]);
$this
->drupalLogin($this->user);
$vid = $this->vocabulary
->id();
$this
->saveSitemapForm([
"plugins[vocabulary:{$vid}][enabled]" => TRUE,
]);
}
protected function createTerms(Vocabulary $vocabulary) {
$term0 = $this
->createTerm($vocabulary);
$term1 = $this
->createTerm($vocabulary);
$term2 = $this
->createTerm($vocabulary);
return [
$term0,
$term1,
$term2,
];
}
protected function createNestedTerms(Vocabulary $vocabulary) {
$term0 = $this
->createTerm($vocabulary);
$term1 = $this
->createTerm($vocabulary, [
'parent' => $term0
->id(),
]);
$term2 = $this
->createTerm($vocabulary, [
'parent' => $term1
->id(),
]);
return [
$term0,
$term1,
$term2,
];
}
protected function createNodeWithTerms(array $terms = []) {
if (empty($terms)) {
$this->terms = $this
->createTerms($this->vocabulary);
}
$this
->addEntityreferenceField();
$values = [];
foreach ($terms as $term) {
$values[] = $term
->getName();
}
$title = $this
->randomString();
$edit = [
'title[0][value]' => $title,
$this->fieldTagsName . '[target_id]' => implode(',', $values),
];
$this
->drupalPostForm('node/add/article', $edit, t('Save'));
}
protected function addEntityreferenceField() {
$this->fieldTagsName = 'field_' . $this->vocabulary
->id();
$handler_settings = [
'target_bundles' => [
$this->vocabulary
->id() => $this->vocabulary
->id(),
],
'auto_create' => TRUE,
];
$this
->createEntityReferenceField('node', 'article', $this->fieldTagsName, 'Tags', 'taxonomy_term', 'default', $handler_settings, FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
EntityFormDisplay::load('node.article.default')
->setComponent($this->fieldTagsName, [
'type' => 'entity_reference_autocomplete_tags',
])
->save();
}
}