function ProgrammaticTest::testProgrammaticAccessBypass

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

Tests the programmed_bypass_access_check flag.

File

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

Class

ProgrammaticTest
Tests the programmatic form submission behavior.

Namespace

Drupal\Tests\system\Kernel\Form

Code

public function testProgrammaticAccessBypass() : void {
    $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->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.');
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.