From 768b329cf92b5a1c396aa0b0c8907f18506c743c Mon Sep 17 00:00:00 2001
From: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Date: Wed, 3 Aug 2022 11:20:20 +0000
Subject: [PATCH] Bug 31288: (QA follow-up) Check userenv in disown_or_delete
Content-Type: text/plain; charset=utf-8

Test plan:
Run t/db_dependent/Koha/Virtualshelves.t
Set ListOwnershipUponPatronDeletion to change and leave pref
ListOwnerDesignated empty.
Run t/db_dependent/Koha/AdditionalContents.t
* Without this patch, the test would fail in this context.

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
---
 Koha/Virtualshelves.pm | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/Koha/Virtualshelves.pm b/Koha/Virtualshelves.pm
index d4605883a3..2b82dfc732 100644
--- a/Koha/Virtualshelves.pm
+++ b/Koha/Virtualshelves.pm
@@ -55,12 +55,12 @@ sub disown_or_delete {
                   if C4::Context->preference('ListOwnerDesignated')
                   and Koha::Patrons->find( C4::Context->preference('ListOwnerDesignated') );
 
-                unless ($new_owner) {
+                if( !$new_owner && C4::Context->userenv ) {
                     $new_owner = C4::Context->userenv->{number};
                 }
 
                 while ( my $list = $self->next ) {
-                    if ( $list->is_public or $list->is_shared ) {
+                    if ( $new_owner && ( $list->is_public or $list->is_shared ) ) {
                         $list->transfer_ownership($new_owner);
                     } else {
                         $list->delete;
-- 
2.20.1