<?php
namespace Drupal\tmgmt\Tests;
use Drupal\comment\Tests\CommentTestTrait;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\taxonomy\Entity\Term;
use Drupal\taxonomy\Entity\Vocabulary;
use Drupal\taxonomy\VocabularyInterface;
@trigger_error('The ' . __NAMESPACE__ . '\\EntityTestBase is deprecated. Instead, use \\Drupal\\Tests\\tmgmt\\Functional\\TmgmtEntityTestTrait. See https://www.drupal.org/node/2971931.', E_USER_DEPRECATED);
abstract class EntityTestBase extends TMGMTTestBase {
use CommentTestTrait;
public $field_names = array();
function createNodeType($machine_name, $human_name, $translation = FALSE, $attach_fields = TRUE) {
$type = $this
->drupalCreateContentType(array(
'type' => $machine_name,
'name' => $human_name,
));
$this->field_names['node'][$machine_name][] = 'body';
if (\Drupal::hasService('content_translation.manager') && $translation) {
$content_translation_manager = \Drupal::service('content_translation.manager');
$content_translation_manager
->setEnabled('node', $machine_name, TRUE);
}
$this
->applySchemaUpdates();
if ($attach_fields) {
$this
->attachFields('node', $machine_name, $translation);
}
else {
$body = FieldConfig::loadByName('node', $machine_name, 'body');
$body
->setTranslatable(TRUE);
$body
->save();
}
}
function createTaxonomyVocab($machine_name, $human_name, $fields_translatable = TRUE) {
$vocabulary = Vocabulary::create([
'name' => $human_name,
'vid' => $machine_name,
]);
$vocabulary
->save();
$this
->attachFields('taxonomy_term', $vocabulary
->id(), $fields_translatable);
return $vocabulary;
}
function attachFields($entity_name, $bundle, $translatable = TRUE) {
$field_types = array(
'text',
'text_with_summary',
);
for ($i = 0; $i <= 5; $i++) {
$field_type = $field_types[array_rand($field_types, 1)];
$field_name = mb_strtolower($this
->randomMachineName());
$field_storage = FieldStorageConfig::create(array(
'field_name' => $field_name,
'entity_type' => $entity_name,
'type' => $field_type,
'cardinality' => mt_rand(1, 5),
'translatable' => is_array($translatable) && isset($translatable[$i]) ? $translatable[$i] : (bool) $translatable,
));
$field_storage
->save();
$field = FieldConfig::create(array(
'field_name' => $field_name,
'entity_type' => $entity_name,
'bundle' => $bundle,
'label' => $this
->randomMachineName(10),
'description' => $this
->randomString(30),
'widget' => array(
'type' => $field_type == 'text' ? 'text_textfield' : 'text_textarea_with_summary',
'label' => $this
->randomString(10),
),
));
$field
->save();
$this->field_names[$entity_name][$bundle][] = $field_name;
}
}
protected function createTranslatableNode($bundle, $sourcelang = 'en') {
$node = array(
'type' => $bundle,
'langcode' => $sourcelang,
);
foreach ($this->field_names['node'][$bundle] as $field_name) {
if ($field_storage_config = FieldStorageConfig::loadByName('node', $field_name)) {
$cardinality = $field_storage_config
->getCardinality() == FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED ? 1 : $field_storage_config
->getCardinality();
for ($delta = 0; $delta <= $cardinality; $delta++) {
$node[$field_name][$delta]['value'] = $this
->randomMachineName(20);
$node[$field_name][$delta]['format'] = 'plain_text';
if ($field_storage_config
->getType() == 'text_with_summary') {
$node[$field_name][$delta]['summary'] = $this
->randomMachineName(10);
}
}
}
}
return $this
->drupalCreateNode($node);
}
function createTaxonomyTerm(VocabularyInterface $vocabulary) {
$term = Term::create(array(
'name' => $this
->randomMachineName(),
'description' => $this
->randomMachineName(),
'vid' => $vocabulary
->id(),
'langcode' => 'en',
));
foreach ($this->field_names['taxonomy_term'][$vocabulary
->id()] as $field_name) {
$field_definition = $term
->getFieldDefinition($field_name);
$cardinality = $field_definition
->getFieldStorageDefinition()
->getCardinality() == FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED ? 1 : $field_definition
->getCardinality();
$field_lang = $field_definition
->isTranslatable() ? 'en' : LanguageInterface::LANGCODE_DEFAULT;
for ($delta = 0; $delta <= $cardinality; $delta++) {
$term
->getTranslation($field_lang)
->get($field_name)
->get($delta)->value = $this
->randomMachineName(20);
if ($field_definition
->getType() == 'text_with_summary') {
$term
->getTranslation($field_lang)
->get($field_name)
->get($delta)->summary = $this
->randomMachineName(10);
}
}
}
$term
->save();
return $term;
}
protected function applySchemaUpdates() {
drupal_static_reset();
\Drupal::entityTypeManager()
->clearCachedDefinitions();
\Drupal::service('router.builder')
->rebuild();
\Drupal::service('entity.definition_update_manager')
->applyUpdates();
}
}