View source
<?php
namespace Drupal\Tests\ajax_example\Functional;
use Drupal\Core\Url;
use Drupal\Tests\BrowserTestBase;
class DynamicFormSectionsTest extends BrowserTestBase {
protected $defaultTheme = 'stark';
public static $modules = [
'ajax_example',
];
public function testDynamicFormSections() {
$assert = $this
->assertSession();
$page = $this
->getSession()
->getPage();
$dropdown_url = Url::fromRoute('ajax_example.dynamic_form_sections', [
'nojs' => 'nojs',
]);
$this
->drupalGet($dropdown_url);
$detail_children = $page
->findAll('css', 'div.details-wrapper *');
$this
->assertEmpty($detail_children);
$this
->drupalPostForm($dropdown_url, [
'question_type_select' => 'Choose question style',
], 'Choose');
$detail_children = $page
->findAll('css', 'div.details-wrapper *');
$this
->assertEqual(count($detail_children), 0);
$question_styles = [
'Multiple Choice',
'True/False',
'Fill-in-the-blanks',
];
foreach ($question_styles as $question_style) {
$this
->drupalPostForm($dropdown_url, [
'question_type_select' => $question_style,
], 'Choose');
$detail_children = $page
->findAll('css', 'div.details-wrapper *');
$this
->assertNotEqual($this
->count($detail_children), 0);
$this
->drupalPostForm(NULL, [
'question' => 'George Washington',
], 'Submit your answer');
$assert
->pageTextContains('You got the right answer: George Washington');
}
$this
->drupalPostForm($dropdown_url, [
'question_type_select' => 'Multiple Choice',
], 'Choose');
$detail_children = $page
->findAll('css', 'div.details-wrapper *');
$this
->assertNotEqual($this
->count($detail_children), 0);
$this
->drupalPostForm(NULL, [
'question' => 'Abraham Lincoln',
], 'Submit your answer');
$assert
->pageTextContains('Sorry, your answer (Abraham Lincoln) is wrong');
}
}