You are here

public function ProgrammaticTest::testProgrammaticAccessBypass in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/system/tests/src/Kernel/Form/ProgrammaticTest.php \Drupal\Tests\system\Kernel\Form\ProgrammaticTest::testProgrammaticAccessBypass()
  2. 10 core/modules/system/tests/src/Kernel/Form/ProgrammaticTest.php \Drupal\Tests\system\Kernel\Form\ProgrammaticTest::testProgrammaticAccessBypass()

Test the programmed_bypass_access_check flag.

File

core/modules/system/tests/src/Kernel/Form/ProgrammaticTest.php, line 96

Class

ProgrammaticTest
Tests the programmatic form submission behavior.

Namespace

Drupal\Tests\system\Kernel\Form

Code

public function testProgrammaticAccessBypass() {
  $form_state = (new FormState())
    ->setValues([
    'textfield' => 'dummy value',
    'field_restricted' => 'dummy value',
  ]);

  // 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
    ->assertEqual($values['field_restricted'], 'dummy value', '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
    ->assertNotEqual($values['field_restricted'], 'dummy value', 'The value for the restricted field is not stored.');
}