View source
<?php
namespace Drupal\Tests\entityconnect\Functional;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\Tests\BrowserTestBase;
abstract class EntityconnectTestBase extends BrowserTestBase {
public static $modules = [
'entityconnect',
'field_ui',
'block',
'node',
];
protected $defaultTheme = 'stark';
protected $adminUser;
protected $ecUser;
protected $testContentType;
protected $testRefField;
protected function setUp() {
parent::setUp();
$this->testContentType = $this
->drupalCreateContentType([
'type' => 'ec_test',
'name' => 'EC Test',
]);
$this
->drupalPlaceBlock('page_title_block');
$this->testRefField = $this
->addContentEntityReferenceField();
$this->adminUser = $this
->drupalCreateUser([
'administer entityconnect',
'administer site configuration',
'administer content types',
'administer node fields',
]);
$this
->drupalCreateRole([
'entityconnect add button',
], 'ec_add');
$this
->drupalCreateRole([
'entityconnect edit button',
], 'ec_edit');
$this->ecUser = $this
->drupalCreateUser([
"create {$this->testContentType->id()} content",
"edit any {$this->testContentType->id()} content",
]);
}
protected function addContentEntityReferenceField($field_name = 'entity_reference', $field_bundle = NULL, $target_bundles = NULL) {
$field_storage = FieldStorageConfig::loadByName('node', $field_name);
$field = FieldConfig::loadByName('node', $field_bundle ?? $this->testContentType
->id(), $field_name);
if (empty($field)) {
if (empty($field_storage)) {
$field_storage = FieldStorageConfig::create([
'field_name' => $field_name,
'entity_type' => 'node',
'type' => 'entity_reference',
'cardinality' => 1,
]);
$field_storage
->save();
}
$field = FieldConfig::create([
'field_storage' => $field_storage,
'label' => $field_name,
'bundle' => $field_bundle ?? $this->testContentType
->id(),
'settings' => [
'handler_settings' => [
'target_bundles' => $target_bundles ?? [
$this->testContentType
->id(),
],
],
],
]);
$field
->save();
$form_display = \Drupal::service('entity_display.repository')
->getFormDisplay('node', $field_bundle ?? $this->testContentType
->id(), 'default');
$form_display
->setComponent($field_name, [
'type' => 'options_select',
])
->save();
\Drupal::service('entity_display.repository')
->getViewDisplay('node', $field_bundle ?? $this->testContentType
->id())
->setComponent($field_name, [
'type' => 'entity_reference_label',
])
->save();
}
return $field;
}
protected function updateEntityReferenceFieldTargets(array $target_bundles = []) {
if (!$this->testRefField) {
$this->testRefField = $this
->addContentEntityReferenceField();
}
$handler_settings = $this->testRefField
->getSetting('handler_settings') ?? [];
$handler_settings['target_bundles'] = array_merge($handler_settings['target_bundles'], $target_bundles);
$this->testRefField
->setSetting('handler_settings', $handler_settings);
$this->testRefField
->save();
}
protected function setEcButtons($add = TRUE, $edit = TRUE) {
$this->testRefField
->setThirdPartySetting('entityconnect', 'buttons', [
'button_add' => !$add,
'button_edit' => !$edit,
]);
if (empty($this->testRefField
->getThirdPartySettings('entityconnect')['icons'])) {
$this->testRefField
->setThirdPartySetting('entityconnect', 'icons', [
'icon_add' => 0,
'icon_edit' => 0,
]);
}
$this->testRefField
->save();
}
}