From 6a31620710d39f12bb2f1ef7833f0e15e3241246 Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Fri, 2 Feb 2024 16:23:42 -0300 Subject: [PATCH] Bug 31791: Add Koha::Biblio->can_be_edited Sponsored-by: ByWater Solutions Signed-off-by: Martin Renvoize Signed-off-by: Lucas Gass --- Koha/Biblio.pm | 18 +++++++++++ t/db_dependent/Koha/Biblio.t | 62 +++++++++++++++++++++++++++++++++++- 2 files changed, 79 insertions(+), 1 deletion(-) diff --git a/Koha/Biblio.pm b/Koha/Biblio.pm index 771af6c167..75216bd523 100644 --- a/Koha/Biblio.pm +++ b/Koha/Biblio.pm @@ -224,7 +224,25 @@ sub can_article_request { return q{}; } +=head3 can_be_edited + if ( $biblio->can_be_edited( $patron ) ) { ... } + +Returns a boolean denoting whether the passed I<$patron> meets the required +conditions to manually edit the record. + +=cut + +sub can_be_edited { + my ( $self, $patron ) = @_; + + Koha::Exceptions::MissingParameter->throw( error => "The patron parameter is missing or invalid" ) + unless $patron && ref($patron) eq 'Koha::Patron'; + + return ( + ( $self->metadata->source_allows_editing && $patron->has_permission( { editcatalogue => 'edit_catalogue' } ) ) + || $patron->has_permission( { editcatalogue => 'edit_locked_records' } ) ) ? 1 : 0; +} =head3 check_booking diff --git a/t/db_dependent/Koha/Biblio.t b/t/db_dependent/Koha/Biblio.t index d6cd9995d4..a91832f90e 100755 --- a/t/db_dependent/Koha/Biblio.t +++ b/t/db_dependent/Koha/Biblio.t @@ -17,7 +17,7 @@ use Modern::Perl; -use Test::More tests => 36; +use Test::More tests => 37; use Test::Exception; use Test::Warn; @@ -1549,6 +1549,66 @@ subtest 'opac_summary_html' => sub { ); }; +subtest 'can_be_edited() tests' => sub { + + plan tests => 6; + + $schema->storage->txn_begin; + + my $patron = $builder->build_object( { class => 'Koha::Patrons', value => { flags => 0 } } ); + my $biblio = $builder->build_sample_biblio; + + my $source_allows_editing = 1; + + my $mock_metadata = Test::MockModule->new('Koha::Biblio::Metadata'); + $mock_metadata->mock( 'source_allows_editing', sub { return $source_allows_editing; } ); + + ok( !$biblio->can_be_edited($patron), "Patron needs 'edit_catalog' subpermission" ); + + # Add editcatalogue => edit_catalog subpermission + $builder->build( + { + source => 'UserPermission', + value => { + borrowernumber => $patron->id, + module_bit => 9, # editcatalogue + code => 'edit_catalogue', + }, + } + ); + + ok( $biblio->can_be_edited($patron), "Patron with 'edit_catalogue' can edit" ); + + # Mock the record source doesn't allow direct editing + $source_allows_editing = 0; + ok( !$biblio->can_be_edited($patron), "Patron needs 'edit_locked_record' subpermission for locked records" ); + + # Add editcatalogue => edit_catalog subpermission + $builder->build( + { + source => 'UserPermission', + value => { + borrowernumber => $patron->id, + module_bit => 9, # editcatalogue + code => 'edit_locked_records', + }, + } + ); + ok( $biblio->can_be_edited($patron), "Patron needs 'edit_locked_record' subpermission for locked records" ); + + throws_ok { $biblio->can_be_edited() } + 'Koha::Exceptions::MissingParameter', + 'Exception thrown on missing parameter'; + + my $potato = 'Potato'; + + throws_ok { $biblio->can_be_edited($potato) } + 'Koha::Exceptions::MissingParameter', + 'Exception thrown if parameter not a Koha::Patron reference'; + + $schema->storage->txn_rollback; +}; + sub component_record1 { my $marc = MARC::Record->new; $marc->append_fields( -- 2.30.2