reviewer-1.2.x-dev/tests/src/Traits/ReviewerCreationTrait.php
tests/src/Traits/ReviewerCreationTrait.php
<?php
declare(strict_types=1);
namespace Drupal\Tests\reviewer\Traits;
use Drupal\Tests\node\Traits\ContentTypeCreationTrait;
/**
* Trait to create reviews for functional tests.
*/
trait ReviewerCreationTrait {
use ContentTypeCreationTrait;
/**
* Passes.
*
* @var \Drupal\node\NodeTypeInterface[]
*/
private array $contentTypePass = [];
/**
* Failures.
*
* @var \Drupal\node\NodeTypeInterface[]
*/
private array $contentTypeFail = [];
/**
* Create test content types.
*
* @throws \Exception
*/
private function createContentTypes(): void {
$field = $this->container
->get('entity_type.manager')
->getStorage('field_storage_config')
->load('node.field_link');
if (!$field) {
$field = $this->container
->get('entity_type.manager')
->getStorage('field_storage_config')
->create([
'field_name' => 'field_link',
'entity_type' => 'node',
'type' => 'link',
]);
$field->save();
}
$this->contentTypePass = [
$this->createContentType(),
$this->createContentType(),
];
foreach ($this->contentTypePass as $pass) {
$this->container
->get('entity_type.manager')
->getStorage('field_config')
->create([
'field_storage' => $field,
'bundle' => $pass->id(),
])
->save();
$this->container
->get('entity_display.repository')
->getViewDisplay('node', (string) $pass->id())
->setComponent($field->getName(), [
'label' => 'hidden',
'settings' => [
'trim_length' => NULL,
],
])
->save();
}
$this->contentTypeFail = [
$this->createContentType(),
$this->createContentType(),
];
foreach ($this->contentTypeFail as $fail) {
$this
->container
->get('entity_type.manager')
->getStorage('field_config')
->create([
'field_storage' => $field,
'bundle' => $fail->id(),
])
->save();
$this->container
->get('entity_display.repository')
->getViewDisplay('node', (string) $fail->id())
->setComponent('field_link', [
'label' => 'above',
'settings' => [
'trim_length' => 80,
],
])
->save();
}
}
}
