You are here

function FormCacheTest::testNoCacheToken in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/system/src/Tests/Form/FormCacheTest.php \Drupal\system\Tests\Form\FormCacheTest::testNoCacheToken()

Tests the form cache without a logged-in user.

File

core/modules/system/src/Tests/Form/FormCacheTest.php, line 91
Contains \Drupal\system\Tests\Form\FormCacheTest.

Class

FormCacheTest
Tests \Drupal::formBuilder()->setCache() and \Drupal::formBuilder()->getCache().

Namespace

Drupal\system\Tests\Form

Code

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();
}