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();
......
<?php
/**
* @file
* Contains \Drupal\form_test\Form\FormTestAlterForm.
*/
namespace Drupal\form_test\Form;
use Drupal\Core\Form\FormBase;
/**
* Form builder for testing hook_form_alter() and hook_form_FORM_ID_alter().
*/
class FormTestAlterForm extends FormBase {
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'form_test_alter_form';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, array &$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;
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, array &$form_state) {
}
}
<?php
/**
* @file
* Contains \Drupal\form_test\Form\FormTestButtonClassForm.
*/
namespace Drupal\form_test\Form;
use Drupal\Core\Form\FormBase;
/**
* Builds a simple form to test form button classes.
*/
class FormTestButtonClassForm extends FormBase {
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'form_test_button_class';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, array &$form_state) {
$form['button'] = array(
'#type' => 'button',
'#value' => 'test',
'#button_type' => 'foo',
);
$form['delete'] = array(
'#type' => 'button',
'#value' => 'Delete',
'#button_type' => 'danger',
);
return $form;
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, array &$form_state) {
}
}
......@@ -91,10 +91,7 @@ public function buildForm(array $form, array &$form_state) {
* {@inheritdoc}
*/
public function submitForm(array &$form, array &$form_state) {
$response = new JsonResponse($form_state['values']);
// @todo remove once converted to new routing system.
$response->send();
exit;
$form_state['response'] = new JsonResponse($form_state['values']);
}
}
<?php
/**
* @file
* Contains \Drupal\form_test\Form\FormTestCheckboxTypeJugglingForm.
*/
namespace Drupal\form_test\Form;
use Drupal\Core\Form\FormBase;
/**
* Builds a form to test return values for checkboxes.
*/
class FormTestCheckboxTypeJugglingForm extends FormBase {
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'form_test_checkbox_type_juggling';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, array &$form_state, $default_value = NULL, $return_value = NULL) {
$form['checkbox'] = array(
'#title' => t('Checkbox'),
'#type' => 'checkbox',
'#return_value' => $return_value,
'#default_value' => $default_value,
);
return $form;
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, array &$form_state) {
}
}
<?php
/**
* @file
* Contains \Drupal\form_test\Form\FormTestCheckboxesRadiosForm.
*/
namespace Drupal\form_test\Form;
use Drupal\Core\Form\FormBase;
use Symfony\Component\HttpFoundation\JsonResponse;
/**
* Form constructor to test expansion of #type checkboxes and radios.
*/
class FormTestCheckboxesRadiosForm extends FormBase {
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'form_test_checkboxes_radios';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, array &$form_state, $customize = FALSE) {
// 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;
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, array &$form_state) {
$form_state['response'] = new JsonResponse($form_state['values']);
}
}
<?php
/**
* @file
* Contains \Drupal\form_test\Form\FormTestCheckboxesZeroForm.
*/
namespace Drupal\form_test\Form;
use Drupal\Core\Form\FormBase;
use Symfony\Component\HttpFoundation\JsonResponse;
/**
* Tests checkboxes zero.
*/
class FormTestCheckboxesZeroForm extends FormBase {
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'form_test_checkboxes_zero';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, array &$form_state, $json = TRUE) {
$form_state['json'] = $json;
$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',
);
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;
}
}
}
<?php
/**
* @file
* Contains \Drupal\form_test\Form\FormTestClickedButtonForm.
*/
namespace Drupal\form_test\Form;
use Drupal\Core\Form\FormBase;
/**
* Form builder to test button click detection.
*/
class FormTestClickedButtonForm extends FormBase {
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'form_test_clicked_button';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, array &$form_state, $first = NULL, $second = NULL, $third = NULL) {
// A single text field. In IE, when a form has only one non-button input field
// and the ENTER key is pressed while that field has focus, the form is
// submitted without any information identifying the button responsible for
// the submission. In other browsers, the form is submitted as though the
// first button were clicked.
$form['text'] = array(
'#title' => 'Text',
'#type' => 'textfield',
);
// Loop through each path argument, adding buttons based on the information
// in the argument. For example, if the path is
// form-test/clicked-button/s/i/rb, then 3 buttons are added: a 'submit', an
// 'image_button', and a 'button' with #access=FALSE. This enables form.test
// to test a variety of combinations.
$i = 0;
$args = array($first, $second, $third);
foreach ($args as $arg) {
$name = 'button' . ++$i;
// 's', 'b', or 'i' in the argument define the button type wanted.
if (strpos($arg, 's') !== FALSE) {
$type = 'submit';
}
elseif (strpos($arg, 'b') !== FALSE) {
$type = 'button';
}
elseif (strpos($arg, 'i') !== FALSE) {
$type = 'image_button';
}
else {
$type = NULL;
}
if (isset($type)) {
$form[$name] = array(
'#type' => $type,
'#name' => $name,
);
// Image buttons need a #src; the others need a #value.
if ($type == 'image_button') {
$form[$name]['#src'] = 'core/misc/druplicon.png';
}
else {
$form[$name]['#value'] = $name;
}
// 'r' for restricted, so we can test that button click detection code
// correctly takes #access security into account.
if (strpos($arg, 'r') !== FALSE) {
$form[$name]['#access'] = FALSE;
}
}
}
return $form;
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, array &$form_state) {
if (isset($form_state['triggering_element'])) {
drupal_set_message(t('The clicked button is %name.', array('%name' => $form_state['triggering_element']['#name'])));
}
else {
drupal_set_message('There is no clicked button.');
}
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, array &$form_state) {
drupal_set_message('Submit handler for form_test_clicked_button executed.');
}
}
<?php
/**
* @file
* Contains \Drupal\form_test\Form\FormTestColorForm.
*/
namespace Drupal\form_test\Form;
use Drupal\Core\Form\FormBase;
use Symfony\Component\HttpFoundation\JsonResponse;
/**
* Form constructor for testing #type 'color' elements.
*/
class FormTestColorForm extends FormBase {
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'form_test_color';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, array &$form_state) {
$form['color'] = array(
'#type' => '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']);
}
}
<?php
/**
* @file
* Contains \Drupal\form_test\Form\FormTestDisabledElementsForm.
*/
namespace Drupal\form_test\Form;
use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\Core\Form\FormBase;
use Symfony\Component\HttpFoundation\JsonResponse;
/**
* Builds a form to test disabled elements.
*/
class FormTestDisabledElementsForm extends FormBase {
/**
* {@inheritdoc}
*/
public function getFormId() {
return '_form_test_disabled_elements';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, array &$form_state) {
// 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,