From 8bfa1ea66d0296be649bdce75047a4734a869aee Mon Sep 17 00:00:00 2001
From: Demian Katz <demian.katz@villanova.edu>
Date: Tue, 20 Oct 2015 10:37:50 -0400
Subject: [PATCH] Refactored form filling functionality.

---
 .../src/VuFindTest/Unit/UserCreationTrait.php | 31 ++++++++++++++++++-
 .../src/VuFindTest/Mink/FavoritesTest.php     | 22 +++----------
 2 files changed, 35 insertions(+), 18 deletions(-)

diff --git a/module/VuFind/src/VuFindTest/Unit/UserCreationTrait.php b/module/VuFind/src/VuFindTest/Unit/UserCreationTrait.php
index 10c30b2144c..7a4e82ac3c5 100644
--- a/module/VuFind/src/VuFindTest/Unit/UserCreationTrait.php
+++ b/module/VuFind/src/VuFindTest/Unit/UserCreationTrait.php
@@ -28,6 +28,7 @@
  * @link     http://vufind.org/wiki/vufind2:unit_tests Wiki
  */
 namespace VuFindTest\Unit;
+use Behat\Mink\Element\Element;
 
 /**
  * Trait with utility methods for user creation/management. Assumes that it
@@ -64,6 +65,34 @@ trait UserCreationTrait
         }
     }
 
+    /**
+     * Mink support function: fill in the account creation form.
+     *
+     * @param Element $page      Page element.
+     * @param array   $overrides Optional overrides for form values.
+     *
+     * @return void
+     */
+    protected function fillInAccountForm(Element $page, $overrides = [])
+    {
+        $defaults = [
+            'firstname' => 'Tester',
+            'lastname' => 'McTestenson',
+            'email' => 'username1@ignore.com',
+            'username' => 'username1',
+            'password' => 'test',
+            'password2' => 'test'
+        ];
+
+        foreach ($defaults as $field => $default) {
+            $element = $page->findById('account_' . $field);
+            $this->assertNotNull($element);
+            $element->setValue(
+                isset($overrides[$field]) ? $overrides[$field] : $default
+            );
+        }
+    }
+
     /**
      * Static teardown support function to destroy user accounts. Accounts are
      * expected to exist, and the method will fail if they are missing.
@@ -87,7 +116,7 @@ trait UserCreationTrait
         foreach ((array)$users as $username) {
             $user = $userTable->getByUsername($username, false);
             if (empty($user)) {
-                throw new \Exception('Problem deleting expected user.');
+                throw new \Exception("Problem deleting expected user ($user).");
             }
             $user->delete();
         }
diff --git a/module/VuFind/tests/integration-tests/src/VuFindTest/Mink/FavoritesTest.php b/module/VuFind/tests/integration-tests/src/VuFindTest/Mink/FavoritesTest.php
index 0e35be7a0c1..2409799b6cc 100644
--- a/module/VuFind/tests/integration-tests/src/VuFindTest/Mink/FavoritesTest.php
+++ b/module/VuFind/tests/integration-tests/src/VuFindTest/Mink/FavoritesTest.php
@@ -97,19 +97,13 @@ class FavoritesTest extends \VuFindTest\Unit\MinkTestCase
             $page->find('css', '.modal-body .btn.btn-primary.disabled')
         );
         $page->find('css', '.modal-body .btn.btn-primary')->click();
-        $this->assertNotNull($page->findById('account_firstname'));
+
         // Invalid email
-        $page->findById('account_firstname')->setValue('Tester');
-        $page->findById('account_lastname')->setValue('McTestenson');
-        $page->findById('account_email')->setValue('blargasaurus');
-        $page->findById('account_username')->setValue('username1');
-        $page->findById('account_password')->setValue('test');
-        $page->findById('account_password2')->setValue('test');
+        $this->fillInAccountForm($page, ['email' => 'blargasaurus']);
         $this->assertNull(
             $page->find('css', '.modal-body .btn.btn-primary.disabled')
         );
         $page->find('css', '.modal-body .btn.btn-primary')->click();
-        $this->assertNotNull($page->findById('account_firstname'));
         // Correct
         $page->findById('account_email')->setValue('username1@ignore.com');
         $page->find('css', '.modal-body .btn.btn-primary')->click();
@@ -230,19 +224,13 @@ class FavoritesTest extends \VuFindTest\Unit\MinkTestCase
             $page->find('css', '.modal-body .btn.btn-primary.disabled')
         );
         $page->find('css', '.modal-body .btn.btn-primary')->click();
-        $this->assertNotNull($page->findById('account_firstname'));
-        $page->findById('account_firstname')->setValue('Tester');
-        $page->findById('account_lastname')->setValue('McTestenson');
-        $page->findById('account_password')->setValue('test');
-        $page->findById('account_password2')->setValue('test');
-        $page->findById('account_username')->setValue('username2');
-        // Invalid email
-        $page->findById('account_email')->setValue('blargasaurus');
+        $this->fillInAccountForm(
+            $page, ['username' => 'username2', 'email' => 'blargasaurus']
+        );
         $this->assertNull(
             $page->find('css', '.modal-body .btn.btn-primary.disabled')
         );
         $page->find('css', '.modal-body .btn.btn-primary')->click();
-        $this->assertNotNull($page->findById('account_firstname'));
         $page->findById('account_email')->setValue('username2@ignore.com');
         // Test taken
         $page->findById('account_username')->setValue('username1');
-- 
GitLab