From 9feee4ed4b58d8d9b804397be1cc263d9ed83af5 Mon Sep 17 00:00:00 2001
From: Demian Katz <demian.katz@villanova.edu>
Date: Thu, 17 Oct 2019 09:36:01 -0400
Subject: [PATCH] Prevent print dialogs from popping up during testing. (#1439)

- This prevents some versions of Chrome from getting stuck during automated tests.
---
 build.xml                                 |  3 ++-
 module/VuFind/src/VuFind/Bootstrapper.php | 19 +++++++++++++++++++
 themes/bootstrap3/js/common.js            |  9 ++++++++-
 3 files changed, 29 insertions(+), 2 deletions(-)

diff --git a/build.xml b/build.xml
index 0dc33074cf2..de28a49d5ab 100644
--- a/build.xml
+++ b/build.xml
@@ -306,12 +306,13 @@
       </else>
     </if>
 
-    <!-- Update config.ini to activate DB connection and exception logging -->
+    <!-- Update config.ini to activate DB connection, exception logging and test mode -->
     <copy file="${srcdir}/config/vufind/config.ini" tofile="${srcdir}/local/config/vufind/config.ini">
       <filterchain>
         <replaceregexp>
           <regexp pattern="mysql://root@localhost/vufind" replace="${db_connection_string}" />
           <regexp pattern=";file\s+= /var/log/vufind.log:alert,error,notice,debug" replace="file = ${srcdir}/vufind-exception.log:alert-5,error-5" />
+          <regexp pattern="(\[System\]\s+)" replace="\1runningTestSuite=1" />
         </replaceregexp>
       </filterchain>
     </copy>
diff --git a/module/VuFind/src/VuFind/Bootstrapper.php b/module/VuFind/src/VuFind/Bootstrapper.php
index 440d9ae610f..ff13b3e683e 100644
--- a/module/VuFind/src/VuFind/Bootstrapper.php
+++ b/module/VuFind/src/VuFind/Bootstrapper.php
@@ -103,6 +103,25 @@ class Bootstrapper
         $this->config = $sm->get(\VuFind\Config\PluginManager::class)->get('config');
     }
 
+    /**
+     * Set up cookie to flag test mode.
+     *
+     * @return void
+     */
+    protected function initTestMode()
+    {
+        // If we're in test mode (as determined by the config.ini property installed
+        // by the build.xml startup process), set a cookie so the front-end code can
+        // act accordingly. (This is needed to work around a problem where opening
+        // print dialogs during testing stalls the automated test process).
+        if ($this->config->System->runningTestSuite ?? false) {
+            $app = $this->event->getApplication();
+            $sm = $app->getServiceManager();
+            $cm = $sm->get(\VuFind\Cookie\CookieManager::class);
+            $cm->set('VuFindTestSuiteRunning', '1', 0, false);
+        }
+    }
+
     /**
      * Initialize dynamic debug mode (debug initiated by a ?debug=true parameter).
      *
diff --git a/themes/bootstrap3/js/common.js b/themes/bootstrap3/js/common.js
index 7af5a6f659c..40e10735741 100644
--- a/themes/bootstrap3/js/common.js
+++ b/themes/bootstrap3/js/common.js
@@ -428,7 +428,14 @@ $(document).ready(function commonDocReady() {
   if (url.indexOf('?' + 'print' + '=') !== -1 || url.indexOf('&' + 'print' + '=') !== -1) {
     $("link[media='print']").attr("media", "all");
     $(document).ajaxStop(function triggerPrint() {
-      window.print();
+      // Print dialogs cause problems during testing, so disable them
+      // when the "test mode" cookie is set. This should never happen
+      // under normal usage outside of the Phing startup process.
+      if (document.cookie.indexOf('VuFindTestSuiteRunning=') === -1) {
+        window.print();
+      } else {
+        console.log("Printing disabled due to test mode.");
+      }
     });
     // Make an ajax call to ensure that ajaxStop is triggered
     $.getJSON(VuFind.path + '/AJAX/JSON', {method: 'keepAlive'});
-- 
GitLab