View source
<?php
namespace Drupal\Tests\blazy\Traits;
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Entity\Entity\EntityViewDisplay;
use Drupal\node\Entity\NodeType;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\file\Entity\File;
use Drupal\filter\Entity\FilterFormat;
use Drupal\image\Plugin\Field\FieldType\ImageItem;
trait BlazyCreationTestTrait {
protected $nodeType;
protected $fieldStorageDefinition;
protected $fieldDefinition;
protected function setUpFormatterDisplay($bundle = '', array $data = []) {
$view_mode = empty($data['view_mode']) ? 'default' : $data['view_mode'];
$plugin_id = empty($data['plugin_id']) ? $this->testPluginId : $data['plugin_id'];
$field_name = empty($data['field_name']) ? $this->testFieldName : $data['field_name'];
$settings = empty($data['settings']) ? [] : $data['settings'];
$display_id = $this->entityType . '.' . $bundle . '.' . $view_mode;
$storage = $this->blazyManager
->getEntityTypeManager()
->getStorage('entity_view_display');
$display = $storage
->load($display_id);
if (!$display) {
$values = [
'targetEntityType' => $this->entityType,
'bundle' => $bundle,
'mode' => $view_mode,
'status' => TRUE,
];
$display = $storage
->create($values);
}
$settings['current_view_mode'] = $settings['view_mode'] = $view_mode;
$display
->setComponent($field_name, [
'type' => $plugin_id,
'settings' => $settings,
'label' => 'hidden',
]);
$display
->save();
return $display;
}
protected function getBlazyFieldDefinition($field_name = '') {
if (!$this->fieldDefinition) {
$field_name = empty($field_name) ? $this->testFieldName : $field_name;
$field_storage_config = $this
->getBlazyFieldStorageDefinition($field_name);
if ($field_storage_config) {
$this->fieldDefinition = BaseFieldDefinition::createFromFieldStorageDefinition($field_storage_config);
}
}
return $this->fieldDefinition;
}
protected function getBlazyFieldStorageDefinition($field_name = '') {
if (!$this->fieldStorageDefinition) {
$field_name = empty($field_name) ? $this->testFieldName : $field_name;
$field_storage_definitions = $this->entityFieldManager
->getFieldStorageDefinitions($this->entityType);
$this->fieldStorageDefinition = isset($field_storage_definitions[$field_name]) ? $field_storage_definitions[$field_name] : NULL;
}
return $this->fieldStorageDefinition;
}
protected function getFormatterInstance($plugin_id = '', $field_name = '') {
$plugin_id = empty($plugin_id) ? $this->testPluginId : $plugin_id;
$field_name = empty($field_name) ? $this->testFieldName : $field_name;
$settings = $this
->getFormatterSettings() + $this->formatterPluginManager
->getDefaultSettings($plugin_id);
if (!$this
->getBlazyFieldDefinition($field_name)) {
return NULL;
}
$options = [
'field_definition' => $this
->getBlazyFieldDefinition($field_name),
'configuration' => [
'type' => $plugin_id,
'settings' => $settings,
],
'view_mode' => 'default',
];
return $this->formatterPluginManager
->getInstance($options);
}
protected function setUpContentTypeTest($bundle = '', array $settings = []) {
$node_type = NodeType::load($bundle);
$restricted_html = $this->blazyManager
->entityLoad('restricted_html', 'filter_format');
if (empty($node_type)) {
$node_type = NodeType::create([
'type' => $bundle,
'name' => $bundle,
]);
$node_type
->save();
}
if (!$restricted_html) {
FilterFormat::create([
'format' => 'restricted_html',
'name' => 'Basic HML',
'weight' => 2,
'filters' => [],
])
->save();
}
node_add_body_field($node_type);
if (!empty($this->testFieldName)) {
$settings['fields'][$this->testFieldName] = empty($this->testFieldType) ? 'image' : $this->testFieldType;
}
if (!empty($settings['field_name']) && !empty($settings['field_type'])) {
$settings['fields'][$settings['field_name']] = $settings['field_type'];
}
$data = [];
if (!empty($settings['fields'])) {
foreach ($settings['fields'] as $field_name => $field_type) {
$data['field_name'] = $field_name;
$data['field_type'] = $field_type;
$this
->setUpFieldConfig($bundle, $data);
}
}
$node_type
->save();
return $node_type;
}
protected function setUpContentWithItems($bundle = '', array $settings = []) {
$title = empty($settings['title']) ? $this->testPluginId : $settings['title'];
$data = empty($settings['values']) ? [] : $settings['values'];
$values = $data + [
'title' => $title . ' : ' . $this
->randomMachineName(),
'type' => $bundle,
'status' => TRUE,
];
$node = $this->blazyManager
->getEntityTypeManager()
->getStorage($this->entityType)
->create($values);
$node
->save();
if (isset($node->body)) {
$node->body->value = $this
->getRandomGenerator()
->paragraphs($this->maxParagraphs);
$node->body->format = 'restricted_html';
}
if (!empty($this->testFieldName)) {
$settings['fields'][$this->testFieldName] = empty($this->testFieldType) ? 'image' : $this->testFieldType;
}
if (!empty($settings['field_name']) && !empty($settings['field_type'])) {
$settings['fields'][$settings['field_name']] = $settings['field_type'];
}
if (!empty($settings['fields'])) {
foreach ($settings['fields'] as $field_name => $field_type) {
$multiple = $field_type == 'image' || strpos($field_name, 'mul') !== FALSE;
if (strpos($field_name, 'empty') !== FALSE) {
continue;
}
if (isset($this->entityFieldName) && $field_name == $this->entityFieldName) {
continue;
}
$max = $multiple ? $this->maxItems : 2;
if (isset($node->{$field_name})) {
$node->{$field_name}
->generateSampleItems($max);
}
}
}
$node
->save();
$this->testItems = $node->{$this->testFieldName};
$this->entity = $node;
return $node;
}
protected function setUpFieldConfig($bundle = '', array $data = []) {
$config = [];
$default = empty($this->testFieldType) ? 'image' : $this->testFieldType;
$field_type = empty($data['field_type']) ? $default : $data['field_type'];
$field_name = empty($data['field_name']) ? $this->testFieldName : $data['field_name'];
$multiple = strpos($field_name, 'mul') !== FALSE;
if (in_array($field_type, [
'file',
'image',
])) {
$config['file_directory'] = $this->testPluginId;
$config['file_extensions'] = 'png gif jpg jpeg';
if ($field_type == 'file') {
$config['file_extensions'] .= ' txt';
}
if ($field_type == 'image') {
$config['title_field'] = 1;
$config['title_field_required'] = 1;
}
$multiple = TRUE;
}
$field_storage = FieldStorageConfig::loadByName($this->entityType, $field_name);
$storage_settings = [];
if ($field_type == 'entity_reference') {
$storage_settings['target_type'] = isset($this->targetType) ? $this->targetType : $this->entityType;
$bundle = $this->bundle;
$multiple = FALSE;
}
if ($field_name == 'field_image') {
$multiple = FALSE;
}
if (!$field_storage) {
FieldStorageConfig::create([
'entity_type' => $this->entityType,
'field_name' => $field_name,
'type' => $field_type,
'cardinality' => $multiple ? -1 : 1,
'settings' => $storage_settings,
])
->save();
}
$field_config = FieldConfig::loadByName($this->entityType, $bundle, $field_name);
if ($field_type == 'entity_reference' && !empty($this->targetBundles)) {
$config['handler'] = 'default';
$config['handler_settings']['target_bundles'] = $this->targetBundles;
$config['handler_settings']['sort']['field'] = '_none';
$bundle = $this->bundle;
}
if (!$field_config) {
$field_config = FieldConfig::create([
'field_storage' => $field_storage,
'field_name' => $field_name,
'entity_type' => $this->entityType,
'bundle' => $bundle,
'label' => str_replace('_', ' ', $field_name),
'settings' => $config,
]);
$field_config
->save();
}
return $field_config;
}
protected function buildEntityReferenceRenderArray(array $referenced_entities, $type = '', array $settings = []) {
$type = empty($type) ? $this->entityPluginId : $type;
$items = $this->referencingEntity
->get($this->entityFieldName);
foreach ($referenced_entities as $referenced_entity) {
$items[] = [
'entity' => $referenced_entity,
];
}
$data['type'] = $type;
if ($settings) {
$data['settings'] = $settings;
}
return $items
->view($data);
}
protected function collectRenderDisplay(array $entity, array $settings = []) {
$view_mode = empty($settings['view_mode']) ? 'default' : $settings['view_mode'];
$display = EntityViewDisplay::collectRenderDisplay($entity, $view_mode);
return $display
->build($entity);
}
protected function setUpContentWithEntityReference(array $settings = []) {
$target_bundle = $this->targetBundle;
$bundle = $this->bundle;
$fields = empty($settings['fields']) ? [] : $settings['fields'];
$image_settings = empty($settings['image_settings']) ? [] : $settings['image_settings'];
$entity_settings = empty($settings['entity_settings']) ? [] : $settings['entity_settings'];
$er_field_name = empty($settings['entity_field_name']) ? $this->entityFieldName : $settings['entity_field_name'];
$er_plugin_id = empty($settings['entity_plugin_id']) ? $this->entityPluginId : $settings['entity_plugin_id'];
$referenced_data['title'] = 'Referenced ' . $this->testPluginId;
$referenced_data['fields'] = array_merge($this
->getDefaultFields(), $fields);
$this
->setUpContentTypeTest($target_bundle, $referenced_data);
$referencing_data['fields'] = [
$er_field_name => 'entity_reference',
];
$this
->setUpContentTypeTest($bundle, $referencing_data);
$referenced_formatter_link = [
'field_name' => 'field_link',
'plugin_id' => 'link',
'settings' => [],
];
$this
->setUpFormatterDisplay($target_bundle, $referenced_formatter_link);
$referenced_formatter_data = [
'field_name' => $this->testFieldName,
'plugin_id' => $this->testPluginId,
'settings' => $image_settings + $this
->getFormatterSettings(),
];
$this->referencedDisplay = $this
->setUpFormatterDisplay($target_bundle, $referenced_formatter_data);
$this->referencedEntity = $this
->setUpContentWithItems($target_bundle, $referenced_data);
$referencing_formatter_settings = $this
->getDefaultFields(TRUE);
$referencing_formatter_data = [
'field_name' => $er_field_name,
'plugin_id' => $er_plugin_id,
'settings' => empty($entity_settings) ? $referencing_formatter_settings : array_merge($referencing_formatter_settings, $entity_settings),
];
$this->referencingDisplay = $this
->setUpFormatterDisplay($bundle, $referencing_formatter_data);
}
protected function createReferencingEntity(array $data = []) {
if (empty($data['values']) && $this->referencedEntity
->id()) {
$data['values'] = [
$this->entityFieldName => [
[
'target_id' => $this->referencedEntity
->id(),
],
],
];
}
return $this
->setUpContentWithItems($this->bundle, $data);
}
protected function setUpRealImage() {
$this->uri = $this
->getImagePath();
$item = $this->dummyItem;
if (isset($this->testItems[0])) {
$item = $this->testItems[0];
if ($item instanceof ImageItem) {
$this->uri = ($entity = $item->entity) && empty($item->uri) ? $entity
->getFileUri() : $item->uri;
}
}
$this->testItem = $item;
$this->data = [
'settings' => $this
->getFormatterSettings(),
'item' => $item,
];
$this->imageFactory = $this->container
->get('image.factory');
}
protected function getImagePath($is_dir = FALSE) {
$path = $this->root . '/sites/default/files/simpletest/' . $this->testPluginId;
$item = $this
->createDummyImage();
$uri = ($entity = $item->entity) && empty($item->uri) ? $entity
->getFileUri() : $item->uri;
$this->dummyUri = $uri;
$this->dummyItem = $item;
$this->dummyData = [
'settings' => $this
->getFormatterSettings(),
'item' => $item,
];
return $is_dir ? $path : $uri;
}
protected function createDummyImage($name = '', $source = '') {
$path = $this->root . '/sites/default/files/simpletest/' . $this->testPluginId;
$name = empty($name) ? $this->testPluginId . '.png' : $name;
$source = empty($source) ? $this->root . '/core/misc/druplicon.png' : $source;
$uri = $path . '/' . $name;
if (!is_file($uri)) {
$this
->prepareTestDirectory();
$this->fileSystem
->saveData($source, $uri, FileSystemInterface::EXISTS_REPLACE);
}
$uri = 'public://simpletest/' . $this->testPluginId . '/' . $name;
$item = File::create([
'uri' => $uri,
'uid' => 1,
'status' => FILE_STATUS_PERMANENT,
'filename' => $name,
]);
$item
->save();
return $item;
}
protected function prepareTestDirectory() {
$this->testDirPath = $this->root . '/sites/default/files/simpletest/' . $this->testPluginId;
$this->fileSystem
->prepareDirectory($this->testDirPath, FileSystemInterface::CREATE_DIRECTORY);
}
}