diff options
author | Dries Buytaert <dries@buytaert.net> | 2009-04-22 09:12:44 +0000 |
---|---|---|
committer | Dries Buytaert <dries@buytaert.net> | 2009-04-22 09:12:44 +0000 |
commit | 2bc19555bfca04551333e361509c2f51841e16c2 (patch) | |
tree | 2548445f39fe4e4ffc0910d4446f8c3fdaf46093 /modules/simpletest/tests/form_test.module | |
parent | 920babfe6ea1c929d7df16631e4de52f0646e518 (diff) | |
download | brdo-2bc19555bfca04551333e361509c2f51841e16c2.tar.gz brdo-2bc19555bfca04551333e361509c2f51841e16c2.tar.bz2 |
- Patch #302240 by fago: fixed various problems with form storage and added tests. Yay.
Diffstat (limited to 'modules/simpletest/tests/form_test.module')
-rw-r--r-- | modules/simpletest/tests/form_test.module | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/modules/simpletest/tests/form_test.module b/modules/simpletest/tests/form_test.module index dd91f6ebf..afa08ba89 100644 --- a/modules/simpletest/tests/form_test.module +++ b/modules/simpletest/tests/form_test.module @@ -58,6 +58,14 @@ function form_test_menu() { 'type' => MENU_CALLBACK, ); + $items['form_test/form-storage'] = array( + 'title' => 'Form storage test', + 'page callback' => 'drupal_get_form', + 'page arguments' => array('form_storage_test_form'), + 'access arguments' => array('access content'), + 'type' => MENU_CALLBACK, + ); + return $items; } @@ -279,3 +287,81 @@ function form_test_mock_form($form_state) { function form_test_mock_form_submit($form, &$form_state) { variable_set('form_test_mock_submit', $form_state['values']['test_value']); } + +/** + * A multistep form for testing the form storage. + * + * It uses two steps for editing a virtual "thing". Any changes to it are saved + * in the form storage and have to be present during any step. By setting the + * request parameter "cache" the form can be tested with caching enabled, as + * it would be the case, if the form would contain some #ahah callbacks. + * + * @see form_storage_test_form_submit(). + */ +function form_storage_test_form(&$form_state) { + // Initialize + if (!isset($form_state['storage'])) { + if (empty($form_state['input'])) { + $_SESSION['constructions'] = 0; + } + // Put the initial thing into the storage + $form_state['storage'] = array( + 'thing' => array( + 'title' => 'none', + 'value' => '', + ), + ); + $form_state['storage'] += array('step' => 1); + } + + // Count how often the form is constructed + $_SESSION['constructions']++; + + if ($form_state['storage']['step'] == 1) { + $form['title'] = array( + '#type' => 'textfield', + '#title' => 'title', + '#default_value' => $form_state['storage']['thing']['title'], + '#required' => TRUE, + ); + $form['value'] = array( + '#type' => 'textfield', + '#title' => 'value', + '#default_value' => $form_state['storage']['thing']['value'], + ); + $form['submit'] = array( + '#type' => 'submit', + '#value' => 'Continue', + ); + } + else { + $form['content'] = array('#value' => 'This is the second step.'); + $form['submit'] = array( + '#type' => 'submit', + '#value' => 'Save', + ); + } + + if (isset($_REQUEST['cache'])) { + // Manually activate caching, so we can test that the storage keeps working + // when it's enabled. + $form['#cache'] = TRUE; + } + + return $form; +} + +/** + * Multistep form submit callback. + */ +function form_storage_test_form_submit($form, &$form_state) { + if ($form_state['storage']['step'] == 1) { + $form_state['storage']['thing']['title'] = $form_state['values']['title']; + $form_state['storage']['thing']['value'] = $form_state['values']['value']; + } + else { + drupal_set_message("Title: ". check_plain($form_state['storage']['thing']['title'])); + } + $form_state['storage']['step']++; + drupal_set_message("Form constructions: ". $_SESSION['constructions']); +} |