Bug 36081: Check SupportedFormats server side

Test plan:
Add article request with format via OPAC.
Run t/db_dependent/Koha/ArticleRequest.t

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Signed-off-by: Owen Leonard <oleonard@myacpl.org>
Signed-off-by: Magnus Enger <magnus@libriotech.no>
Tests in t/db_dependent/Koha/ArticleRequest.t pass. I can add an
article request with a type. If I allow PHOTOCOPY but change the
HTML in the OPAC form so SCAN is submitted I get a nice (but
somewhat generic) error.

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
Signed-off-by: Alex Buckley <alexbuckley@catalyst.net.nz>
(cherry picked from commit 3a817ca380)
Signed-off-by: Fridolin Somers <fridolin.somers@biblibre.com>
This commit is contained in:
Marcel de Rooy 2024-02-13 13:32:06 +00:00 committed by Fridolin Somers
parent 9d37421c0c
commit ea1fbf8e7b
4 changed files with 26 additions and 2 deletions

View File

@ -266,6 +266,10 @@ sub store {
if ( !$self->in_storage ) {
$self->created_on( dt_from_string() );
}
my $format = $self->format;
if ( C4::Context->preference('ArticleRequestsSupportedFormats') !~ /\b$format\b/ ) {
Koha::Exceptions::ArticleRequest::WrongFormat->throw;
}
return $self->SUPER::store;
}

View File

@ -27,6 +27,10 @@ use Exception::Class (
isa => 'Koha::Exceptions::ArticleRequest',
description => 'Article request limit was reached'
},
'Koha::Exceptions::ArticleRequest::WrongFormat' => {
isa => 'Koha::Exceptions::ArticleRequest',
description => 'Passed format is not locally supported'
},
);
=head1 NAME

View File

@ -295,7 +295,7 @@ $(document).ready( function() {
});
// Initialize format(s)
var supported_formats = "[% Koha.Preference('ArticleRequestsSupportedFormats') | $raw %]";
var supported_formats = "[% Koha.Preference('ArticleRequestsSupportedFormats') | html %]";
if( !supported_formats.match(/PHOTOCOPY/) )
$('#format option[value="PHOTOCOPY"]').remove();
if( !supported_formats.match(/SCAN/) )

View File

@ -17,13 +17,15 @@
use Modern::Perl;
use Test::More tests => 5;
use Test::More tests => 6;
use Test::MockModule;
use Test::Exception;
use t::lib::TestBuilder;
use t::lib::Mocks;
use Koha::ArticleRequests;
use Koha::Exceptions::ArticleRequest;
my $schema = Koha::Database->new->schema;
my $builder = t::lib::TestBuilder->new;
@ -209,3 +211,17 @@ subtest 'cancel() tests' => sub {
$schema->storage->txn_rollback;
};
subtest 'store' => sub {
plan tests => 2;
$schema->storage->txn_begin;
t::lib::Mocks::mock_preference( 'ArticleRequestsSupportedFormats', 'SCAN' );
my $ar = $builder->build_object( { class => 'Koha::ArticleRequests', value => { format => 'PHOTOCOPY' } } );
throws_ok { $ar->format('test')->store } 'Koha::Exceptions::ArticleRequest::WrongFormat',
'Format not supported';
t::lib::Mocks::mock_preference( 'ArticleRequestsSupportedFormats', 'SCAN|PHOTOCOPY|ELSE' );
lives_ok { $ar->format('PHOTOCOPY')->store } 'Now we do support it';
$schema->storage->txn_rollback;
};