From cc761250b66029c6dd00c8406dd5bca3645c2905 Mon Sep 17 00:00:00 2001
From: Robert Lange <robert.lange@uni-leipzig.de>
Date: Mon, 6 Jan 2020 15:30:29 +0100
Subject: [PATCH] refs #16473 [master] fix duplicate resolver links

* prevent duplicate array entries when calling 'isActive' of OpenUrl-helper twice
* initialise record resolvers as empty array
* add test for current resolvers, context and active
---
 .../src/finc/View/Helper/Root/OpenUrl.php     | 27 +++++++++++
 .../fincTest/View/Helper/Root/OpenUrlTest.php | 46 +++++++++++++++++++
 2 files changed, 73 insertions(+)

diff --git a/module/finc/src/finc/View/Helper/Root/OpenUrl.php b/module/finc/src/finc/View/Helper/Root/OpenUrl.php
index eac03d439f4..d5edf896fbd 100644
--- a/module/finc/src/finc/View/Helper/Root/OpenUrl.php
+++ b/module/finc/src/finc/View/Helper/Root/OpenUrl.php
@@ -355,6 +355,8 @@ class OpenUrl extends \Zend\View\Helper\AbstractHelper
      */
     protected function checkIfRulesApply()
     {
+        $this->recordResolvers = [];
+
         if ($this->activeResolvers) {
             foreach ($this->activeResolvers as $resolver) {
                 if (isset($this->openUrlRules[$resolver])) {
@@ -565,4 +567,29 @@ class OpenUrl extends \Zend\View\Helper\AbstractHelper
         // no sufficient param found
         return false;
     }
+
+
+    /**
+     * @return string
+     */
+    public function getArea()
+    {
+        return $this->area;
+    }
+
+    /**
+     * @return mixed
+     */
+    public function getActiveResolvers()
+    {
+        return $this->activeResolvers;
+    }
+
+    /**
+     * @return mixed
+     */
+    public function getRecordResolvers()
+    {
+        return $this->recordResolvers;
+    }
 }
diff --git a/module/finc/tests/unit-tests/src/fincTest/View/Helper/Root/OpenUrlTest.php b/module/finc/tests/unit-tests/src/fincTest/View/Helper/Root/OpenUrlTest.php
index 6866f6c4b78..3f9e9f69604 100644
--- a/module/finc/tests/unit-tests/src/fincTest/View/Helper/Root/OpenUrlTest.php
+++ b/module/finc/tests/unit-tests/src/fincTest/View/Helper/Root/OpenUrlTest.php
@@ -147,6 +147,52 @@ class OpenUrlTest extends \VuFindTest\View\Helper\Root\OpenUrlTest
         $this->assertTrue($instanceWithIssnAndZdbId->isActive());
     }
 
+    public function testIsActiveIsIdempotent()
+    {
+        $this->rulesConfig['Ezb'] = [0 => ['include' => ["methods" => "getSourceID"]]];
+        $instance = $this->getInstance(false, true, false);
+
+        $instance->isActive();
+        $activeResolversFirst = $instance->getActiveResolvers();
+        $resolverCountFirst = $instance->getRecordResolvers();
+        $areaFirst = $instance->getArea();
+
+        $instance->isActive();
+        $activeResolversSecond = $instance->getActiveResolvers();
+        $resolverCountSecond = $instance->getRecordResolvers();
+        $areaSecond = $instance->getArea();
+
+        $this->assertEquals(
+            $activeResolversFirst,
+            $activeResolversSecond,
+            sprintf(
+                "Active resolvers must not change on recall, first %s then %s.",
+                implode(", ", $activeResolversFirst),
+                implode(", ", $activeResolversSecond)
+            )
+        );
+
+        $this->assertEquals(
+            $resolverCountFirst,
+            $resolverCountSecond,
+            sprintf(
+                "Resolvers fetched by current rules must not change on recall, first %s then %s.",
+                implode(", ", $resolverCountFirst),
+                implode(", ", $resolverCountSecond)
+            )
+        );
+
+        $this->assertEquals(
+            $areaFirst,
+            $areaSecond,
+            sprintf(
+                "Context for same record must not change on recall, first %s then %s.",
+                $areaFirst,
+                $areaSecond
+            )
+        );
+    }
+
     /*
         Helper Functions
     */
-- 
GitLab