Commit 8f2862aa authored by alexpott's avatar alexpott

Issue #2809531 by Lendude, martin107, larowlan: Convert AJAX part of...

Issue #2809531 by Lendude, martin107, larowlan: Convert AJAX part of \Drupal\system\Tests\Ajax\FormValuesTest::testSimpleAjaxFormValue to JavascriptTestBase

(cherry picked from commit 752dd393)
parent 6ef9ab93
<?php
namespace Drupal\system\Tests\Ajax;
use Drupal\Core\Ajax\DataCommand;
/**
* Tests that form values are properly delivered to AJAX callbacks.
*
* @group Ajax
*/
class FormValuesTest extends AjaxTestBase {
protected function setUp() {
parent::setUp();
$this->drupalLogin($this->drupalCreateUser(['access content']));
}
/**
* Submits forms with select and checkbox elements via Ajax.
*/
public function testSimpleAjaxFormValue() {
// Verify form values of a select element.
foreach (['red', 'green', 'blue'] as $item) {
$edit = [
'select' => $item,
];
$commands = $this->drupalPostAjaxForm('ajax_forms_test_get_form', $edit, 'select');
$expected = new DataCommand('#ajax_selected_color', 'form_state_value_select', $item);
$this->assertCommand($commands, $expected->render(), 'Verification of AJAX form values from a selectbox issued with a correct value.');
}
// Verify form values of a checkbox element.
foreach ([FALSE, TRUE] as $item) {
$edit = [
'checkbox' => $item,
];
$commands = $this->drupalPostAjaxForm('ajax_forms_test_get_form', $edit, 'checkbox');
$expected = new DataCommand('#ajax_checkbox_value', 'form_state_value_select', (int) $item);
$this->assertCommand($commands, $expected->render(), 'Verification of AJAX form values from a checkbox issued with a correct value.');
}
// Verify that AJAX elements with invalid callbacks return error code 500.
// Ensure the test error log is empty before these tests.
$this->assertNoErrorsLogged();
// We don't need to check for the X-Drupal-Ajax-Token header with these
// invalid requests.
$this->assertAjaxHeader = FALSE;
foreach (['null', 'empty', 'nonexistent'] as $key) {
$element_name = 'select_' . $key . '_callback';
$edit = [
$element_name => 'red',
];
$commands = $this->drupalPostAjaxForm('ajax_forms_test_get_form', $edit, $element_name);
$this->assertResponse(500);
}
// Switch this back to the default.
$this->assertAjaxHeader = TRUE;
// The exceptions are expected. Do not interpret them as a test failure.
// Not using File API; a potential error must trigger a PHP warning.
unlink(\Drupal::root() . '/' . $this->siteDirectory . '/error.log');
}
}
......@@ -50,7 +50,7 @@ public function datetimeCallback($form, FormStateInterface $form_state) {
*/
public function checkboxCallback($form, FormStateInterface $form_state) {
$response = new AjaxResponse();
$response->addCommand(new HtmlCommand('#ajax_checkbox_value', (int) $form_state->getValue('checkbox')));
$response->addCommand(new HtmlCommand('#ajax_checkbox_value', $form_state->getValue('checkbox') ? 'checked' : 'unchecked'));
$response->addCommand(new DataCommand('#ajax_checkbox_value', 'form_state_value_select', (int) $form_state->getValue('checkbox')));
return $response;
}
......
......@@ -65,7 +65,7 @@ public function buildForm(array $form, FormStateInterface $form_state) {
$form['select_' . $key . '_callback'] = [
'#type' => 'select',
'#title' => $this->t('Test %key callbacks', ['%key' => $key]),
'#options' => ['red' => 'red'],
'#options' => ['red' => 'red', 'green' => 'green'],
'#ajax' => ['callback' => $value],
];
}
......
<?php
namespace Drupal\FunctionalJavascriptTests\Ajax;
use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
/**
* Tests that form values are properly delivered to AJAX callbacks.
*
* @group Ajax
*/
class FormValuesTest extends WebDriverTestBase {
/**
* {@inheritdoc}
*/
public static $modules = ['node', 'ajax_test', 'ajax_forms_test'];
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->drupalLogin($this->drupalCreateUser(['access content']));
}
/**
* Submits forms with select and checkbox elements via Ajax.
*/
public function testSimpleAjaxFormValue() {
$this->drupalGet('ajax_forms_test_get_form');
$session = $this->getSession();
$assertSession = $this->assertSession();
// Verify form values of a select element.
foreach (['green', 'blue', 'red'] as $item) {
// Updating the field will trigger a AJAX request/response.
$session->getPage()->selectFieldOption('select', $item);
// The AJAX command in the response will update the DOM
$select = $assertSession->waitForElement('css', "div#ajax_selected_color:contains('$item')");
$this->assertNotNull($select, "DataCommand has updated the page with a value of $item.");
}
// Verify form values of a checkbox element.
$session->getPage()->checkField('checkbox');
$div0 = $this->assertSession()->waitForElement('css', "div#ajax_checkbox_value:contains('checked')");
$this->assertNotNull($div0, 'DataCommand updates the DOM as expected when a checkbox is selected');
$session->getPage()->uncheckField('checkbox');
$div1 = $this->assertSession()->waitForElement('css', "div#ajax_checkbox_value:contains('unchecked')");
$this->assertNotNull($div1, 'DataCommand updates the DOM as expected when a checkbox is de-selected');
// Verify that AJAX elements with invalid callbacks return error code 500.
// Ensure the test error log is empty before these tests.
$this->assertFalse(file_exists(DRUPAL_ROOT . '/' . $this->siteDirectory . '/error.log'), 'PHP error.log is empty.');
// We don't need to check for the X-Drupal-Ajax-Token header with these
// invalid requests.
$this->assertAjaxHeader = FALSE;
foreach (['null', 'empty', 'nonexistent'] as $key) {
$element_name = 'select_' . $key . '_callback';
// Updating the field will trigger a 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 receieved.
$this->assertSession()->waitForElement('css', "select[name=\"$element_name\"]:enabled");
$this->assertTrue(file_exists(DRUPAL_ROOT . '/' . $this->siteDirectory . '/error.log'), 'PHP error.log is not empty.');
$this->assertContains('"The specified #ajax callback is empty or not callable."', file_get_contents(DRUPAL_ROOT . '/' . $this->siteDirectory . '/error.log'));
// The exceptions are expected. Do not interpret them as a test failure.
// Not using File API; a potential error must trigger a PHP warning.
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');
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment