You are here

public function DisableFieldTest::testDisableFieldKeepValuesOnDisabledState in Disable Field 8.2

Test that a disabled field keeps it's value.

File

tests/src/Functional/DisableFieldTest.php, line 302

Class

DisableFieldTest
Disable field tests.

Namespace

Drupal\Tests\disable_field\Functional

Code

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',
  ]);

  // The admin user can edit the field. Make sure the value is saved.
  $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');

  // User 1 cannot edit the field. Make sure the value stays the same.
  $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');

  // User 1 cannot edit the field. Make sure the value stays the same.
  // Even when the user is tampering with the data.
  $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');

  // Check if a disabled field keeps it default 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');
}