From 786d172c72923b6f26db9ff682a926ed573da06f Mon Sep 17 00:00:00 2001
From: Demian Katz <demian.katz@villanova.edu>
Date: Tue, 20 Oct 2015 13:57:01 -0400
Subject: [PATCH] More test refactoring.

---
 .../src/VuFindTest/Unit/UserCreationTrait.php | 54 ++++++++++++++++++-
 .../src/VuFindTest/Mink/FavoritesTest.php     | 42 ++++++---------
 2 files changed, 68 insertions(+), 28 deletions(-)

diff --git a/module/VuFind/src/VuFindTest/Unit/UserCreationTrait.php b/module/VuFind/src/VuFindTest/Unit/UserCreationTrait.php
index 7a4e82ac3c5..b3f7d14d067 100644
--- a/module/VuFind/src/VuFindTest/Unit/UserCreationTrait.php
+++ b/module/VuFind/src/VuFindTest/Unit/UserCreationTrait.php
@@ -65,6 +65,21 @@ trait UserCreationTrait
         }
     }
 
+    /**
+     * Mink support function: assert a warning message in the lightbox.
+     *
+     * @param Element $page    Page element
+     * @param string  $message Expected message
+     *
+     * @return void
+     */
+    protected function assertLightboxWarning(Element $page, $message)
+    {
+        $warning = $page->find('css', '.modal-body .alert-danger .message');
+        $this->assertTrue(is_object($warning));
+        $this->assertEquals($message, $warning->getText());
+    }
+
     /**
      * Mink support function: fill in the account creation form.
      *
@@ -93,6 +108,43 @@ trait UserCreationTrait
         }
     }
 
+    /**
+     * Mink support function: fill in the login form.
+     *
+     * @param Element $page     Page element.
+     * @param string  $username Username to set (null to skip)
+     * @param string  $password Password to set (null to skip)
+     *
+     * @return void
+     */
+    protected function fillInLoginForm(Element $page, $username, $password)
+    {
+        if (null !== $username) {
+            $usernameField = $page->find('css', '.modal-body [name="username"]');
+            $this->assertNotNull($usernameField);
+            $usernameField->setValue($username);
+        }
+        if (null !== $password) {
+            $passwordField = $page->find('css', '.modal-body [name="password"]');
+            $this->assertNotNull($passwordField);
+            $passwordField->setValue($password);
+        }
+    }
+
+    /**
+     * Submit the login form (assuming it's open).
+     *
+     * @param Element $page Page element.
+     *
+     * @return void
+     */
+    protected function submitLoginForm(Element $page)
+    {
+        $button = $page->find('css', '.modal-body .btn.btn-primary');
+        $this->assertNotNull($button);
+        $button->click();
+    }
+
     /**
      * Static teardown support function to destroy user accounts. Accounts are
      * expected to exist, and the method will fail if they are missing.
@@ -116,7 +168,7 @@ trait UserCreationTrait
         foreach ((array)$users as $username) {
             $user = $userTable->getByUsername($username, false);
             if (empty($user)) {
-                throw new \Exception("Problem deleting expected user ($user).");
+                throw new \Exception("Problem deleting expected user ($username).");
             }
             $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 2409799b6cc..028ec72fc12 100644
--- a/module/VuFind/tests/integration-tests/src/VuFindTest/Mink/FavoritesTest.php
+++ b/module/VuFind/tests/integration-tests/src/VuFindTest/Mink/FavoritesTest.php
@@ -142,22 +142,17 @@ class FavoritesTest extends \VuFindTest\Unit\MinkTestCase
         $page = $this->gotoRecord($session);
 
         $page->findById('save-record')->click();
-        $username = '.modal-body [name="username"]';
-        $password = '.modal-body [name="password"]';
-        $this->assertNotNull($page->find('css', $username));
-        $this->assertNotNull($page->find('css', $password));
         // Login
         // - empty
-        $page->find('css', '.modal-body .btn.btn-primary')->click();
-        $this->assertNotNull($page->find('css', $username));
+        $this->submitLoginForm($page);
+        $this->assertLightboxWarning($page, 'Login information cannot be blank.');
         // - wrong
-        $page->find('css', $username)->setValue('username1');
-        $page->find('css', $password)->setValue('superwrong');
-        $page->find('css', '.modal-body .btn.btn-primary')->click();
-        $this->assertNotNull($page->find('css', $username));
+        $this->fillInLoginForm($page, 'username1', 'superwrong');
+        $this->submitLoginForm($page);
+        $this->assertLightboxWarning($page, 'Invalid login -- please try again.');
         // - for real
-        $page->find('css', $password)->setValue('test');
-        $page->find('css', '.modal-body .btn.btn-primary')->click();
+        $this->fillInLoginForm($page, null, 'test');
+        $this->submitLoginForm($page);
         // Make sure we don't have Favorites because we have another populated list
         $this->assertNull($page->find('css', '.modal-body #save_list'));
         // Make Two Lists
@@ -194,9 +189,8 @@ class FavoritesTest extends \VuFindTest\Unit\MinkTestCase
         $page = $this->gotoRecord($session);
         // Login
         $page->find('css', '#loginOptions a')->click();
-        $page->find('css', '.modal-body [name="username"]')->setValue('username1');
-        $page->find('css', '.modal-body [name="password"]')->setValue('test');
-        $page->find('css', '.modal-body .btn.btn-primary')->click();
+        $this->fillInLoginForm($page, 'username1', 'test');
+        $this->submitLoginForm($page);
         $session->reload();
         // Save Record
         $page->findById('save-record')->click();
@@ -278,18 +272,13 @@ class FavoritesTest extends \VuFindTest\Unit\MinkTestCase
         $page = $this->gotoSearch($session);
 
         $page->find('css', '.save-record')->click();
-        $username = '.modal-body [name="username"]';
-        $password = '.modal-body [name="password"]';
-        $this->assertNotNull($page->find('css', $username));
-        $this->assertNotNull($page->find('css', $password));
         // Login
         // - empty
-        $page->find('css', '.modal-body .btn.btn-primary')->click();
-        $this->assertNotNull($page->find('css', $username));
+        $this->submitLoginForm($page);
+        $this->assertLightboxWarning($page, 'Login information cannot be blank.');
         // - for real
-        $page->find('css', $username)->setValue('username2');
-        $page->find('css', $password)->setValue('test');
-        $page->find('css', '.modal-body .btn.btn-primary')->click();
+        $this->fillInLoginForm($page, 'username2', 'test');
+        $this->submitLoginForm($page);
         // Make sure we don't have Favorites because we have another populated list
         $this->assertNull($page->find('css', '.modal-body #save_list'));
         // Make Two Lists
@@ -326,9 +315,8 @@ class FavoritesTest extends \VuFindTest\Unit\MinkTestCase
         $page = $this->gotoSearch($session);
         // Login
         $page->find('css', '#loginOptions a')->click();
-        $page->find('css', '.modal-body [name="username"]')->setValue('username2');
-        $page->find('css', '.modal-body [name="password"]')->setValue('test');
-        $page->find('css', '.modal-body .btn.btn-primary')->click();
+        $this->fillInLoginForm($page, 'username2', 'test');
+        $this->submitLoginForm($page);
         $session->reload();
         // Save Record
         $page->find('css', '.save-record')->click();
-- 
GitLab