View source
<?php
namespace Drupal\Tests\system\Functional\Form;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Tests\BrowserTestBase;
class ElementTest extends BrowserTestBase {
protected static $modules = [
'form_test',
];
protected $defaultTheme = 'classy';
public function testPlaceHolderText() {
$this
->drupalGet('form-test/placeholder-text');
foreach ([
'textfield',
'tel',
'url',
'password',
'email',
'number',
'textarea',
] as $type) {
$field = $this
->assertSession()
->fieldExists("edit-{$type}");
$this
->assertSame('placeholder-text', $field
->getAttribute('placeholder'));
}
}
public function testOptions() {
$this
->drupalGet('form-test/checkboxes-radios');
foreach ([
'checkbox',
'radio',
] as $type) {
$elements = $this
->xpath('//input[@type=:type]', [
':type' => $type,
]);
$expected_values = [
'0',
'foo',
'1',
'bar',
'>',
];
foreach ($elements as $element) {
$expected = array_shift($expected_values);
$this
->assertSame($expected, (string) $element
->getAttribute('value'));
}
}
$this
->assertSession()
->responseContains("<em>Special Char</em>alert('checkboxes');");
$this
->assertSession()
->responseContains("<em>Special Char</em>alert('radios');");
$this
->assertSession()
->responseContains('<em>Bar - checkboxes</em>');
$this
->assertSession()
->responseContains('<em>Bar - radios</em>');
$this
->drupalGet('form-test/checkboxes-radios/customize');
foreach ([
'checkbox',
'radio',
] as $type) {
$elements = $this
->xpath('//input[@type=:type]', [
':type' => $type,
]);
$expected_values = [
'0',
'foo',
'bar',
'>',
'1',
];
foreach ($elements as $element) {
$expected = array_shift($expected_values);
$this
->assertSame($expected, (string) $element
->getAttribute('value'));
}
}
foreach ([
'checkboxes',
'radios',
] as $type) {
$this
->assertSession()
->elementExists('xpath', "//input[@id='edit-{$type}-foo']/following-sibling::div[@class='description']");
}
}
public function testRadiosChecked() {
$this
->drupalGet('form-test/radios-checked');
$this
->assertSession()
->elementsCount('xpath', '//input[@name="radios" and @checked]', 1);
$this
->assertSession()
->fieldValueEquals("radios", '0');
$this
->assertSession()
->elementsCount('xpath', '//input[@name="radios-string" and @checked]', 1);
$this
->assertSession()
->fieldValueEquals("radios-string", 'bar');
$this
->assertSession()
->elementsCount('xpath', '//input[@name="radios-boolean-true" and @checked]', 1);
$this
->assertSession()
->fieldValueEquals("radios-boolean-true", '1');
$this
->assertSession()
->elementNotExists('xpath', '//input[@name="radios-boolean-false" and @checked]');
$this
->assertSession()
->elementsCount('xpath', '//input[@name="radios-boolean-any" and @checked]', 1);
$this
->assertSession()
->fieldValueEquals("radios-boolean-any", 'All');
$this
->assertSession()
->elementsCount('xpath', '//input[@name="radios-string-zero" and @checked]', 1);
$this
->assertSession()
->fieldValueEquals("radios-string-zero", '0');
$this
->assertSession()
->elementsCount('xpath', '//input[@name="radios-int-non-zero" and @checked]', 1);
$this
->assertSession()
->fieldValueEquals("radios-int-non-zero", '10');
$this
->assertSession()
->elementsCount('xpath', '//input[@name="radios-int-non-zero-as-string" and @checked]', 1);
$this
->assertSession()
->fieldValueEquals("radios-int-non-zero-as-string", '100');
$this
->assertSession()
->elementsCount('xpath', '//input[@name="radios-empty-string" and @checked]', 1);
$this
->assertSession()
->fieldValueEquals("radios-empty-string", '0');
$this
->assertSession()
->elementNotExists('xpath', '//input[@name="radios-empty-array" and @checked]');
$this
->assertSession()
->elementsCount('xpath', '//input[@name="radios-key-FALSE" and @checked]', 1);
$this
->assertSession()
->fieldValueEquals("radios-key-FALSE", '0');
}
public function testWrapperIds() {
$this
->drupalGet('form-test/checkboxes-radios');
foreach ([
'checkboxes',
'radios',
] as $type) {
$this
->assertSession()
->elementsCount('xpath', "//div[@id='edit-{$type}']", 1);
$wrapper_ids = $this
->xpath('//fieldset[@id=:id]', [
':id' => 'edit-' . $type . '--wrapper',
]);
$this
->assertCount(1, $wrapper_ids, new FormattableMarkup('A single wrapper id found for type %type', [
'%type' => $type,
]));
}
}
public function testButtonClasses() {
$this
->drupalGet('form-test/button-class');
$this
->assertCount(2, $this
->xpath('//*[contains(concat(" ", @class, " "), " button ")]'));
$this
->assertCount(1, $this
->xpath('//*[contains(concat(" ", @class, " "), " button--foo ")]'));
$this
->assertCount(1, $this
->xpath('//*[contains(concat(" ", @class, " "), " button--danger ")]'));
}
public function testGroupElements() {
$this
->drupalGet('form-test/group-details');
$this
->assertSession()
->elementsCount('xpath', '//div[@class="details-wrapper"]//div[@class="details-wrapper"]//label', 1);
$this
->drupalGet('form-test/group-container');
$this
->assertSession()
->elementsCount('xpath', '//div[@id="edit-container"]//div[@class="details-wrapper"]//label', 1);
$this
->drupalGet('form-test/group-fieldset');
$this
->assertSession()
->elementsCount('xpath', '//fieldset[@id="edit-fieldset"]//div[@id="edit-meta"]//label', 1);
$this
->assertSession()
->elementTextEquals('xpath', '//fieldset[@id="edit-fieldset-zero"]//legend', '0');
$this
->drupalGet('form-test/group-vertical-tabs');
$this
->assertSession()
->elementsCount('xpath', '//div[@data-vertical-tabs-panes]//details[@id="edit-meta"]//label', 1);
$this
->assertSession()
->elementsCount('xpath', '//div[@data-vertical-tabs-panes]//details[@id="edit-meta-2"]//label', 1);
}
public function testRequiredFieldsetsAndDetails() {
$this
->drupalGet('form-test/group-details');
$this
->assertEmpty($this
->cssSelect('summary.form-required'));
$this
->drupalGet('form-test/group-details/1');
$this
->assertNotEmpty($this
->cssSelect('summary.form-required'));
$this
->drupalGet('form-test/group-fieldset');
$this
->assertEmpty($this
->cssSelect('span.form-required'));
$this
->drupalGet('form-test/group-fieldset/1');
$this
->assertNotEmpty($this
->cssSelect('span.form-required'));
}
public function testFormAutocomplete() {
$this
->drupalGet('form-test/autocomplete');
$this
->assertSession()
->elementNotExists('xpath', '//input[@id="edit-autocomplete-1" and contains(@data-autocomplete-path, "form-test/autocomplete-1")]');
$this
->assertSession()
->elementNotExists('xpath', '//input[@id="edit-autocomplete-2" and contains(@data-autocomplete-path, "form-test/autocomplete-2/value")]');
$user = $this
->drupalCreateUser([
'access autocomplete test',
]);
$this
->drupalLogin($user);
$this
->drupalGet('form-test/autocomplete');
$this
->assertSession()
->responseContains('core/misc/autocomplete.js');
$this
->assertSession()
->elementExists('xpath', '//input[@id="edit-autocomplete-1" and contains(@data-autocomplete-path, "form-test/autocomplete-1")]');
$this
->assertSession()
->elementExists('xpath', '//input[@id="edit-autocomplete-2" and contains(@data-autocomplete-path, "form-test/autocomplete-2/value")]');
}
public function testFormElementErrors() {
$this
->drupalGet('form_test/details-form');
$this
->submitForm([], 'Submit');
$this
->assertSession()
->pageTextContains('I am an error on the details element.');
}
public function testDetailsSummaryAttributes() {
$this
->drupalGet('form-test/group-details');
$this
->assertSession()
->elementExists('css', 'summary[data-summary-attribute="test"]');
}
}