FormCacheTest.php 3.39 KB
Newer Older
1 2 3 4 5 6 7 8 9
<?php

/**
 * @file
 * Contains \Drupal\system\Tests\Form\FormCacheTest.
 */

namespace Drupal\system\Tests\Form;

10
use Drupal\Core\Form\FormState;
11
use Drupal\Core\Session\UserSession;
12 13 14
use Drupal\simpletest\DrupalUnitTestBase;

/**
15 16
 * Tests \Drupal::formBuilder()->setCache() and
 * \Drupal::formBuilder()->getCache().
17 18
 *
 * @group Form
19 20 21 22 23 24 25 26 27 28
 */
class FormCacheTest extends DrupalUnitTestBase {

  /**
   * Modules to enable.
   *
   * @var array
   */
  public static $modules = array('system', 'user');

29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
  /**
   * @var string
   */
  protected $form_build_id;

  /**
   * @var array
   */
  protected $form;

  /**
   * @var array
   */
  protected $form_state;

44
  protected function setUp() {
45 46 47
    parent::setUp();
    $this->installSchema('system', array('key_value_expire'));

48
    $this->form_build_id = $this->randomMachineName();
49
    $this->form = array(
50
      '#property' => $this->randomMachineName(),
51
    );
52
    $this->form_state = new FormState();
53
    $this->form_state->set('example', $this->randomMachineName());
54 55 56 57 58 59
  }

  /**
   * Tests the form cache with a logged-in user.
   */
  function testCacheToken() {
60
    \Drupal::currentUser()->setAccount(new UserSession(array('uid' => 1)));
61
    \Drupal::formBuilder()->setCache($this->form_build_id, $this->form, $this->form_state);
62

63
    $cached_form_state = new FormState();
64 65 66
    $cached_form = form_get_cache($this->form_build_id, $cached_form_state);
    $this->assertEqual($this->form['#property'], $cached_form['#property']);
    $this->assertTrue(!empty($cached_form['#cache_token']), 'Form has a cache token');
67
    $this->assertEqual($this->form_state->get('example'), $cached_form_state->get('example'));
68 69 70 71

    // Test that the form cache isn't loaded when the session/token has changed.
    // Change the private key. (We cannot change the session ID because this
    // will break the parent site test runner batch.)
72
    \Drupal::state()->set('system.private_key', 'invalid');
73
    $cached_form_state = new FormState();
74
    $cached_form = \Drupal::formBuilder()->getCache($this->form_build_id, $cached_form_state);
75
    $this->assertFalse($cached_form, 'No form returned from cache');
76 77
    $cached_form_state_example = $cached_form_state->get('example');
    $this->assertTrue(empty($cached_form_state_example));
78 79

    // Test that loading the cache with a different form_id fails.
80
    $wrong_form_build_id = $this->randomMachineName(9);
81
    $cached_form_state = new FormState();
82
    $this->assertFalse(\Drupal::formBuilder()->getCache($wrong_form_build_id, $cached_form_state), 'No form returned from cache');
83 84
    $cached_form_state_example = $cached_form_state->get('example');
    $this->assertTrue(empty($cached_form_state_example), 'Cached form state was not loaded');
85 86 87 88 89 90
  }

  /**
   * Tests the form cache without a logged-in user.
   */
  function testNoCacheToken() {
91
    $this->container->set('current_user', new UserSession(array('uid' => 0)));
92

93
    $this->form_state->set('example', $this->randomMachineName());
94
    \Drupal::formBuilder()->setCache($this->form_build_id, $this->form, $this->form_state);
95

96
    $cached_form_state = new FormState();
97
    $cached_form = \Drupal::formBuilder()->getCache($this->form_build_id, $cached_form_state);
98 99
    $this->assertEqual($this->form['#property'], $cached_form['#property']);
    $this->assertTrue(empty($cached_form['#cache_token']), 'Form has no cache token');
100
    $this->assertEqual($this->form_state->get('example'), $cached_form_state->get('example'));
101 102 103
  }

}