From ca0ceacd27b10c277489c46d2438c59a1ee194c4 Mon Sep 17 00:00:00 2001
From: Tim Hunt <T.J.Hunt@open.ac.uk>
Date: Mon, 9 Mar 2015 13:42:13 +0000
Subject: [PATCH] MDL-49446 behat: should_not_be_visible steps were not working

I fixed all similar problems in this class, even ones where it did
actually work becuase the two exceptions were of different types.
This pattern seems to have been copied and pasted a lot in this class,
and I am sure that will happen again, so I did not want to leave any
dangerous potential examples around.
---
 lib/tests/behat/behat_general.php | 37 +++++++++++++++++--------------
 1 file changed, 20 insertions(+), 17 deletions(-)

diff --git a/lib/tests/behat/behat_general.php b/lib/tests/behat/behat_general.php
index 2cdd394145a..b12eb48982b 100644
--- a/lib/tests/behat/behat_general.php
+++ b/lib/tests/behat/behat_general.php
@@ -448,10 +448,11 @@ class behat_general extends behat_base {
 
         try {
             $this->should_be_visible($element, $selectortype);
-            throw new ExpectationException('"' . $element . '" "' . $selectortype . '" is visible', $this->getSession());
         } catch (ExpectationException $e) {
             // All as expected.
+            return;
         }
+        throw new ExpectationException('"' . $element . '" "' . $selectortype . '" is visible', $this->getSession());
     }
 
     /**
@@ -501,13 +502,14 @@ class behat_general extends behat_base {
 
         try {
             $this->in_the_should_be_visible($element, $selectortype, $nodeelement, $nodeselectortype);
-            throw new ExpectationException(
-                '"' . $element . '" "' . $selectortype . '" in the "' . $nodeelement . '" "' . $nodeselectortype . '" is visible',
-                $this->getSession()
-            );
         } catch (ExpectationException $e) {
             // All as expected.
+            return;
         }
+        throw new ExpectationException(
+            '"' . $element . '" "' . $selectortype . '" in the "' . $nodeelement . '" "' . $nodeselectortype . '" is visible',
+            $this->getSession()
+        );
     }
 
     /**
@@ -898,12 +900,13 @@ class behat_general extends behat_base {
                 $exception,
                 false
             );
-
-            throw new ExpectationException('The "' . $element . '" "' . $selectortype . '" exists in the current page', $this->getSession());
         } catch (ElementNotFoundException $e) {
             // It passes.
             return;
         }
+
+        throw new ExpectationException('The "' . $element . '" "' . $selectortype .
+                '" exists in the current page', $this->getSession());
     }
 
     /**
@@ -968,12 +971,12 @@ class behat_general extends behat_base {
             // but we would need to duplicate the whole find_all() logic to do it, the benefit of
             // changing to 1 second sleep is not significant.
             $this->find($selector, $locator, false, $containernode, self::REDUCED_TIMEOUT);
-            throw new ExpectationException('The "' . $element . '" "' . $selectortype . '" exists in the "' .
-                $containerelement . '" "' . $containerselectortype . '"', $this->getSession());
         } catch (ElementNotFoundException $e) {
             // It passes.
             return;
         }
+        throw new ExpectationException('The "' . $element . '" "' . $selectortype . '" exists in the "' .
+                $containerelement . '" "' . $containerselectortype . '"', $this->getSession());
     }
 
     /**
@@ -1106,15 +1109,15 @@ class behat_general extends behat_base {
     public function row_column_of_table_should_not_contain($row, $column, $table, $value) {
         try {
             $this->row_column_of_table_should_contain($row, $column, $table, $value);
-            // Throw exception if found.
-            throw new ExpectationException(
-                '"' . $column . '" with value "' . $value . '" is present in "' . $row . '"  row for table "' . $table . '"',
-                $this->getSession()
-            );
         } catch (ElementNotFoundException $e) {
             // Table row/column doesn't contain this value. Nothing to do.
             return;
         }
+        // Throw exception if found.
+        throw new ExpectationException(
+            '"' . $column . '" with value "' . $value . '" is present in "' . $row . '"  row for table "' . $table . '"',
+            $this->getSession()
+        );
     }
 
     /**
@@ -1167,13 +1170,13 @@ class behat_general extends behat_base {
                 try {
                     $this->row_column_of_table_should_contain($row, $column, $table, $value);
                     // Throw exception if found.
-                    throw new ExpectationException('"' . $column . '" with value "' . $value . '" is present in "' .
-                        $row . '"  row for table "' . $table . '"', $this->getSession()
-                    );
                 } catch (ElementNotFoundException $e) {
                     // Table row/column doesn't contain this value. Nothing to do.
                     continue;
                 }
+                throw new ExpectationException('"' . $column . '" with value "' . $value . '" is present in "' .
+                    $row . '"  row for table "' . $table . '"', $this->getSession()
+                );
             }
         }
     }