From 02499a45caf241cc55674e89da56e727026391e6 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Tue, 28 Apr 2020 11:23:02 +0200 Subject: [PATCH] Bug 17355: Override delete methods Signed-off-by: Victor Grousset/tuxayo Signed-off-by: Katrin Fischer Signed-off-by: Martin Renvoize --- Koha/AuthorisedValueCategories.pm | 20 ++++++++++++++++++++ Koha/AuthorisedValueCategory.pm | 15 +++++++++++++++ t/db_dependent/AuthorisedValues.t | 27 +++++++++++++++++++++++++-- 3 files changed, 60 insertions(+), 2 deletions(-) diff --git a/Koha/AuthorisedValueCategories.pm b/Koha/AuthorisedValueCategories.pm index afe6692a25..2d36352c88 100644 --- a/Koha/AuthorisedValueCategories.pm +++ b/Koha/AuthorisedValueCategories.pm @@ -20,6 +20,7 @@ use Modern::Perl; use Carp; use Koha::Database; +use Koha::Exceptions::CannotDeleteDefault; use Koha::AuthorisedValueCategory; @@ -35,6 +36,25 @@ Koha::AuthorisedValueCategories - Koha AuthorisedValueCategory Object set class =cut +=head3 delete + +Overridden delete method to prevent system default deletions + +=cut + +sub delete { + my ($self) = @_; + + my @set = $self->as_list; + for my $type (@set) { + if ( $type->is_system ) { + Koha::Exceptions::CannotDeleteDefault->throw; + } + } + + return $self->SUPER::delete; +} + =head3 type =cut diff --git a/Koha/AuthorisedValueCategory.pm b/Koha/AuthorisedValueCategory.pm index 14eb91db25..d2c196dadf 100644 --- a/Koha/AuthorisedValueCategory.pm +++ b/Koha/AuthorisedValueCategory.pm @@ -20,6 +20,7 @@ use Modern::Perl; use Carp; use Koha::Database; +use Koha::Exceptions::CannotDeleteDefault; use base qw(Koha::Object); @@ -33,6 +34,20 @@ Koha::AuthorisedValueCategory - Koha AuthorisedValueCategory Object class =cut +=head3 delete + +Overridden delete method to prevent system default deletions + +=cut + +sub delete { + my ($self) = @_; + + Koha::Exceptions::CannotDeleteDefault->throw if $self->is_system; + + return $self->SUPER::delete; +} + =head3 type =cut diff --git a/t/db_dependent/AuthorisedValues.t b/t/db_dependent/AuthorisedValues.t index d16d282731..860e97baef 100644 --- a/t/db_dependent/AuthorisedValues.t +++ b/t/db_dependent/AuthorisedValues.t @@ -1,7 +1,8 @@ #!/usr/bin/perl use Modern::Perl; -use Test::More tests => 15; +use Test::More tests => 17; +use Try::Tiny; use t::lib::TestBuilder; @@ -20,7 +21,7 @@ Koha::AuthorisedValues->delete; Koha::AuthorisedValueCategories->delete; # insert -Koha::AuthorisedValueCategory->new({ category_name => 'av_for_testing' })->store; +Koha::AuthorisedValueCategory->new({ category_name => 'av_for_testing', is_system => 1 })->store; Koha::AuthorisedValueCategory->new({ category_name => 'aaav_for_testing' })->store; Koha::AuthorisedValueCategory->new({ category_name => 'restricted_for_testing' })->store; my $av1 = Koha::AuthorisedValue->new( @@ -84,6 +85,28 @@ ok( $av2->id(), 'AV 2 is inserted' ); ok( $av3->id(), 'AV 3 is inserted' ); ok( $av4->id(), 'AV 4 is inserted' ); +{ # delete is_system AV categories + try { + Koha::AuthorisedValueCategories->find('av_for_testing')->delete + } + catch { + ok( + $_->isa('Koha::Exceptions::CannotDeleteDefault'), + 'A system AV category cannot be deleted' + ); + }; + + try { + Koha::AuthorisedValueCategories->search->delete + } + catch { + ok( + $_->isa('Koha::Exceptions::CannotDeleteDefault'), + 'system AV categories cannot be deleted' + ); + }; +} + is( $av3->opac_description, 'opac display value 3', 'Got correction opac description if lib_opac is set' ); $av3->lib_opac(''); is( $av3->opac_description, 'display value 3', 'Got correction opac description if lib_opac is *not* set' ); -- 2.39.5