@@ -, +, @@ --- Koha/AuthorisedValueCategories.pm | 20 ++++++++++++++++++++ Koha/AuthorisedValueCategory.pm | 15 +++++++++++++++ t/db_dependent/AuthorisedValues.t | 27 +++++++++++++++++++++++++-- 3 files changed, 60 insertions(+), 2 deletions(-) --- a/Koha/AuthorisedValueCategories.pm +++ a/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 --- a/Koha/AuthorisedValueCategory.pm +++ a/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 --- a/t/db_dependent/AuthorisedValues.t +++ a/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' ); --