From 1c0810e8983b6d3e769a73300d5c5a47ff58eca3 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 Signed-off-by: Magnus Enger 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. --- Koha/ArticleRequest.pm | 4 +++ Koha/Exceptions/ArticleRequest.pm | 4 +++ .../en/modules/opac-request-article.tt | 33 +++++++++++++++++-- t/db_dependent/Koha/ArticleRequest.t | 18 +++++++++- 4 files changed, 56 insertions(+), 3 deletions(-) diff --git a/Koha/ArticleRequest.pm b/Koha/ArticleRequest.pm index 5beb5dfeb1..d7c6e6fc30 100644 --- a/Koha/ArticleRequest.pm +++ b/Koha/ArticleRequest.pm @@ -268,6 +268,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 890ba9d95f..d14bf15657 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 @@ -300,6 +300,35 @@ var first_format = supported_formats.split('|')[0].replace(/^\s*|\s*$/g, ''); $('#format option[value="'+first_format+'"]').attr('selected', true); } - }); - + + if ( f.length ) { + alert( _("The following fields are required and not filled in: ") + f.join(", ") ); + return 0; + } + + // Check if all fields are blank + if( m.length == 0 && $('#title').val()=='' && $('#author').val()=='' && $('#volume').val()=='' && $('#issue').val()=='' && $('#date').val()=='' && $('#pages').val()=='' && $('#chapters').val()=='' && $('#patron_notes').val()=='' && !$('input[name="toc_request"]').prop('checked') ) { + alert( _("Please fill in at least one field.") ); + return 0; + } + + allow_submit = true; + $('#place-article-request').submit(); + } + }); + + // Initialize format(s) + var supported_formats = "[% Koha.Preference('ArticleRequestsSupportedFormats') | html %]"; + if( !supported_formats.match(/PHOTOCOPY/) ) + $('#format option[value="PHOTOCOPY"]').remove(); + if( !supported_formats.match(/SCAN/) ) + $('#format option[value="SCAN"]').remove(); + + if( $('#format option').length > 1 ) { + // Select first listed format + var first_format = supported_formats.split('|')[0].replace(/^\s*|\s*$/g, ''); + $('#format option[value="'+first_format+'"]').attr('selected', true); + } +}); + [% END %] diff --git a/t/db_dependent/Koha/ArticleRequest.t b/t/db_dependent/Koha/ArticleRequest.t index f2feb3b3b1..00e87bada9 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; @@ -211,3 +213,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.34.1