From d2f5b62f070906658e0592602dd7cce84a5260a7 Mon Sep 17 00:00:00 2001 From: xjm <xjm@65776.no-reply.drupal.org> Date: Tue, 12 Sep 2017 16:33:00 -0500 Subject: [PATCH] Issue #2907485 by Lendude, jonathan1055: Add getAllOptions() to AssertLegacyTrait --- .../form_test/src/Form/FormTestSelectForm.php | 8 ++++++++ .../FunctionalTests/AssertLegacyTrait.php | 18 ++++++++++++++++++ .../FunctionalTests/BrowserTestBaseTest.php | 4 ++++ 3 files changed, 30 insertions(+) diff --git a/core/modules/system/tests/modules/form_test/src/Form/FormTestSelectForm.php b/core/modules/system/tests/modules/form_test/src/Form/FormTestSelectForm.php index 20242ec2ebd9..a922d2a86f88 100644 --- a/core/modules/system/tests/modules/form_test/src/Form/FormTestSelectForm.php +++ b/core/modules/system/tests/modules/form_test/src/Form/FormTestSelectForm.php @@ -115,6 +115,14 @@ public function buildForm(array $form, FormStateInterface $form_state) { '#multiple' => TRUE, ]; + $form['opt_groups'] = [ + '#type' => 'select', + '#options' => [ + 'optgroup_one' => ['one' => 'one', 'two' => 'two', 'three' => 'three', 'four' => '<strong>four</strong>'], + 'optgroup_two' => ['five' => 'five', 'six' => 'six'], + ], + ]; + $form['submit'] = ['#type' => 'submit', '#value' => 'Submit']; return $form; } diff --git a/core/tests/Drupal/FunctionalTests/AssertLegacyTrait.php b/core/tests/Drupal/FunctionalTests/AssertLegacyTrait.php index d11ef7686f36..be1538785fb7 100644 --- a/core/tests/Drupal/FunctionalTests/AssertLegacyTrait.php +++ b/core/tests/Drupal/FunctionalTests/AssertLegacyTrait.php @@ -2,6 +2,7 @@ namespace Drupal\FunctionalTests; +use Behat\Mink\Element\NodeElement; use Behat\Mink\Exception\ExpectationException; use Behat\Mink\Selector\Xpath\Escaper; use Drupal\Component\Render\FormattableMarkup; @@ -812,4 +813,21 @@ protected function getRawContent() { return $this->getSession()->getPage()->getContent(); } + /** + * Get all option elements, including nested options, in a select. + * + * @param \Behat\Mink\Element\NodeElement $element + * The element for which to get the options. + * + * @return \Behat\Mink\Element\NodeElement[] + * Option elements in select. + * + * @deprecated Scheduled for removal in Drupal 9.0.0. + * Use $element->findAll('xpath', 'option') instead. + */ + protected function getAllOptions(NodeElement $element) { + @trigger_error('AssertLegacyTrait::getAllOptions() is scheduled for removal in Drupal 9.0.0. Use $element->findAll(\'xpath\', \'option\') instead.', E_USER_DEPRECATED); + return $element->findAll('xpath', '//option'); + } + } diff --git a/core/tests/Drupal/FunctionalTests/BrowserTestBaseTest.php b/core/tests/Drupal/FunctionalTests/BrowserTestBaseTest.php index 20bf689c1930..882e6edcd7cc 100644 --- a/core/tests/Drupal/FunctionalTests/BrowserTestBaseTest.php +++ b/core/tests/Drupal/FunctionalTests/BrowserTestBaseTest.php @@ -434,6 +434,10 @@ public function testLegacyFieldAssertsForOptions() { catch (\PHPUnit_Framework_ExpectationFailedException $e) { $this->pass($e->getMessage()); } + + // Test \Drupal\FunctionalTests\AssertLegacyTrait::getAllOptions. + $this->drupalGet('/form-test/select'); + $this->assertCount(6, $this->getAllOptions($this->cssSelect('select[name="opt_groups"]')[0])); } /** -- GitLab