Skip to content

Instantly share code, notes, and snippets.

@GoZOo
Last active January 19, 2017 11:24
Show Gist options
  • Save GoZOo/25ff090283364cf0f3b2d4f57851430e to your computer and use it in GitHub Desktop.
Save GoZOo/25ff090283364cf0f3b2d4f57851430e to your computer and use it in GitHub Desktop.
Work in progress to add tests on States attribute of form for Drupal 8
...
form_test.states_simple:
path: '/form_test/form-states-simple'
defaults:
_form: '\Drupal\form_test\Form\FormTestFormStatesSimpleForm'
_title: 'Form #states attribute simple select test'
requirements:
_access: 'TRUE'
...
<?php
namespace Drupal\FunctionalJavascriptTests\Form;
use Drupal\FunctionalJavascriptTests\JavascriptTestBase;
/**
* Tests Form #states attribute.
*
* @group Form
*/
class FormStatesTest extends JavascriptTestBase {
/**
* {@inheritdoc}
*/
public static $modules = ['form_test'];
/**
* Tests Form #states attribute for simple select list.
*/
public function testFormStatesSimple() {
// First visit the form.
$this->drupalGet('form_test/form-states-simple');
$assert = $this->assertSession();
$page = $this->getSession()->getPage();
$this->assertEquals('', $page->getHTML());
// Check dependee_1 A option is not selected by default.
$option_field = $assert->optionExists('dependee_1', 'a');
$this->assertFalse($option_field->hasAttribute('selected'), "Option 'a' for field dependee_1 is not selected.");
// Check dependent_1 field is not visible.
$assert->fieldNotExists('dependent_1');
// Select dependent_1 A option and check dependent field is visible.
$this->getSession()->getPage()->fillField('dependee_1', 'a');
$assert->fieldExists('dependent_1');
}
}
<?php
namespace Drupal\form_test\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
/**
* Form to test #states form attribute for simple select list.
*/
class FormTestFormStatesSimpleForm extends FormBase {
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'form_test_form_states_simple';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
// Build form with simple select list and no default option. dependent_1
// field should not be visible.
$form = [
'dependee_1' => [
'#type' => 'select',
'#title' => 'Dependee 1',
'#options' => [
'a' => 'Option A',
'b' => 'Option B',
'c' => 'Option C',
],
],
'dependent_1' => [
'#type' => 'textfield',
'#title' => 'Dependent 1',
'#states' => [
'visible' => [
'select[name="dependee_1"]' => ['value' => 'a'],
],
],
],
// Add same tests fields but with default A value. dependent_2 field
// should be visible.
'dependee_2' => [
'#type' => 'select',
'#title' => 'Dependee 2',
'#options' => [
'a' => 'Option A',
'b' => 'Option B',
'c' => 'Option C',
],
'#default_value' => 'a',
],
'dependent_2' => [
'#type' => 'textfield',
'#title' => 'Dependent 2',
'#states' => [
'visible' => [
'select[name="dependee_2"]' => ['value' => 'a'],
],
],
],
// Add same tests fields but with default B value. dependent_3 field
// should not be visible.
'dependee_3' => [
'#type' => 'select',
'#title' => 'Dependee 3',
'#options' => [
'a' => 'Option A',
'b' => 'Option B',
'c' => 'Option C',
],
'#default_value' => 'b',
],
'dependent_3' => [
'#type' => 'textfield',
'#title' => 'Dependent 3',
'#states' => [
'visible' => [
'select[name="dependee_3"]' => ['value' => 'a'],
],
],
],
];
return $form;
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$form_state->setResponse(new JsonResponse($form_state->getValues()));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment