From 514bc81844504ddc0b08e1b17932c8da6a36898d Mon Sep 17 00:00:00 2001 From: Kyle Hall Date: Thu, 24 Mar 2022 06:57:06 -0400 Subject: [PATCH] Bug 30344: Changing a patron from a category that allows guarantees to one that doesn't should remove that link If an adult type patron is changed to a child type patron, it hides the patron's guarantees, but does not remove that link. We should be removing that link as we do when a guarantee becomes an adult type patron. Test Plan: 1) Create an adult with a guarantor 2) Turn that adult into a child patron tyupe 3) Note the guarantees show on the patron details, but not in the patron editor 4) Turn that patron back into an adult 5) Note the guarantees how again in the editor 6) Apply this patch 7) Restart all the things! 8) Turn that patron back into a child again 9) Note the guarantees no longer show on the patron details 10) Turn that patron back into an adult 11) Note the patron still has no guarantees --- Koha/Patron.pm | 4 ++++ t/db_dependent/Patrons.t | 22 +++++++++++++++++++++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/Koha/Patron.pm b/Koha/Patron.pm index 0c16079fa9..1d6a812ad5 100644 --- a/Koha/Patron.pm +++ b/Koha/Patron.pm @@ -305,6 +305,10 @@ sub store { if ( $self->category->category_type ne 'C' && $self->category->category_type ne 'P' ); + # Only these patron types can be guarantors, + # remove guarantee relationships otherwise + $self->guarantee_relationships->delete + unless $self->category->category_type =~ /A|I|S|X/; } # Actionlogs diff --git a/t/db_dependent/Patrons.t b/t/db_dependent/Patrons.t index 8c90f7e8a1..884924337e 100755 --- a/t/db_dependent/Patrons.t +++ b/t/db_dependent/Patrons.t @@ -106,7 +106,7 @@ foreach my $b ( $patrons->as_list() ) { } subtest "Update patron categories" => sub { - plan tests => 24; + plan tests => 28; t::lib::Mocks::mock_preference( 'borrowerRelationship', 'test' ); my $c_categorycode = $builder->build({ source => 'Category', value => { category_type=>'C', @@ -207,6 +207,26 @@ subtest "Update patron categories" => sub { is( Koha::Patrons->search_patrons_to_update_category({from=>$p_categorycode})->update_category_to({category=>$a_categorycode}),1,'One professional patron updated to adult category'); is( Koha::Patrons->find($inst->borrowernumber)->guarantee_relationships->guarantees->count,0,'Guarantee was removed when made adult'); + my $adult4 = $builder->build_object({class => 'Koha::Patrons', value => { + categorycode=>$a_categorycode, + branchcode=>$branchcode2, + dateenrolled=>'2017-01-01', + } + }); + my $child4 = $builder->build_object({class => 'Koha::Patrons', value => { + dateofbirth => dt_from_string->add(years=>-18), + categorycode=>$c_categorycode, + branchcode=>$branchcode1, + } + }); + $child4->add_guarantor({guarantor_id => $adult4->borrowernumber, relationship => 'test'}); + + is( $adult4->guarantee_relationships->count, 1, "Adult has guarantee relationship" ); + is( $child4->guarantor_relationships->count, 1, "Child has guarantor relationship" ); + $adult4->categorycode($c_categorycode); + $adult4->store(); + is( $adult4->guarantee_relationships->count, 0, "Adult turned child now has no guarantee relationship" ); + is( $child4->guarantor_relationships->count, 0, "Child of adult now turned child has no guarantor relationship" ); }; -- 2.30.2