DependentDropdownTest.php
Namespace
Drupal\Tests\ajax_example\Functional
File
-
modules/ajax_example/tests/src/Functional/DependentDropdownTest.php
View source
<?php
namespace Drupal\Tests\ajax_example\Functional;
use Drupal\Core\Url;
use Drupal\Tests\BrowserTestBase;
class DependentDropdownTest extends BrowserTestBase {
protected $defaultTheme = 'stark';
protected static $modules = [
'ajax_example',
];
public function testDependentDropdown() {
$session = $this->getSession();
$assert = $this->assertSession();
$page = $session->getPage();
$dropdown_url = Url::fromRoute('ajax_example.dependent_dropdown', [
'nojs' => 'nojs',
]);
$this->drupalGet($dropdown_url);
$assert->fieldDisabled('instrument_dropdown');
$assert->fieldValueEquals('instrument_dropdown', 'none');
$submit_button = $page->findButton('edit-submit');
$this->assertTrue($submit_button->hasAttribute('disabled'));
$families = [
'String' => [
'Violin',
'Viola',
'Cello',
'Double Bass',
],
'Woodwind' => [
'Flute',
'Clarinet',
'Oboe',
'Bassoon',
],
'Brass' => [
'Trumpet',
'Trombone',
'French Horn',
'Euphonium',
],
'Percussion' => [
'Bass Drum',
'Timpani',
'Snare Drum',
'Tambourine',
],
];
foreach ($families as $family => $instruments) {
$this->drupalGet($dropdown_url);
$this->submitForm([
'instrument_family_dropdown' => $family,
], 'Choose');
$instrument_options = $page->findAll('css', '#edit-instrument-dropdown option');
$this->assertCount(count($instruments), $instrument_options);
foreach ($instrument_options as $instrument) {
$this->assertContains($instrument->getAttribute('value'), $instruments);
}
foreach ($instruments as $instrument) {
$this->drupalGet($dropdown_url);
$this->submitForm([
'instrument_family_dropdown' => $family,
], 'Choose');
$this->submitForm([
'instrument_dropdown' => $instrument,
], 'Submit');
$assert->pageTextContains("Your values have been submitted. Instrument family: {$family}, Instrument: {$instrument}");
}
}
}
}
Classes