Commit e008d3a5 authored by webchick's avatar webchick

Issue #2030165 by Berdir, tim.plunkett, vijaycs85, tkuldeep17 | rteijeiro:...

Issue #2030165 by Berdir, tim.plunkett, vijaycs85, tkuldeep17 | rteijeiro: Convert form_test_* functions to classes.
parent b96e8232
......@@ -32,7 +32,7 @@ function testFormCheckbox() {
// #return_value, with the exception of integer 0, which is not supported.
// @see form_process_checkbox().
foreach (array('0', '', 1, '1', 'foobar', '1foobar') as $return_value) {
$form_array = \Drupal::formBuilder()->getForm('form_test_checkbox_type_juggling', $default_value, $return_value);
$form_array = \Drupal::formBuilder()->getForm('\Drupal\form_test\Form\FormTestCheckboxTypeJugglingForm', $default_value, $return_value);
$form = drupal_render($form_array);
if ($default_value === TRUE) {
$checked = TRUE;
......
......@@ -10,6 +10,7 @@
use Drupal\Component\Serialization\Json;
use Drupal\Component\Utility\String;
use Drupal\Core\Render\Element;
use Drupal\form_test\Form\FormTestDisabledElementsForm;
use Drupal\simpletest\WebTestBase;
/**
......@@ -158,11 +159,10 @@ function testRequiredFields() {
* is submitted twice, first without values for required fields and then
* with values. Each submission is checked for relevant error messages.
*
* @see form_test_validate_required_form()
* @see \Drupal\form_test\Form\FormTestValidateRequiredForm
*/
function testRequiredCheckboxesRadio() {
$form = $form_state = array();
$form = form_test_validate_required_form($form, $form_state);
$form = \Drupal::formBuilder()->getForm('\Drupal\form_test\Form\FormTestValidateRequiredForm');
// Attempt to submit the form with no required fields set.
$edit = array();
......@@ -235,7 +235,7 @@ function testRequiredCheckboxesRadio() {
* and then with value. Each submission is checked for relevant error
* messages.
*
* @see form_test_validate_required_form_no_title()
* @see \Drupal\form_test\Form\FormTestValidateRequiredNoTitleForm
*/
function testRequiredTextfieldNoTitle() {
// Attempt to submit the form with no required field set.
......@@ -293,8 +293,7 @@ function testCheckboxProcessing() {
* Tests validation of #type 'select' elements.
*/
function testSelect() {
$form = $form_state = array();
$form = form_test_select($form, $form_state);
$form = \Drupal::formBuilder()->getForm('Drupal\form_test\Form\FormTestSelectForm');
$error = '!name field is required.';
$this->drupalGet('form-test/select');
......@@ -366,8 +365,7 @@ function testEmptySelect() {
* Tests validation of #type 'number' and 'range' elements.
*/
function testNumber() {
$form = $form_state = array();
$form = form_test_number($form, $form_state);
$form = \Drupal::formBuilder()->getForm('\Drupal\form_test\Form\FormTestNumberForm');
// Array with all the error messages to be checked.
$error_messages = array(
......@@ -484,7 +482,7 @@ function testColorValidation() {
function testDisabledElements() {
// Get the raw form in its original state.
$form_state = array();
$form = _form_test_disabled_elements(array(), $form_state);
$form = (new FormTestDisabledElementsForm())->buildForm(array(), $form_state);
// Build a submission that tries to hijack the form by submitting input for
// elements that are disabled.
......@@ -568,8 +566,7 @@ function assertFormValuesDefault($values, $form) {
*/
function testDisabledMarkup() {
$this->drupalGet('form-test/disabled-elements');
$form_state = array();
$form = _form_test_disabled_elements(array(), $form_state);
$form = \Drupal::formBuilder()->getForm('\Drupal\form_test\Form\FormTestDisabledElementsForm');
$type_map = array(
'textarea' => 'textarea',
'select' => 'select',
......
......@@ -72,7 +72,7 @@ function testSubmissionWorkflow() {
private function submitForm($values, $valid_input) {
// Programmatically submit the given values.
$form_state = array('values' => $values);
drupal_form_submit('form_test_programmatic_form', $form_state);
\Drupal::formBuilder()->submitForm('\Drupal\form_test\Form\FormTestProgrammaticForm', $form_state);
// Check that the form returns an error when expected, and vice versa.
$errors = form_get_errors($form_state);
......@@ -106,7 +106,7 @@ public function testProgrammaticAccessBypass() {
// Programmatically submit the form with a value for the restricted field.
// Since programmed_bypass_access_check is set to TRUE by default, the
// field is accessible and can be set.
\Drupal::formBuilder()->submitForm('form_test_programmatic_form', $form_state);
\Drupal::formBuilder()->submitForm('\Drupal\form_test\Form\FormTestProgrammaticForm', $form_state);
$values = $form_state['storage']['programmatic_form_submit'];
$this->assertEqual($values['field_restricted'], 'dummy value', 'The value for the restricted field is stored correctly.');
......@@ -115,7 +115,7 @@ public function testProgrammaticAccessBypass() {
// restrictions apply, the restricted field is inaccessible, and the value
// should not be stored.
$form_state['programmed_bypass_access_check'] = FALSE;
\Drupal::formBuilder()->submitForm('form_test_programmatic_form', $form_state);
\Drupal::formBuilder()->submitForm('\Drupal\form_test\Form\FormTestProgrammaticForm', $form_state);
$values = $form_state['storage']['programmatic_form_submit'];
$this->assertNotEqual($values['field_restricted'], 'dummy value', 'The value for the restricted field is not stored.');
......
......@@ -197,11 +197,10 @@ function testPatternValidation() {
/**
* Tests #required with custom validation errors.
*
* @see form_test_validate_required_form()
* @see \Drupal\form_test\Form\FormTestValidateRequiredForm
*/
function testCustomRequiredError() {
$form = $form_state = array();
$form = form_test_validate_required_form($form, $form_state);
$form = \Drupal::formBuilder()->getForm('\Drupal\form_test\Form\FormTestValidateRequiredForm');
// Verify that a custom #required error can be set.
$edit = array();
......
......@@ -5,14 +5,6 @@
* Helper module for the form API tests.
*/
use Drupal\Component\Serialization\Json;
use Drupal\Component\Utility\String;
use Drupal\Core\Database\Database;
use Drupal\Core\Language\LanguageInterface;
use Drupal\form_test\Callbacks;
use Drupal\Core\Datetime\DrupalDateTime;
use Symfony\Component\HttpFoundation\JsonResponse;
/**
* Implements hook_permission().
*/
......@@ -28,28 +20,6 @@ function form_test_permission() {
return $perms;
}
/**
* Form submit handler to return form values as JSON.
*/
function _form_test_submit_values_json($form, &$form_state) {
$response = new JsonResponse($form_state['values']);
// @todo remove once converted to new routing system.
$response->send();
exit;
}
/**
* Form builder for testing hook_form_alter() and hook_form_FORM_ID_alter().
*
* @deprecated Use \Drupal\form_test\alterForm()
*/
function form_test_alter_form($form, &$form_state) {
// Elements can be added as needed for future testing needs, but for now,
// we're only testing alter hooks that do not require any elements added by
// this function.
return $form;
}
/**
* Implements hook_form_FORM_ID_alter() on behalf of block.module.
*/
......@@ -80,272 +50,6 @@ function system_form_form_test_alter_form_alter(&$form, &$form_state) {
drupal_set_message('system_form_form_test_alter_form_alter() executed.');
}
/**
* Form builder for testing \Drupal\Core\Form\FormValidatorInterface::validateForm().
*
* Serves for testing form processing and alterations by form validation
* handlers, especially for the case of a validation error:
* - form_set_value() should be able to alter submitted values in
* $form_state['values'] without affecting the form element.
* - #element_validate handlers should be able to alter the $element in the form
* structure and the alterations should be contained in the rebuilt form.
* - #validate handlers should be able to alter the $form and the alterations
* should be contained in the rebuilt form.
*
* @deprecated Use \Drupal\form_test\validateForm()
*/
function form_test_validate_form($form, &$form_state) {
$object = new Callbacks();
$form['name'] = array(
'#type' => 'textfield',
'#title' => 'Name',
'#default_value' => '',
'#element_validate' => array(array($object, 'validateName')),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Save',
);
// To simplify this test, enable form caching and use form storage to
// remember our alteration.
$form_state['cache'] = TRUE;
return $form;
}
/**
* Form validation handler for form_test_validate_form().
*/
function form_test_validate_form_validate(&$form, &$form_state) {
if ($form_state['values']['name'] == 'validate') {
// Alter the form element.
$form['name']['#value'] = '#value changed by #validate';
// Alter the submitted value in $form_state.
form_set_value($form['name'], 'value changed by form_set_value() in #validate', $form_state);
// Output the element's value from $form_state.
drupal_set_message(t('@label value: @value', array('@label' => $form['name']['#title'], '@value' => $form_state['values']['name'])));
// Trigger a form validation error to see our changes.
form_set_error('', $form_state);
}
}
/**
* Form constructor to test the #required property.
*
* @deprecated Use \Drupal\form_test\validateRequiredForm()
*/
function form_test_validate_required_form($form, &$form_state) {
$options = array('foo' => 'foo', 'bar' => 'bar');
$validate = array('form_test_validate_required_form_element_validate');
$form['textfield'] = array(
'#type' => 'textfield',
'#title' => 'Name',
'#required' => TRUE,
'#required_error' => t('Please enter a name.'),
);
$form['checkboxes'] = array(
'#type' => 'checkboxes',
'#title' => 'Checkboxes',
'#options' => $options,
'#required' => TRUE,
'#form_test_required_error' => t('Please choose at least one option.'),
'#element_validate' => $validate,
);
$form['select'] = array(
'#type' => 'select',
'#title' => 'Select',
'#options' => $options,
'#required' => TRUE,
'#form_test_required_error' => t('Please select something.'),
'#element_validate' => $validate,
);
$form['radios'] = array(
'#type' => 'radios',
'#title' => 'Radios',
'#options' => $options,
'#required' => TRUE,
);
$form['radios_optional'] = array(
'#type' => 'radios',
'#title' => 'Radios (optional)',
'#options' => $options,
);
$form['radios_optional_default_value_false'] = array(
'#type' => 'radios',
'#title' => 'Radios (optional, with a default value of FALSE)',
'#options' => $options,
'#default_value' => FALSE,
);
$form['actions'] = array('#type' => 'actions');
$form['actions']['submit'] = array('#type' => 'submit', '#value' => 'Submit');
return $form;
}
/**
* Form element validation handler for 'Name' field in form_test_validate_required_form().
*/
function form_test_validate_required_form_element_validate($element, &$form_state) {
// Set a custom validation error on the #required element.
if (!empty($element['#required_but_empty']) && isset($element['#form_test_required_error'])) {
form_error($element, $form_state, $element['#form_test_required_error']);
}
}
/**
* Form submission handler for form_test_validate_required_form().
*/
function form_test_validate_required_form_submit($form, &$form_state) {
drupal_set_message('The form_test_validate_required_form form was submitted successfully.');
}
/**
* Form constructor to test the #required property without #title.
*
* @deprecated Use \Drupal\form_test\validateRequiredFormNoTitle()
*/
function form_test_validate_required_form_no_title($form, &$form_state) {
$form['textfield'] = array(
'#type' => 'textfield',
'#required' => TRUE,
);
$form['actions'] = array('#type' => 'actions');
$form['actions']['submit'] = array('#type' => 'submit', '#value' => 'Submit');
return $form;
}
/**
* Form submission handler for form_test_validate_required_form_no_title().
*/
function form_test_validate_required_form_no_title_submit($form, &$form_state) {
drupal_set_message('The form_test_validate_required_form_no_title form was submitted successfully.');
}
/**
* Builds a simple form with a button triggering partial validation.
*
* @deprecated Use \Drupal\form_test\validateFormWithErrorSuppression()
*/
function form_test_limit_validation_errors_form($form, &$form_state) {
$form['title'] = array(
'#type' => 'textfield',
'#title' => 'Title',
'#required' => TRUE,
);
$form['test'] = array(
'#title' => 'Test',
'#type' => 'textfield',
'#element_validate' => array('form_test_limit_validation_errors_element_validate_test'),
);
$form['test_numeric_index'] = array(
'#tree' => TRUE,
);
$form['test_numeric_index'][0] = array(
'#title' => 'Test (numeric index)',
'#type' => 'textfield',
'#element_validate' => array('form_test_limit_validation_errors_element_validate_test'),
);
$form['test_substring'] = array(
'#tree' => TRUE,
);
$form['test_substring']['foo'] = array(
'#title' => 'Test (substring) foo',
'#type' => 'textfield',
'#element_validate' => array('form_test_limit_validation_errors_element_validate_test'),
);
$form['test_substring']['foobar'] = array(
'#title' => 'Test (substring) foobar',
'#type' => 'textfield',
'#element_validate' => array('form_test_limit_validation_errors_element_validate_test'),
);
$form['actions']['partial'] = array(
'#type' => 'submit',
'#limit_validation_errors' => array(array('test')),
'#submit' => array('form_test_limit_validation_errors_form_partial_submit'),
'#value' => t('Partial validate'),
);
$form['actions']['partial_numeric_index'] = array(
'#type' => 'submit',
'#limit_validation_errors' => array(array('test_numeric_index', 0)),
'#submit' => array('form_test_limit_validation_errors_form_partial_submit'),
'#value' => t('Partial validate (numeric index)'),
);
$form['actions']['substring'] = array(
'#type' => 'submit',
'#limit_validation_errors' => array(array('test_substring', 'foo')),
'#submit' => array('form_test_limit_validation_errors_form_partial_submit'),
'#value' => t('Partial validate (substring)'),
);
$form['actions']['full'] = array(
'#type' => 'submit',
'#value' => t('Full validate'),
);
return $form;
}
/**
* Form element validation handler for the 'test' element.
*/
function form_test_limit_validation_errors_element_validate_test(&$element, &$form_state) {
if ($element['#value'] == 'invalid') {
form_error($element, $form_state, t('@label element is invalid', array('@label' => $element['#title'])));
}
}
/**
* Form submit handler for the partial validation submit button.
*/
function form_test_limit_validation_errors_form_partial_submit($form, $form_state) {
// The title has not been validated, thus its value - in case of the test case
// an empty string - may not be set.
if (!isset($form_state['values']['title']) && isset($form_state['values']['test'])) {
drupal_set_message('Only validated values appear in the form values.');
}
}
/**
* Builds a simple form using the FAPI #pattern proterty.
*
* @deprecated Use \Drupal\form_test\validatePattern()
*/
function form_test_pattern_form($form, &$form_state) {
$form['textfield'] = array(
'#type' => 'textfield',
'#title' => 'One digit followed by lowercase letters',
'#pattern' => '[0-9][a-z]+',
);
$form['tel'] = array(
'#type' => 'tel',
'#title' => 'Everything except numbers',
'#pattern' => '[^\d]*',
);
$form['password'] = array(
'#type' => 'password',
'#title' => 'Password',
'#pattern' => '[01]+',
);
$form['url'] = array(
'#type' => 'url',
'#title' => 'Client side validation',
'#decription' => 'Just client side validation, using the #pattern attribute.',
'#attributes' => array(
'pattern' => '.*foo.*',
),
'#pattern' => 'ignored',
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Submit',
);
return $form;
}
/**
* Create a header and options array. Helper function for callbacks.
*/
......@@ -385,1534 +89,33 @@ function _form_test_tableselect_get_data() {
}
/**
* 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 #ajax callbacks.
*
* @see form_test_storage_form_submit()
* Implements hook_form_FORM_ID_alter().
*
* @deprecated Use \Drupal\form_test\testStorage()
* @see form_test_state_persist()
*/
function form_test_storage_form($form, &$form_state) {
if ($form_state['rebuild']) {
$form_state['input'] = array();
}
// Initialize
if (empty($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' => '',
),
);
}
// Count how often the form is constructed.
$_SESSION['constructions']++;
drupal_set_message("Form constructions: " . $_SESSION['constructions']);
$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'],
'#element_validate' => array('form_test_storage_element_validate_value_cached'),
);
$form['continue_button'] = array(
'#type' => 'button',
'#value' => 'Reset',
// Rebuilds the form without keeping the values.
);
$form['continue_submit'] = array(
'#type' => 'submit',
'#value' => 'Continue submit',
'#submit' => array('form_storage_test_form_continue_submit'),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Save',
);
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 (\Drupal::request()->get('cache')) {
// Manually activate caching, so we can test that the storage keeps working
// when it's enabled.
$form_state['cache'] = TRUE;
}
return $form;
}
/**
* Form element validation handler for 'value' element in form_test_storage_form().
*
* Tests updating of cached form storage during validation.
*/
function form_test_storage_element_validate_value_cached($element, &$form_state) {
// If caching is enabled and we receive a certain value, change the storage.
// This presumes that another submitted form value triggers a validation error
// elsewhere in the form. Form API should still update the cached form storage
// though.
if (\Drupal::request()->get('cache') && $form_state['values']['value'] == 'change_title') {
$form_state['storage']['thing']['changed'] = TRUE;
}
}
/**
* Form submit handler to continue multi-step form.
*/
function form_storage_test_form_continue_submit($form, &$form_state) {
$form_state['storage']['thing']['title'] = $form_state['values']['title'];
$form_state['storage']['thing']['value'] = $form_state['values']['value'];
$form_state['rebuild'] = TRUE;
}
/**
* Form submit handler to finish multi-step form.
*/
function form_test_storage_form_submit($form, &$form_state) {
drupal_set_message("Title: " . String::checkPlain($form_state['values']['title']));
drupal_set_message("Form constructions: " . $_SESSION['constructions']);
if (isset($form_state['storage']['thing']['changed'])) {
drupal_set_message("The thing has been changed.");
}
$form_state['redirect_route']['route_name'] = '<front>';
}
/**
* A form for testing form labels and required marks.
*
* @deprecated Use \Drupal\form_test\testLabel()
*/
function form_label_test_form() {
$form['form_checkboxes_test'] = array(
'#type' => 'checkboxes',
'#title' => t('Checkboxes test'),
'#options' => array(
'first-checkbox' => t('First checkbox'),
'second-checkbox' => t('Second checkbox'),
'third-checkbox' => t('Third checkbox'),
'0' => t('0'),
),
);
$form['form_radios_test'] = array(
'#type' => 'radios',
'#title' => t('Radios test'),
'#options' => array(
'first-radio' => t('First radio'),
'second-radio' => t('Second radio'),
'third-radio' => t('Third radio'),
'0' => t('0'),
),
// Test #field_prefix and #field_suffix placement.
'#field_prefix' => '<span id="form-test-radios-field-prefix">' . t('Radios #field_prefix element') . '</span>',
'#field_suffix' => '<span id="form-test-radios-field-suffix">' . t('Radios #field_suffix element') . '</span>',
);
$form['form_checkbox_test'] = array(
'#type' => 'checkbox',
'#title' => t('Checkbox test'),
);
$form['form_textfield_test_title_and_required'] = array(
'#type' => 'textfield',
'#title' => t('Textfield test for required with title'),
'#required' => TRUE,
);
$form['form_textfield_test_no_title_required'] = array(
'#type' => 'textfield',
// We use an empty title, since not setting #title suppresses the label
// and required marker.
'#title' => '',
'#required' => TRUE,
);
$form['form_textfield_test_title'] = array(
'#type' => 'textfield',
'#title' => t('Textfield test for title only'),
// Not required.
// Test #prefix and #suffix placement.
'#prefix' => '<div id="form-test-textfield-title-prefix">' . t('Textfield #prefix element') . '</div>',
'#suffix' => '<div id="form-test-textfield-title-suffix">' . t('Textfield #suffix element') . '</div>',
);
$form['form_textfield_test_title_after'] = array(
'#type' => 'textfield',
'#title' => t('Textfield test for title after element'),
'#title_display' => 'after',
);
$form['form_textfield_test_title_invisible'] = array(
'#type' => 'textfield',
'#title' => t('Textfield test for invisible title'),
'#title_display' => 'invisible',
);
// Textfield test for title set not to display.
$form['form_textfield_test_title_no_show'] = array(
'#type' => 'textfield',
);
// Checkboxes & radios with title as attribute.
$form['form_checkboxes_title_attribute'] = array(
'#type' => 'checkboxes',
'#title' => 'Checkboxes test',
'#title_display' => 'attribute',
'#options' => array(
'first-checkbox' => 'First checkbox',
'second-checkbox' => 'Second checkbox',
),
'#required' => TRUE,
);
$form['form_radios_title_attribute'] = array(
'#type' => 'radios',
'#title' => 'Radios test',
'#title_display' => 'attribute',
'#options' => array(
'first-radio' => 'First radio',
'second-radio' => 'Second radio',
),
'#required' => TRUE,
);
return $form;
}
/**
* Form builder for form_state_values_clean() test.
*
* @deprecated Use \Drupal\form_test\testFormStateClean()
*/
function form_test_form_state_values_clean_form($form, &$form_state) {
// Build an example form containing multiple submit and button elements; not
// only on the top-level.
$form = array('#tree' => TRUE);
$form['foo'] = array('#type' => 'submit', '#value' => t('Submit'));
$form['bar'] = array('#type' => 'submit', '#value' => t('Submit'));
$form['beer'] = array('#type' => 'value', '#value' => 1000);
$form['baz']['foo'] = array('#type' => 'button', '#value' => t('Submit'));
$form['baz']['baz'] = array('#type' => 'submit', '#value' => t('Submit'));
$form['baz']['beer'] = array('#type' => 'value', '#value' => 2000);
return $form;
}
/**
* Form submit handler for form_state_values_clean() test form.
*/
function form_test_form_state_values_clean_form_submit($form, &$form_state) {
form_state_values_clean($form_state);
// This won't have a proper JSON header, but Drupal doesn't check for that
// anyway so this is fine until it's replaced with a JsonResponse.
print Json::encode($form_state['values']);
exit;
}
/**
* Form constructor for the form_state_values_clean() test.
*
* @deprecated Use \Drupal\form_test\testFormStateCleanAdvanced()
* Implements hook_form_FORM_ID_alter() for the registration form.
*/
function form_test_form_state_values_clean_advanced_form($form, &$form_state) {
// Build an example form containing a managed file and a submit form element.
$form['image'] = array(
'#type' => 'managed_file',
'#title' => t('Image'),
'#upload_location' => 'public://',
'#default_value' => 0,
);
$form['submit'] = array(
function form_test_form_user_register_form_alter(&$form, &$form_state) {
$form['test_rebuild'] = array(