From 3fe2fde528e49adb915c66dc291c9777d8f68ffd Mon Sep 17 00:00:00 2001 From: Marcel de Rooy Date: Tue, 13 Feb 2024 13:32:06 +0000 Subject: [PATCH] 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 Signed-off-by: Owen Leonard --- Koha/ArticleRequest.pm | 4 ++++ Koha/Exceptions/ArticleRequest.pm | 4 ++++ .../en/modules/opac-request-article.tt | 2 +- t/db_dependent/Koha/ArticleRequest.t | 18 +++++++++++++++++- 4 files changed, 26 insertions(+), 2 deletions(-) diff --git a/Koha/ArticleRequest.pm b/Koha/ArticleRequest.pm index 1bfbda3484..cacd5347df 100644 --- a/Koha/ArticleRequest.pm +++ b/Koha/ArticleRequest.pm @@ -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; } diff --git a/Koha/Exceptions/ArticleRequest.pm b/Koha/Exceptions/ArticleRequest.pm index cfb5baa55e..9e016a6f7e 100644 --- a/Koha/Exceptions/ArticleRequest.pm +++ b/Koha/Exceptions/ArticleRequest.pm @@ -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 diff --git a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-request-article.tt b/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-request-article.tt index 1b6ef84443..fd05b5cd54 100644 --- a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-request-article.tt +++ b/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-request-article.tt @@ -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/) ) diff --git a/t/db_dependent/Koha/ArticleRequest.t b/t/db_dependent/Koha/ArticleRequest.t index 2d6819e763..a73c651e90 100755 --- a/t/db_dependent/Koha/ArticleRequest.t +++ b/t/db_dependent/Koha/ArticleRequest.t @@ -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; +}; -- 2.30.2