diff --git a/t/db_dependent/Koha/Patron.t b/t/db_dependent/Koha/Patron.t index feb236451a..8a6769cde0 100755 --- a/t/db_dependent/Koha/Patron.t +++ b/t/db_dependent/Koha/Patron.t @@ -19,7 +19,7 @@ use Modern::Perl; -use Test::More tests => 44; +use Test::More tests => 45; use Test::NoWarnings; use Test::Exception; use Test::Warn; @@ -3364,3 +3364,91 @@ subtest 'reset_2fa() tests' => sub { $schema->storage->txn_rollback; }; + +subtest "create_hold_group, hold_groups, visual_hold_group_id tests" => sub { + + plan tests => 13; + + $schema->storage->txn_begin; + + my $patron = $builder->build_object( { class => 'Koha::Patrons' } ); + my $hold1 = $builder->build_object( + { + class => 'Koha::Holds', + value => { hold_group_id => undef, found => undef, borrowernumber => $patron->borrowernumber } + } + ); + my $hold2 = $builder->build_object( + { + class => 'Koha::Holds', + value => { hold_group_id => undef, found => undef, borrowernumber => $patron->borrowernumber } + } + ); + my $hold3 = $builder->build_object( + { + class => 'Koha::Holds', + value => { hold_group_id => undef, found => undef, borrowernumber => $patron->borrowernumber } + } + ); + + my $hold4 = $builder->build_object( + { + class => 'Koha::Holds', + value => { hold_group_id => undef, found => undef, borrowernumber => $patron->borrowernumber } + } + ); + my $hold5 = $builder->build_object( + { + class => 'Koha::Holds', + value => { hold_group_id => undef, found => undef, borrowernumber => $patron->borrowernumber } + } + ); + + my $hold6 = $builder->build_object( + { + class => 'Koha::Holds', + value => { hold_group_id => undef, found => undef, borrowernumber => $patron->borrowernumber } + } + ); + + my $patron_hold_groups = $patron->hold_groups; + is( $patron_hold_groups->count, 0, 'Patron does not have any hold groups' ); + + # Create 1st hold group + $patron->create_hold_group( [ $hold1->reserve_id, $hold2->reserve_id, $hold3->reserve_id ] ); + is( $patron_hold_groups->count, 1, 'Patron has one hold group' ); + + my $hold_group = $patron->hold_groups->as_list->[0]; + is( $hold_group->visual_hold_group_id, 1, 'Visual hold group id is 1' ); + is( $hold_group->hold_group_id, $hold1->get_from_storage->hold_group_id, 'hold1 added to hold_group' ); + is( $hold_group->hold_group_id, $hold2->get_from_storage->hold_group_id, 'hold2 added to hold_group' ); + is( $hold_group->hold_group_id, $hold3->get_from_storage->hold_group_id, 'hold3 added to hold_group' ); + + # Create 2nd hold group + $patron->create_hold_group( [ $hold4->reserve_id, $hold5->reserve_id ] ); + is( $patron_hold_groups->count, 2, 'Patron has two hold groups' ); + + my $second_hold_group = $patron->hold_groups->as_list->[1]; + is( $second_hold_group->visual_hold_group_id, 2, 'Visual hold group id is 2' ); + is( + $second_hold_group->hold_group_id, $hold4->get_from_storage->hold_group_id, + 'hold4 added to second hold_group' + ); + is( + $second_hold_group->hold_group_id, $hold5->get_from_storage->hold_group_id, + 'hold5 added to second hold_group' + ); + + $hold3->get_from_storage->fill(); + is( $patron->get_from_storage->hold_groups->count, 1, 'Patron only has one hold group again' ); + + $hold4->get_from_storage->cancel(); + is( $patron->get_from_storage->hold_groups->count, 0, 'Patron does not have any hold groups again' ); + + # Create 3rd hold group + $patron->create_hold_group( [ $hold5->reserve_id, $hold6->reserve_id ] ); + my $third_hold_group = $patron->hold_groups->as_list->[0]; + is( $third_hold_group->visual_hold_group_id, 1, 'Visual hold group id is 1' ); + + $schema->storage->txn_rollback; +};