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 <J.P.Knight@lboro.ac.uk>

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
This commit is contained in:
Jonathan Druart 2017-10-02 16:42:46 -03:00
parent a70c38f380
commit bdf85f5e39
3 changed files with 78 additions and 21 deletions

View File

@ -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;
}
}

15
Koha/MoreUtils.pm Normal file
View File

@ -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;

View File

@ -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');
@ -444,29 +475,27 @@ sub expected_record_2 {
a => 'The art of computer programming',
c => 'Donald E. Knuth.',
),
MARC::Field->new(
245, '1', '4',
a => 'Bad title',
c => 'Donald E. Knuth.',
),
MARC::Field->new(
650, ' ', '0',
9 => '462',
),
MARC::Field->new(
952, ' ', ' ',
p => '3010023917_updated',
y => 'BK',
c => 'GEN',
e => '2001-06-25',
),
MARC::Field->new(
246, '', ' ',
a => 'The art of computer programming',
),
MARC::Field->new(
651, ' ', '0',
a => 'Computer algorithms.',
a => 'Computer programming.',
9 => '499',
),
MARC::Field->new(
999, ' ', ' ',
a => 'non existent.',
952, ' ', ' ',
p => '3010023917',
y => 'BK',
c => 'GEN',
d => '2001-06-25',
),
MARC::Field->new(
651, ' ', '0',
a => 'Computer programming.',
9 => '462',
),
);
$record->append_fields(@fields);