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. 10 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 913

Class

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

Namespace

Drupal\Tests\Core\Form

Code

public function testFormTokenCacheability($token, $is_authenticated, $method, $opted_in_for_cache) : 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;
    }
    if ($opted_in_for_cache) {
        $form['#cache']['max-age'] = Cache::PERMANENT;
    }
    $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);
    // FormBuilder does not set a form token when:
    // - #token is set to FALSE.
    // - #method is set to 'GET' and #token is not a string. This means the GET
    //   form did not get a form token by default, and the form did not
    //   explicitly opt in.
    if ($token === FALSE || $method == 'get' && !is_string($token)) {
        $this->assertEquals($built_form['#cache'], [
            'tags' => [
                'CACHE_MISS_IF_UNCACHEABLE_HTTP_METHOD:form',
            ],
        ]);
        $this->assertFalse(isset($built_form['form_token']));
    }
    else {
        // For forms that are eligible for form tokens, a cache context must be
        // set that indicates the form token only exists for logged in users.
        $this->assertTrue(isset($built_form['#cache']));
        $expected_cacheability_metadata = [
            'tags' => [
                'CACHE_MISS_IF_UNCACHEABLE_HTTP_METHOD:form',
            ],
            'contexts' => [
                'user.roles:authenticated',
            ],
        ];
        if ($opted_in_for_cache) {
            $expected_cacheability_metadata['max-age'] = Cache::PERMANENT;
        }
        $this->assertEquals($expected_cacheability_metadata, $built_form['#cache']);
        // Finally, verify that a form token is generated when appropriate, with
        // the expected cacheability metadata (or lack thereof).
        if (!$is_authenticated) {
            $this->assertFalse(isset($built_form['form_token']));
        }
        else {
            $this->assertTrue(isset($built_form['form_token']));
            if ($opted_in_for_cache) {
                $this->assertFalse(isset($built_form['form_token']['#cache']));
            }
            else {
                $this->assertEquals([
                    'max-age' => 0,
                ], $built_form['form_token']['#cache']);
            }
        }
    }
}

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