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 {
14 protected function setUp() {
17 $this->drupalLogin($this->drupalCreateUser(['access content']));
21 * Submits forms with select and checkbox elements via Ajax.
23 public function testSimpleAjaxFormValue() {
24 // Verify form values of a select element.
25 foreach (['red', 'green', 'blue'] as $item) {
29 $commands = $this->drupalPostAjaxForm('ajax_forms_test_get_form', $edit, 'select');
30 $expected = new DataCommand('#ajax_selected_color', 'form_state_value_select', $item);
31 $this->assertCommand($commands, $expected->render(), 'Verification of AJAX form values from a selectbox issued with a correct value.');
34 // Verify form values of a checkbox element.
35 foreach ([FALSE, TRUE] as $item) {
39 $commands = $this->drupalPostAjaxForm('ajax_forms_test_get_form', $edit, 'checkbox');
40 $expected = new DataCommand('#ajax_checkbox_value', 'form_state_value_select', (int) $item);
41 $this->assertCommand($commands, $expected->render(), 'Verification of AJAX form values from a checkbox issued with a correct value.');
44 // Verify that AJAX elements with invalid callbacks return error code 500.
45 // Ensure the test error log is empty before these tests.
46 $this->assertNoErrorsLogged();
47 // We don't need to check for the X-Drupal-Ajax-Token header with these
49 $this->assertAjaxHeader = FALSE;
50 foreach (['null', 'empty', 'nonexistent'] as $key) {
51 $element_name = 'select_' . $key . '_callback';
53 $element_name => 'red',
55 $commands = $this->drupalPostAjaxForm('ajax_forms_test_get_form', $edit, $element_name);
56 $this->assertResponse(500);
58 // Switch this back to the default.
59 $this->assertAjaxHeader = TRUE;
60 // The exceptions are expected. Do not interpret them as a test failure.
61 // Not using File API; a potential error must trigger a PHP warning.
62 unlink(\Drupal::root() . '/' . $this->siteDirectory . '/error.log');