View source
<?php
namespace Drupal\Tests\ajax_example\FunctionalJavascript;
use Drupal\Core\Url;
use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
class DependentDropdownTest extends WebDriverTestBase {
protected $defaultTheme = 'stark';
public static $modules = [
'ajax_example',
];
public function testDependentDropdown() {
$assert = $this
->assertSession();
$page = $this
->getSession()
->getPage();
$dropdown_url = Url::fromRoute('ajax_example.dependent_dropdown', [
'nojs' => 'ajax',
]);
$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) {
$family_dropdown = $assert
->fieldExists('instrument_family_dropdown');
$family_dropdown
->setValue($family);
$assert
->assertWaitOnAjaxRequest();
$instrument_options = $page
->findAll('css', 'select[name="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);
$family_dropdown
->setValue($family);
$assert
->assertWaitOnAjaxRequest();
$this
->drupalPostForm(NULL, [
'instrument_dropdown' => $instrument,
], 'Submit');
$assert
->pageTextContains("Your values have been submitted. Instrument family: {$family}, Instrument: {$instrument}");
}
}
}
}