// Programmatically submit the form with a value for the restricted field.
// Since programmed_bypass_access_check is set to TRUE by default, the
// field is accessible and can be set.
\Drupal::
formBuilder()->
submitForm('\Drupal\form_test\Form\FormTestProgrammaticForm',
$form_state);
$values =
$form_state->
get('programmatic_form_submit'
);
$this->
assertEquals('dummy value',
$values['field_restricted'
], 'The value for the restricted field is stored correctly.'
);
// Programmatically submit the form with a value for the restricted field
// with programmed_bypass_access_check set to FALSE. Since access
// restrictions apply, the restricted field is inaccessible, and the value
// should not be stored.
$form_state->
setProgrammedBypassAccessCheck(FALSE
);
\Drupal::
formBuilder()->
submitForm('\Drupal\form_test\Form\FormTestProgrammaticForm',
$form_state);
$values =
$form_state->
get('programmatic_form_submit'
);
$this->
assertNotSame('dummy value',
$values['field_restricted'
], 'The value for the restricted field is not stored.'
);
}}