From 14142f6fb99081c72d791b1c1a0f60fec48f7982 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 --- Koha/Exceptions/Hold.pm | 4 +++ Koha/Hold.pm | 33 ++++++++++++++++++++ t/db_dependent/Koha/Hold.t | 62 +++++++++++++++++++++++++++++++++++++- 3 files changed, 98 insertions(+), 1 deletion(-) 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 378b902638..21ccef40bc 100644 --- a/Koha/Hold.pm +++ b/Koha/Hold.pm @@ -806,6 +806,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 fe59013719..417fda273e 100755 --- a/t/db_dependent/Koha/Hold.t +++ b/t/db_dependent/Koha/Hold.t @@ -19,7 +19,7 @@ use Modern::Perl; -use Test::More tests => 10; +use Test::More tests => 11; use Test::Exception; use Test::MockModule; @@ -911,3 +911,63 @@ subtest 'can_update_pickup_location_opac() 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.25.1