From d008c1719ca8b8dc0237621d39740805c626481b Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Mon, 2 Oct 2017 16:42:46 -0300 Subject: [PATCH] Bug 19069: Fix 'does not match' behaviour in MARC modification template The "does not match" condition does not behave as expected. We want it to process the action if the subfield exists and that the value does not match a given pattern. Test plan: Be creative and write different template actions using the "does not match" condition. Using the "Batch record modification" and the "Show MARC" popup, confirm that the processed record is the one you are expecting. Signed-off-by: Jon Knight --- C4/MarcModificationTemplates.pm | 17 +++++++-- Koha/MoreUtils.pm | 15 ++++++++ t/db_dependent/MarcModificationTemplates.t | 57 ++++++++++++++++++++++-------- 3 files changed, 73 insertions(+), 16 deletions(-) create mode 100644 Koha/MoreUtils.pm diff --git a/C4/MarcModificationTemplates.pm b/C4/MarcModificationTemplates.pm index cbd4204..bbfb1c6 100644 --- a/C4/MarcModificationTemplates.pm +++ b/C4/MarcModificationTemplates.pm @@ -23,6 +23,7 @@ use DateTime; use C4::Context; use Koha::SimpleMARC; +use Koha::MoreUtils; use vars qw(@ISA @EXPORT); @@ -567,9 +568,21 @@ sub ModifyRecordWithTemplate { subfield => $conditional_subfield, is_regex => $conditional_regex, }); + my $all_fields = [ + 1 .. scalar @{ + field_exists( + { + record => $record, + field => $conditional_field, + subfield => $conditional_subfield + } + ) + } + ]; + $field_numbers = [Koha::MoreUtils::singleton ( @$field_numbers, @$all_fields ) ]; $do = $conditional eq 'if' - ? not @$field_numbers - : @$field_numbers; + ? @$field_numbers + : not @$field_numbers; } } diff --git a/Koha/MoreUtils.pm b/Koha/MoreUtils.pm new file mode 100644 index 0000000..53d55e4 --- /dev/null +++ b/Koha/MoreUtils.pm @@ -0,0 +1,15 @@ +package Koha::MoreUtils; + +use Modern::Perl; + +# From List::MoreUtils v4.0 +sub singleton (@) +{ + my %seen = (); + my $k; + my $seen_undef; + grep { 1 == ( defined $_ ? $seen{ $k = $_ } : $seen_undef ) } + grep { defined $_ ? not $seen{ $k = $_ }++ : not $seen_undef++ } @_; +} + +1; diff --git a/t/db_dependent/MarcModificationTemplates.t b/t/db_dependent/MarcModificationTemplates.t index 3ea702a..d2fcffc 100644 --- a/t/db_dependent/MarcModificationTemplates.t +++ b/t/db_dependent/MarcModificationTemplates.t @@ -1,6 +1,6 @@ use Modern::Perl; -use Test::More tests => 95; +use Test::More tests => 96; use Koha::SimpleMARC; @@ -346,6 +346,37 @@ subtest 'GetModificationTemplates' => sub { is_deeply( [map{$_->{name}} @templates], ['aaa', 'mmm', 'zzz'] ); }; +subtest "not_equals" => sub { + plan tests => 2; + $dbh->do(q|DELETE FROM marc_modification_templates|); + my $template_id = AddModificationTemplate("template_name"); + AddModificationTemplateAction( + $template_id, 'move_field', 0, + '650', '', '', '651', '', + '', '', '', + 'if', '650', '9', 'not_equals', '499', '', + 'Move field 650 to 651 if 650$9 != 499' + ); + my $record = new_record(); + ModifyRecordWithTemplate( $template_id, $record ); + my $expected_record = expected_record_2(); + is_deeply( $record, $expected_record, '650 has been moved to 651 when 650$9 != 499' ); + + $dbh->do(q|DELETE FROM marc_modification_templates|); + $template_id = AddModificationTemplate("template_name"); + AddModificationTemplateAction( + $template_id, 'move_field', 0, + '650', '', '', '651', '', + '', '', '', + 'if', '650', 'b', 'not_equals', '499', '', + 'Move field 650 to 651 if 650$b != 499' + ); + $record = new_record(); + ModifyRecordWithTemplate( $template_id, $record ); + $expected_record = new_record(); + is_deeply( $record, $expected_record, 'None 650 have been moved, no $650$b exists' ); +}; + sub new_record { my $record = MARC::Record->new; $record->leader('03174nam a2200445 a 4500'); @@ -445,28 +476,26 @@ sub expected_record_2 { c => 'Donald E. Knuth.', ), MARC::Field->new( + 245, '1', '4', + a => 'Bad title', + c => 'Donald E. Knuth.', + ), + MARC::Field->new( 650, ' ', '0', - 9 => '462', + a => 'Computer programming.', + 9 => '499', ), MARC::Field->new( 952, ' ', ' ', - p => '3010023917_updated', + p => '3010023917', y => 'BK', c => 'GEN', - e => '2001-06-25', - ), - MARC::Field->new( - 246, '', ' ', - a => 'The art of computer programming', + d => '2001-06-25', ), MARC::Field->new( 651, ' ', '0', - a => 'Computer algorithms.', - 9 => '499', - ), - MARC::Field->new( - 999, ' ', ' ', - a => 'non existent.', + a => 'Computer programming.', + 9 => '462', ), ); $record->append_fields(@fields); -- 2.1.4