From a55ba7861bdc1293e4e008277153b8b9fcf7bd13 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Mon, 6 Nov 2017 15:35:50 -0300 Subject: [PATCH] Bug 19578: Remove MARC punctuation in notices (TT syntax) Jenkins fails (run 287) on a test in t/db_dependent/Letters/TemplateToolkit.t: With the historical syntax: # Your request for an article from tQYRS (c3Av58O0P5xkkIGu) has been canceled for the following reason: With the TT syntax: # Your request for an article from tQYRS_ (c3Av58O0P5xkkIGu) has been canceled for the following reason: The last character of the biblio's title has been removed because it's a punctuation character. It comes from: C4::Letters::_parseletter 893 $val =~ s/\p{P}$// if $val && $table=~/biblio/; The same replacement is done for patron's attributes too. Test plan: - Confirm that the new tests pass. That should be enough to confirm this change make sense. Test plan (manual): - Create a biblio with a title ending with a punctuation (like "with_punctuation_"), or any other fields of biblio/biblioitem - Generate a notice which will display this field (CHECKIN for instance) Use the historical syntax and the TT syntax, both should display the title without the punctuation character at the end CHECKIN historical: The following items have been checked in: ---- <> ---- CHECKIN TT syntax: The following items have been checked in: ---- [% biblio.title %] ---- Signed-off-by: Katrin Fischer --- C4/Letters.pm | 26 ++++++++++++++- Koha/Template/Plugin/Remove_MARC_punctuation.pm | 31 ++++++++++++++++++ t/db_dependent/Letters/TemplateToolkit.t | 42 ++++++++++++++++++++++++- 3 files changed, 97 insertions(+), 2 deletions(-) create mode 100644 Koha/Template/Plugin/Remove_MARC_punctuation.pm diff --git a/C4/Letters.pm b/C4/Letters.pm index 62579eb..d4664d0 100644 --- a/C4/Letters.pm +++ b/C4/Letters.pm @@ -1518,7 +1518,8 @@ sub _process_tt { my $tt_params = { %{ _get_tt_params( $tables ) }, %{ _get_tt_params( $loops, 'is_a_loop' ) }, %$substitute }; - $content = qq|[% USE KohaDates %]$content|; + $content = add_tt_filters( $content ); + $content = qq|[% USE KohaDates %][% USE Remove_MARC_punctuation %]$content|; my $output; $template->process( \$content, $tt_params, \$output ) || croak "ERROR PROCESSING TEMPLATE: " . $template->error(); @@ -1545,6 +1546,12 @@ sub _get_tt_params { plural => 'biblios', pk => 'biblionumber', }, + biblioitems => { + module => 'Koha::Biblioitems', + singular => 'biblioitem', + plural => 'biblioitems', + pk => 'biblioitemnumber', + }, borrowers => { module => 'Koha::Patrons', singular => 'borrower', @@ -1694,6 +1701,23 @@ sub _get_tt_params { return $params; } +=head3 add_tt_filters + +$content = add_tt_filters( $content ); + +Add TT filters to some specific fields if needed. + +For now we only add the Remove_MARC_punctuation TT filter to biblio and biblioitem fields + +=cut + +sub add_tt_filters { + my ( $content ) = @_; + $content =~ s|\[%\s*biblio\.(.*?)\s*%\]|[% biblio.$1 \| \$Remove_MARC_punctuation %]|gxms; + $content =~ s|\[%\s*biblioitem\.(.*?)\s*%\]|[% biblioitem.$1 \| \$Remove_MARC_punctuation %]|gxms; + return $content; +} + =head2 get_item_content my $item = Koha::Items->find(...)->unblessed; diff --git a/Koha/Template/Plugin/Remove_MARC_punctuation.pm b/Koha/Template/Plugin/Remove_MARC_punctuation.pm new file mode 100644 index 0000000..19c6740 --- /dev/null +++ b/Koha/Template/Plugin/Remove_MARC_punctuation.pm @@ -0,0 +1,31 @@ +package Koha::Template::Plugin::Remove_MARC_punctuation; + +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . + +use Modern::Perl; + +use Template::Plugin::Filter; +use base qw( Template::Plugin::Filter ); + +our $DYNAMIC = 1; + +sub filter { + my ( $self, $value ) = @_; + $value =~ s/\p{P}$//; + return $value; +} + +1; diff --git a/t/db_dependent/Letters/TemplateToolkit.t b/t/db_dependent/Letters/TemplateToolkit.t index 746c870..3501d49 100644 --- a/t/db_dependent/Letters/TemplateToolkit.t +++ b/t/db_dependent/Letters/TemplateToolkit.t @@ -19,7 +19,7 @@ # along with Koha; if not, see . use Modern::Perl; -use Test::More tests => 17; +use Test::More tests => 18; use Test::Warn; use MARC::Record; @@ -894,6 +894,46 @@ subtest 'loops' => sub { }; }; +subtest 'add_tt_filters' => sub { + plan tests => 1; + my $code = "TEST"; + my $module = "TEST"; + + my $patron = $builder->build_object( + { + class => 'Koha::Patrons', + value => { surname => "with_punctuation_" } + } + ); + my $biblio = $builder->build_object( + { class => 'Koha::Biblios', value => { title => "with_punctuation_" } } + ); + my $biblioitem = $builder->build_object( + { + class => 'Koha::Biblioitems', + value => { + biblionumber => $biblio->biblionumber, + isbn => "with_punctuation_" + } + } + ); + + my $template = q|patron=[% borrower.surname %];biblio=[% biblio.title %];biblioitems=[% biblioitem.isbn %]|; + reset_template( { template => $template, code => $code, module => $module } ); + my $letter = GetPreparedLetter( + module => $module, + letter_code => $code, + tables => { + borrowers => $patron->borrowernumber, + biblio => $biblio->biblionumber, + biblioitems => $biblioitem->biblioitemnumber + } + ); + my $expected_letter = q|patron=with_punctuation_;biblio=with_punctuation;biblioitems=with_punctuation|; + is( $letter->{content}, $expected_letter, "Pre-processing should call TT plugin to remove punctuation if table is biblio or biblioitems"); +}; + + sub reset_template { my ( $params ) = @_; my $template = $params->{template}; -- 2.1.4