PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/web/core/modules/system/tests/src/Functional/Form/ElementTest.php

https://gitlab.com/mohamed_hussein/prodt
PHP | 205 lines | 120 code | 25 blank | 60 comment | 0 complexity | 1361e4c1737e2e793c0766ff970e3d6c MD5 | raw file
  1. <?php
  2. namespace Drupal\Tests\system\Functional\Form;
  3. use Drupal\Component\Render\FormattableMarkup;
  4. use Drupal\Tests\BrowserTestBase;
  5. /**
  6. * Tests building and processing of core form elements.
  7. *
  8. * @group Form
  9. */
  10. class ElementTest extends BrowserTestBase {
  11. /**
  12. * Modules to enable.
  13. *
  14. * @var array
  15. */
  16. protected static $modules = ['form_test'];
  17. /**
  18. * {@inheritdoc}
  19. */
  20. protected $defaultTheme = 'classy';
  21. /**
  22. * Tests placeholder text for elements that support placeholders.
  23. */
  24. public function testPlaceHolderText() {
  25. $this->drupalGet('form-test/placeholder-text');
  26. foreach (['textfield', 'tel', 'url', 'password', 'email', 'number', 'textarea'] as $type) {
  27. $field = $this->assertSession()->fieldExists("edit-$type");
  28. $this->assertSame('placeholder-text', $field->getAttribute('placeholder'));
  29. }
  30. }
  31. /**
  32. * Tests expansion of #options for #type checkboxes and radios.
  33. */
  34. public function testOptions() {
  35. $this->drupalGet('form-test/checkboxes-radios');
  36. // Verify that all options appear in their defined order.
  37. foreach (['checkbox', 'radio'] as $type) {
  38. $elements = $this->xpath('//input[@type=:type]', [':type' => $type]);
  39. $expected_values = ['0', 'foo', '1', 'bar', '>'];
  40. foreach ($elements as $element) {
  41. $expected = array_shift($expected_values);
  42. $this->assertSame($expected, (string) $element->getAttribute('value'));
  43. }
  44. }
  45. // Verify that the choices are admin filtered as expected.
  46. $this->assertSession()->responseContains("<em>Special Char</em>alert('checkboxes');");
  47. $this->assertSession()->responseContains("<em>Special Char</em>alert('radios');");
  48. $this->assertSession()->responseContains('<em>Bar - checkboxes</em>');
  49. $this->assertSession()->responseContains('<em>Bar - radios</em>');
  50. // Enable customized option sub-elements.
  51. $this->drupalGet('form-test/checkboxes-radios/customize');
  52. // Verify that all options appear in their defined order, taking a custom
  53. // #weight into account.
  54. foreach (['checkbox', 'radio'] as $type) {
  55. $elements = $this->xpath('//input[@type=:type]', [':type' => $type]);
  56. $expected_values = ['0', 'foo', 'bar', '>', '1'];
  57. foreach ($elements as $element) {
  58. $expected = array_shift($expected_values);
  59. $this->assertSame($expected, (string) $element->getAttribute('value'));
  60. }
  61. }
  62. // Verify that custom #description properties are output.
  63. foreach (['checkboxes', 'radios'] as $type) {
  64. $this->assertSession()->elementExists('xpath', "//input[@id='edit-$type-foo']/following-sibling::div[@class='description']");
  65. }
  66. }
  67. /**
  68. * Tests correct checked attribute for radios element.
  69. */
  70. public function testRadiosChecked() {
  71. // Verify that there is only one radio option checked.
  72. $this->drupalGet('form-test/radios-checked');
  73. $this->assertSession()->elementsCount('xpath', '//input[@name="radios" and @checked]', 1);
  74. $this->assertSession()->fieldValueEquals("radios", '0');
  75. $this->assertSession()->elementsCount('xpath', '//input[@name="radios-string" and @checked]', 1);
  76. $this->assertSession()->fieldValueEquals("radios-string", 'bar');
  77. $this->assertSession()->elementsCount('xpath', '//input[@name="radios-boolean-true" and @checked]', 1);
  78. $this->assertSession()->fieldValueEquals("radios-boolean-true", '1');
  79. // A default value of FALSE indicates that nothing is set.
  80. $this->assertSession()->elementNotExists('xpath', '//input[@name="radios-boolean-false" and @checked]');
  81. $this->assertSession()->elementsCount('xpath', '//input[@name="radios-boolean-any" and @checked]', 1);
  82. $this->assertSession()->fieldValueEquals("radios-boolean-any", 'All');
  83. $this->assertSession()->elementsCount('xpath', '//input[@name="radios-string-zero" and @checked]', 1);
  84. $this->assertSession()->fieldValueEquals("radios-string-zero", '0');
  85. $this->assertSession()->elementsCount('xpath', '//input[@name="radios-int-non-zero" and @checked]', 1);
  86. $this->assertSession()->fieldValueEquals("radios-int-non-zero", '10');
  87. $this->assertSession()->elementsCount('xpath', '//input[@name="radios-int-non-zero-as-string" and @checked]', 1);
  88. $this->assertSession()->fieldValueEquals("radios-int-non-zero-as-string", '100');
  89. $this->assertSession()->elementsCount('xpath', '//input[@name="radios-empty-string" and @checked]', 1);
  90. $this->assertSession()->fieldValueEquals("radios-empty-string", '0');
  91. $this->assertSession()->elementNotExists('xpath', '//input[@name="radios-empty-array" and @checked]');
  92. $this->assertSession()->elementsCount('xpath', '//input[@name="radios-key-FALSE" and @checked]', 1);
  93. $this->assertSession()->fieldValueEquals("radios-key-FALSE", '0');
  94. }
  95. /**
  96. * Tests wrapper ids for checkboxes and radios.
  97. */
  98. public function testWrapperIds() {
  99. $this->drupalGet('form-test/checkboxes-radios');
  100. // Verify that wrapper id is different from element id.
  101. foreach (['checkboxes', 'radios'] as $type) {
  102. // A single element id is found.
  103. $this->assertSession()->elementsCount('xpath', "//div[@id='edit-$type']", 1);
  104. $wrapper_ids = $this->xpath('//fieldset[@id=:id]', [':id' => 'edit-' . $type . '--wrapper']);
  105. $this->assertCount(1, $wrapper_ids, new FormattableMarkup('A single wrapper id found for type %type', ['%type' => $type]));
  106. }
  107. }
  108. /**
  109. * Tests button classes.
  110. */
  111. public function testButtonClasses() {
  112. $this->drupalGet('form-test/button-class');
  113. // Just contains(@class, "button") won't do because then
  114. // "button--foo" would contain "button". Instead, check
  115. // " button ". Make sure it matches in the beginning and the end too
  116. // by adding a space before and after.
  117. $this->assertCount(2, $this->xpath('//*[contains(concat(" ", @class, " "), " button ")]'));
  118. $this->assertCount(1, $this->xpath('//*[contains(concat(" ", @class, " "), " button--foo ")]'));
  119. $this->assertCount(1, $this->xpath('//*[contains(concat(" ", @class, " "), " button--danger ")]'));
  120. }
  121. /**
  122. * Tests the #group property.
  123. */
  124. public function testGroupElements() {
  125. $this->drupalGet('form-test/group-details');
  126. $this->assertSession()->elementsCount('xpath', '//div[@class="details-wrapper"]//div[@class="details-wrapper"]//label', 1);
  127. $this->drupalGet('form-test/group-container');
  128. $this->assertSession()->elementsCount('xpath', '//div[@id="edit-container"]//div[@class="details-wrapper"]//label', 1);
  129. $this->drupalGet('form-test/group-fieldset');
  130. $this->assertSession()->elementsCount('xpath', '//fieldset[@id="edit-fieldset"]//div[@id="edit-meta"]//label', 1);
  131. $this->drupalGet('form-test/group-vertical-tabs');
  132. $this->assertSession()->elementsCount('xpath', '//div[@data-vertical-tabs-panes]//details[@id="edit-meta"]//label', 1);
  133. $this->assertSession()->elementsCount('xpath', '//div[@data-vertical-tabs-panes]//details[@id="edit-meta-2"]//label', 1);
  134. }
  135. /**
  136. * Tests the #required property on details and fieldset elements.
  137. */
  138. public function testRequiredFieldsetsAndDetails() {
  139. $this->drupalGet('form-test/group-details');
  140. $this->assertEmpty($this->cssSelect('summary.form-required'));
  141. $this->drupalGet('form-test/group-details/1');
  142. $this->assertNotEmpty($this->cssSelect('summary.form-required'));
  143. $this->drupalGet('form-test/group-fieldset');
  144. $this->assertEmpty($this->cssSelect('span.form-required'));
  145. $this->drupalGet('form-test/group-fieldset/1');
  146. $this->assertNotEmpty($this->cssSelect('span.form-required'));
  147. }
  148. /**
  149. * Tests a form with an autocomplete setting..
  150. */
  151. public function testFormAutocomplete() {
  152. $this->drupalGet('form-test/autocomplete');
  153. // Ensure that the user does not have access to the autocompletion.
  154. $this->assertSession()->elementNotExists('xpath', '//input[@id="edit-autocomplete-1" and contains(@data-autocomplete-path, "form-test/autocomplete-1")]');
  155. $this->assertSession()->elementNotExists('xpath', '//input[@id="edit-autocomplete-2" and contains(@data-autocomplete-path, "form-test/autocomplete-2/value")]');
  156. $user = $this->drupalCreateUser(['access autocomplete test']);
  157. $this->drupalLogin($user);
  158. $this->drupalGet('form-test/autocomplete');
  159. // Make sure that the autocomplete library is added.
  160. $this->assertSession()->responseContains('core/misc/autocomplete.js');
  161. // Ensure that the user does have access to the autocompletion.
  162. $this->assertSession()->elementExists('xpath', '//input[@id="edit-autocomplete-1" and contains(@data-autocomplete-path, "form-test/autocomplete-1")]');
  163. $this->assertSession()->elementExists('xpath', '//input[@id="edit-autocomplete-2" and contains(@data-autocomplete-path, "form-test/autocomplete-2/value")]');
  164. }
  165. /**
  166. * Tests form element error messages.
  167. */
  168. public function testFormElementErrors() {
  169. $this->drupalGet('form_test/details-form');
  170. $this->submitForm([], 'Submit');
  171. $this->assertSession()->pageTextContains('I am an error on the details element.');
  172. }
  173. /**
  174. * Tests summary attributes of details.
  175. */
  176. public function testDetailsSummaryAttributes() {
  177. $this->drupalGet('form-test/group-details');
  178. $this->assertSession()->elementExists('css', 'summary[data-summary-attribute="test"]');
  179. }
  180. }