Chris@17: drupalLogin($this->drupalCreateUser(['access content'])); Chris@17: } Chris@17: Chris@17: /** Chris@17: * Submits forms with select and checkbox elements via Ajax. Chris@17: */ Chris@17: public function testSimpleAjaxFormValue() { Chris@17: Chris@17: $this->drupalGet('ajax_forms_test_get_form'); Chris@17: Chris@17: $session = $this->getSession(); Chris@17: $assertSession = $this->assertSession(); Chris@17: Chris@17: // Verify form values of a select element. Chris@17: foreach (['green', 'blue', 'red'] as $item) { Chris@17: // Updating the field will trigger a AJAX request/response. Chris@17: $session->getPage()->selectFieldOption('select', $item); Chris@17: Chris@17: // The AJAX command in the response will update the DOM Chris@17: $select = $assertSession->waitForElement('css', "div#ajax_selected_color:contains('$item')"); Chris@17: $this->assertNotNull($select, "DataCommand has updated the page with a value of $item."); Chris@17: } Chris@17: Chris@17: // Verify form values of a checkbox element. Chris@17: $session->getPage()->checkField('checkbox'); Chris@17: $div0 = $this->assertSession()->waitForElement('css', "div#ajax_checkbox_value:contains('checked')"); Chris@17: $this->assertNotNull($div0, 'DataCommand updates the DOM as expected when a checkbox is selected'); Chris@17: Chris@17: $session->getPage()->uncheckField('checkbox'); Chris@17: $div1 = $this->assertSession()->waitForElement('css', "div#ajax_checkbox_value:contains('unchecked')"); Chris@17: $this->assertNotNull($div1, 'DataCommand updates the DOM as expected when a checkbox is de-selected'); Chris@17: Chris@17: // Verify that AJAX elements with invalid callbacks return error code 500. Chris@17: // Ensure the test error log is empty before these tests. Chris@17: $this->assertFalse(file_exists(DRUPAL_ROOT . '/' . $this->siteDirectory . '/error.log'), 'PHP error.log is empty.'); Chris@17: // We don't need to check for the X-Drupal-Ajax-Token header with these Chris@17: // invalid requests. Chris@17: $this->assertAjaxHeader = FALSE; Chris@17: foreach (['null', 'empty', 'nonexistent'] as $key) { Chris@17: $element_name = 'select_' . $key . '_callback'; Chris@17: // Updating the field will trigger a AJAX request/response. Chris@17: $session->getPage()->selectFieldOption($element_name, 'green'); Chris@17: Chris@17: // The select element is disabled as the AJAX request is issued. Chris@17: $this->assertSession()->waitForElement('css', "select[name=\"$element_name\"]:disabled"); Chris@17: Chris@17: // The select element is enabled as the response is receieved. Chris@17: $this->assertSession()->waitForElement('css', "select[name=\"$element_name\"]:enabled"); Chris@17: $this->assertTrue(file_exists(DRUPAL_ROOT . '/' . $this->siteDirectory . '/error.log'), 'PHP error.log is not empty.'); Chris@17: $this->assertContains('"The specified #ajax callback is empty or not callable."', file_get_contents(DRUPAL_ROOT . '/' . $this->siteDirectory . '/error.log')); Chris@17: // The exceptions are expected. Do not interpret them as a test failure. Chris@17: // Not using File API; a potential error must trigger a PHP warning. Chris@17: unlink(\Drupal::root() . '/' . $this->siteDirectory . '/error.log'); Chris@17: } Chris@17: // We need to reload the page to kill any unfinished AJAX calls before Chris@17: // tearDown() is called. Chris@17: $this->drupalGet('ajax_forms_test_get_form'); Chris@17: } Chris@17: Chris@17: }