drupal/modules/simpletest/tests/form_test.module

368 lines
10 KiB
Plaintext

<?php
// $Id$
/**
* @file
* Helper module for the form API tests.
*/
/**
* Implement hook_menu().
*/
function form_test_menu() {
$items = array();
$items['form_test/tableselect/multiple-true'] = array(
'title' => 'Tableselect checkboxes test',
'page callback' => 'drupal_get_form',
'page arguments' => array('_form_test_tableselect_multiple_true_form'),
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
$items['form_test/tableselect/multiple-false'] = array(
'title' => 'Tableselect radio button test',
'page callback' => 'drupal_get_form',
'page arguments' => array('_form_test_tableselect_multiple_false_form'),
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
$items['form_test/tableselect/empty-text'] = array(
'title' => 'Tableselect empty text test',
'page callback' => 'drupal_get_form',
'page arguments' => array('_form_test_tableselect_empty_form'),
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
$items['form_test/tableselect/advanced-select'] = array(
'title' => 'Tableselect js_select tests',
'page callback' => 'drupal_get_form',
'page arguments' => array('_form_test_tableselect_js_select_form'),
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
$items['form_test/form_clean_id'] = array(
'title' => 'form_clean_id test',
'page callback' => 'form_test_form_clean_id_page',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
$items['form_test/drupal_form_submit_batch_api'] = array(
'title' => 'BatchAPI Drupal_form_submit tests',
'page callback' => 'form_test_drupal_form_submit_batch_api',
'access arguments' => array('access content'),
'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;
}
/**
* Generate a page with three forms, to test the clean_id generation.
*/
function form_test_form_clean_id_page() {
$build['form_test_test_form1'] = drupal_get_form('form_test_test_form');
$build['form_test_test_form2'] = drupal_get_form('form_test_test_form');
$build['form_test_test_form3'] = drupal_get_form('form_test_test_form');
return $build;
}
/**
* A simple form to test clean_id generation.
*/
function form_test_test_form(&$form_state) {
$form['input'] = array(
'#type' => 'item',
'#title' => 'Test Textfield',
'#markup' => form_clean_id('form_test_form_clean_id_presence'),
);
return $form;
}
/**
* Create a header and options array. Helper function for callbacks.
*/
function _form_test_tableselect_get_data() {
$header = array(
'one' => t('One'),
'two' => t('Two'),
'three' => t('Three'),
'four' => t('Four'),
);
$options['row1'] = array(
'one' => 'row1col1',
'two' => t('row1col2'),
'three' => t('row1col3'),
'four' => t('row1col4'),
);
$options['row2'] = array(
'one' => 'row2col1',
'two' => t('row2col2'),
'three' => t('row2col3'),
'four' => t('row2col4'),
);
$options['row3'] = array(
'one' => 'row3col1',
'two' => t('row3col2'),
'three' => t('row3col3'),
'four' => t('row3col4'),
);
return array($header, $options);
}
/**
* Build a form to test the tableselect element.
*
* @param $form_state
* The form_state
* @param $element_properties
* An array of element properties for the tableselect element.
*
* @return
* A form with a tableselect element and a submit button.
*/
function _form_test_tableselect_form_builder($form_state, $element_properties) {
$form = array();
list($header, $options) = _form_test_tableselect_get_data();
$form['tableselect'] = $element_properties;
$form['tableselect'] += array(
'#type' => 'tableselect',
'#header' => $header,
'#options' => $options,
'#multiple' => FALSE,
'#empty' => t('Empty text.'),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
);
return $form;
}
/**
* Test the tableselect #multiple = TRUE functionality.
*/
function _form_test_tableselect_multiple_true_form($form_state) {
return _form_test_tableselect_form_builder($form_state, array('#multiple' => TRUE));
}
/**
* Process the tableselect #multiple = TRUE submitted values.
*/
function _form_test_tableselect_multiple_true_form_submit($form, &$form_state) {
$selected = $form_state['values']['tableselect'];
foreach ($selected as $key => $value) {
drupal_set_message(t('Submitted: @key = @value', array('@key' => $key, '@value' => $value)));
}
}
/**
* Test the tableselect #multiple = FALSE functionality.
*/
function _form_test_tableselect_multiple_false_form($form_state) {
return _form_test_tableselect_form_builder($form_state, array('#multiple' => FALSE));
}
/**
* Process the tableselect #multiple = FALSE submitted values.
*/
function _form_test_tableselect_multiple_false_form_submit($form, &$form_state) {
drupal_set_message(t('Submitted: @value', array('@value' => $form_state['values']['tableselect'])));
}
/**
* Test functionality of the tableselect #empty property.
*/
function _form_test_tableselect_empty_form($form_state) {
return _form_test_tableselect_form_builder($form_state, array('#options' => array()));
}
/**
* Test functionality of the tableselect #js_select property.
*/
function _form_test_tableselect_js_select_form($form_state, $action) {
switch ($action) {
case 'multiple-true-default':
$options = array('#multiple' => TRUE);
break;
case 'multiple-false-default':
$options = array('#multiple' => FALSE);
break;
case 'multiple-true-no-advanced-select':
$options = array('#multiple' => TRUE, '#js_select' => FALSE);
break;
case 'multiple-false-advanced-select':
$options = array('#multiple' => FALSE, '#js_select' => TRUE);
break;
}
return _form_test_tableselect_form_builder($form_state, $options);
}
/**
* Page callback for the batch/drupal_form_submit interaction test.
*
* When called without any arguments we set up a batch that calls
* form_test_batch_callback. That function will submit a form using
* drupal_form_submit using the values specified in this function.
*
* The form's field test_value begins at 'initial_value', and is changed
* to 'form_submitted' when the form is submitted successfully. On
* completion this function is passed 'done' to complete the process.
*/
function form_test_drupal_form_submit_batch_api($arg = '') {
// If we're at the end of the batch process, return.
if ($arg == 'done') {
return t('Done');
}
// Otherwise set up the batch.
$batch['operations'] = array(
array('form_test_batch_callback', array('form_submitted')),
);
// Set the batch and process it.
batch_set($batch);
batch_process('form_test/drupal_form_submit_batch_api/done');
}
/**
* Submits form_test_mock_form using drupal_form_submit using the given $value.
*/
function form_test_batch_callback($value) {
$state['values']['test_value'] = $value;
drupal_form_submit('form_test_mock_form', $state);
}
/**
* A simple form with a textfield and submit button.
*/
function form_test_mock_form($form_state) {
$form = array();
$form['test_value'] = array(
'#type' => 'textfield',
'#default_value' => 'initial_state',
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
);
return $form;
}
/**
* Form submission callback.
*
* Updates the variable 'form_test_mock_submit' to the submitted form value.
*/
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['body'] = 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']);
}