358db893388b4865766b30c09fc516904edebbbd
[yaffs-website] / web / core / modules / system / src / Tests / Ajax / FormValuesTest.php
1 <?php
2
3 namespace Drupal\system\Tests\Ajax;
4
5 use Drupal\Core\Ajax\DataCommand;
6
7 /**
8  * Tests that form values are properly delivered to AJAX callbacks.
9  *
10  * @group Ajax
11  */
12 class FormValuesTest extends AjaxTestBase {
13
14   protected function setUp() {
15     parent::setUp();
16
17     $this->drupalLogin($this->drupalCreateUser(['access content']));
18   }
19
20   /**
21    * Submits forms with select and checkbox elements via Ajax.
22    */
23   public function testSimpleAjaxFormValue() {
24     // Verify form values of a select element.
25     foreach (['red', 'green', 'blue'] as $item) {
26       $edit = [
27         'select' => $item,
28       ];
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.');
32     }
33
34     // Verify form values of a checkbox element.
35     foreach ([FALSE, TRUE] as $item) {
36       $edit = [
37         'checkbox' => $item,
38       ];
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.');
42     }
43
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
48     // invalid requests.
49     $this->assertAjaxHeader = FALSE;
50     foreach (['null', 'empty', 'nonexistent'] as $key) {
51       $element_name = 'select_' . $key . '_callback';
52       $edit = [
53         $element_name => 'red',
54       ];
55       $commands = $this->drupalPostAjaxForm('ajax_forms_test_get_form', $edit, $element_name);
56       $this->assertResponse(500);
57     }
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');
63   }
64
65 }