FormValuesTest.php in Drupal 10
File
core/tests/Drupal/FunctionalJavascriptTests/Ajax/FormValuesTest.php
View source
<?php
namespace Drupal\FunctionalJavascriptTests\Ajax;
use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
class FormValuesTest extends WebDriverTestBase {
protected static $modules = [
'node',
'ajax_test',
'ajax_forms_test',
];
protected $defaultTheme = 'stark';
protected function setUp() : void {
parent::setUp();
$this
->drupalLogin($this
->drupalCreateUser([
'access content',
]));
}
public function testSimpleAjaxFormValue() {
$this
->drupalGet('ajax_forms_test_get_form');
$session = $this
->getSession();
$assertSession = $this
->assertSession();
foreach ([
'green',
'blue',
'red',
] as $item) {
$session
->getPage()
->selectFieldOption('select', $item);
$select = $assertSession
->waitForElement('css', "div#ajax_selected_color:contains('{$item}')");
$this
->assertNotNull($select, "DataCommand has updated the page with a value of {$item}.");
}
$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');
$this
->assertFileDoesNotExist(DRUPAL_ROOT . '/' . $this->siteDirectory . '/error.log');
foreach ([
'null',
'empty',
'nonexistent',
] as $key) {
$element_name = 'select_' . $key . '_callback';
$session
->getPage()
->selectFieldOption($element_name, 'green');
$this
->assertSession()
->waitForElement('css', "select[name=\"{$element_name}\"]:disabled");
$this
->assertSession()
->waitForElement('css', "select[name=\"{$element_name}\"]:enabled");
$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'));
unlink(\Drupal::root() . '/' . $this->siteDirectory . '/error.log');
}
$this
->drupalGet('ajax_forms_test_get_form');
}
}
Classes
Name |
Description |
FormValuesTest |
Tests that form values are properly delivered to AJAX callbacks. |