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 <david@davidnind.com>

Signed-off-by: Katrin Fischer <katrin.fischer.83@web.de>

Signed-off-by: Katrin Fischer <katrin.fischer.83@web.de>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
This commit is contained in:
Lari Taskula 2022-10-05 14:54:03 +00:00 committed by Tomas Cohen Arazi
parent b703d97816
commit 26afea24f8
No known key found for this signature in database
GPG Key ID: 0A272EA1B2F3C15F
3 changed files with 97 additions and 0 deletions

View File

@ -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",

View File

@ -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

View File

@ -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;
};