From 0014a64d37e42ccc63f0b44a0ce32a06fca67b1d Mon Sep 17 00:00:00 2001 From: Dries Buytaert Date: Sat, 5 Dec 2009 14:33:55 +0000 Subject: - Patch #641356 by fago, sun: cache more of () when form caching is enabled. --- modules/simpletest/tests/form.test | 30 +++++++++++++++++++ modules/simpletest/tests/form_test.module | 50 +++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+) (limited to 'modules/simpletest/tests') diff --git a/modules/simpletest/tests/form.test b/modules/simpletest/tests/form.test index 0f1769d43..78dff3d45 100644 --- a/modules/simpletest/tests/form.test +++ b/modules/simpletest/tests/form.test @@ -597,6 +597,36 @@ class FormsFormStorageTestCase extends DrupalWebTestCase { $this->assertFieldByName('title', 'title_changed', t('The altered form storage value was updated in cache and taken over.')); $this->assertText('Title: title_changed', t('The form storage has stored the values.')); } + + /** + * Tests a form using form state without using 'storage' to pass data from the + * constructor to a submit handler. The data has to persist even when caching + * gets activated, what may happen when a modules alter the form and adds + * #ajax properties. + */ + function testFormStatePersist() { + // Test the form one time with caching activated and one time without. + $run_options = array( + array(), + array('query' => array('cache' => 1)), + ); + foreach($run_options as $options) { + $this->drupalPost('form-test/state-persist', array(), t('Submit'), $options); + // The submit handler outputs the value in $form_state, assert it's there. + $this->assertText('State persisted.'); + + // Test it again, but first trigger a validation error, then test. + $this->drupalPost('form-test/state-persist', array('title' => ''), t('Submit'), $options); + $this->assertText(t('!name field is required.', array('!name' => 'title'))); + // Submit the form again triggering no validation error. + $this->drupalPost(NULL, array('title' => 'foo'), t('Submit'), $options); + $this->assertText('State persisted.'); + + // Now post to the rebuilt form and verify it's still there afterwards. + $this->drupalPost(NULL, array('title' => 'bar'), t('Submit'), $options); + $this->assertText('State persisted.'); + } + } } /** diff --git a/modules/simpletest/tests/form_test.module b/modules/simpletest/tests/form_test.module index 024917b86..3e9a3720a 100644 --- a/modules/simpletest/tests/form_test.module +++ b/modules/simpletest/tests/form_test.module @@ -102,6 +102,14 @@ function form_test_menu() { 'type' => MENU_CALLBACK, ); + $items['form-test/state-persist'] = array( + 'title' => 'Form state persistence without storage', + 'page callback' => 'drupal_get_form', + 'page arguments' => array('form_test_state_persist'), + 'access callback' => TRUE, + 'type' => MENU_CALLBACK, + ); + return $items; } @@ -742,3 +750,45 @@ function form_test_form_rebuild_preserve_values_form_submit($form, &$form_state) // Finish the workflow. Do not rebuild. drupal_set_message(t('Form values: %values', array('%values' => var_export($form_state['values'], TRUE)))); } + +/** + * Form constructor for testing form state persistence. + */ +function form_test_state_persist($form, &$form_state) { + $form['title'] = array( + '#type' => 'textfield', + '#title' => 'title', + '#default_value' => 'DEFAULT', + '#required' => TRUE, + ); + $form_state['value'] = 'State persisted.'; + + $form['submit'] = array( + '#type' => 'submit', + '#value' => t('Submit'), + ); + return $form; +} + +/** + * Submit handler. + * + * @see form_test_state_persist() + */ +function form_test_state_persist_submit($form, &$form_state) { + drupal_set_message($form_state['value']); + $form_state['rebuild'] = TRUE; +} + +/** + * Implements hook_form_FORM_ID_alter(). + * + * @see form_test_state_persist() + */ +function form_test_form_form_test_state_persist_alter(&$form, &$form_state) { + // Simulate a form alter implementation inserting form elements that enable + // caching of the form, e.g. elements having #ajax. + if (!empty($_REQUEST['cache'])) { + $form_state['cache'] = TRUE; + } +} -- cgit v1.2.3