function FormCacheTest::testNoCacheToken

Same name and namespace in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Form/FormCacheTest.php \Drupal\KernelTests\Core\Form\FormCacheTest::testNoCacheToken()
  2. 10 core/tests/Drupal/KernelTests/Core/Form/FormCacheTest.php \Drupal\KernelTests\Core\Form\FormCacheTest::testNoCacheToken()
  3. 11.x core/tests/Drupal/KernelTests/Core/Form/FormCacheTest.php \Drupal\KernelTests\Core\Form\FormCacheTest::testNoCacheToken()

Tests the form cache without a logged-in user.

File

core/tests/Drupal/KernelTests/Core/Form/FormCacheTest.php, line 87

Class

FormCacheTest
Tests <a href="/api/drupal/core%21lib%21Drupal.php/function/Drupal%3A%3AformBuilder/8.9.x" title="Returns the form builder service." class="local">\Drupal::formBuilder</a>()-&gt;setCache() and <a href="/api/drupal/core%21lib%21Drupal.php/function/Drupal%3A%3AformBuilder/8.9.x" title="Returns the form builder service." class="local">\Drupal::formBuilder</a>()-&gt;getCache().

Namespace

Drupal\KernelTests\Core\Form

Code

public function testNoCacheToken() {
    // Switch to a anonymous user account.
    $account_switcher = \Drupal::service('account_switcher');
    $account_switcher->switchTo(new AnonymousUserSession());
    $this->formState
        ->set('example', $this->randomMachineName());
    \Drupal::formBuilder()->setCache($this->formBuildId, $this->form, $this->formState);
    $cached_form_state = new FormState();
    $cached_form = \Drupal::formBuilder()->getCache($this->formBuildId, $cached_form_state);
    $this->assertEqual($this->form['#property'], $cached_form['#property']);
    $this->assertTrue(empty($cached_form['#cache_token']), 'Form has no cache token');
    $this->assertEqual($this->formState
        ->get('example'), $cached_form_state->get('example'));
    // Restore user account.
    $account_switcher->switchBack();
}

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