paragraphs_sets-8.x-2.x-dev/tests/src/Traits/ParagraphsSetsFunctionalTestTrait.php
tests/src/Traits/ParagraphsSetsFunctionalTestTrait.php
<?php
namespace Drupal\Tests\paragraphs_sets\Traits;
use Drupal\Core\Serialization\Yaml;
use Drupal\paragraphs_sets\Entity\ParagraphsSet;
use Drupal\Tests\paragraphs\FunctionalJavascript\ParagraphsTestBaseTrait;
/**
* Contains functions common to functional paragraphs sets tests.
*/
trait ParagraphsSetsFunctionalTestTrait {
use ParagraphsTestBaseTrait;
/**
* Create a node entity bundle (content type).
*
* @param string $node_type
* The machine name for the node type.
*/
public function addNodeType(string $node_type): void {
$this->drupalCreateContentType(['type' => $node_type, 'name' => $node_type]);
}
/**
* Create a paragraph reference field in a given node bundle.
*
* @param string $paragraph_ref_field_name
* The machine name for the paragraph reference field.
* @param string $node_type
* The machine name for the node type.
*/
public function addParagraphRefFieldInNodeType(string $paragraph_ref_field_name, string $node_type): void {
$this->addParagraphsField($node_type, $paragraph_ref_field_name, 'node', 'paragraphs');
/** @var \Drupal\Core\Entity\Entity\EntityFormDisplay $form_display */
$form_display = \Drupal::service('entity_display.repository')->getFormDisplay('node', $node_type);
$display_settings = $form_display->getComponent($paragraph_ref_field_name);
$display_settings['third_party_settings']['paragraphs_sets']['paragraphs_sets']['use_paragraphs_sets'] = 1;
$form_display->setComponent($paragraph_ref_field_name, $display_settings);
$form_display->save();
}
/**
* Create a text field in a given paragraph bundle.
*
* @param string $text_field_name
* The machine name for the text field.
* @param string $paragraph_type
* The machine name for the paragraph type.
*/
public function addTextFieldInParagraphType(string $text_field_name, string $paragraph_type): void {
$this->addFieldtoParagraphType($paragraph_type, $text_field_name, 'string', []);
}
/**
* Create a paragraphs set.
*
* @param string $paragraph_set
* The machine name for the new paragraphs set.
* @param string $config
* Configuration for the new paragraphs set. Defaults to an empty set.
*/
public function addParagraphSet(string $paragraph_set, string $config = 'paragraphs: []'): void {
/** @var array<string, string|int|array<mixed>> $decoded_config */
$decoded_config = Yaml::decode($config);
$paragraphs_set = ParagraphsSet::create([
'id' => $paragraph_set,
'label' => $paragraph_set,
'description' => $paragraph_set,
'paragraphs' => $decoded_config['paragraphs'],
]);
$paragraphs_set->save();
}
}
