View source
<?php
namespace Drupal\Tests\disable_field\Functional;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\disable_field\Traits\DisableFieldTestTrait;
use Drupal\Tests\field_ui\Traits\FieldUiTestTrait;
use Drupal\Tests\paragraphs\FunctionalJavascript\ParagraphsTestBaseTrait;
class DisableFieldTest extends BrowserTestBase {
use DisableFieldTestTrait;
use FieldUiTestTrait;
use ParagraphsTestBaseTrait;
protected static $modules = [
'disable_field',
'block',
'field_ui',
'node',
'paragraphs',
'base_field_override_ui',
];
protected $defaultTheme = 'stark';
protected $adminUser;
protected $role1;
protected $role2;
protected $user1;
protected $user2;
protected function setUp() : void {
parent::setUp();
$this
->drupalPlaceBlock('system_breadcrumb_block');
$this
->drupalCreateContentType([
'type' => 'test',
]);
$this->adminUser = $this
->createUser([], NULL, TRUE);
$this->role1 = $this
->drupalCreateRole([
'access content',
'edit any test content',
'create test content',
'administer content types',
'administer node fields',
'administer node form display',
'administer node display',
'administer disable field settings',
]);
$this->role2 = $this
->drupalCreateRole([
'access content',
'edit any test content',
'create test content',
'administer content types',
'administer node fields',
'administer node form display',
'administer node display',
]);
$this->user1 = $this
->createUser([], NULL, FALSE, [
'roles' => [
$this->role1,
],
]);
$this->user2 = $this
->createUser([], NULL, FALSE, [
'roles' => [
$this->role2,
],
]);
$this
->drupalLogin($this->adminUser);
}
public function testDisableFieldOnAddFormEnableForAllRoles() : void {
$this
->fieldUIAddNewField('admin/structure/types/manage/test', 'test', 'Test field', 'string');
$this
->drupalGet('node/add/test');
$this
->checkIfFieldIsNotDisabledByFieldName('field_test');
$this
->drupalLogin($this->user1);
$this
->drupalGet('node/add/test');
$this
->checkIfFieldIsNotDisabledByFieldName('field_test');
$this
->drupalLogin($this->user2);
$this
->drupalGet('node/add/test');
$this
->checkIfFieldIsNotDisabledByFieldName('field_test');
}
public function testDisableFieldOnAddFormDisableForAllRoles() : void {
$this
->fieldUIAddNewField('admin/structure/types/manage/test', 'test', 'Test field', 'string', [], [
'disable_field[add][disable]' => 'all',
]);
$this
->drupalGet('node/add/test');
$this
->checkIfFieldIsDisabledByFieldName('field_test');
$this
->drupalLogin($this->user1);
$this
->drupalGet('node/add/test');
$this
->checkIfFieldIsDisabledByFieldName('field_test');
$this
->drupalLogin($this->user2);
$this
->drupalGet('node/add/test');
$this
->checkIfFieldIsDisabledByFieldName('field_test');
}
public function testDisableFieldOnAddFormDisableForCertainRoles() : void {
$this
->fieldUIAddNewField('admin/structure/types/manage/test', 'test', 'Test field', 'string', [], [
'disable_field[add][disable]' => 'roles',
'disable_field[add][roles][]' => [
$this->role1,
],
]);
$this
->drupalGet('node/add/test');
$this
->checkIfFieldIsNotDisabledByFieldName('field_test');
$this
->drupalLogin($this->user1);
$this
->drupalGet('node/add/test');
$this
->checkIfFieldIsDisabledByFieldName('field_test');
$this
->drupalLogin($this->user2);
$this
->drupalGet('node/add/test');
$this
->checkIfFieldIsNotDisabledByFieldName('field_test');
}
public function testDisableFieldOnAddFormEnableForCertainRoles() : void {
$this
->fieldUIAddNewField('admin/structure/types/manage/test', 'test', 'Test field', 'string', [], [
'disable_field[add][disable]' => 'roles_enable',
'disable_field[add][roles][]' => [
$this->role1,
],
]);
$this
->drupalGet('node/add/test');
$this
->checkIfFieldIsDisabledByFieldName('field_test');
$this
->drupalLogin($this->user1);
$this
->drupalGet('node/add/test');
$this
->checkIfFieldIsNotDisabledByFieldName('field_test');
$this
->drupalLogin($this->user2);
$this
->drupalGet('node/add/test');
$this
->checkIfFieldIsDisabledByFieldName('field_test');
}
public function testDisableFieldOnEditFormEnableForAllRoles() : void {
$this
->fieldUIAddNewField('admin/structure/types/manage/test', 'test', 'Test field', 'string');
$node = $this
->drupalCreateNode([
'type' => 'test',
]);
$this
->drupalGet($node
->toUrl('edit-form'));
$this
->checkIfFieldIsNotDisabledByFieldName('field_test');
$this
->drupalLogin($this->user1);
$this
->drupalGet($node
->toUrl('edit-form'));
$this
->checkIfFieldIsNotDisabledByFieldName('field_test');
$this
->drupalLogin($this->user2);
$this
->drupalGet($node
->toUrl('edit-form'));
$this
->checkIfFieldIsNotDisabledByFieldName('field_test');
}
public function testDisableFieldOnEditFormDisableForAllRoles() : void {
$this
->fieldUIAddNewField('admin/structure/types/manage/test', 'test', 'Test field', 'string', [], [
'disable_field[edit][disable]' => 'all',
]);
$this
->drupalGet('/admin/structure/types/manage/test/fields/node.test.field_test');
$this
->checkIfFieldIsNotDisabledByFieldName('default-value-input-field-test');
$node = $this
->drupalCreateNode([
'type' => 'test',
]);
$this
->drupalGet($node
->toUrl('edit-form'));
$this
->checkIfFieldIsDisabledByFieldName('field_test');
$this
->drupalLogin($this->user1);
$this
->drupalGet($node
->toUrl('edit-form'));
$this
->checkIfFieldIsDisabledByFieldName('field_test');
$this
->drupalLogin($this->user2);
$this
->drupalGet($node
->toUrl('edit-form'));
$this
->checkIfFieldIsDisabledByFieldName('field_test');
}
public function testDisableFieldOnEditFormDisableForCertainRoles() : void {
$this
->fieldUIAddNewField('admin/structure/types/manage/test', 'test', 'Test field', 'string', [], [
'disable_field[edit][disable]' => 'roles',
'disable_field[edit][roles][]' => [
$this->role1,
],
]);
$node = $this
->drupalCreateNode([
'type' => 'test',
]);
$this
->drupalGet($node
->toUrl('edit-form'));
$this
->checkIfFieldIsNotDisabledByFieldName('field_test');
$this
->drupalLogin($this->user1);
$this
->drupalGet($node
->toUrl('edit-form'));
$this
->checkIfFieldIsDisabledByFieldName('field_test');
$this
->drupalLogin($this->user2);
$this
->drupalGet($node
->toUrl('edit-form'));
$this
->checkIfFieldIsNotDisabledByFieldName('field_test');
}
public function testDisableFieldOnEditFormEnableForCertainRoles() : void {
$this
->fieldUIAddNewField('admin/structure/types/manage/test', 'test', 'Test field', 'string', [], [
'disable_field[edit][disable]' => 'roles_enable',
'disable_field[edit][roles][]' => [
$this->role1,
],
]);
$node = $this
->drupalCreateNode([
'type' => 'test',
]);
$this
->drupalGet($node
->toUrl('edit-form'));
$this
->checkIfFieldIsDisabledByFieldName('field_test');
$this
->drupalLogin($this->user1);
$this
->drupalGet($node
->toUrl('edit-form'));
$this
->checkIfFieldIsNotDisabledByFieldName('field_test');
$this
->drupalLogin($this->user2);
$this
->drupalGet($node
->toUrl('edit-form'));
$this
->checkIfFieldIsDisabledByFieldName('field_test');
}
public function testDisableFieldSettingsPermission() {
$assert_session = $this
->assertSession();
$this
->fieldUIAddNewField('admin/structure/types/manage/test', 'test', 'Test field', 'string');
$this
->drupalLogin($this->user1);
$this
->drupalGet('/admin/structure/types/manage/test/fields/node.test.field_test');
$assert_session
->elementExists('css', 'select[name="disable_field[add][disable]"]');
$assert_session
->elementExists('css', 'select[name="disable_field[edit][disable]"]');
$this
->drupalLogin($this->user2);
$this
->drupalGet('/admin/structure/types/manage/test/fields/node.test.field_test');
$assert_session
->elementNotExists('css', 'select[name="disable_field[add][disable]"]');
$assert_session
->elementNotExists('css', 'select[name="disable_field[edit][disable]"]');
}
public function testDisableFieldKeepValuesOnDisabledState() {
$assert_session = $this
->assertSession();
$this
->fieldUIAddNewField('admin/structure/types/manage/test', 'test', 'Test field', 'string', [], [
'default_value_input[field_test][0][value]' => 'default_test_value',
'disable_field[add][disable]' => 'roles',
'disable_field[add][roles][]' => [
$this->role1,
],
'disable_field[edit][disable]' => 'roles',
'disable_field[edit][roles][]' => [
$this->role1,
],
]);
$node = $this
->drupalCreateNode([
'type' => 'test',
]);
$this
->drupalGet($node
->toUrl('edit-form'));
$this
->checkIfFieldIsNotDisabledByFieldName('field_test');
$this
->submitForm([
'field_test[0][value]' => 'test_value',
], 'Save');
$this
->drupalGet($node
->toUrl('edit-form'));
$assert_session
->elementAttributeContains('css', 'input[name="field_test[0][value]"]', 'value', 'test_value');
$this
->drupalLogin($this->user1);
$this
->drupalGet($node
->toUrl('edit-form'));
$this
->checkIfFieldIsDisabledByFieldName('field_test');
$assert_session
->elementAttributeContains('css', 'input[name="field_test[0][value]"]', 'value', 'test_value');
$this
->submitForm([], 'Save');
$this
->drupalGet($node
->toUrl('edit-form'));
$assert_session
->elementAttributeContains('css', 'input[name="field_test[0][value]"]', 'value', 'test_value');
$this
->drupalGet($node
->toUrl('edit-form'));
$this
->checkIfFieldIsDisabledByFieldName('field_test');
$this
->submitForm([
'field_test[0][value]' => 'new_value',
], 'Save');
$this
->drupalGet($node
->toUrl('edit-form'));
$assert_session
->elementAttributeContains('css', 'input[name="field_test[0][value]"]', 'value', 'test_value');
$this
->drupalGet('/node/add/test');
$this
->checkIfFieldIsDisabledByFieldName('field_test');
$this
->submitForm([
'title[0][value]' => 'test_title',
], 'Save');
$node = $this
->drupalGetNodeByTitle('test_title');
$this
->drupalGet($node
->toUrl('edit-form'));
$this
->checkIfFieldIsDisabledByFieldName('field_test');
$assert_session
->elementAttributeContains('css', 'input[name="field_test[0][value]"]', 'value', 'default_test_value');
$this
->submitForm([
'field_test[0][value]' => 'new_value',
], 'Save');
$this
->drupalGet($node
->toUrl('edit-form'));
$assert_session
->elementAttributeContains('css', 'input[name="field_test[0][value]"]', 'value', 'default_test_value');
}
public function testDisableFieldWithParagraphsField() {
$this
->addParagraphedContentType('paragraphed_content_type');
$this
->drupalGet('/admin/structure/types/manage/test/fields/node.paragraphed_content_type.field_paragraphs');
$this
->submitForm([
'disable_field[add][disable]' => 'all',
'disable_field[edit][disable]' => 'all',
], 'Save settings');
$paragraph_type = 'text_paragraph';
$this
->addParagraphsType($paragraph_type);
$this
->fieldUIAddNewField('admin/structure/paragraphs_type/' . $paragraph_type, 'text', 'Text', 'text_long');
$this
->drupalGet('/node/add/paragraphed_content_type');
$this
->checkIfFieldIsDisabledById('edit-field-paragraphs-0-subform-field-text-0-value');
$this
->checkIfFieldIsDisabledById('field-paragraphs-text-paragraph-add-more');
}
public function testDisableFieldWithBaseFieldOverrideUi() {
$this
->drupalGet('/admin/structure/types/manage/test/fields/base-field-override/title/add');
$this
->submitForm([
'disable_field[add][disable]' => 'all',
'disable_field[edit][disable]' => 'all',
], 'Save settings');
$this
->drupalGet('node/add/test');
$this
->checkIfFieldIsDisabledByFieldName('title');
}
}