From 31bbe3b7da71926aa5af310184b0dd2c635af555 Mon Sep 17 00:00:00 2001
From: Ere Maijala <ere.maijala@helsinki.fi>
Date: Mon, 21 Sep 2020 21:44:39 +0300
Subject: [PATCH] [VUFIND-1422] Fix a couple of prefix handling issues in the
 MultiBackend driver. (#1711)

---
 .../src/VuFind/ILS/Driver/MultiBackend.php    |  9 ++++--
 .../ILS/Driver/MultiBackendTest.php           | 31 +++++++++++++++++--
 2 files changed, 35 insertions(+), 5 deletions(-)

diff --git a/module/VuFind/src/VuFind/ILS/Driver/MultiBackend.php b/module/VuFind/src/VuFind/ILS/Driver/MultiBackend.php
index 9e028cdd0cd..9dd7e45b312 100644
--- a/module/VuFind/src/VuFind/ILS/Driver/MultiBackend.php
+++ b/module/VuFind/src/VuFind/ILS/Driver/MultiBackend.php
@@ -936,7 +936,9 @@ class MultiBackend extends AbstractBase implements \Laminas\Log\LoggerAwareInter
         );
         $driver = $this->getDriver($source);
         if ($driver) {
-            $holdDetails = $this->stripIdPrefixes($holdDetails, $source);
+            $holdDetails = $this->stripIdPrefixes(
+                $holdDetails, $source, ['id', 'item_id', 'cat_username']
+            );
             return $driver->getCancelHoldDetails($holdDetails);
         }
         throw new ILSException('No suitable backend driver found');
@@ -1552,7 +1554,7 @@ class MultiBackend extends AbstractBase implements \Laminas\Log\LoggerAwareInter
                     $value, $source, $modifyFields
                 );
             } else {
-                if (!is_numeric($key)
+                if (!ctype_digit((string)$key)
                     && $value !== ''
                     && in_array($key, $modifyFields)
                 ) {
@@ -1593,7 +1595,8 @@ class MultiBackend extends AbstractBase implements \Laminas\Log\LoggerAwareInter
                 );
             } else {
                 $prefixLen = strlen($source) + 1;
-                if ((!is_array($data) || in_array($key, $modifyFields))
+                if ((!is_array($data)
+                    || (!ctype_digit((string)$key) && in_array($key, $modifyFields)))
                     && strncmp("$source.", $value, $prefixLen) == 0
                 ) {
                     $array[$key] = substr($value, $prefixLen);
diff --git a/module/VuFind/tests/unit-tests/src/VuFindTest/ILS/Driver/MultiBackendTest.php b/module/VuFind/tests/unit-tests/src/VuFindTest/ILS/Driver/MultiBackendTest.php
index 04c7d3117a3..3b2bebfaf79 100644
--- a/module/VuFind/tests/unit-tests/src/VuFindTest/ILS/Driver/MultiBackendTest.php
+++ b/module/VuFind/tests/unit-tests/src/VuFindTest/ILS/Driver/MultiBackendTest.php
@@ -5,7 +5,7 @@
  * PHP version 7
  *
  * Copyright (C) Villanova University 2011.
- * Copyright (C) The National Library of Finland 2014-2016.
+ * Copyright (C) The National Library of Finland 2014-2020.
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License version 2,
@@ -29,7 +29,6 @@
  */
 namespace VuFindTest\ILS\Driver;
 
-use Laminas\Log\Writer\Mock;
 use VuFind\ILS\Driver\MultiBackend;
 
 /**
@@ -281,6 +280,20 @@ class MultiBackendTest extends \VuFindTest\Unit\TestCase
             $driver, 'addIdPrefixes', [$data, $source, $modify]
         );
         $this->assertEquals($expected, $result);
+
+        // Numeric keys are not considered
+        $data = [
+            'id' => 'record1',
+            'cat_username' => ['foo', 'bar']
+        ];
+        $expected = [
+            'id' => "$source.record1",
+            'cat_username' => ['foo', 'bar']
+        ];
+        $result = $this->callMethod(
+            $driver, 'addIdPrefixes', [$data, $source, $modify]
+        );
+        $this->assertEquals($expected, $result);
     }
 
     /**
@@ -346,6 +359,20 @@ class MultiBackendTest extends \VuFindTest\Unit\TestCase
             $driver, 'stripIdPrefixes', [$data, $source, $modify]
         );
         $this->assertEquals($expected, $result);
+
+        // Numeric keys are not considered
+        $data = [
+            'id' => "$source.record1",
+            'test' => ["$source.foo", "$source.bar"]
+        ];
+        $expected = [
+            'id' => "record1",
+            'test' => ["$source.foo", "$source.bar"]
+        ];
+        $result = $this->callMethod(
+            $driver, 'stripIdPrefixes', [$data, $source]
+        );
+        $this->assertEquals($expected, $result);
     }
 
     /**
-- 
GitLab