diff --git a/core/modules/system/src/Tests/Form/FormTest.php b/core/modules/system/src/Tests/Form/FormTest.php index a380bd7..10d168f 100644 --- a/core/modules/system/src/Tests/Form/FormTest.php +++ b/core/modules/system/src/Tests/Form/FormTest.php @@ -161,8 +161,7 @@ function testRequiredFields() { * @see form_test_validate_required_form() */ 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(); @@ -293,8 +292,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 +364,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( @@ -483,8 +480,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 = \Drupal::formBuilder()->getForm('\Drupal\form_test\Form\FormTestDisabledElementsForm'); // Build a submission that tries to hijack the form by submitting input for // elements that are disabled. @@ -568,8 +564,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', diff --git a/core/modules/system/src/Tests/Form/ValidationTest.php b/core/modules/system/src/Tests/Form/ValidationTest.php index 7fef3c9..e6d65e3 100644 --- a/core/modules/system/src/Tests/Form/ValidationTest.php +++ b/core/modules/system/src/Tests/Form/ValidationTest.php @@ -200,8 +200,7 @@ function testPatternValidation() { * @see form_test_validate_required_form() */ 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(); diff --git a/core/modules/system/tests/modules/form_test/form_test.module b/core/modules/system/tests/modules/form_test/form_test.module index 340c1f9..1fe0bbb 100644 --- a/core/modules/system/tests/modules/form_test/form_test.module +++ b/core/modules/system/tests/modules/form_test/form_test.module @@ -29,28 +29,6 @@ function form_test_permission() { } /** - * 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. */ function block_form_form_test_alter_form_alter(&$form, &$form_state) { @@ -81,111 +59,6 @@ function system_form_form_test_alter_form_alter(&$form, &$form_state) { } /** - * Form builder for testing drupal_validate_form(). - * - * 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) { @@ -196,100 +69,6 @@ function form_test_validate_required_form_element_validate($element, &$form_stat } /** - * 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) { @@ -299,54 +78,6 @@ function form_test_limit_validation_errors_element_validate_test(&$element, &$fo } /** - * 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. */ function _form_test_tableselect_get_data() { @@ -385,75 +116,6 @@ 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() - * - * @deprecated Use \Drupal\form_test\testStorage() - */ -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', - ); - - 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. @@ -478,18 +140,6 @@ function form_storage_test_form_continue_submit($form, &$form_state) { } /** - * 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'] = ''; -} - -/** * A form for testing form labels and required marks. * * @deprecated Use \Drupal\form_test\testLabel() @@ -582,772 +232,6 @@ function form_label_test_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() - */ -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( - '#type' => 'submit', - '#value' => t('Submit'), - ); - return $form; -} - -/** - * Form submission handler for form_test_form_state_values_clean_advanced_form(). - */ -function form_test_form_state_values_clean_advanced_form_submit($form, &$form_state) { - form_state_values_clean($form_state); - print t('You WIN!'); - exit; -} - -/** - * Builds a form to test #type 'select' validation. - * - * @deprecated Use \Drupal\form_test\testSelect() - */ -function form_test_select($form, &$form_state) { - $form['#submit'] = array('_form_test_submit_values_json'); - - $base = array( - '#type' => 'select', - '#options' => array('one' => 'one', 'two' => 'two', 'three' => 'three'), - ); - - $form['select'] = $base + array( - '#title' => '#default_value one', - '#default_value' => 'one', - ); - $form['select_required'] = $base + array( - '#title' => '#default_value one, #required', - '#required' => TRUE, - '#default_value' => 'one', - ); - $form['select_optional'] = $base + array( - '#title' => '#default_value one, not #required', - '#required' => FALSE, - '#default_value' => 'one', - ); - $form['empty_value'] = $base + array( - '#title' => '#default_value one, #required, #empty_value 0', - '#required' => TRUE, - '#default_value' => 'one', - '#empty_value' => 0, - ); - $form['empty_value_one'] = $base + array( - '#title' => '#default_value = #empty_value, #required', - '#required' => TRUE, - '#default_value' => 'one', - '#empty_value' => 'one', - ); - - $form['no_default'] = $base + array( - '#title' => 'No #default_value, #required', - '#required' => TRUE, - ); - $form['no_default_optional'] = $base + array( - '#title' => 'No #default_value, not #required', - '#required' => FALSE, - '#description' => 'Should result in "one" because it is not required and there is no #empty_value requested, so default browser behavior of preselecting first option is in effect.', - ); - $form['no_default_optional_empty_value'] = $base + array( - '#title' => 'No #default_value, not #required, #empty_value empty string', - '#empty_value' => '', - '#required' => FALSE, - '#description' => 'Should result in an empty string (due to #empty_value) because it is optional.', - ); - - $form['no_default_empty_option'] = $base + array( - '#title' => 'No #default_value, #required, #empty_option', - '#required' => TRUE, - '#empty_option' => '- Choose -', - ); - $form['no_default_empty_option_optional'] = $base + array( - '#title' => 'No #default_value, not #required, #empty_option', - '#empty_option' => '- Dismiss -', - '#description' => 'Should result in an empty string (default of #empty_value) because it is optional.', - ); - - $form['no_default_empty_value'] = $base + array( - '#title' => 'No #default_value, #required, #empty_value 0', - '#required' => TRUE, - '#empty_value' => 0, - '#description' => 'Should never result in 0.', - ); - $form['no_default_empty_value_one'] = $base + array( - '#title' => 'No #default_value, #required, #empty_value one', - '#required' => TRUE, - '#empty_value' => 'one', - '#description' => 'A mistakenly assigned #empty_value contained in #options should not be valid.', - ); - $form['no_default_empty_value_optional'] = $base + array( - '#title' => 'No #default_value, not #required, #empty_value 0', - '#required' => FALSE, - '#empty_value' => 0, - '#description' => 'Should result in 0 because it is optional.', - ); - - $form['multiple'] = $base + array( - '#title' => '#multiple, #default_value two', - '#default_value' => array('two'), - '#multiple' => TRUE, - ); - $form['multiple_no_default'] = $base + array( - '#title' => '#multiple, no #default_value', - '#multiple' => TRUE, - ); - $form['multiple_no_default_required'] = $base + array( - '#title' => '#multiple, #required, no #default_value', - '#required' => TRUE, - '#multiple' => TRUE, - ); - - $form['submit'] = array('#type' => 'submit', '#value' => 'Submit'); - return $form; -} - -/** - * Builds a form to test select elements when #options is not an array. - * - * @deprecated Use \Drupal\form_test\testEmptySelect() - */ -function form_test_empty_select($form, &$form_state) { - $form['empty_select'] = array( - '#type' => 'select', - '#title' => t('Empty Select'), - '#multiple' => FALSE, - '#options' => NULL, - ); - return $form; -} - -/** - * Builds a form to test the language select form element. - * - * @deprecated Use \Drupal\form_test\testLanguageSelect() - */ -function form_test_language_select() { - $form['#submit'] = array('_form_test_submit_values_json'); - - $form['languages_all'] = array( - '#title' => t('Languages: All'), - '#type' => 'language_select', - '#languages' => LanguageInterface::STATE_ALL, - '#default_value' => 'xx', - ); - $form['languages_configurable'] = array( - '#title' => t('Languages: Configurable'), - '#type' => 'language_select', - '#languages' => LanguageInterface::STATE_CONFIGURABLE, - '#default_value' => 'en', - ); - $form['languages_locked'] = array( - '#title' => t('Languages: Locked'), - '#type' => 'language_select', - '#languages' => LanguageInterface::STATE_LOCKED, - ); - $form['languages_config_and_locked'] = array( - '#title' => t('Languages: Configurable and locked'), - '#type' => 'language_select', - '#languages' => LanguageInterface::STATE_CONFIGURABLE | LanguageInterface::STATE_LOCKED, - '#default_value' => 'dummy_value', - ); - $form['language_custom_options'] = array( - '#title' => t('Languages: Custom'), - '#type' => 'language_select', - '#languages' => LanguageInterface::STATE_CONFIGURABLE | LanguageInterface::STATE_LOCKED, - '#options' => array('opt1' => 'First option', 'opt2' => 'Second option', 'opt3' => 'Third option'), - '#default_value' => 'opt2', - ); - - $form['submit'] = array('#type' => 'submit', '#value' => 'Submit'); - return $form; -} - -/** - * Builds a form to test #type 'number' and 'range' validation. - * - * @param $element - * The element type to test. Can be 'number' or 'range'. Defaults to 'number'. - * - * @deprecated Use \Drupal\form_test\testNumber() - * - * @deprecated Use \Drupal\form_test\testNumberRange() - */ -function form_test_number($form, &$form_state, $element = 'number') { - $base = array( - '#type' => $element, - ); - - $form['integer_no_number'] = $base + array( - '#title' => 'Integer test, #no_error', - '#default_value' => '#no_number', - ); - $form['integer_no_step'] = $base + array( - '#title' => 'Integer test without step', - '#default_value' => 5, - ); - $form['integer_no_step_step_error'] = $base + array( - '#title' => 'Integer test without step, #step_error', - '#default_value' => 4.5, - ); - $form['integer_step'] = $base + array( - '#title' => 'Integer test with step', - '#default_value' => 5, - '#step' => 1, - ); - $form['integer_step_error'] = $base + array( - '#title' => 'Integer test, with step, #step_error', - '#default_value' => 5, - '#step' => 2, - ); - $form['integer_step_min'] = $base + array( - '#title' => 'Integer test with min value', - '#default_value' => 5, - '#min' => 0, - '#step' => 1, - ); - $form['integer_step_min_error'] = $base + array( - '#title' => 'Integer test with min value, #min_error', - '#default_value' => 5, - '#min' => 6, - '#step' => 1, - ); - $form['integer_step_max'] = $base + array( - '#title' => 'Integer test with max value', - '#default_value' => 5, - '#max' => 6, - '#step' => 1, - ); - $form['integer_step_max_error'] = $base + array( - '#title' => 'Integer test with max value, #max_error', - '#default_value' => 5, - '#max' => 4, - '#step' => 1, - ); - $form['integer_step_min_border'] = $base + array( - '#title' => 'Integer test with min border check', - '#default_value' => -1, - '#min' => -1, - '#step' => 1, - ); - $form['integer_step_max_border'] = $base + array( - '#title' => 'Integer test with max border check', - '#default_value' => 5, - '#max' => 5, - '#step' => 1, - ); - $form['integer_step_based_on_min'] = $base + array( - '#title' => 'Integer test with step based on min check', - '#default_value' => 3, - '#min' => -1, - '#step' => 2, - ); - $form['integer_step_based_on_min_error'] = $base + array( - '#title' => 'Integer test with step based on min check, #step_error', - '#default_value' => 4, - '#min' => -1, - '#step' => 2, - ); - $form['float_small_step'] = $base + array( - '#title' => 'Float test with a small step', - '#default_value' => 4, - '#step' => 0.0000000000001, - ); - $form['float_step_no_error'] = $base + array( - '#title' => 'Float test', - '#default_value' => 1.2, - '#step' => 0.3, - ); - $form['float_step_error'] = $base + array( - '#title' => 'Float test, #step_error', - '#default_value' => 1.3, - '#step' => 0.3, - ); - $form['float_step_hard_no_error'] = $base + array( - '#title' => 'Float test hard', - '#default_value' => 0.9411764729088, - '#step' => 0.00392156863712, - ); - $form['float_step_hard_error'] = $base + array( - '#title' => 'Float test hard, #step_error', - '#default_value' => 0.9411764, - '#step' => 0.00392156863, - ); - $form['float_step_any_no_error'] = $base + array( - '#title' => 'Arbitrary float', - '#default_value' => 0.839562930284, - '#step' => 'aNy', - ); - $form['submit'] = array( - '#type' => 'submit', - '#value' => 'Submit', - ); - return $form; -} - -/** - * Form constructor for testing #type 'range' elements. - * - * @see form_test_range_submit() - * - * @deprecated Use \Drupal\form_test\testRange() - */ -function form_test_range($form, &$form_state) { - $form['#submit'] = array('_form_test_submit_values_json'); - - $form['with_default_value'] = array( - '#type' => 'range', - '#title' => 'Range with default value', - '#min' => 10, - '#max' => 20, - '#step' => 2, - '#default_value' => 18, - '#description' => 'The default value is 18.', - ); - $form['float'] = array( - '#type' => 'range', - '#title' => 'Float', - '#min' => 10, - '#max' => 11, - '#step' => 'any', - '#description' => 'Floating point number between 10 and 11.', - ); - $form['integer'] = array( - '#type' => 'range', - '#title' => 'Integer', - '#min' => 2, - '#max' => 8, - '#step' => 2, - '#description' => 'Even integer between 2 and 8.', - ); - $form['offset'] = array( - '#type' => 'range', - '#title' => 'Offset', - '#min' => 2.9, - '#max' => 10.9, - '#description' => 'Value between 2.9 and 10.9.', - ); - $form['submit'] = array( - '#type' => 'submit', - '#value' => 'Submit', - ); - return $form; -} - -/** - * Form constructor for testing invalid #type 'range' elements. - * - * @deprecated Use \Drupal\form_test\testRangeInvalid() - */ -function form_test_range_invalid($form, &$form_state) { - $form['minmax'] = array( - '#type' => 'range', - '#min' => 10, - '#max' => 5, - '#title' => 'Invalid range', - '#description' => 'Minimum greater than maximum.', - ); - $form['submit'] = array( - '#type' => 'submit', - '#value' => 'Submit', - ); - return $form; -} - -/** - * Form constructor for testing #type 'color' elements. - * - * @see form_test_color_submit() - * - * @deprecated Use \Drupal\form_test\testColor() - */ -function form_test_color($form, &$form_state) { - $form['#submit'] = array('_form_test_submit_values_json'); - - $form['color'] = array( - '#type' => 'color', - '#title' => 'Color', - ); - $form['submit'] = array( - '#type' => 'submit', - '#value' => 'Submit', - ); - return $form; -} - -/** - * Builds a form to test the placeholder attribute. - * - * @deprecated Use \Drupal\form_test\testPlaceholder() - */ -function form_test_placeholder_test($form, &$form_state) { - foreach (array('textfield', 'textarea', 'url', 'password', 'search', 'tel', 'email', 'number') as $type) { - $form[$type] = array( - '#type' => $type, - '#title' => $type, - '#placeholder' => 'placeholder-text', - ); - } - - return $form; -} - -/** - * Form constructor to test expansion of #type checkboxes and radios. - * - * @deprecated Use \Drupal\form_test\testCheckboxesRadios() - */ -function form_test_checkboxes_radios($form, &$form_state, $customize = FALSE) { - $form['#submit'] = array('_form_test_submit_values_json'); - - // Expand #type checkboxes, setting custom element properties for some but not - // all options. - $form['checkboxes'] = array( - '#type' => 'checkboxes', - '#title' => 'Checkboxes', - '#options' => array( - 0 => 'Zero', - 'foo' => 'Foo', - 1 => 'One', - 'bar' => 'Bar', - '>' => 'Special Char', - ), - ); - if ($customize) { - $form['checkboxes'] += array( - 'foo' => array( - '#description' => 'Enable to foo.', - ), - 1 => array( - '#weight' => 10, - ), - ); - } - - // Expand #type radios, setting custom element properties for some but not - // all options. - $form['radios'] = array( - '#type' => 'radios', - '#title' => 'Radios', - '#options' => array( - 0 => 'Zero', - 'foo' => 'Foo', - 1 => 'One', - 'bar' => 'Bar', - '>' => 'Special Char', - ), - ); - if ($customize) { - $form['radios'] += array( - 'foo' => array( - '#description' => 'Enable to foo.', - ), - 1 => array( - '#weight' => 10, - ), - ); - } - - $form['submit'] = array('#type' => 'submit', '#value' => 'Submit'); - - return $form; -} - -/** - * Form constructor for testing #type 'email' elements. - * - * @see form_test_email_submit() - * - * @deprecated Use \Drupal\form_test\testEmail() - */ -function form_test_email($form, &$form_state) { - $form['#submit'] = array('_form_test_submit_values_json'); - $form['email'] = array( - '#type' => 'email', - '#title' => 'Email address', - '#description' => 'An email address.', - ); - $form['email_required'] = array( - '#type' => 'email', - '#title' => 'Address', - '#required' => TRUE, - '#description' => 'A required email address field.', - ); - $form['submit'] = array( - '#type' => 'submit', - '#value' => 'Submit', - ); - return $form; -} - -/** - * Form constructor for testing #type 'url' elements. - * - * @see form_test_url_submit() - * - * @deprecated Use \Drupal\form_test\testUrl() - */ -function form_test_url($form, &$form_state) { - $form['#submit'] = array('_form_test_submit_values_json'); - $form['url'] = array( - '#type' => 'url', - '#title' => 'Optional URL', - '#description' => 'An optional URL field.', - ); - $form['url_required'] = array( - '#type' => 'url', - '#title' => 'Required URL', - '#description' => 'A required URL field.', - '#required' => TRUE, - ); - $form['submit'] = array( - '#type' => 'submit', - '#value' => 'Submit', - ); - return $form; -} - -/** - * Build a form to test disabled elements. - * - * @deprecated Use \Drupal\form_test\testDisabledElements() - */ -function _form_test_disabled_elements($form, &$form_state) { - $form['#submit'] = array('_form_test_submit_values_json'); - - // Elements that take a simple default value. - foreach (array('textfield', 'textarea', 'search', 'tel', 'hidden') as $type) { - $form[$type] = array( - '#type' => $type, - '#title' => $type, - '#default_value' => $type, - '#test_hijack_value' => 'HIJACK', - '#disabled' => TRUE, - ); - } - - // Multiple values option elements. - foreach (array('checkboxes', 'select') as $type) { - $form[$type . '_multiple'] = array( - '#type' => $type, - '#title' => $type . ' (multiple)', - '#options' => array( - 'test_1' => 'Test 1', - 'test_2' => 'Test 2', - ), - '#multiple' => TRUE, - '#default_value' => array('test_2' => 'test_2'), - // The keys of #test_hijack_value need to match the #name of the control. - // @see FormsTestCase::testDisabledElements() - '#test_hijack_value' => $type == 'select' ? array('' => 'test_1') : array('test_1' => 'test_1'), - '#disabled' => TRUE, - ); - } - - // Single values option elements. - foreach (array('radios', 'select') as $type) { - $form[$type . '_single'] = array( - '#type' => $type, - '#title' => $type . ' (single)', - '#options' => array( - 'test_1' => 'Test 1', - 'test_2' => 'Test 2', - ), - '#multiple' => FALSE, - '#default_value' => 'test_2', - '#test_hijack_value' => 'test_1', - '#disabled' => TRUE, - ); - } - - // Checkbox and radio. - foreach (array('checkbox', 'radio') as $type) { - $form[$type . '_unchecked'] = array( - '#type' => $type, - '#title' => $type . ' (unchecked)', - '#return_value' => 1, - '#default_value' => 0, - '#test_hijack_value' => 1, - '#disabled' => TRUE, - ); - $form[$type . '_checked'] = array( - '#type' => $type, - '#title' => $type . ' (checked)', - '#return_value' => 1, - '#default_value' => 1, - '#test_hijack_value' => NULL, - '#disabled' => TRUE, - ); - } - - // Weight, number, range. - foreach (array('weight', 'number', 'range') as $type) { - $form[$type] = array( - '#type' => $type, - '#title' => $type, - '#default_value' => 10, - '#test_hijack_value' => 5, - '#disabled' => TRUE, - ); - } - - // Color. - $form['color'] = array( - '#type' => 'color', - '#title' => 'color', - '#default_value' => '#0000ff', - '#test_hijack_value' => '#ff0000', - '#disabled' => TRUE, - ); - - // The #disabled state should propagate to children. - $form['disabled_container'] = array( - '#disabled' => TRUE, - ); - foreach (array('textfield', 'textarea', 'hidden', 'tel', 'url') as $type) { - $form['disabled_container']['disabled_container_' . $type] = array( - '#type' => $type, - '#title' => $type, - '#default_value' => $type, - '#test_hijack_value' => 'HIJACK', - ); - } - - // Date. - $date = new DrupalDateTime('1978-11-01 10:30:00', 'Europe/Berlin'); - $expected = array('date' => '1978-11-01 10:30:00', 'timezone_type' => 3, 'timezone' => 'Europe/Berlin',); - $form['disabled_container']['disabled_container_datetime'] = array( - '#type' => 'datetime', - '#title' => 'datetime', - '#default_value' => $date, - '#expected_value' => $expected, - '#test_hijack_value' => new DrupalDateTime('1978-12-02 11:30:00', 'Europe/Berlin'), - '#date_timezone' => 'Europe/Berlin', - ); - - - // Try to hijack the email field with a valid email. - $form['disabled_container']['disabled_container_email'] = array( - '#type' => 'email', - '#title' => 'email', - '#default_value' => 'foo@example.com', - '#test_hijack_value' => 'bar@example.com', - ); - - // Try to hijack the URL field with a valid URL. - $form['disabled_container']['disabled_container_url'] = array( - '#type' => 'url', - '#title' => 'url', - '#default_value' => 'http://example.com', - '#test_hijack_value' => 'http://example.com/foo', - ); - - // Text format. - $form['text_format'] = array( - '#type' => 'text_format', - '#title' => 'Text format', - '#disabled' => TRUE, - '#default_value' => 'Text value', - '#format' => 'plain_text', - '#expected_value' => array( - 'value' => 'Text value', - 'format' => 'plain_text', - ), - '#test_hijack_value' => array( - 'value' => 'HIJACK', - 'format' => 'filtered_html', - ), - ); - - // Password fields. - $form['password'] = array( - '#type' => 'password', - '#title' => 'Password', - '#disabled' => TRUE, - ); - $form['password_confirm'] = array( - '#type' => 'password_confirm', - '#title' => 'Password confirm', - '#disabled' => TRUE, - ); - - // Files. - $form['file'] = array( - '#type' => 'file', - '#title' => 'File', - '#disabled' => TRUE, - ); - $form['managed_file'] = array( - '#type' => 'managed_file', - '#title' => 'Managed file', - '#disabled' => TRUE, - ); - - // Buttons. - $form['image_button'] = array( - '#type' => 'image_button', - '#value' => 'Image button', - '#disabled' => TRUE, - ); - $form['button'] = array( - '#type' => 'button', - '#value' => 'Button', - '#disabled' => TRUE, - ); - $form['submit_disabled'] = array( - '#type' => 'submit', - '#value' => 'Submit', - '#disabled' => TRUE, - ); - - $form['submit'] = array( - '#type' => 'submit', - '#value' => t('Submit'), - ); - - return $form; -} - -/** * Form builder for testing preservation of values during a rebuild. * * @deprecated Use \Drupal\form_test\testRebuildPreservation() @@ -1705,46 +589,6 @@ function form_test_checkbox_type_juggling($form, $form_state, $default_value, $r } /** - * Tests checkboxes zero. - * - * @deprecated Use \Drupal\form_test\testCheckboxesZero() - */ -function form_test_checkboxes_zero($form, &$form_state, $json = TRUE) { - $form['checkbox_off'] = array( - '#title' => t('Checkbox off'), - '#type' => 'checkboxes', - '#options' => array('foo', 'bar', 'baz'), - ); - $form['checkbox_zero_default'] = array( - '#title' => t('Zero default'), - '#type' => 'checkboxes', - '#options' => array('foo', 'bar', 'baz'), - '#default_value' => array(0), - ); - $form['checkbox_string_zero_default'] = array( - '#title' => t('Zero default (string)'), - '#type' => 'checkboxes', - '#options' => array('foo', 'bar', 'baz'), - '#default_value' => array('0'), - ); - $form['submit'] = array( - '#type' => 'submit', - '#value' => 'Save', - ); - if ($json) { - $form['#submit'][] = '_form_test_submit_values_json'; - } - else { - $form['#submit'][] = '_form_test_checkboxes_zero_no_redirect'; - } - return $form; -} - -function _form_test_checkboxes_zero_no_redirect($form, &$form_state) { - $form_state['redirect'] = FALSE; -} - -/** * Builds a form to test the required attribute. * * @deprecated Use \Drupal\form_test\testRequired() diff --git a/core/modules/system/tests/modules/form_test/form_test.routing.yml b/core/modules/system/tests/modules/form_test/form_test.routing.yml index 56bd2d5..26d412f 100644 --- a/core/modules/system/tests/modules/form_test/form_test.routing.yml +++ b/core/modules/system/tests/modules/form_test/form_test.routing.yml @@ -71,7 +71,7 @@ form_test.autocomplete_2: form_test.alter_form: path: '/form-test/alter' defaults: - _content: '\Drupal\form_test\Form\FormTestForm::alterForm' + _form: '\Drupal\form_test\Form\FormTestAlterForm' _title: 'Form altering test' requirements: _access: 'TRUE' @@ -79,7 +79,7 @@ form_test.alter_form: form_test.validate_form: path: '/form-test/validate' defaults: - _content: '\Drupal\form_test\Form\FormTestForm::validateForm' + _form: '\Drupal\form_test\Form\FormTestValidateForm' _title: 'Form validation handlers test' requirements: _access: 'TRUE' @@ -87,7 +87,7 @@ form_test.validate_form: form_test.validate_required: path: '/form-test/validate-required' defaults: - _content: '\Drupal\form_test\Form\FormTestForm::validateRequiredForm' + _form: '\Drupal\form_test\Form\FormTestValidateRequiredForm' _title: 'Form #required validation' requirements: _access: 'TRUE' @@ -95,7 +95,7 @@ form_test.validate_required: form_test.validate_required_no_title: path: '/form-test/validate-required-no-title' defaults: - _content: '\Drupal\form_test\Form\FormTestForm::validateRequiredFormNoTitle' + _form: '\Drupal\form_test\Form\FormTestValidateRequiredNoTitleForm' _title: 'Form #required validation without #title' requirements: _access: 'TRUE' @@ -103,7 +103,7 @@ form_test.validate_required_no_title: form_test.validate_with_error_suppresion: path: '/form-test/limit-validation-errors' defaults: - _content: '\Drupal\form_test\Form\FormTestForm::validateFormWithErrorSuppression' + _form: '\Drupal\form_test\Form\FormTestLimitValidationErrorsForm' _title: 'Form validation with some error suppression' requirements: _access: 'TRUE' @@ -111,7 +111,7 @@ form_test.validate_with_error_suppresion: form_test.pattern: path: '/form-test/pattern' defaults: - _content: '\Drupal\form_test\Form\FormTestForm::validatePattern' + _form: '\Drupal\form_test\Form\FormTestPatternForm' _title: 'Pattern validation' requirements: _access: 'TRUE' @@ -167,7 +167,7 @@ form_test.vertical_tabs: form_test.storage: path: '/form_test/form-storage' defaults: - _content: '\Drupal\form_test\Form\FormTestForm::testStorage' + _form: '\Drupal\form_test\Form\FormTestStorageForm' _title: 'Form storage test' requirements: _access: 'TRUE' @@ -175,7 +175,7 @@ form_test.storage: form_test.state_clean: path: '/form_test/form-state-values-clean' defaults: - _content: '\Drupal\form_test\Form\FormTestForm::testFormStateClean' + _form: '\Drupal\form_test\Form\FormTestFormStateValuesCleanForm' _title: 'Form state values clearance test' requirements: _access: 'TRUE' @@ -183,7 +183,7 @@ form_test.state_clean: form_test.state_clean_advanced: path: '/form_test/form-state-values-clean-advanced' defaults: - _content: '\Drupal\form_test\Form\FormTestForm::testFormStateCleanAdvanced' + _form: '\Drupal\form_test\Form\FormTestFormStateValuesCleanAdvancedForm' _title: 'Form state values clearance advanced test' requirements: _access: 'TRUE' @@ -199,7 +199,7 @@ form_test.checkbox: form_test.select: path: '/form-test/select' defaults: - _content: '\Drupal\form_test\Form\FormTestForm::testSelect' + _form: '\Drupal\form_test\Form\FormTestSelectForm' _title: 'Select' requirements: _access: 'TRUE' @@ -207,7 +207,7 @@ form_test.select: form_test.empty_select: path: '/form-test/empty-select' defaults: - _content: '\Drupal\form_test\Form\FormTestForm::testEmptySelect' + _form: '\Drupal\form_test\Form\FormTestEmptySelectForm' _title: 'Empty Select Element' requirements: _access: 'TRUE' @@ -215,7 +215,7 @@ form_test.empty_select: form_test.language_select: path: '/form-test/language_select' defaults: - _content: '\Drupal\form_test\Form\FormTestForm::testLanguageSelect' + _form: '\Drupal\form_test\Form\FormTestLanguageSelectForm' _title: 'Language Select' requirements: _access: 'TRUE' @@ -223,7 +223,7 @@ form_test.language_select: form_test.placeholder: path: '/form-test/placeholder-text' defaults: - _content: '\Drupal\form_test\Form\FormTestForm::testPlaceholder' + _form: '\Drupal\form_test\Form\FormTestPlaceholderForm' _title: 'Placeholder' requirements: _access: 'TRUE' @@ -231,7 +231,7 @@ form_test.placeholder: form_test.number: path: '/form-test/number' defaults: - _content: '\Drupal\form_test\Form\FormTestForm::testNumber' + _form: '\Drupal\form_test\Form\FormTestNumberForm' _title: 'Number' requirements: _access: 'TRUE' @@ -239,15 +239,16 @@ form_test.number: form_test.number_range: path: '/form-test/number/range' defaults: - _content: '\Drupal\form_test\Form\FormTestForm::testNumberRange' + _form: '\Drupal\form_test\Form\FormTestNumberForm' _title: 'Range' + element: 'range' requirements: _access: 'TRUE' form_test.range: path: '/form-test/range' defaults: - _content: '\Drupal\form_test\Form\FormTestForm::testRange' + _form: '\Drupal\form_test\Form\FormTestRangeForm' _title: 'Range' requirements: _access: 'TRUE' @@ -255,7 +256,7 @@ form_test.range: form_test.range_invalid: path: '/form-test/range/invalid' defaults: - _content: '\Drupal\form_test\Form\FormTestForm::testRangeInvalid' + _form: '\Drupal\form_test\Form\FormTestRangeInvalidForm' _title: 'Invalid range' requirements: _access: 'TRUE' @@ -263,7 +264,7 @@ form_test.range_invalid: form_test.color: path: '/form-test/color' defaults: - _content: '\Drupal\form_test\Form\FormTestForm::testColor' + _form: '\Drupal\form_test\Form\FormTestColorForm' _title: 'Color' requirements: _access: 'TRUE' @@ -271,7 +272,7 @@ form_test.color: form_test.checkboxes_radios: path: '/form-test/checkboxes-radios/{customize}' defaults: - _content: '\Drupal\form_test\Form\FormTestForm::testCheckboxesRadios' + _form: '\Drupal\form_test\Form\FormTestCheckboxesRadiosForm' _title: 'Checkboxes, Radios' customize: FALSE requirements: @@ -280,7 +281,7 @@ form_test.checkboxes_radios: form_test.email: path: '/form-test/email' defaults: - _content: '\Drupal\form_test\Form\FormTestForm::testEmail' + _form: '\Drupal\form_test\Form\FormTestEmailForm' _title: 'Email fields' requirements: _access: 'TRUE' @@ -288,7 +289,7 @@ form_test.email: form_test.url: path: '/form-test/url' defaults: - _content: '\Drupal\form_test\Form\FormTestForm::testUrl' + _form: '\Drupal\form_test\Form\FormTestUrlForm' _title: 'URL' requirements: _access: 'TRUE' @@ -296,7 +297,7 @@ form_test.url: form_test.disabled_elements: path: '/form-test/disabled-elements' defaults: - _content: '\Drupal\form_test\Form\FormTestForm::testDisabledElements' + _form: '\Drupal\form_test\Form\FormTestDisabledElementsForm' _title: 'Form test' requirements: _access: 'TRUE' @@ -355,7 +356,7 @@ form_test.clicked_button: form_test.checkboxes_zero: path: '/form-test/checkboxes-zero/{json}' defaults: - _content: '\Drupal\form_test\Form\FormTestForm::testCheckboxesZero' + _form: '\Drupal\form_test\Form\FormTestCheckboxesZeroForm' _title: 'FAPI test involving checkboxes and zero' json: TRUE requirements: diff --git a/core/modules/system/tests/modules/form_test/src/Form/FormTestAlterForm.php b/core/modules/system/tests/modules/form_test/src/Form/FormTestAlterForm.php new file mode 100644 index 0000000..3052d76 --- /dev/null +++ b/core/modules/system/tests/modules/form_test/src/Form/FormTestAlterForm.php @@ -0,0 +1,40 @@ +send(); - exit; + $form_state['response'] = new JsonResponse($form_state['values']); } } diff --git a/core/modules/system/tests/modules/form_test/src/Form/FormTestCheckboxesRadiosForm.php b/core/modules/system/tests/modules/form_test/src/Form/FormTestCheckboxesRadiosForm.php new file mode 100644 index 0000000..4b84cf1 --- /dev/null +++ b/core/modules/system/tests/modules/form_test/src/Form/FormTestCheckboxesRadiosForm.php @@ -0,0 +1,89 @@ + 'checkboxes', + '#title' => 'Checkboxes', + '#options' => array( + 0 => 'Zero', + 'foo' => 'Foo', + 1 => 'One', + 'bar' => 'Bar', + '>' => 'Special Char', + ), + ); + if ($customize) { + $form['checkboxes'] += array( + 'foo' => array( + '#description' => 'Enable to foo.', + ), + 1 => array( + '#weight' => 10, + ), + ); + } + + // Expand #type radios, setting custom element properties for some but not + // all options. + $form['radios'] = array( + '#type' => 'radios', + '#title' => 'Radios', + '#options' => array( + 0 => 'Zero', + 'foo' => 'Foo', + 1 => 'One', + 'bar' => 'Bar', + '>' => 'Special Char', + ), + ); + if ($customize) { + $form['radios'] += array( + 'foo' => array( + '#description' => 'Enable to foo.', + ), + 1 => array( + '#weight' => 10, + ), + ); + } + + $form['submit'] = array('#type' => 'submit', '#value' => 'Submit'); + + return $form; + } + + /** + * {@inheritdoc} + */ + public function submitForm(array &$form, array &$form_state) { + $form_state['response'] = new JsonResponse($form_state['values']); + } + +} diff --git a/core/modules/system/tests/modules/form_test/src/Form/FormTestCheckboxesZeroForm.php b/core/modules/system/tests/modules/form_test/src/Form/FormTestCheckboxesZeroForm.php new file mode 100644 index 0000000..08356ad --- /dev/null +++ b/core/modules/system/tests/modules/form_test/src/Form/FormTestCheckboxesZeroForm.php @@ -0,0 +1,66 @@ + t('Checkbox off'), + '#type' => 'checkboxes', + '#options' => array('foo', 'bar', 'baz'), + ); + $form['checkbox_zero_default'] = array( + '#title' => t('Zero default'), + '#type' => 'checkboxes', + '#options' => array('foo', 'bar', 'baz'), + '#default_value' => array(0), + ); + $form['checkbox_string_zero_default'] = array( + '#title' => t('Zero default (string)'), + '#type' => 'checkboxes', + '#options' => array('foo', 'bar', 'baz'), + '#default_value' => array('0'), + ); + $form['submit'] = array( + '#type' => 'submit', + '#value' => 'Save', + ); + return $form; + } + + /** + * {@inheritdoc} + */ + public function submitForm(array &$form, array &$form_state) { + if (!empty($form_state['json'])) { + $form_state['response'] = new JsonResponse($form_state['values']); + } + else { + $form_state['redirect'] = FALSE; + } + } + +} diff --git a/core/modules/system/tests/modules/form_test/src/Form/FormTestColorForm.php b/core/modules/system/tests/modules/form_test/src/Form/FormTestColorForm.php new file mode 100644 index 0000000..0856026 --- /dev/null +++ b/core/modules/system/tests/modules/form_test/src/Form/FormTestColorForm.php @@ -0,0 +1,47 @@ + 'color', + '#title' => 'Color', + ); + $form['submit'] = array( + '#type' => 'submit', + '#value' => 'Submit', + ); + return $form; + } + + /** + * {@inheritdoc} + */ + public function submitForm(array &$form, array &$form_state) { + $form_state['response'] = new JsonResponse($form_state['values']); + } + +} diff --git a/core/modules/system/tests/modules/form_test/src/Form/FormTestDisabledElementsForm.php b/core/modules/system/tests/modules/form_test/src/Form/FormTestDisabledElementsForm.php new file mode 100644 index 0000000..9f4e168 --- /dev/null +++ b/core/modules/system/tests/modules/form_test/src/Form/FormTestDisabledElementsForm.php @@ -0,0 +1,230 @@ + $type, + '#title' => $type, + '#default_value' => $type, + '#test_hijack_value' => 'HIJACK', + '#disabled' => TRUE, + ); + } + + // Multiple values option elements. + foreach (array('checkboxes', 'select') as $type) { + $form[$type . '_multiple'] = array( + '#type' => $type, + '#title' => $type . ' (multiple)', + '#options' => array( + 'test_1' => 'Test 1', + 'test_2' => 'Test 2', + ), + '#multiple' => TRUE, + '#default_value' => array('test_2' => 'test_2'), + // The keys of #test_hijack_value need to match the #name of the control. + // @see FormsTestCase::testDisabledElements() + '#test_hijack_value' => $type == 'select' ? array('' => 'test_1') : array('test_1' => 'test_1'), + '#disabled' => TRUE, + ); + } + + // Single values option elements. + foreach (array('radios', 'select') as $type) { + $form[$type . '_single'] = array( + '#type' => $type, + '#title' => $type . ' (single)', + '#options' => array( + 'test_1' => 'Test 1', + 'test_2' => 'Test 2', + ), + '#multiple' => FALSE, + '#default_value' => 'test_2', + '#test_hijack_value' => 'test_1', + '#disabled' => TRUE, + ); + } + + // Checkbox and radio. + foreach (array('checkbox', 'radio') as $type) { + $form[$type . '_unchecked'] = array( + '#type' => $type, + '#title' => $type . ' (unchecked)', + '#return_value' => 1, + '#default_value' => 0, + '#test_hijack_value' => 1, + '#disabled' => TRUE, + ); + $form[$type . '_checked'] = array( + '#type' => $type, + '#title' => $type . ' (checked)', + '#return_value' => 1, + '#default_value' => 1, + '#test_hijack_value' => NULL, + '#disabled' => TRUE, + ); + } + + // Weight, number, range. + foreach (array('weight', 'number', 'range') as $type) { + $form[$type] = array( + '#type' => $type, + '#title' => $type, + '#default_value' => 10, + '#test_hijack_value' => 5, + '#disabled' => TRUE, + ); + } + + // Color. + $form['color'] = array( + '#type' => 'color', + '#title' => 'color', + '#default_value' => '#0000ff', + '#test_hijack_value' => '#ff0000', + '#disabled' => TRUE, + ); + + // The #disabled state should propagate to children. + $form['disabled_container'] = array( + '#disabled' => TRUE, + ); + foreach (array('textfield', 'textarea', 'hidden', 'tel', 'url') as $type) { + $form['disabled_container']['disabled_container_' . $type] = array( + '#type' => $type, + '#title' => $type, + '#default_value' => $type, + '#test_hijack_value' => 'HIJACK', + ); + } + + // Date. + $date = new DrupalDateTime('1978-11-01 10:30:00', 'Europe/Berlin'); + $expected = array('date' => '1978-11-01 10:30:00', 'timezone_type' => 3, 'timezone' => 'Europe/Berlin',); + $form['disabled_container']['disabled_container_datetime'] = array( + '#type' => 'datetime', + '#title' => 'datetime', + '#default_value' => $date, + '#expected_value' => $expected, + '#test_hijack_value' => new DrupalDateTime('1978-12-02 11:30:00', 'Europe/Berlin'), + '#date_timezone' => 'Europe/Berlin', + ); + + + // Try to hijack the email field with a valid email. + $form['disabled_container']['disabled_container_email'] = array( + '#type' => 'email', + '#title' => 'email', + '#default_value' => 'foo@example.com', + '#test_hijack_value' => 'bar@example.com', + ); + + // Try to hijack the URL field with a valid URL. + $form['disabled_container']['disabled_container_url'] = array( + '#type' => 'url', + '#title' => 'url', + '#default_value' => 'http://example.com', + '#test_hijack_value' => 'http://example.com/foo', + ); + + // Text format. + $form['text_format'] = array( + '#type' => 'text_format', + '#title' => 'Text format', + '#disabled' => TRUE, + '#default_value' => 'Text value', + '#format' => 'plain_text', + '#expected_value' => array( + 'value' => 'Text value', + 'format' => 'plain_text', + ), + '#test_hijack_value' => array( + 'value' => 'HIJACK', + 'format' => 'filtered_html', + ), + ); + + // Password fields. + $form['password'] = array( + '#type' => 'password', + '#title' => 'Password', + '#disabled' => TRUE, + ); + $form['password_confirm'] = array( + '#type' => 'password_confirm', + '#title' => 'Password confirm', + '#disabled' => TRUE, + ); + + // Files. + $form['file'] = array( + '#type' => 'file', + '#title' => 'File', + '#disabled' => TRUE, + ); + $form['managed_file'] = array( + '#type' => 'managed_file', + '#title' => 'Managed file', + '#disabled' => TRUE, + ); + + // Buttons. + $form['image_button'] = array( + '#type' => 'image_button', + '#value' => 'Image button', + '#disabled' => TRUE, + ); + $form['button'] = array( + '#type' => 'button', + '#value' => 'Button', + '#disabled' => TRUE, + ); + $form['submit_disabled'] = array( + '#type' => 'submit', + '#value' => 'Submit', + '#disabled' => TRUE, + ); + + $form['submit'] = array( + '#type' => 'submit', + '#value' => t('Submit'), + ); + + return $form; + } + + /** + * {@inheritdoc} + */ + public function submitForm(array &$form, array &$form_state) { + $form_state['response'] = new JsonResponse($form_state['values']); + } + +} diff --git a/core/modules/system/tests/modules/form_test/src/Form/FormTestEmailForm.php b/core/modules/system/tests/modules/form_test/src/Form/FormTestEmailForm.php new file mode 100644 index 0000000..f46ec64 --- /dev/null +++ b/core/modules/system/tests/modules/form_test/src/Form/FormTestEmailForm.php @@ -0,0 +1,54 @@ + 'email', + '#title' => 'Email address', + '#description' => 'An email address.', + ); + $form['email_required'] = array( + '#type' => 'email', + '#title' => 'Address', + '#required' => TRUE, + '#description' => 'A required email address field.', + ); + $form['submit'] = array( + '#type' => 'submit', + '#value' => 'Submit', + ); + return $form; + } + + /** + * {@inheritdoc} + */ + public function submitForm(array &$form, array &$form_state) { + $form_state['response'] = new JsonResponse($form_state['values']); + } + +} diff --git a/core/modules/system/tests/modules/form_test/src/Form/FormTestEmptySelectForm.php b/core/modules/system/tests/modules/form_test/src/Form/FormTestEmptySelectForm.php new file mode 100644 index 0000000..1434370 --- /dev/null +++ b/core/modules/system/tests/modules/form_test/src/Form/FormTestEmptySelectForm.php @@ -0,0 +1,43 @@ + 'select', + '#title' => t('Empty Select'), + '#multiple' => FALSE, + '#options' => NULL, + ); + return $form; + } + + /** + * {@inheritdoc} + */ + public function submitForm(array &$form, array &$form_state) { + } + +} diff --git a/core/modules/system/tests/modules/form_test/src/Form/FormTestForm.php b/core/modules/system/tests/modules/form_test/src/Form/FormTestForm.php index c777732..fe54019 100644 --- a/core/modules/system/tests/modules/form_test/src/Form/FormTestForm.php +++ b/core/modules/system/tests/modules/form_test/src/Form/FormTestForm.php @@ -13,204 +13,6 @@ class FormTestForm { /** - * Wraps form_test_alter_form(). - * - * @todo Remove form_test_alter_form(). - */ - public function alterForm() { - return \Drupal::formBuilder()->getForm('form_test_alter_form'); - } - - /** - * Wraps form_test_validate_form(). - * - * @todo Remove form_test_validate_form(). - */ - public function validateForm() { - return \Drupal::formBuilder()->getForm('form_test_validate_form'); - } - - /** - * Wraps form_test_validate_required_form(). - * - * @todo Remove form_test_validate_required_form(). - */ - public function validateRequiredForm() { - return \Drupal::formBuilder()->getForm('form_test_validate_required_form'); - } - - /** - * Wraps form_test_validate_required_form_no_title(). - * - * @todo Remove form_test_validate_required_form_no_title(). - */ - public function validateRequiredFormNoTitle() { - return \Drupal::formBuilder()->getForm('form_test_validate_required_form_no_title'); - } - - /** - * Wraps form_test_limit_validation_errors_form(). - * - * @todo Remove form_test_limit_validation_errors_form(). - */ - public function validateFormWithErrorSuppression() { - return \Drupal::formBuilder()->getForm('form_test_limit_validation_errors_form'); - } - - /** - * Wraps form_test_pattern_form(). - * - * @todo Remove form_test_pattern_form(). - */ - public function validatePattern() { - return \Drupal::formBuilder()->getForm('form_test_pattern_form'); - } - - /** - * Wraps form_test_storage_form(). - * - * @todo Remove form_test_storage_form(). - */ - public function testStorage() { - return \Drupal::formBuilder()->getForm('form_test_storage_form'); - } - - /** - * Wraps form_test_form_state_values_clean_form(). - * - * @todo Remove form_test_form_state_values_clean_form(). - */ - public function testFormStateClean() { - return \Drupal::formBuilder()->getForm('form_test_form_state_values_clean_form'); - } - - /** - * Wraps form_test_form_state_values_clean_advanced_form(). - * - * @todo Remove form_test_form_state_values_clean_advanced_form(). - */ - public function testFormStateCleanAdvanced() { - return \Drupal::formBuilder()->getForm('form_test_form_state_values_clean_advanced_form'); - } - - /** - * Wraps form_test_select(). - * - * @todo Remove form_test_select(). - */ - public function testSelect() { - return \Drupal::formBuilder()->getForm('form_test_select'); - } - - /** - * Wraps form_test_empty_select(). - * - * @todo Remove form_test_empty_select(). - */ - public function testEmptySelect() { - return \Drupal::formBuilder()->getForm('form_test_empty_select'); - } - - /** - * Wraps form_test_language_select(). - * - * @todo Remove form_test_language_select(). - */ - public function testLanguageSelect() { - return \Drupal::formBuilder()->getForm('form_test_language_select'); - } - - /** - * Wraps form_test_placeholder_test(). - * - * @todo Remove form_test_placeholder_test(). - */ - public function testPlaceholder() { - return \Drupal::formBuilder()->getForm('form_test_placeholder_test'); - } - - /** - * Wraps form_test_number(). - * - * @todo Remove form_test_number(). - */ - public function testNumber() { - return \Drupal::formBuilder()->getForm('form_test_number'); - } - - /** - * Wraps form_test_number(). - * - * @todo Remove form_test_number(). - */ - public function testNumberRange() { - return \Drupal::formBuilder()->getForm('form_test_number', 'range'); - } - - /** - * Wraps form_test_range(). - * - * @todo Remove form_test_range(). - */ - public function testRange() { - return \Drupal::formBuilder()->getForm('form_test_range'); - } - - /** - * Wraps form_test_range_invalid(). - * - * @todo Remove form_test_range_invalid(). - */ - public function testRangeInvalid() { - return \Drupal::formBuilder()->getForm('form_test_range_invalid'); - } - - /** - * Wraps form_test_color(). - * - * @todo Remove form_test_color(). - */ - public function testColor() { - return \Drupal::formBuilder()->getForm('form_test_color'); - } - - /** - * Wraps form_test_checkboxes_radios(). - * - * @todo Remove form_test_checkboxes_radios(). - */ - public function testCheckboxesRadios($customize) { - return \Drupal::formBuilder()->getForm('form_test_checkboxes_radios', $customize); - } - - /** - * Wraps form_test_email(). - * - * @todo Remove form_test_email(). - */ - public function testEmail() { - return \Drupal::formBuilder()->getForm('form_test_email'); - } - - /** - * Wraps form_test_url(). - * - * @todo Remove form_test_url(). - */ - public function testUrl() { - return \Drupal::formBuilder()->getForm('form_test_url'); - } - - /** - * Wraps _form_test_disabled_elements(). - * - * @todo Remove _form_test_disabled_elements(). - */ - public function testDisabledElements() { - return \Drupal::formBuilder()->getForm('_form_test_disabled_elements'); - } - - /** * Wraps form_test_form_rebuild_preserve_values_form(). * * @todo Remove form_test_form_rebuild_preserve_values_form(). @@ -256,15 +58,6 @@ public function testClickedButton($first, $second, $third) { } /** - * Wraps form_test_checkboxes_zero(). - * - * @todo Remove form_test_checkboxes_zero(). - */ - public function testCheckboxesZero($json) { - return \Drupal::formBuilder()->getForm('form_test_checkboxes_zero', $json); - } - - /** * Wraps form_test_required_attribute(). * * @todo Remove form_test_required_attribute(). diff --git a/core/modules/system/tests/modules/form_test/src/Form/FormTestFormStateValuesCleanAdvancedForm.php b/core/modules/system/tests/modules/form_test/src/Form/FormTestFormStateValuesCleanAdvancedForm.php new file mode 100644 index 0000000..879d7b3 --- /dev/null +++ b/core/modules/system/tests/modules/form_test/src/Form/FormTestFormStateValuesCleanAdvancedForm.php @@ -0,0 +1,51 @@ + 'managed_file', + '#title' => t('Image'), + '#upload_location' => 'public://', + '#default_value' => 0, + ); + $form['submit'] = array( + '#type' => 'submit', + '#value' => t('Submit'), + ); + return $form; + } + + /** + * {@inheritdoc} + */ + public function submitForm(array &$form, array &$form_state) { + form_state_values_clean($form_state); + print t('You WIN!'); + exit; + } + +} diff --git a/core/modules/system/tests/modules/form_test/src/Form/FormTestFormStateValuesCleanForm.php b/core/modules/system/tests/modules/form_test/src/Form/FormTestFormStateValuesCleanForm.php new file mode 100644 index 0000000..5c258d4 --- /dev/null +++ b/core/modules/system/tests/modules/form_test/src/Form/FormTestFormStateValuesCleanForm.php @@ -0,0 +1,52 @@ + 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; + } + + /** + * {@inheritdoc} + */ + public function submitForm(array &$form, array &$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; + } + +} diff --git a/core/modules/system/tests/modules/form_test/src/Form/FormTestLanguageSelectForm.php b/core/modules/system/tests/modules/form_test/src/Form/FormTestLanguageSelectForm.php new file mode 100644 index 0000000..711b369 --- /dev/null +++ b/core/modules/system/tests/modules/form_test/src/Form/FormTestLanguageSelectForm.php @@ -0,0 +1,72 @@ + t('Languages: All'), + '#type' => 'language_select', + '#languages' => LanguageInterface::STATE_ALL, + '#default_value' => 'xx', + ); + $form['languages_configurable'] = array( + '#title' => t('Languages: Configurable'), + '#type' => 'language_select', + '#languages' => LanguageInterface::STATE_CONFIGURABLE, + '#default_value' => 'en', + ); + $form['languages_locked'] = array( + '#title' => t('Languages: Locked'), + '#type' => 'language_select', + '#languages' => LanguageInterface::STATE_LOCKED, + ); + $form['languages_config_and_locked'] = array( + '#title' => t('Languages: Configurable and locked'), + '#type' => 'language_select', + '#languages' => LanguageInterface::STATE_CONFIGURABLE | LanguageInterface::STATE_LOCKED, + '#default_value' => 'dummy_value', + ); + $form['language_custom_options'] = array( + '#title' => t('Languages: Custom'), + '#type' => 'language_select', + '#languages' => LanguageInterface::STATE_CONFIGURABLE | LanguageInterface::STATE_LOCKED, + '#options' => array('opt1' => 'First option', 'opt2' => 'Second option', 'opt3' => 'Third option'), + '#default_value' => 'opt2', + ); + + $form['submit'] = array('#type' => 'submit', '#value' => 'Submit'); + return $form; + } + + /** + * {@inheritdoc} + */ + public function submitForm(array &$form, array &$form_state) { + $form_state['response'] = new JsonResponse($form_state['values']); + } + +} diff --git a/core/modules/system/tests/modules/form_test/src/Form/FormTestLimitValidationErrorsForm.php b/core/modules/system/tests/modules/form_test/src/Form/FormTestLimitValidationErrorsForm.php new file mode 100644 index 0000000..3838b5e --- /dev/null +++ b/core/modules/system/tests/modules/form_test/src/Form/FormTestLimitValidationErrorsForm.php @@ -0,0 +1,104 @@ + '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(array($this, 'partialSubmitForm')), + '#value' => t('Partial validate'), + ); + $form['actions']['partial_numeric_index'] = array( + '#type' => 'submit', + '#limit_validation_errors' => array(array('test_numeric_index', 0)), + '#submit' => array(array($this, 'partialSubmitForm')), + '#value' => t('Partial validate (numeric index)'), + ); + $form['actions']['substring'] = array( + '#type' => 'submit', + '#limit_validation_errors' => array(array('test_substring', 'foo')), + '#submit' => array(array($this, 'partialSubmitForm')), + '#value' => t('Partial validate (substring)'), + ); + $form['actions']['full'] = array( + '#type' => 'submit', + '#value' => t('Full validate'), + ); + return $form; + } + + /** + * {@inheritdoc} + */ + public function submitForm(array &$form, array &$form_state) { + } + + /** + * {@inheritdoc} + */ + public function partialSubmitForm(array &$form, array &$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.'); + } + } + +} diff --git a/core/modules/system/tests/modules/form_test/src/Form/FormTestNumberForm.php b/core/modules/system/tests/modules/form_test/src/Form/FormTestNumberForm.php new file mode 100644 index 0000000..7c313fc --- /dev/null +++ b/core/modules/system/tests/modules/form_test/src/Form/FormTestNumberForm.php @@ -0,0 +1,145 @@ + $element, + ); + + $form['integer_no_number'] = $base + array( + '#title' => 'Integer test, #no_error', + '#default_value' => '#no_number', + ); + $form['integer_no_step'] = $base + array( + '#title' => 'Integer test without step', + '#default_value' => 5, + ); + $form['integer_no_step_step_error'] = $base + array( + '#title' => 'Integer test without step, #step_error', + '#default_value' => 4.5, + ); + $form['integer_step'] = $base + array( + '#title' => 'Integer test with step', + '#default_value' => 5, + '#step' => 1, + ); + $form['integer_step_error'] = $base + array( + '#title' => 'Integer test, with step, #step_error', + '#default_value' => 5, + '#step' => 2, + ); + $form['integer_step_min'] = $base + array( + '#title' => 'Integer test with min value', + '#default_value' => 5, + '#min' => 0, + '#step' => 1, + ); + $form['integer_step_min_error'] = $base + array( + '#title' => 'Integer test with min value, #min_error', + '#default_value' => 5, + '#min' => 6, + '#step' => 1, + ); + $form['integer_step_max'] = $base + array( + '#title' => 'Integer test with max value', + '#default_value' => 5, + '#max' => 6, + '#step' => 1, + ); + $form['integer_step_max_error'] = $base + array( + '#title' => 'Integer test with max value, #max_error', + '#default_value' => 5, + '#max' => 4, + '#step' => 1, + ); + $form['integer_step_min_border'] = $base + array( + '#title' => 'Integer test with min border check', + '#default_value' => -1, + '#min' => -1, + '#step' => 1, + ); + $form['integer_step_max_border'] = $base + array( + '#title' => 'Integer test with max border check', + '#default_value' => 5, + '#max' => 5, + '#step' => 1, + ); + $form['integer_step_based_on_min'] = $base + array( + '#title' => 'Integer test with step based on min check', + '#default_value' => 3, + '#min' => -1, + '#step' => 2, + ); + $form['integer_step_based_on_min_error'] = $base + array( + '#title' => 'Integer test with step based on min check, #step_error', + '#default_value' => 4, + '#min' => -1, + '#step' => 2, + ); + $form['float_small_step'] = $base + array( + '#title' => 'Float test with a small step', + '#default_value' => 4, + '#step' => 0.0000000000001, + ); + $form['float_step_no_error'] = $base + array( + '#title' => 'Float test', + '#default_value' => 1.2, + '#step' => 0.3, + ); + $form['float_step_error'] = $base + array( + '#title' => 'Float test, #step_error', + '#default_value' => 1.3, + '#step' => 0.3, + ); + $form['float_step_hard_no_error'] = $base + array( + '#title' => 'Float test hard', + '#default_value' => 0.9411764729088, + '#step' => 0.00392156863712, + ); + $form['float_step_hard_error'] = $base + array( + '#title' => 'Float test hard, #step_error', + '#default_value' => 0.9411764, + '#step' => 0.00392156863, + ); + $form['float_step_any_no_error'] = $base + array( + '#title' => 'Arbitrary float', + '#default_value' => 0.839562930284, + '#step' => 'aNy', + ); + $form['submit'] = array( + '#type' => 'submit', + '#value' => 'Submit', + ); + return $form; + } + + /** + * {@inheritdoc} + */ + public function submitForm(array &$form, array &$form_state) { + } + +} diff --git a/core/modules/system/tests/modules/form_test/src/Form/FormTestPatternForm.php b/core/modules/system/tests/modules/form_test/src/Form/FormTestPatternForm.php new file mode 100644 index 0000000..7df5f7a --- /dev/null +++ b/core/modules/system/tests/modules/form_test/src/Form/FormTestPatternForm.php @@ -0,0 +1,65 @@ + '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; + } + + /** + * {@inheritdoc} + */ + public function submitForm(array &$form, array &$form_state) { + } + +} diff --git a/core/modules/system/tests/modules/form_test/src/Form/FormTestPlaceholderForm.php b/core/modules/system/tests/modules/form_test/src/Form/FormTestPlaceholderForm.php new file mode 100644 index 0000000..f11d9d4 --- /dev/null +++ b/core/modules/system/tests/modules/form_test/src/Form/FormTestPlaceholderForm.php @@ -0,0 +1,45 @@ + $type, + '#title' => $type, + '#placeholder' => 'placeholder-text', + ); + } + + return $form; + } + + /** + * {@inheritdoc} + */ + public function submitForm(array &$form, array &$form_state) { + } + +} diff --git a/core/modules/system/tests/modules/form_test/src/Form/FormTestRangeForm.php b/core/modules/system/tests/modules/form_test/src/Form/FormTestRangeForm.php new file mode 100644 index 0000000..fff5126 --- /dev/null +++ b/core/modules/system/tests/modules/form_test/src/Form/FormTestRangeForm.php @@ -0,0 +1,75 @@ + 'range', + '#title' => 'Range with default value', + '#min' => 10, + '#max' => 20, + '#step' => 2, + '#default_value' => 18, + '#description' => 'The default value is 18.', + ); + $form['float'] = array( + '#type' => 'range', + '#title' => 'Float', + '#min' => 10, + '#max' => 11, + '#step' => 'any', + '#description' => 'Floating point number between 10 and 11.', + ); + $form['integer'] = array( + '#type' => 'range', + '#title' => 'Integer', + '#min' => 2, + '#max' => 8, + '#step' => 2, + '#description' => 'Even integer between 2 and 8.', + ); + $form['offset'] = array( + '#type' => 'range', + '#title' => 'Offset', + '#min' => 2.9, + '#max' => 10.9, + '#description' => 'Value between 2.9 and 10.9.', + ); + $form['submit'] = array( + '#type' => 'submit', + '#value' => 'Submit', + ); + return $form; + } + + /** + * {@inheritdoc} + */ + public function submitForm(array &$form, array &$form_state) { + $form_state['response'] = new JsonResponse($form_state['values']); + } + +} diff --git a/core/modules/system/tests/modules/form_test/src/Form/FormTestRangeInvalidForm.php b/core/modules/system/tests/modules/form_test/src/Form/FormTestRangeInvalidForm.php new file mode 100644 index 0000000..7b685a7 --- /dev/null +++ b/core/modules/system/tests/modules/form_test/src/Form/FormTestRangeInvalidForm.php @@ -0,0 +1,48 @@ + 'range', + '#min' => 10, + '#max' => 5, + '#title' => 'Invalid range', + '#description' => 'Minimum greater than maximum.', + ); + $form['submit'] = array( + '#type' => 'submit', + '#value' => 'Submit', + ); + return $form; + } + + /** + * {@inheritdoc} + */ + public function submitForm(array &$form, array &$form_state) { + } + +} diff --git a/core/modules/system/tests/modules/form_test/src/Form/FormTestSelectForm.php b/core/modules/system/tests/modules/form_test/src/Form/FormTestSelectForm.php new file mode 100644 index 0000000..5227ae4 --- /dev/null +++ b/core/modules/system/tests/modules/form_test/src/Form/FormTestSelectForm.php @@ -0,0 +1,133 @@ + 'select', + '#options' => array('one' => 'one', 'two' => 'two', 'three' => 'three'), + ); + + $form['select'] = $base + array( + '#title' => '#default_value one', + '#default_value' => 'one', + ); + $form['select_required'] = $base + array( + '#title' => '#default_value one, #required', + '#required' => TRUE, + '#default_value' => 'one', + ); + $form['select_optional'] = $base + array( + '#title' => '#default_value one, not #required', + '#required' => FALSE, + '#default_value' => 'one', + ); + $form['empty_value'] = $base + array( + '#title' => '#default_value one, #required, #empty_value 0', + '#required' => TRUE, + '#default_value' => 'one', + '#empty_value' => 0, + ); + $form['empty_value_one'] = $base + array( + '#title' => '#default_value = #empty_value, #required', + '#required' => TRUE, + '#default_value' => 'one', + '#empty_value' => 'one', + ); + + $form['no_default'] = $base + array( + '#title' => 'No #default_value, #required', + '#required' => TRUE, + ); + $form['no_default_optional'] = $base + array( + '#title' => 'No #default_value, not #required', + '#required' => FALSE, + '#description' => 'Should result in "one" because it is not required and there is no #empty_value requested, so default browser behavior of preselecting first option is in effect.', + ); + $form['no_default_optional_empty_value'] = $base + array( + '#title' => 'No #default_value, not #required, #empty_value empty string', + '#empty_value' => '', + '#required' => FALSE, + '#description' => 'Should result in an empty string (due to #empty_value) because it is optional.', + ); + + $form['no_default_empty_option'] = $base + array( + '#title' => 'No #default_value, #required, #empty_option', + '#required' => TRUE, + '#empty_option' => '- Choose -', + ); + $form['no_default_empty_option_optional'] = $base + array( + '#title' => 'No #default_value, not #required, #empty_option', + '#empty_option' => '- Dismiss -', + '#description' => 'Should result in an empty string (default of #empty_value) because it is optional.', + ); + + $form['no_default_empty_value'] = $base + array( + '#title' => 'No #default_value, #required, #empty_value 0', + '#required' => TRUE, + '#empty_value' => 0, + '#description' => 'Should never result in 0.', + ); + $form['no_default_empty_value_one'] = $base + array( + '#title' => 'No #default_value, #required, #empty_value one', + '#required' => TRUE, + '#empty_value' => 'one', + '#description' => 'A mistakenly assigned #empty_value contained in #options should not be valid.', + ); + $form['no_default_empty_value_optional'] = $base + array( + '#title' => 'No #default_value, not #required, #empty_value 0', + '#required' => FALSE, + '#empty_value' => 0, + '#description' => 'Should result in 0 because it is optional.', + ); + + $form['multiple'] = $base + array( + '#title' => '#multiple, #default_value two', + '#default_value' => array('two'), + '#multiple' => TRUE, + ); + $form['multiple_no_default'] = $base + array( + '#title' => '#multiple, no #default_value', + '#multiple' => TRUE, + ); + $form['multiple_no_default_required'] = $base + array( + '#title' => '#multiple, #required, no #default_value', + '#required' => TRUE, + '#multiple' => TRUE, + ); + + $form['submit'] = array('#type' => 'submit', '#value' => 'Submit'); + return $form; + } + + /** + * {@inheritdoc} + */ + public function submitForm(array &$form, array &$form_state) { + $form_state['response'] = new JsonResponse($form_state['values']); + } + +} diff --git a/core/modules/system/tests/modules/form_test/src/Form/FormTestStorageForm.php b/core/modules/system/tests/modules/form_test/src/Form/FormTestStorageForm.php new file mode 100644 index 0000000..9106013 --- /dev/null +++ b/core/modules/system/tests/modules/form_test/src/Form/FormTestStorageForm.php @@ -0,0 +1,102 @@ + 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', + ); + + 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; + } + + /** + * {@inheritdoc} + */ + public function submitForm(array &$form, array &$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'] = ''; + } + +} diff --git a/core/modules/system/tests/modules/form_test/src/Form/FormTestUrlForm.php b/core/modules/system/tests/modules/form_test/src/Form/FormTestUrlForm.php new file mode 100644 index 0000000..8e2c5a0 --- /dev/null +++ b/core/modules/system/tests/modules/form_test/src/Form/FormTestUrlForm.php @@ -0,0 +1,54 @@ + 'url', + '#title' => 'Optional URL', + '#description' => 'An optional URL field.', + ); + $form['url_required'] = array( + '#type' => 'url', + '#title' => 'Required URL', + '#description' => 'A required URL field.', + '#required' => TRUE, + ); + $form['submit'] = array( + '#type' => 'submit', + '#value' => 'Submit', + ); + return $form; + } + + /** + * {@inheritdoc} + */ + public function submitForm(array &$form, array &$form_state) { + $form_state['response'] = new JsonResponse($form_state['values']); + } + +} diff --git a/core/modules/system/tests/modules/form_test/src/Form/FormTestValidateForm.php b/core/modules/system/tests/modules/form_test/src/Form/FormTestValidateForm.php new file mode 100644 index 0000000..50cf98f --- /dev/null +++ b/core/modules/system/tests/modules/form_test/src/Form/FormTestValidateForm.php @@ -0,0 +1,81 @@ + '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; + } + + /** + * {@inheritdoc} + */ + public function validateForm(array &$form, array &$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); + } + } + + /** + * {@inheritdoc} + */ + public function submitForm(array &$form, array &$form_state) { + } + +} diff --git a/core/modules/system/tests/modules/form_test/src/Form/FormTestValidateRequiredForm.php b/core/modules/system/tests/modules/form_test/src/Form/FormTestValidateRequiredForm.php new file mode 100644 index 0000000..e25d18f --- /dev/null +++ b/core/modules/system/tests/modules/form_test/src/Form/FormTestValidateRequiredForm.php @@ -0,0 +1,82 @@ + '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; + } + + /** + * {@inheritdoc} + */ + public function submitForm(array &$form, array &$form_state) { + drupal_set_message('The form_test_validate_required_form form was submitted successfully.'); + } + +} diff --git a/core/modules/system/tests/modules/form_test/src/Form/FormTestValidateRequiredNoTitleForm.php b/core/modules/system/tests/modules/form_test/src/Form/FormTestValidateRequiredNoTitleForm.php new file mode 100644 index 0000000..25e5076 --- /dev/null +++ b/core/modules/system/tests/modules/form_test/src/Form/FormTestValidateRequiredNoTitleForm.php @@ -0,0 +1,44 @@ + 'textfield', + '#required' => TRUE, + ); + $form['actions'] = array('#type' => 'actions'); + $form['actions']['submit'] = array('#type' => 'submit', '#value' => 'Submit'); + return $form; + } + + /** + * {@inheritdoc} + */ + public function submitForm(array &$form, array &$form_state) { + drupal_set_message('The form_test_validate_required_form_no_title form was submitted successfully.'); + } + +}