function FormBuilderTest::testFormTokenCacheability

Same name in other branches
  1. 9 core/tests/Drupal/Tests/Core/Form/FormBuilderTest.php \Drupal\Tests\Core\Form\FormBuilderTest::testFormTokenCacheability()
  2. 8.9.x core/tests/Drupal/Tests/Core/Form/FormBuilderTest.php \Drupal\Tests\Core\Form\FormBuilderTest::testFormTokenCacheability()
  3. 11.x core/tests/Drupal/Tests/Core/Form/FormBuilderTest.php \Drupal\Tests\Core\Form\FormBuilderTest::testFormTokenCacheability()

@covers ::prepareForm

@dataProvider providerTestFormTokenCacheability

File

core/tests/Drupal/Tests/Core/Form/FormBuilderTest.php, line 912

Class

FormBuilderTest
@coversDefaultClass \Drupal\Core\Form\FormBuilder @group Form

Namespace

Drupal\Tests\Core\Form

Code

public function testFormTokenCacheability($token, $is_authenticated, $expected_form_cacheability, $expected_token_cacheability, $method) : void {
    $user = $this->prophesize(AccountProxyInterface::class);
    $user->isAuthenticated()
        ->willReturn($is_authenticated);
    $this->container
        ->set('current_user', $user->reveal());
    \Drupal::setContainer($this->container);
    $form_id = 'test_form_id';
    $form = $form_id();
    $form['#method'] = $method;
    if (isset($token)) {
        $form['#token'] = $token;
    }
    $form_arg = $this->createMock('Drupal\\Core\\Form\\FormInterface');
    $form_arg->expects($this->once())
        ->method('getFormId')
        ->willReturn($form_id);
    $form_arg->expects($this->once())
        ->method('buildForm')
        ->willReturn($form);
    $form_state = new FormState();
    $built_form = $this->formBuilder
        ->buildForm($form_arg, $form_state);
    if (!isset($expected_form_cacheability) || $method == 'get' && !is_string($token)) {
        $this->assertEquals($built_form['#cache'], [
            'tags' => [
                'CACHE_MISS_IF_UNCACHEABLE_HTTP_METHOD:form',
            ],
        ]);
    }
    else {
        $this->assertTrue(isset($built_form['#cache']));
        $this->assertEquals($expected_form_cacheability, $built_form['#cache']);
    }
    if (!isset($expected_token_cacheability)) {
        $this->assertFalse(isset($built_form['form_token']));
    }
    else {
        $this->assertTrue(isset($built_form['form_token']));
        $this->assertEquals($expected_token_cacheability, $built_form['form_token']['#cache']);
    }
}

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