<?php
namespace Drupal\Tests\paragraphs\Functional\WidgetLegacy;
use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\field_ui\Traits\FieldUiTestTrait;
use Drupal\Tests\paragraphs\FunctionalJavascript\ParagraphsTestBaseTrait;
use Drupal\Tests\paragraphs\Traits\ParagraphsCoreVersionUiTestTrait;
use Drupal\user\Entity\Role;
abstract class ParagraphsTestBase extends BrowserTestBase {
use FieldUiTestTrait, ParagraphsCoreVersionUiTestTrait, ParagraphsTestBaseTrait;
protected $admin_user = NULL;
protected $admin_permissions = [];
protected static $modules = [
'node',
'paragraphs',
'field',
'field_ui',
'block',
'paragraphs_test',
];
protected $defaultTheme = 'stark';
protected function setUp() : void {
parent::setUp();
$this
->placeDefaultBlocks();
$this->admin_permissions = [
'administer content types',
'administer node fields',
'administer paragraphs types',
'administer node form display',
'administer paragraph fields',
'administer paragraph form display',
'bypass node access',
];
}
function loginAsAdmin($additional_permissions = [], $reset_permissions = FALSE) {
$permissions = $this->admin_permissions;
if ($reset_permissions) {
$permissions = $additional_permissions;
}
elseif (!empty($additional_permissions)) {
$permissions = array_merge($permissions, $additional_permissions);
}
$this->admin_user = $this
->drupalCreateUser($permissions);
$this
->drupalLogin($this->admin_user);
return $this->admin_user;
}
protected function setAddMode($content_type, $paragraphs_field, $mode) {
$form_display = EntityFormDisplay::load('node.' . $content_type . '.default')
->setComponent($paragraphs_field, [
'type' => 'entity_reference_paragraphs',
'settings' => [
'add_mode' => $mode,
],
]);
$form_display
->save();
}
protected function setAllowedParagraphsTypes($content_type, $paragraphs_types, $selected, $paragraphs_field) {
$edit = [];
$this
->drupalGet('admin/structure/types/manage/' . $content_type . '/fields/node.' . $content_type . '.' . $paragraphs_field);
foreach ($paragraphs_types as $paragraphs_type) {
$edit['settings[handler_settings][target_bundles_drag_drop][' . $paragraphs_type . '][enabled]'] = $selected;
}
$this
->submitForm($edit, 'Save settings');
}
protected function setParagraphsTypeWeight($content_type, $paragraphs_type, $weight, $paragraphs_field) {
$this
->drupalGet('admin/structure/types/manage/' . $content_type . '/fields/node.' . $content_type . '.' . $paragraphs_field);
$edit['settings[handler_settings][target_bundles_drag_drop][' . $paragraphs_type . '][weight]'] = $weight;
$this
->submitForm($edit, 'Save settings');
}
protected function setDefaultParagraphType($content_type, $paragraphs_name, $paragraphs_field_name, $default_type) {
$this
->drupalGet('admin/structure/types/manage/' . $content_type . '/form-display');
$this
->submitForm([], $paragraphs_field_name);
$this
->submitForm([
'fields[' . $paragraphs_name . '][settings_edit_form][settings][default_paragraph_type]' => $default_type,
], 'Update');
$this
->submitForm([], 'Save');
}
protected function removeDefaultParagraphType($content_type) {
$this
->drupalGet('node/add/' . $content_type);
$this
->submitForm([], 'Remove');
$this
->submitForm([], 'Confirm removal');
$this
->assertSession()
->pageTextNotContains('No paragraphs added yet.');
}
protected function setParagraphsWidgetMode($content_type, $paragraphs_field, $mode) {
$this
->drupalGet('admin/structure/types/manage/' . $content_type . '/form-display');
$this
->submitForm([], $paragraphs_field . '_settings_edit');
$this
->submitForm([
'fields[' . $paragraphs_field . '][settings_edit_form][settings][edit_mode]' => $mode,
], 'Update');
$this
->submitForm([], 'Save');
}
}