function FormValuesTest::testSimpleInvalidCallbacksAjaxFormValue

Same name in other branches
  1. 10 core/tests/Drupal/FunctionalJavascriptTests/Ajax/FormValuesTest.php \Drupal\FunctionalJavascriptTests\Ajax\FormValuesTest::testSimpleInvalidCallbacksAjaxFormValue()

Tests that AJAX elements with invalid callbacks return error code 500.

File

core/tests/Drupal/FunctionalJavascriptTests/Ajax/FormValuesTest.php, line 81

Class

FormValuesTest
Tests that form values are properly delivered to AJAX callbacks.

Namespace

Drupal\FunctionalJavascriptTests\Ajax

Code

public function testSimpleInvalidCallbacksAjaxFormValue() : void {
    $this->drupalGet('ajax_forms_test_get_form');
    $session = $this->getSession();
    // Ensure the test error log is empty before these tests.
    $this->assertFileDoesNotExist(DRUPAL_ROOT . '/' . $this->siteDirectory . '/error.log');
    // We're going to do some invalid requests. The JavaScript errors thrown
    // whilst doing so are expected. Do not interpret them as a test failure.
    $this->failOnJavascriptConsoleErrors = FALSE;
    // We don't need to check for the X-Drupal-Ajax-Token header with these
    // invalid requests.
    foreach ([
        'null',
        'empty',
        'nonexistent',
    ] as $key) {
        $element_name = 'select_' . $key . '_callback';
        // Updating the field will trigger an AJAX request/response.
        $session->getPage()
            ->selectFieldOption($element_name, 'green');
        // The select element is disabled as the AJAX request is issued.
        $this->assertSession()
            ->waitForElement('css', "select[name=\"{$element_name}\"]:disabled");
        // The select element is enabled as the response is received.
        $this->assertSession()
            ->waitForElement('css', "select[name=\"{$element_name}\"]:enabled");
        // Not using File API, a potential error must trigger a PHP warning, which
        // should be logged in the error.log.
        $this->assertFileExists(DRUPAL_ROOT . '/' . $this->siteDirectory . '/error.log');
        $this->assertStringContainsString('"The specified #ajax callback is empty or not callable."', file_get_contents(DRUPAL_ROOT . '/' . $this->siteDirectory . '/error.log'));
        // Remove error.log, so we have a clean slate for the next request.
        unlink(\Drupal::root() . '/' . $this->siteDirectory . '/error.log');
    }
    // We need to reload the page to kill any unfinished AJAX calls before
    // tearDown() is called.
    $this->drupalGet('ajax_forms_test_get_form');
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.