From e8595f9d622c5126bc1b3be931c1c8ba032311bb 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 Signed-off-by: Arthur Suzuki Signed-off-by: Katrin Fischer --- 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 684f9b700c..3516a30f67 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 eb528655e5..03f4b6146f 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.39.5