From e3b019f4809f5940446e4eb1a113f9dc0531884e Mon Sep 17 00:00:00 2001
From: Angie Byron <webchick@24967.no-reply.drupal.org>
Date: Wed, 17 Sep 2008 00:46:44 +0000
Subject: [PATCH] #299186 by boombatower: Fix assertFieldByXPath so that it
 recognizes select and textarea values.

---
 modules/simpletest/drupal_web_test_case.php | 42 ++++++++++++++++++++-
 1 file changed, 41 insertions(+), 1 deletion(-)

diff --git a/modules/simpletest/drupal_web_test_case.php b/modules/simpletest/drupal_web_test_case.php
index 20d7081749ef..08927d7a730f 100644
--- a/modules/simpletest/drupal_web_test_case.php
+++ b/modules/simpletest/drupal_web_test_case.php
@@ -1445,7 +1445,25 @@ function assertFieldByXPath($xpath, $value, $message, $group = 'Other') {
       $found = FALSE;
       if ($fields) {
         foreach ($fields as $field) {
-          if ($field['value'] == $value) {
+          if (isset($field['value']) && $field['value'] == $value) {
+            // Input element with correct value.
+            $found = TRUE;
+          }
+          else if (isset($field->option)) {
+            // Select element found.
+            if ($this->getSelectedItem($field) == $value) {
+              $found = TRUE;
+            }
+            else {
+              // No item selected so use first item.
+              $items = $this->getAllOptions($field);
+              if (!empty($items) && $items[0]['value'] == $value) {
+                $found = TRUE;
+              }
+            }
+          }
+          else if (isset($field[0]) && $field[0] == $value) {
+            // Text area with correct text.
             $found = TRUE;
           }
         }
@@ -1454,6 +1472,28 @@ function assertFieldByXPath($xpath, $value, $message, $group = 'Other') {
     return $this->assertTrue($fields && $found, $message, $group);
   }
 
+  /**
+   * Get the selected value from a select field.
+   *
+   * @param $element
+   *   SimpleXMLElement select element.
+   * @return
+   *   The selected value or FALSE.
+   */
+  function getSelectedItem(SimpleXMLElement $element) {
+    foreach ($element->children() as $item) {
+      if (isset($item['selected'])) {
+        return $item['value'];
+      }
+      else if ($item->getName() == 'optgroup') {
+        if ($value = $this->getSelectedItem($item)) {
+          return $value;
+        }
+      }
+    }
+    return FALSE;
+  }
+
   /**
    * Assert that a field does not exist in the current page by the given XPath.
    *
-- 
GitLab