3 namespace Drupal\system\Tests\Ajax;
5 use Drupal\Core\Ajax\DataCommand;
8 * Tests that form values are properly delivered to AJAX callbacks.
12 class FormValuesTest extends AjaxTestBase {
13 protected function setUp() {
16 $this->drupalLogin($this->drupalCreateUser(['access content']));
20 * Submits forms with select and checkbox elements via Ajax.
22 public function testSimpleAjaxFormValue() {
23 // Verify form values of a select element.
24 foreach (['red', 'green', 'blue'] as $item) {
28 $commands = $this->drupalPostAjaxForm('ajax_forms_test_get_form', $edit, 'select');
29 $expected = new DataCommand('#ajax_selected_color', 'form_state_value_select', $item);
30 $this->assertCommand($commands, $expected->render(), 'Verification of AJAX form values from a selectbox issued with a correct value.');
33 // Verify form values of a checkbox element.
34 foreach ([FALSE, TRUE] as $item) {
38 $commands = $this->drupalPostAjaxForm('ajax_forms_test_get_form', $edit, 'checkbox');
39 $expected = new DataCommand('#ajax_checkbox_value', 'form_state_value_select', (int) $item);
40 $this->assertCommand($commands, $expected->render(), 'Verification of AJAX form values from a checkbox issued with a correct value.');
43 // Verify that AJAX elements with invalid callbacks return error code 500.
44 // Ensure the test error log is empty before these tests.
45 $this->assertNoErrorsLogged();
46 // We don't need to check for the X-Drupal-Ajax-Token header with these
48 $this->assertAjaxHeader = FALSE;
49 foreach (['null', 'empty', 'nonexistent'] as $key) {
50 $element_name = 'select_' . $key . '_callback';
52 $element_name => 'red',
54 $commands = $this->drupalPostAjaxForm('ajax_forms_test_get_form', $edit, $element_name);
55 $this->assertResponse(500);
57 // Switch this back to the default.
58 $this->assertAjaxHeader = TRUE;
59 // The exceptions are expected. Do not interpret them as a test failure.
60 // Not using File API; a potential error must trigger a PHP warning.
61 unlink(\Drupal::root() . '/' . $this->siteDirectory . '/error.log');