3 namespace Drupal\Tests\blazy\Traits;
5 use Drupal\Core\Field\BaseFieldDefinition;
6 use Drupal\Core\Entity\Entity\EntityViewDisplay;
7 use Drupal\node\Entity\NodeType;
8 use Drupal\field\Entity\FieldConfig;
9 use Drupal\field\Entity\FieldStorageConfig;
10 use Drupal\file\Entity\File;
11 use Drupal\filter\Entity\FilterFormat;
12 use Drupal\image\Plugin\Field\FieldType\ImageItem;
15 * A Trait common for Blazy tests.
17 * @todo: Consider using ContentTypeCreationTrait, TestFileCreationTrait.
19 trait BlazyCreationTestTrait {
24 * @var \Drupal\node\Entity\NodeType
29 * The field storage definition.
31 * @var \Drupal\field\FieldStorageConfigInterface
33 protected $fieldStorageDefinition;
36 * The field definition.
38 * @var \Drupal\Core\Field\FieldDefinitionInterface
40 protected $fieldDefinition;
43 * Setup formatter displays, default to image, and update its settings.
45 * @param string $bundle
48 * May contain formatter settings to be added to defaults.
50 * @return \Drupal\Core\Entity\Entity\EntityViewDisplay
51 * The formatter display instance.
53 protected function setUpFormatterDisplay($bundle = '', array $data = []) {
54 $view_mode = empty($data['view_mode']) ? 'default' : $data['view_mode'];
55 $plugin_id = empty($data['plugin_id']) ? $this->testPluginId : $data['plugin_id'];
56 $field_name = empty($data['field_name']) ? $this->testFieldName : $data['field_name'];
57 $settings = empty($data['settings']) ? [] : $data['settings'];
58 $display_id = $this->entityType . '.' . $bundle . '.' . $view_mode;
59 $storage = $this->blazyManager->getEntityTypeManager()->getStorage('entity_view_display');
60 $display = $storage->load($display_id);
64 'targetEntityType' => $this->entityType,
70 $display = $storage->create($values);
73 $settings['current_view_mode'] = $settings['view_mode'] = $view_mode;
74 $display->setComponent($field_name, [
76 'settings' => $settings,
86 * Gets the field definition.
88 * @param string $field_name
89 * Formatted field name.
91 * @return \Drupal\Core\Field\FieldDefinitionInterface
92 * The field definition.
94 * @see BaseFieldDefinition::createFromFieldStorageDefinition()
96 protected function getBlazyFieldDefinition($field_name = '') {
97 if (!$this->fieldDefinition) {
98 $field_name = empty($field_name) ? $this->testFieldName : $field_name;
99 $field_storage_config = $this->getBlazyFieldStorageDefinition($field_name);
101 $this->fieldDefinition = BaseFieldDefinition::createFromFieldStorageDefinition($field_storage_config);
103 return $this->fieldDefinition;
107 * Gets the field storage configuration.
109 * @param string $field_name
110 * Formatted field name.
112 * @return \Drupal\field\FieldStorageConfigInterface
113 * The field storage definition.
115 protected function getBlazyFieldStorageDefinition($field_name = '') {
116 if (!$this->fieldStorageDefinition) {
117 $field_name = empty($field_name) ? $this->testFieldName : $field_name;
118 $field_storage_definitions = $this->entityFieldManager->getFieldStorageDefinitions($this->entityType);
119 $this->fieldStorageDefinition = $field_storage_definitions[$field_name];
121 return $this->fieldStorageDefinition;
125 * Returns the field formatter instance.
127 * @param string $plugin_id
128 * Formatter plugin ID.
129 * @param string $field_name
130 * Formatted field name.
132 * @return \Drupal\Core\Field\FormatterInterface|null
133 * The field formatter instance.
135 protected function getFormatterInstance($plugin_id = '', $field_name = '') {
136 $plugin_id = empty($plugin_id) ? $this->testPluginId : $plugin_id;
137 $field_name = empty($field_name) ? $this->testFieldName : $field_name;
138 $settings = $this->getFormatterSettings() + $this->formatterPluginManager->getDefaultSettings($plugin_id);
141 'field_definition' => $this->getBlazyFieldDefinition($field_name),
143 'type' => $plugin_id,
144 'settings' => $settings,
146 'view_mode' => 'default',
149 return $this->formatterPluginManager->getInstance($options);
153 * Build dummy content types.
155 * @param string $bundle
157 * @param array $settings
158 * (Optional) configurable settings.
160 protected function setUpContentTypeTest($bundle = '', array $settings = []) {
161 $node_type = NodeType::load($bundle);
162 $restricted_html = $this->blazyManager->entityLoad('restricted_html', 'filter_format');
164 if (empty($node_type)) {
165 $node_type = NodeType::create([
172 if (!$restricted_html) {
173 FilterFormat::create([
174 'format' => 'restricted_html',
175 'name' => 'Basic HML',
181 node_add_body_field($node_type);
183 if (!empty($this->testFieldName)) {
184 $settings['fields'][$this->testFieldName] = empty($this->testFieldType) ? 'image' : $this->testFieldType;
186 if (!empty($settings['field_name']) && !empty($settings['field_type'])) {
187 $settings['fields'][$settings['field_name']] = $settings['field_type'];
191 foreach ($settings['fields'] as $field_name => $field_type) {
192 $data['field_name'] = $field_name;
193 $data['field_type'] = $field_type;
194 $this->setUpFieldConfig($bundle, $data);
203 * Build dummy nodes with optional fields.
205 * @param string $bundle
207 * @param array $settings
208 * (Optional) configurable settings.
210 * @return \Drupal\node\Entity\Node|null
213 protected function setUpContentWithItems($bundle = '', array $settings = []) {
214 $title = empty($settings['title']) ? $this->testPluginId : $settings['title'];
215 $data = empty($settings['values']) ? [] : $settings['values'];
218 'title' => $title . ' : ' . $this->randomMachineName(),
223 $node = $this->blazyManager->getEntityTypeManager()
224 ->getStorage($this->entityType)
229 if (isset($node->body)) {
230 $node->body->value = $this->getRandomGenerator()->paragraphs($this->maxParagraphs);
231 $node->body->format = 'restricted_html';
234 if (!empty($this->testFieldName)) {
235 $settings['fields'][$this->testFieldName] = empty($this->testFieldType) ? 'image' : $this->testFieldType;
237 if (!empty($settings['field_name']) && !empty($settings['field_type'])) {
238 $settings['fields'][$settings['field_name']] = $settings['field_type'];
241 if (!empty($settings['fields'])) {
242 foreach ($settings['fields'] as $field_name => $field_type) {
243 $multiple = $field_type == 'image' || strpos($field_name, 'mul') !== FALSE;
245 if (strpos($field_name, 'empty') !== FALSE) {
249 if (isset($this->entityFieldName) && ($field_name == $this->entityFieldName)) {
253 $max = $multiple ? $this->maxItems : 2;
254 $node->{$field_name}->generateSampleItems($max);
259 $this->testItems = $node->{$this->testFieldName};
260 $this->entity = $node;
266 * Setup a new image field.
268 * @param string $bundle
271 * (Optional) A list of field data.
273 protected function setUpFieldConfig($bundle = '', array $data = []) {
275 $default = empty($this->testFieldType) ? 'image' : $this->testFieldType;
276 $field_type = empty($data['field_type']) ? $default : $data['field_type'];
277 $field_name = empty($data['field_name']) ? $this->testFieldName : $data['field_name'];
278 $multiple = strpos($field_name, 'mul') !== FALSE;
280 if (in_array($field_type, ['file', 'image'])) {
281 $config['file_directory'] = $this->testPluginId;
282 $config['file_extensions'] = 'png gif jpg jpeg';
284 if ($field_type == 'file') {
285 $config['file_extensions'] .= ' txt';
288 if ($field_type == 'image') {
289 $config['title_field'] = 1;
290 $config['title_field_required'] = 1;
296 $field_storage = FieldStorageConfig::loadByName($this->entityType, $field_name);
298 $storage_settings = [];
299 if ($field_type == 'entity_reference') {
300 $storage_settings['target_type'] = isset($this->targetType) ? $this->targetType : $this->entityType;
301 $bundle = $this->bundle;
305 if ($field_name == 'field_image') {
309 if (!$field_storage) {
310 FieldStorageConfig::create([
311 'entity_type' => $this->entityType,
312 'field_name' => $field_name,
313 'type' => $field_type,
314 'cardinality' => $multiple ? -1 : 1,
315 'settings' => $storage_settings,
319 $field_config = FieldConfig::loadByName($this->entityType, $bundle, $field_name);
321 if ($field_type == 'entity_reference' && !empty($this->targetBundles)) {
322 $config['handler'] = 'default';
323 $config['handler_settings']['target_bundles'] = $this->targetBundles;
324 $config['handler_settings']['sort']['field'] = '_none';
325 $bundle = $this->bundle;
328 if (!$field_config) {
329 $field_config = FieldConfig::create([
330 'field_storage' => $field_storage,
331 'field_name' => $field_name,
332 'entity_type' => $this->entityType,
334 'label' => str_replace('_', ' ', $field_name),
335 'settings' => $config,
338 $field_config->save();
341 return $field_config;
345 * Sets field values as built by FieldItemListInterface::view().
347 * @param \Drupal\Core\Entity\EntityInterface[] $referenced_entities
348 * An array of entity objects that will be referenced.
349 * @param string $type
350 * The formatter plugin ID.
351 * @param array $settings
352 * Settings specific to the formatter. Defaults to the formatter's defaults.
357 protected function buildEntityReferenceRenderArray(array $referenced_entities, $type = '', array $settings = []) {
358 $type = empty($type) ? $this->entityPluginId : $type;
359 $items = $this->referencingEntity->get($this->entityFieldName);
361 // Assign the referenced entities.
362 foreach ($referenced_entities as $referenced_entity) {
363 $items[] = ['entity' => $referenced_entity];
366 // Build the renderable array for the field.
367 $data['type'] = $type;
369 $data['settings'] = $settings;
371 return $items->view($data);
375 * Sets field values as built by FieldItemListInterface::view().
377 * @param \Drupal\Core\Entity\EntityInterface[] $entity
378 * An entity object that will be displayed.
379 * @param array $settings
380 * Settings specific to the formatter. Defaults to the formatter's defaults.
385 protected function collectRenderDisplay(array $entity, array $settings = []) {
386 $view_mode = empty($settings['view_mode']) ? 'default' : $settings['view_mode'];
388 $display = EntityViewDisplay::collectRenderDisplay($entity, $view_mode);
390 return $display->build($entity);
394 * Build dummy contents with entity references.
396 * @param array $settings
397 * (Optional) configurable settings.
399 protected function setUpContentWithEntityReference(array $settings = []) {
400 $target_bundle = $this->targetBundle;
401 $bundle = $this->bundle;
402 $fields = empty($settings['fields']) ? [] : $settings['fields'];
403 $image_settings = empty($settings['image_settings']) ? [] : $settings['image_settings'];
404 $entity_settings = empty($settings['entity_settings']) ? [] : $settings['entity_settings'];
405 $er_field_name = empty($settings['entity_field_name']) ? $this->entityFieldName : $settings['entity_field_name'];
406 $er_plugin_id = empty($settings['entity_plugin_id']) ? $this->entityPluginId : $settings['entity_plugin_id'];
408 // Create referenced entity.
409 $referenced_data['title'] = 'Referenced ' . $this->testPluginId;
411 // Create dummy fields.
412 $referenced_data['fields'] = $fields + $this->getDefaultFields();
414 // Create referenced entity type.
415 $this->setUpContentTypeTest($target_bundle, $referenced_data);
417 // Create referencing entity type.
418 $referencing_data['fields'] = [
419 $er_field_name => 'entity_reference',
421 $this->setUpContentTypeTest($bundle, $referencing_data);
423 // 1. Build the referenced entities.
424 $referenced_formatter_link = [
425 'field_name' => 'field_link',
426 'plugin_id' => 'link',
429 $this->setUpFormatterDisplay($target_bundle, $referenced_formatter_link);
431 $referenced_formatter_data = [
432 'field_name' => $this->testFieldName,
433 'plugin_id' => $this->testPluginId,
434 'settings' => $image_settings + $this->getFormatterSettings(),
436 $this->referencedDisplay = $this->setUpFormatterDisplay($target_bundle, $referenced_formatter_data);
438 // Create referenced entities.
439 $this->referencedEntity = $this->setUpContentWithItems($target_bundle, $referenced_data);
441 // 2. Build the referencing entity.
442 $referencing_formatter_settings = $this->getDefaultFields(TRUE);
443 $referencing_formatter_data = [
444 'field_name' => $er_field_name,
445 'plugin_id' => $er_plugin_id,
446 'settings' => empty($entity_settings) ? $referencing_formatter_settings : array_merge($referencing_formatter_settings, $entity_settings),
448 $this->referencingDisplay = $this->setUpFormatterDisplay($bundle, $referencing_formatter_data);
452 * Create referencing entity.
454 protected function createReferencingEntity(array $data = []) {
455 if (empty($data['values']) && $this->referencedEntity->id()) {
457 $this->entityFieldName => [
458 ['target_id' => $this->referencedEntity->id()],
463 return $this->setUpContentWithItems($this->bundle, $data);
467 * Set up dummy image.
469 protected function setUpRealImage() {
470 $this->uri = $this->getImagePath();
471 $item = $this->dummyItem;
473 if (isset($this->testItems[0])) {
474 $item = $this->testItems[0];
476 if ($item instanceof ImageItem) {
477 $this->uri = ($entity = $item->entity) && empty($item->uri) ? $entity->getFileUri() : $item->uri;
481 $this->testItem = $item;
484 'settings' => $this->getFormatterSettings(),
488 $this->imageFactory = $this->container->get('image.factory');
492 * Returns path to the stored image location.
494 protected function getImagePath($is_dir = FALSE) {
495 $path = \Drupal::root() . '/sites/default/files/simpletest/' . $this->testPluginId;
496 $name = $this->testPluginId . '.png';
497 $source = \Drupal::root() . '/core/misc/druplicon.png';
498 $uri = $path . '/' . $name;
500 if (!is_file($uri)) {
501 file_prepare_directory($path, FILE_CREATE_DIRECTORY);
502 file_unmanaged_copy($source, $uri, FILE_EXISTS_REPLACE);
505 $item = File::create([
507 'uid' => \Drupal::currentUser()->id(),
508 'status' => FILE_STATUS_PERMANENT,
512 $uri = ($entity = $item->entity) && empty($item->uri) ? $entity->getFileUri() : $item->uri;
514 $this->dummyUri = $uri;
515 $this->dummyItem = $item;
518 'settings' => $this->getFormatterSettings(),
522 return $is_dir ? $path : $uri;