FormGroupingElementsTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. namespace Drupal\FunctionalJavascriptTests\Core\Form;
  3. use Drupal\FunctionalJavascriptTests\JavascriptTestBase;
  4. /**
  5. * Tests for form grouping elements.
  6. *
  7. * @group form
  8. */
  9. class FormGroupingElementsTest extends JavascriptTestBase {
  10. /**
  11. * Required modules.
  12. *
  13. * @var array
  14. */
  15. public static $modules = ['form_test'];
  16. /**
  17. * {@inheritdoc}
  18. */
  19. protected function setUp() {
  20. parent::setUp();
  21. $account = $this->drupalCreateUser();
  22. $this->drupalLogin($account);
  23. }
  24. /**
  25. * Tests that vertical tab children become visible.
  26. *
  27. * Makes sure that a child element of a vertical tab that is not visible,
  28. * becomes visible when the tab is clicked, a fragment link to the child is
  29. * clicked or when the URI fragment pointing to that child changes.
  30. */
  31. public function testVerticalTabChildVisibility() {
  32. $session = $this->getSession();
  33. $web_assert = $this->assertSession();
  34. // Request the group vertical tabs testing page with a fragment identifier
  35. // to the second element.
  36. $this->drupalGet('form-test/group-vertical-tabs', ['fragment' => 'edit-element-2']);
  37. $page = $session->getPage();
  38. $tab_link_1 = $page->find('css', '.vertical-tabs__menu-item > a');
  39. $child_1_selector = '#edit-element';
  40. $child_1 = $page->find('css', $child_1_selector);
  41. $child_2_selector = '#edit-element-2';
  42. $child_2 = $page->find('css', $child_2_selector);
  43. // Assert that the child in the second vertical tab becomes visible.
  44. // It should be visible after initial load due to the fragment in the URI.
  45. $this->assertTrue($child_2->isVisible(), 'Child 2 is visible due to a URI fragment');
  46. // Click on a fragment link pointing to an invisible child inside an
  47. // inactive vertical tab.
  48. $session->executeScript("jQuery('<a href=\"$child_1_selector\"></a>').insertAfter('h1')[0].click()");
  49. // Assert that the child in the first vertical tab becomes visible.
  50. $web_assert->waitForElementVisible('css', $child_1_selector, 50);
  51. // Trigger a URI fragment change (hashchange) to show the second vertical
  52. // tab again.
  53. $session->executeScript("location.replace('$child_2_selector')");
  54. // Assert that the child in the second vertical tab becomes visible again.
  55. $web_assert->waitForElementVisible('css', $child_2_selector, 50);
  56. $tab_link_1->click();
  57. // Assert that the child in the first vertical tab is visible again after
  58. // a click on the first tab.
  59. $this->assertTrue($child_1->isVisible(), 'Child 1 is visible after clicking the parent tab');
  60. }
  61. /**
  62. * Tests that details element children become visible.
  63. *
  64. * Makes sure that a child element of a details element that is not visible,
  65. * becomes visible when a fragment link to the child is clicked or when the
  66. * URI fragment pointing to that child changes.
  67. */
  68. public function testDetailsChildVisibility() {
  69. $session = $this->getSession();
  70. $web_assert = $this->assertSession();
  71. // Store reusable JavaScript code to remove the current URI fragment and
  72. // close all details.
  73. $reset_js = "location.replace('#'); jQuery('details').removeAttr('open')";
  74. // Request the group details testing page.
  75. $this->drupalGet('form-test/group-details');
  76. $page = $session->getPage();
  77. $session->executeScript($reset_js);
  78. $child_selector = '#edit-element';
  79. $child = $page->find('css', $child_selector);
  80. // Assert that the child is not visible.
  81. $this->assertFalse($child->isVisible(), 'Child is not visible');
  82. // Trigger a URI fragment change (hashchange) to open all parent details
  83. // elements of the child.
  84. $session->executeScript("location.replace('$child_selector')");
  85. // Assert that the child becomes visible again after a hash change.
  86. $web_assert->waitForElementVisible('css', $child_selector, 50);
  87. $session->executeScript($reset_js);
  88. // Click on a fragment link pointing to an invisible child inside a closed
  89. // details element.
  90. $session->executeScript("jQuery('<a href=\"$child_selector\"></a>').insertAfter('h1')[0].click()");
  91. // Assert that the child is visible again after a fragment link click.
  92. $web_assert->waitForElementVisible('css', $child_selector, 50);
  93. // Find the summary belonging to the closest details element.
  94. $summary = $page->find('css', '#edit-meta > summary');
  95. // Assert that both aria-expanded and aria-pressed are true.
  96. $this->assertTrue($summary->getAttribute('aria-expanded'));
  97. $this->assertTrue($summary->getAttribute('aria-pressed'));
  98. }
  99. }