Issue #2355179 by legolasbo, er.pushpinderrana: Remove usage of form_get_cache() and form_set_cache().
parent
81d6f8702b
commit
f803d64bfb
|
@ -183,8 +183,9 @@ class FormCache implements FormCacheInterface {
|
||||||
|
|
||||||
// Ensure that the form build_id embedded in the form structure is the same
|
// Ensure that the form build_id embedded in the form structure is the same
|
||||||
// as the one passed in as a parameter. This is an additional safety measure
|
// as the one passed in as a parameter. This is an additional safety measure
|
||||||
// to prevent legacy code operating directly with form_get_cache and
|
// to prevent legacy code operating directly with
|
||||||
// form_set_cache from accidentally overwriting immutable form state.
|
// \Drupal::formBuilder()->getCache() and \Drupal::formBuilder()->setCache()
|
||||||
|
// from accidentally overwriting immutable form state.
|
||||||
if (isset($form['#build_id']) && $form['#build_id'] != $form_build_id) {
|
if (isset($form['#build_id']) && $form['#build_id'] != $form_build_id) {
|
||||||
$this->logger->error('Form build-id mismatch detected while attempting to store a form in the cache.');
|
$this->logger->error('Form build-id mismatch detected while attempting to store a form in the cache.');
|
||||||
return;
|
return;
|
||||||
|
|
|
@ -50,8 +50,8 @@ class FormState implements FormStateInterface {
|
||||||
* for building the form. Each array entry may be the path to a file or
|
* for building the form. Each array entry may be the path to a file or
|
||||||
* another array containing values for the parameters 'type', 'module' and
|
* another array containing values for the parameters 'type', 'module' and
|
||||||
* 'name' as needed by module_load_include(). The files listed here are
|
* 'name' as needed by module_load_include(). The files listed here are
|
||||||
* automatically loaded by form_get_cache(). By default the current menu
|
* automatically loaded by \Drupal::formBuilder()->getCache(). By default
|
||||||
* router item's 'file' definition is added, if any. Use
|
* the current menu router item's 'file' definition is added, if any. Use
|
||||||
* self::loadInclude() to add include files from a form constructor.
|
* self::loadInclude() to add include files from a form constructor.
|
||||||
* - form_id: Identification of the primary form being constructed and
|
* - form_id: Identification of the primary form being constructed and
|
||||||
* processed.
|
* processed.
|
||||||
|
|
|
@ -116,7 +116,7 @@ class FormAjaxController implements ContainerInjectionInterface {
|
||||||
$form_build_id = $request->request->get('form_build_id');
|
$form_build_id = $request->request->get('form_build_id');
|
||||||
|
|
||||||
// Get the form from the cache.
|
// Get the form from the cache.
|
||||||
$form = form_get_cache($form_build_id, $form_state);
|
$form = \Drupal::formBuilder()->getCache($form_build_id, $form_state);
|
||||||
if (!$form) {
|
if (!$form) {
|
||||||
// If $form cannot be loaded from the cache, the form_build_id must be
|
// If $form cannot be loaded from the cache, the form_build_id must be
|
||||||
// invalid, which means that someone performed a POST request onto
|
// invalid, which means that someone performed a POST request onto
|
||||||
|
@ -128,9 +128,9 @@ class FormAjaxController implements ContainerInjectionInterface {
|
||||||
}
|
}
|
||||||
|
|
||||||
// When a page level cache is enabled, the form-build id might have been
|
// When a page level cache is enabled, the form-build id might have been
|
||||||
// replaced from within form_get_cache. If this is the case, it is also
|
// replaced from within \Drupal::formBuilder()->getCache(). If this is the
|
||||||
// necessary to update it in the browser by issuing an appropriate Ajax
|
// case, it is also necessary to update it in the browser by issuing an
|
||||||
// command.
|
// appropriate Ajax command.
|
||||||
$commands = [];
|
$commands = [];
|
||||||
if (isset($form['#build_id_old']) && $form['#build_id_old'] != $form['#build_id']) {
|
if (isset($form['#build_id_old']) && $form['#build_id_old'] != $form['#build_id']) {
|
||||||
// If the form build ID has changed, issue an Ajax command to update it.
|
// If the form build ID has changed, issue an Ajax command to update it.
|
||||||
|
|
|
@ -12,7 +12,8 @@ use Drupal\Core\Session\UserSession;
|
||||||
use Drupal\simpletest\DrupalUnitTestBase;
|
use Drupal\simpletest\DrupalUnitTestBase;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tests form_set_cache() and form_get_cache().
|
* Tests \Drupal::formBuilder()->setCache() and
|
||||||
|
* \Drupal::formBuilder()->getCache().
|
||||||
*
|
*
|
||||||
* @group Form
|
* @group Form
|
||||||
*/
|
*/
|
||||||
|
@ -57,7 +58,7 @@ class FormCacheTest extends DrupalUnitTestBase {
|
||||||
*/
|
*/
|
||||||
function testCacheToken() {
|
function testCacheToken() {
|
||||||
\Drupal::currentUser()->setAccount(new UserSession(array('uid' => 1)));
|
\Drupal::currentUser()->setAccount(new UserSession(array('uid' => 1)));
|
||||||
form_set_cache($this->form_build_id, $this->form, $this->form_state);
|
\Drupal::formBuilder()->setCache($this->form_build_id, $this->form, $this->form_state);
|
||||||
|
|
||||||
$cached_form_state = new FormState();
|
$cached_form_state = new FormState();
|
||||||
$cached_form = form_get_cache($this->form_build_id, $cached_form_state);
|
$cached_form = form_get_cache($this->form_build_id, $cached_form_state);
|
||||||
|
@ -70,7 +71,7 @@ class FormCacheTest extends DrupalUnitTestBase {
|
||||||
// will break the parent site test runner batch.)
|
// will break the parent site test runner batch.)
|
||||||
\Drupal::state()->set('system.private_key', 'invalid');
|
\Drupal::state()->set('system.private_key', 'invalid');
|
||||||
$cached_form_state = new FormState();
|
$cached_form_state = new FormState();
|
||||||
$cached_form = form_get_cache($this->form_build_id, $cached_form_state);
|
$cached_form = \Drupal::formBuilder()->getCache($this->form_build_id, $cached_form_state);
|
||||||
$this->assertFalse($cached_form, 'No form returned from cache');
|
$this->assertFalse($cached_form, 'No form returned from cache');
|
||||||
$cached_form_state_example = $cached_form_state->get('example');
|
$cached_form_state_example = $cached_form_state->get('example');
|
||||||
$this->assertTrue(empty($cached_form_state_example));
|
$this->assertTrue(empty($cached_form_state_example));
|
||||||
|
@ -78,7 +79,7 @@ class FormCacheTest extends DrupalUnitTestBase {
|
||||||
// Test that loading the cache with a different form_id fails.
|
// Test that loading the cache with a different form_id fails.
|
||||||
$wrong_form_build_id = $this->randomMachineName(9);
|
$wrong_form_build_id = $this->randomMachineName(9);
|
||||||
$cached_form_state = new FormState();
|
$cached_form_state = new FormState();
|
||||||
$this->assertFalse(form_get_cache($wrong_form_build_id, $cached_form_state), 'No form returned from cache');
|
$this->assertFalse(\Drupal::formBuilder()->getCache($wrong_form_build_id, $cached_form_state), 'No form returned from cache');
|
||||||
$cached_form_state_example = $cached_form_state->get('example');
|
$cached_form_state_example = $cached_form_state->get('example');
|
||||||
$this->assertTrue(empty($cached_form_state_example), 'Cached form state was not loaded');
|
$this->assertTrue(empty($cached_form_state_example), 'Cached form state was not loaded');
|
||||||
}
|
}
|
||||||
|
@ -90,10 +91,10 @@ class FormCacheTest extends DrupalUnitTestBase {
|
||||||
$this->container->set('current_user', new UserSession(array('uid' => 0)));
|
$this->container->set('current_user', new UserSession(array('uid' => 0)));
|
||||||
|
|
||||||
$this->form_state->set('example', $this->randomMachineName());
|
$this->form_state->set('example', $this->randomMachineName());
|
||||||
form_set_cache($this->form_build_id, $this->form, $this->form_state);
|
\Drupal::formBuilder()->setCache($this->form_build_id, $this->form, $this->form_state);
|
||||||
|
|
||||||
$cached_form_state = new FormState();
|
$cached_form_state = new FormState();
|
||||||
$cached_form = form_get_cache($this->form_build_id, $cached_form_state);
|
$cached_form = \Drupal::formBuilder()->getCache($this->form_build_id, $cached_form_state);
|
||||||
$this->assertEqual($this->form['#property'], $cached_form['#property']);
|
$this->assertEqual($this->form['#property'], $cached_form['#property']);
|
||||||
$this->assertTrue(empty($cached_form['#cache_token']), 'Form has no cache token');
|
$this->assertTrue(empty($cached_form['#cache_token']), 'Form has no cache token');
|
||||||
$this->assertEqual($this->form_state->get('example'), $cached_form_state->get('example'));
|
$this->assertEqual($this->form_state->get('example'), $cached_form_state->get('example'));
|
||||||
|
|
|
@ -224,9 +224,10 @@ class StorageTest extends WebTestBase {
|
||||||
$this->assertEqual($original['form']['#build_id_old'], $build_id, 'Original build_id was recorded');
|
$this->assertEqual($original['form']['#build_id_old'], $build_id, 'Original build_id was recorded');
|
||||||
$this->assertNotEqual($original['form']['#build_id'], $build_id, 'New build_id was generated');
|
$this->assertNotEqual($original['form']['#build_id'], $build_id, 'New build_id was generated');
|
||||||
|
|
||||||
// Assert that a watchdog message was logged by form_set_cache.
|
// Assert that a watchdog message was logged by
|
||||||
|
// \Drupal::formBuilder()->setCache().
|
||||||
$status = (bool) db_query_range('SELECT 1 FROM {watchdog} WHERE message = :message', 0, 1, [':message' => 'Form build-id mismatch detected while attempting to store a form in the cache.']);
|
$status = (bool) db_query_range('SELECT 1 FROM {watchdog} WHERE message = :message', 0, 1, [':message' => 'Form build-id mismatch detected while attempting to store a form in the cache.']);
|
||||||
$this->assert($status, 'A watchdog message was logged by form_set_cache');
|
$this->assert($status, 'A watchdog message was logged by \Drupal::formBuilder()->setCache');
|
||||||
|
|
||||||
// Ensure that the form state was not poisoned by the preceding call.
|
// Ensure that the form state was not poisoned by the preceding call.
|
||||||
$original = $this->drupalGetAJAX('form-test/form-storage-legacy/' . $build_id);
|
$original = $this->drupalGetAJAX('form-test/form-storage-legacy/' . $build_id);
|
||||||
|
|
Loading…
Reference in New Issue