3 namespace Drupal\Tests\system\Functional\Form;
5 use Drupal\Component\Render\FormattableMarkup;
6 use Drupal\Component\Serialization\Json;
7 use Drupal\Tests\BrowserTestBase;
10 * Tests the vertical_tabs form element for expected behavior.
14 class ElementsVerticalTabsTest extends BrowserTestBase {
21 public static $modules = ['form_test'];
24 * A user with permission to access vertical_tab_test_tabs.
26 * @var \Drupal\user\UserInterface
33 * @var \Drupal\user\UserInterface
37 protected function setUp() {
40 $this->adminUser = $this->drupalCreateUser(['access vertical_tab_test tabs']);
41 $this->webUser = $this->drupalCreateUser();
42 $this->drupalLogin($this->adminUser);
46 * Ensures that vertical-tabs.js is included before collapse.js.
48 * Otherwise, collapse.js adds "SHOW" or "HIDE" labels to the tabs.
50 public function testJavaScriptOrdering() {
51 $this->drupalGet('form_test/vertical-tabs');
52 $content = $this->getSession()->getPage()->getContent();
53 $position1 = strpos($content, 'core/misc/vertical-tabs.js');
54 $position2 = strpos($content, 'core/misc/collapse.js');
55 $this->assertTrue($position1 !== FALSE && $position2 !== FALSE && $position1 < $position2, 'vertical-tabs.js is included before collapse.js');
59 * Ensures that vertical tab markup is not shown if user has no tab access.
61 public function testWrapperNotShownWhenEmpty() {
62 // Test admin user can see vertical tabs and wrapper.
63 $this->drupalGet('form_test/vertical-tabs');
64 $wrapper = $this->xpath("//div[@data-vertical-tabs-panes]");
65 $this->assertTrue(isset($wrapper[0]), 'Vertical tab panes found.');
67 // Test wrapper markup not present for non-privileged web user.
68 $this->drupalLogin($this->webUser);
69 $this->drupalGet('form_test/vertical-tabs');
70 $wrapper = $this->xpath("//div[@data-vertical-tabs-panes]");
71 $this->assertFalse(isset($wrapper[0]), 'Vertical tab wrappers are not displayed to unprivileged users.');
75 * Ensures that default vertical tab is correctly selected.
77 public function testDefaultTab() {
78 $this->drupalGet('form_test/vertical-tabs');
80 $value = $this->assertSession()
81 ->elementExists('css', 'input[name="vertical_tabs__active_tab"]')
84 $this->assertSame('edit-tab3', $value, t('The default vertical tab is correctly selected.'));
88 * Ensures that vertical tab form values are cleaned.
90 public function testDefaultTabCleaned() {
91 $values = Json::decode($this->drupalPostForm('form_test/form-state-values-clean', [], t('Submit')));
92 $this->assertFalse(isset($values['vertical_tabs__active_tab']), new FormattableMarkup('%element was removed.', ['%element' => 'vertical_tabs__active_tab']));