From 81aafe0e51f806a3f133bfb711535eead1cc3550 Mon Sep 17 00:00:00 2001 From: Julian Maurice Date: Thu, 31 Aug 2023 13:39:33 +0200 Subject: [PATCH] Bug 32980: Make the return value of Koha::Patron::siblings consistent Without the patch, Koha::Patron::siblings can return: - undef (or an empty list in list context) if patron has no guarantors - a Koha::Patrons object if patron has at least one guarantor, which doesn't necessarily mean that they have siblings (they can be the only guarantee) For ease of use, this subroutine should always return a Koha::Patrons object, even if it represents an empty set. Signed-off-by: Owen Leonard --- Koha/Patron.pm | 6 +--- t/db_dependent/Koha/Patron/siblings.t | 42 +++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 5 deletions(-) create mode 100755 t/db_dependent/Koha/Patron/siblings.t diff --git a/Koha/Patron.pm b/Koha/Patron.pm index 6937ea904a..e0b031499c 100644 --- a/Koha/Patron.pm +++ b/Koha/Patron.pm @@ -584,7 +584,7 @@ sub housebound_role { =head3 siblings -Returns the siblings of this patron. +Returns the siblings of this patron as a C object =cut @@ -593,13 +593,9 @@ sub siblings { my @guarantors = $self->guarantor_relationships()->guarantors()->as_list; - return unless @guarantors; - my @siblings = map { $_->guarantee_relationships()->guarantees()->as_list } @guarantors; - return unless @siblings; - my %seen; @siblings = grep { !$seen{ $_->id }++ && ( $_->id != $self->id ) } @siblings; diff --git a/t/db_dependent/Koha/Patron/siblings.t b/t/db_dependent/Koha/Patron/siblings.t new file mode 100755 index 0000000000..1b7b7cc974 --- /dev/null +++ b/t/db_dependent/Koha/Patron/siblings.t @@ -0,0 +1,42 @@ +#!/usr/bin/perl + +use Modern::Perl; + +use String::Random; +use Test::More tests => 1; + +use Koha::Database; +use Koha::Patrons; + +my $schema = Koha::Database->schema; + +subtest 'Koha::Patrons::siblings should return a Koha::Patrons object even if patron has no guarantor' => sub { + plan tests => 2; + $schema->txn_begin; + + my $random = String::Random->new; + + my $categorycode = $random->randpattern('CCCCCCCCCC'); + my $branchcode = $random->randpattern('CCCCCCCCCC'); + + my $category = $schema->resultset('Category')->create( { categorycode => $categorycode } ); + my $branch = $schema->resultset('Branch')->create( + { + branchcode => $branchcode, + branchname => 'Test branch for Koha::Patron::siblings tests', + } + ); + my $borrower = $schema->resultset('Borrower')->create( + { + categorycode => $categorycode, + branchcode => $branchcode, + } + ); + my $patron = Koha::Patrons->find( $borrower->borrowernumber ); + + my $siblings = $patron->siblings; + isa_ok( $siblings, 'Koha::Patrons' ); + is( $siblings->count, 0 ); + + $schema->txn_rollback; +}; -- 2.30.2