3 namespace Drupal\FunctionalJavascriptTests\Ajax;
5 use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
8 * Tests that form values are properly delivered to AJAX callbacks.
12 class FormValuesTest extends WebDriverTestBase {
17 public static $modules = ['node', 'ajax_test', 'ajax_forms_test'];
22 protected function setUp() {
25 $this->drupalLogin($this->drupalCreateUser(['access content']));
29 * Submits forms with select and checkbox elements via Ajax.
31 public function testSimpleAjaxFormValue() {
33 $this->drupalGet('ajax_forms_test_get_form');
35 $session = $this->getSession();
36 $assertSession = $this->assertSession();
38 // Verify form values of a select element.
39 foreach (['green', 'blue', 'red'] as $item) {
40 // Updating the field will trigger a AJAX request/response.
41 $session->getPage()->selectFieldOption('select', $item);
43 // The AJAX command in the response will update the DOM
44 $select = $assertSession->waitForElement('css', "div#ajax_selected_color:contains('$item')");
45 $this->assertNotNull($select, "DataCommand has updated the page with a value of $item.");
48 // Verify form values of a checkbox element.
49 $session->getPage()->checkField('checkbox');
50 $div0 = $this->assertSession()->waitForElement('css', "div#ajax_checkbox_value:contains('checked')");
51 $this->assertNotNull($div0, 'DataCommand updates the DOM as expected when a checkbox is selected');
53 $session->getPage()->uncheckField('checkbox');
54 $div1 = $this->assertSession()->waitForElement('css', "div#ajax_checkbox_value:contains('unchecked')");
55 $this->assertNotNull($div1, 'DataCommand updates the DOM as expected when a checkbox is de-selected');
57 // Verify that AJAX elements with invalid callbacks return error code 500.
58 // Ensure the test error log is empty before these tests.
59 $this->assertFalse(file_exists(DRUPAL_ROOT . '/' . $this->siteDirectory . '/error.log'), 'PHP error.log is empty.');
60 // We don't need to check for the X-Drupal-Ajax-Token header with these
62 $this->assertAjaxHeader = FALSE;
63 foreach (['null', 'empty', 'nonexistent'] as $key) {
64 $element_name = 'select_' . $key . '_callback';
65 // Updating the field will trigger a AJAX request/response.
66 $session->getPage()->selectFieldOption($element_name, 'green');
68 // The select element is disabled as the AJAX request is issued.
69 $this->assertSession()->waitForElement('css', "select[name=\"$element_name\"]:disabled");
71 // The select element is enabled as the response is receieved.
72 $this->assertSession()->waitForElement('css', "select[name=\"$element_name\"]:enabled");
73 $this->assertTrue(file_exists(DRUPAL_ROOT . '/' . $this->siteDirectory . '/error.log'), 'PHP error.log is not empty.');
74 $this->assertContains('"The specified #ajax callback is empty or not callable."', file_get_contents(DRUPAL_ROOT . '/' . $this->siteDirectory . '/error.log'));
75 // The exceptions are expected. Do not interpret them as a test failure.
76 // Not using File API; a potential error must trigger a PHP warning.
77 unlink(\Drupal::root() . '/' . $this->siteDirectory . '/error.log');
79 // We need to reload the page to kill any unfinished AJAX calls before
80 // tearDown() is called.
81 $this->drupalGet('ajax_forms_test_get_form');