From 26afea24f82c8eaaa7442a8c4edffc20545fe0d8 Mon Sep 17 00:00:00 2001 From: Lari Taskula Date: Wed, 5 Oct 2022 14:54:03 +0000 Subject: [PATCH] Bug 31692: Add Koha::Hold::change_type and unit tests To test: 1. prove t/db_dependent/Koha/Hold.t Signed-off-by: David Nind Signed-off-by: Katrin Fischer Signed-off-by: Katrin Fischer Signed-off-by: Tomas Cohen Arazi --- Koha/Exceptions/Hold.pm | 4 +++ Koha/Hold.pm | 33 +++++++++++++++++++++ t/db_dependent/Koha/Hold.t | 60 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 97 insertions(+) diff --git a/Koha/Exceptions/Hold.pm b/Koha/Exceptions/Hold.pm index b09dcd65b5..38577b4126 100644 --- a/Koha/Exceptions/Hold.pm +++ b/Koha/Exceptions/Hold.pm @@ -23,6 +23,10 @@ use Exception::Class ( 'Koha::Exceptions::Hold' => { isa => 'Koha::Exception', }, + 'Koha::Exceptions::Hold::CannotChangeHoldType' => { + isa => 'Koha::Exceptions::Hold', + description => "Record cannot have both record and item level holds at the same time", + }, 'Koha::Exceptions::Hold::CannotSuspendFound' => { isa => 'Koha::Exceptions::Hold', description => "Found holds cannot be suspended", diff --git a/Koha/Hold.pm b/Koha/Hold.pm index 2775e59184..e3e8880fa6 100644 --- a/Koha/Hold.pm +++ b/Koha/Hold.pm @@ -852,6 +852,39 @@ sub fill { return $self; } +=head3 sub change_type + + $hold->change_type # to record level + $hold->change_type( $itemnumber ) # to item level + +Changes hold type between record and item level holds, only if record has +exactly one hold for a patron. This is because Koha expects all holds for +a patron on a record to be alike. + +=cut + +sub change_type { + my ( $self, $itemnumber ) = @_; + + my $record_holds_per_patron = Koha::Holds->search( { + borrowernumber => $self->borrowernumber, + biblionumber => $self->biblionumber, + } ); + + if ( $itemnumber && $self->itemnumber ) { + $self->itemnumber( $itemnumber )->store; + return $self; + } + + if ( $record_holds_per_patron->count == 1 ) { + $self->set({ itemnumber => $itemnumber ? $itemnumber : undef })->store; + } else { + Koha::Exceptions::Hold::CannotChangeHoldType->throw(); + } + + return $self; +} + =head3 store Override base store method to set default diff --git a/t/db_dependent/Koha/Hold.t b/t/db_dependent/Koha/Hold.t index 50d6ffdbe9..f7741bf20c 100755 --- a/t/db_dependent/Koha/Hold.t +++ b/t/db_dependent/Koha/Hold.t @@ -997,3 +997,63 @@ subtest 'Koha::Hold::item_group tests' => sub { $schema->storage->txn_rollback; }; + +subtest 'change_type tests' => sub { + + plan tests => 9; + + $schema->storage->txn_begin; + + my $item = $builder->build_object( { class => 'Koha::Items', } ); + my $hold = $builder->build_object( { + class => 'Koha::Holds', + value => { + itemnumber => undef, + } + } ); + + my $hold2 = $builder->build_object( { + class => 'Koha::Holds', + value => { + borrowernumber => $hold->borrowernumber, + } + } ); + + ok( $hold->change_type ); + + $hold->discard_changes; + + is( $hold->itemnumber, undef, 'record hold to record hold, no changes'); + + ok( $hold->change_type( $item->itemnumber ) ); + + $hold->discard_changes; + + is( $hold->itemnumber, $item->itemnumber, 'record hold to item hold'); + + ok( $hold->change_type( $item->itemnumber ) ); + + $hold->discard_changes; + + is( $hold->itemnumber, $item->itemnumber, 'item hold to item hold, no changes'); + + ok( $hold->change_type ); + + $hold->discard_changes; + + is( $hold->itemnumber, undef, 'item hold to record hold'); + + my $hold3 = $builder->build_object( { + class => 'Koha::Holds', + value => { + biblionumber => $hold->biblionumber, + borrowernumber => $hold->borrowernumber, + } + } ); + + throws_ok { $hold->change_type } + 'Koha::Exceptions::Hold::CannotChangeHoldType', + 'Exception thrown because more than one hold per record'; + + $schema->storage->txn_rollback; +}; -- 2.39.5