Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Open sidebar
project
drupal
Commits
123bf46c
Commit
123bf46c
authored
Jul 26, 2010
by
Dries
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
- Patch
#827430
by David_Rothstein: drupal_form_submit() no longer works with checkboxes.
parent
803101b5
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
64 additions
and
25 deletions
+64
-25
includes/form.inc
includes/form.inc
+15
-1
modules/simpletest/tests/form.test
modules/simpletest/tests/form.test
+33
-18
modules/simpletest/tests/form_test.module
modules/simpletest/tests/form_test.module
+16
-6
No files found.
includes/form.inc
View file @
123bf46c
...
...
@@ -1978,8 +1978,22 @@ function form_type_checkboxes_value($element, $input = FALSE) {
}
return
$value
;
}
elseif
(
is_array
(
$input
))
{
// Programmatic form submissions use NULL to indicate that a checkbox
// should be unchecked; see drupal_form_submit(). We therefore remove all
// NULL elements from the array before constructing the return value, to
// simulate the behavior of web browsers (which do not send unchecked
// checkboxes to the server at all). This will not affect non-programmatic
// form submissions, since a checkbox can never legitimately be NULL.
foreach
(
$input
as
$key
=>
$value
)
{
if
(
is_null
(
$value
))
{
unset
(
$input
[
$key
]);
}
}
return
drupal_map_assoc
(
$input
);
}
else
{
return
is_array
(
$input
)
?
drupal_map_assoc
(
$input
)
:
array
();
return
array
();
}
}
...
...
modules/simpletest/tests/form.test
View file @
123bf46c
...
...
@@ -973,10 +973,19 @@ class FormsProgrammaticTestCase extends DrupalWebTestCase {
$current_batch
=
$batch
=&
batch_get
();
$batch
=
array
();
$this
->
submitForm
();
$this
->
submitForm
(
'test 1'
);
$this
->
submitForm
();
$this
->
submitForm
(
'test 2'
);
// Test that a programmatic form submission is rejected when a required
// textfield is omitted and correctly processed when it is provided.
$this
->
submitForm
(
array
(),
FALSE
);
$this
->
submitForm
(
array
(
'textfield'
=>
'test 1'
),
TRUE
);
$this
->
submitForm
(
array
(),
FALSE
);
$this
->
submitForm
(
array
(
'textfield'
=>
'test 2'
),
TRUE
);
// Test that a programmatic form submission can turn on and off checkboxes
// which are, by default, checked.
$this
->
submitForm
(
array
(
'textfield'
=>
'dummy value'
,
'checkboxes'
=>
array
(
1
=>
1
,
2
=>
2
)),
TRUE
);
$this
->
submitForm
(
array
(
'textfield'
=>
'dummy value'
,
'checkboxes'
=>
array
(
1
=>
1
,
2
=>
NULL
)),
TRUE
);
$this
->
submitForm
(
array
(
'textfield'
=>
'dummy value'
,
'checkboxes'
=>
array
(
1
=>
NULL
,
2
=>
2
)),
TRUE
);
$this
->
submitForm
(
array
(
'textfield'
=>
'dummy value'
,
'checkboxes'
=>
array
(
1
=>
NULL
,
2
=>
NULL
)),
TRUE
);
// Restore the current batch status.
$batch
=
$current_batch
;
...
...
@@ -984,30 +993,36 @@ class FormsProgrammaticTestCase extends DrupalWebTestCase {
/**
* Helper function used to programmatically submit the form defined in
* form_test.module with the given value.
* form_test.module with the given value
s
.
*
* @param string $value
* The field value to be submitted.
* @param $values
* An array of field values to be submitted.
* @param $valid_input
* A boolean indicating whether or not the form submission is expected to
* be valid.
*/
private
function
submitForm
(
$value
=
NULL
)
{
// Programmatically submit the given value.
$form_state
=
array
(
'values'
=>
array
(
'submitted_field'
=>
$value
)
);
private
function
submitForm
(
$value
s
,
$valid_input
)
{
// Programmatically submit the given value
s
.
$form_state
=
array
(
'values'
=>
$value
s
);
drupal_form_submit
(
'form_test_programmatic_form'
,
$form_state
);
// Check that the form returns an error when expected, and vice versa.
$errors
=
form_get_errors
();
$valid_form
=
empty
(
$errors
);
$
valid_input
=
!
empty
(
$value
);
// If no value was passed the form should return an error and viceversa.
$args
=
array
(
'%value'
=>
$value
,
'%errors'
=>
$valid_form
?
''
:
implode
(
' '
,
$errors
)
);
$this
->
assertTrue
(
$valid_input
==
$valid_form
,
t
(
'Input value: %value<br/>Validation handler errors: %errors'
,
$args
));
$
args
=
array
(
'%values'
=>
print_r
(
$values
,
TRUE
),
'%errors'
=>
$valid_form
?
t
(
'None'
)
:
implode
(
' '
,
$errors
),
);
$this
->
assertTrue
(
$valid_input
==
$valid_form
,
t
(
'Input value
s
: %value
s
<br/>Validation handler errors: %errors'
,
$args
));
// We check submitted values only if we have a valid input.
if
(
$valid_input
)
{
// By fetching the value from $form_state['storage'] we ensure that the
// By fetching the value
s
from $form_state['storage'] we ensure that the
// submission handler was properly executed.
$submitted_value
=
$form_state
[
'storage'
][
'programmatic_form_submit'
];
$this
->
assertTrue
(
$submitted_value
==
$value
,
t
(
'Submission handler correctly executed: %submitted_value'
,
array
(
'%submitted_value'
=>
$submitted_value
)));
$stored_values
=
$form_state
[
'storage'
][
'programmatic_form_submit'
];
foreach
(
$values
as
$key
=>
$value
)
{
$this
->
assertTrue
(
isset
(
$stored_values
[
$key
])
&&
$stored_values
[
$key
]
==
$value
,
t
(
'Submission handler correctly executed: %stored_key is %stored_value'
,
array
(
'%stored_key'
=>
$key
,
'%stored_value'
=>
print_r
(
$value
,
TRUE
))));
}
}
}
}
...
...
modules/simpletest/tests/form_test.module
View file @
123bf46c
...
...
@@ -1051,10 +1051,20 @@ function form_test_form_form_test_state_persist_alter(&$form, &$form_state) {
* Form builder to test programmatic form submissions.
*/
function
form_test_programmatic_form
(
$form
,
&
$form_state
)
{
$form
[
'
submitted_
field'
]
=
array
(
'#title'
=>
'
Submitte
d'
,
$form
[
'
text
field'
]
=
array
(
'#title'
=>
'
Textfiel
d'
,
'#type'
=>
'textfield'
,
);
$form
[
'checkboxes'
]
=
array
(
'#type'
=>
'checkboxes'
,
'#options'
=>
array
(
1
=>
'First checkbox'
,
2
=>
'Second checkbox'
,
),
// Both checkboxes are selected by default so that we can test the ability
// of programmatic form submissions to uncheck them.
'#default_value'
=>
array
(
1
,
2
),
);
return
$form
;
}
...
...
@@ -1066,8 +1076,8 @@ function form_test_programmatic_form($form, &$form_state) {
* explicitly required here.
*/
function
form_test_programmatic_form_validate
(
$form
,
&
$form_state
)
{
if
(
empty
(
$form_state
[
'values'
][
'
submitted_
field'
]))
{
form_set_error
(
'
submitted_
field'
,
t
(
'
Submitted
field is required.'
));
if
(
empty
(
$form_state
[
'values'
][
'
text
field'
]))
{
form_set_error
(
'
text
field'
,
t
(
'
Text
field is required.'
));
}
}
...
...
@@ -1075,10 +1085,10 @@ function form_test_programmatic_form_validate($form, &$form_state) {
* Form submit handler for programmatic form submissions.
*
* To test that the submission handler is correctly executed, we store the
* submitted value in a place we can access from the caller context.
* submitted value
s
in a place we can access from the caller context.
*/
function
form_test_programmatic_form_submit
(
$form
,
&
$form_state
)
{
$form_state
[
'storage'
][
'programmatic_form_submit'
]
=
$form_state
[
'values'
]
[
'submitted_field'
]
;
$form_state
[
'storage'
][
'programmatic_form_submit'
]
=
$form_state
[
'values'
];
}
/**
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment