From 6d2e5f58f2db772da3d84b2adf9eb05b4b194ef5 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Wed, 18 Dec 2013 09:37:19 +0100 Subject: [PATCH] Bug 11413: Change the field number logic This patch series is a bugfix for the Marc modification templates tool. Bug description: If you want to do an action (delete/update/move/...) on a multivalued field and if a condition is defined on the same field, it is highly probable the resulted record will not be what you expect. For example: Deleting All (or the first) fields 650 if 245$a="Bad title" works with the current code. BUT if you want to delete All (or the first) fields 650 with a condition on 650$9=42, and if at least one field matches the condition : - if you have selected all, all fields 650 will be deleted, even the ones who do not match the condition. - if you have selected first, the first 650 field will be deleted, even if it does not match the condition. The expected behavior is to delete the fields matching the condition (and not all the 650 fields). What this patch does: This patch introduces 2 changes in the logic of Koha::SimpleMARC. The first change is a change of the prototypes for the 2 routines field_exists and field_equals. Now they return the "field number" of the matching fields. The second change is the type of the "n" parameter for all routines using it in Koha::SimpleMARC. Before this patch, the "n" parameter was a boolean in most cases. If 0, the action was done on all fields, if 1 on the first one only. Now it is possible to specify the "field numbers" (so the array of field numbers which is returned by field_exists or field_equals) for all routines which had the n parameter. Test plan for the patch series: Note: This test plan describes a specific example, feel free to create your own one. 0/ Define a marc modification template with the following action: Delete field 245 if 245$9 = 42 1/ choose and export a record with several 245 fields. For ex: 245 $a The art of computer programming $c Donald E. Knuth. $9 41 245 $a Bad title $c Bad author $9 42 2/ import it using the Stage MARC for import tool. 3/ verify the imported record does not contain any 245 field. 4/ apply all the patches from this bug report 5/ do again steps 2 and 3 6/ verify the imported record contains only one 245 field, the one with 245$9=41 7/ verify the unit tests passed: prove t/SimpleMARC.t prove t/db_dependent/MarcModificationTemplates.t Signed-off-by: Kyle M Hall Signed-off-by: Brendan Gallagher Signed-off-by: Marcel de Rooy Signed-off-by: Tomas Cohen Arazi --- C4/MarcModificationTemplates.pm | 43 ++--- Koha/SimpleMARC.pm | 156 ++++++++++------- t/SimpleMARC.t | 302 +++++++++++++++++++++----------- 3 files changed, 315 insertions(+), 186 deletions(-) diff --git a/C4/MarcModificationTemplates.pm b/C4/MarcModificationTemplates.pm index a428313757..3465f5c601 100644 --- a/C4/MarcModificationTemplates.pm +++ b/C4/MarcModificationTemplates.pm @@ -526,54 +526,55 @@ sub ModifyRecordWithTemplate { } my $do = 1; + my $field_numbers = [ $field_number ]; if ( $conditional ) { if ( $conditional_comparison eq 'exists' ) { - my $exists = field_exists({ - record =>$record, + $field_numbers = field_exists({ + record => $record, field => $conditional_field, subfield => $conditional_subfield, }); $do = $conditional eq 'if' - ? $exists - : not $exists; + ? @$field_numbers + : not @$field_numbers; } elsif ( $conditional_comparison eq 'not_exists' ) { - my $exists = field_exists({ + $field_numbers = field_exists({ record => $record, field => $conditional_field, subfield => $conditional_subfield }); $do = $conditional eq 'if' - ? not $exists - : $exists; + ? not @$field_numbers + : @$field_numbers; } elsif ( $conditional_comparison eq 'equals' ) { - my $equals = field_equals({ + $field_numbers = field_equals({ record => $record, value => $conditional_value, field => $conditional_field, subfield => $conditional_subfield, - regex => $conditional_regex }); $do = $conditional eq 'if' - ? $equals - : not $equals; + ? @$field_numbers + : not @$field_numbers; } elsif ( $conditional_comparison eq 'not_equals' ) { - my $equals = field_equals({ + $field_numbers = field_equals({ record => $record, - value => $conditional_value, field => $conditional_field, - subfield => $conditional_subfield, - regex => $conditional_regex + subfield => $conditional_subfield }); $do = $conditional eq 'if' - ? not $equals - : $equals; + ? not @$field_numbers + : @$field_numbers; } } if ( $do ) { + @$field_numbers = ( $field_number ) + if $from_field ne $to_subfield + and $field_number; if ( $action eq 'copy_field' ) { copy_field({ record => $record, @@ -586,16 +587,16 @@ sub ModifyRecordWithTemplate { replace => $to_regex_replace, modifiers => $to_regex_modifiers }, - n => $field_number, + field_numbers => $field_numbers, }); } - elsif ( $action eq 'update_field' ) { update_field({ record => $record, field => $from_field, subfield => $from_subfield, values => [ $field_value ], + field_numbers => $field_numbers, }); } elsif ( $action eq 'move_field' ) { @@ -610,7 +611,7 @@ sub ModifyRecordWithTemplate { replace => $to_regex_replace, modifiers => $to_regex_modifiers }, - n => $field_number, + field_numbers => $field_numbers, }); } elsif ( $action eq 'delete_field' ) { @@ -618,7 +619,7 @@ sub ModifyRecordWithTemplate { record => $record, field => $from_field, subfield => $from_subfield, - n => $field_number, + field_numbers => $field_numbers, }); } } diff --git a/Koha/SimpleMARC.pm b/Koha/SimpleMARC.pm index f9adf400f0..0c2c403ade 100644 --- a/Koha/SimpleMARC.pm +++ b/Koha/SimpleMARC.pm @@ -80,7 +80,7 @@ sub copy_field { my $toFieldName = $params->{to_field}; my $toSubfieldName = $params->{to_subfield}; my $regex = $params->{regex}; - my $n = $params->{n}; + my $field_numbers = $params->{field_numbers} // []; if ( ! ( $record && $fromFieldName && $toFieldName ) ) { return; } @@ -93,7 +93,7 @@ sub copy_field { from_field => $fromFieldName, to_field => $toFieldName, regex => $regex, - n => $n + field_numbers => $field_numbers, }); } else { _copy_subfield({ @@ -103,7 +103,7 @@ sub copy_field { to_field => $toFieldName, to_subfield => $toSubfieldName, regex => $regex, - n => $n + field_numbers => $field_numbers, }); } @@ -115,14 +115,14 @@ sub _copy_field { my $fromFieldName = $params->{from_field}; my $toFieldName = $params->{to_field}; my $regex = $params->{regex}; - my $n = $params->{n}; + my $field_numbers = $params->{field_numbers} // []; _copy_move_field({ record => $record, from_field => $fromFieldName, to_field => $toFieldName, regex => $regex, - n => $n + field_numbers => $field_numbers, }); } @@ -134,10 +134,12 @@ sub _copy_subfield { my $toFieldName = $params->{to_field}; my $toSubfieldName = $params->{to_subfield}; my $regex = $params->{regex}; - my $n = $params->{n}; + my $field_numbers = $params->{field_numbers} // []; my @values = read_field({ record => $record, field => $fromFieldName, subfield => $fromSubfieldName }); - @values = ( $values[$n-1] ) if ( $n ); + if ( @$field_numbers ) { + @values = map { $_ <= @values ? $values[ $_ - 1 ] : () } @$field_numbers; + } _modify_values({ values => \@values, regex => $regex }); update_field({ record => $record, field => $toFieldName, subfield => $toSubfieldName, values => \@values }); @@ -149,6 +151,7 @@ sub update_field { my $fieldName = $params->{field}; my $subfieldName = $params->{subfield}; my @values = @{ $params->{values} }; + my $field_numbers = $params->{field_numbers} // []; if ( ! ( $record && $fieldName ) ) { return; } @@ -157,7 +160,7 @@ sub update_field { die "This action is not implemented yet"; #_update_field({ record => $record, field => $fieldName, values => \@values }); } else { - _update_subfield({ record => $record, field => $fieldName, subfield => $subfieldName, values => \@values }); + _update_subfield({ record => $record, field => $fieldName, subfield => $subfieldName, values => \@values, field_numbers => $field_numbers }); } } @@ -194,9 +197,16 @@ sub _update_subfield { my $subfieldName = $params->{subfield}; my @values = @{ $params->{values} }; my $dont_erase = $params->{dont_erase}; + my $field_numbers = $params->{field_numbers} // []; my $i = 0; - if ( my @fields = $record->field( $fieldName ) ) { + my @fields = $record->field( $fieldName ); + + if ( @$field_numbers ) { + @fields = map { $_ <= @fields ? $fields[ $_ - 1 ] : () } @$field_numbers; + } + + if ( @fields ) { unless ( $dont_erase ) { @values = ($values[0]) x scalar( @fields ) if @values == 1; @@ -236,12 +246,12 @@ sub read_field { my $record = $params->{record}; my $fieldName = $params->{field}; my $subfieldName = $params->{subfield}; - my $n = $params->{n}; + my $field_numbers = $params->{field_numbers} // []; if ( not $subfieldName or $subfieldName eq '' ) { - _read_field({ record => $record, field => $fieldName, n => $n }); + _read_field({ record => $record, field => $fieldName, field_numbers => $field_numbers }); } else { - _read_subfield({ record => $record, field => $fieldName, subfield => $subfieldName, n => $n }); + _read_subfield({ record => $record, field => $fieldName, subfield => $subfieldName, field_numbers => $field_numbers }); } } @@ -249,7 +259,7 @@ sub _read_field { my ( $params ) = @_; my $record = $params->{record}; my $fieldName = $params->{field}; - my $n = $params->{n}; + my $field_numbers = $params->{field_numbers} // []; my @fields = $record->field( $fieldName ); @@ -259,10 +269,12 @@ sub _read_field { if $fieldName < 10; my @values; - if ( $n ) { - if ( $n <= scalar( @fields ) ) { - for my $sf ( $fields[$n - 1]->subfields ) { - push @values, $sf->[1]; + if ( @$field_numbers ) { + for my $field_number ( @$field_numbers ) { + if ( $field_number <= scalar( @fields ) ) { + for my $sf ( $fields[$field_number - 1]->subfields ) { + push @values, $sf->[1]; + } } } } else { @@ -281,7 +293,7 @@ sub _read_subfield { my $record = $params->{record}; my $fieldName = $params->{field}; my $subfieldName = $params->{subfield}; - my $n = $params->{n}; + my $field_numbers = $params->{field_numbers} // []; my @fields = $record->field( $fieldName ); @@ -293,16 +305,18 @@ sub _read_subfield { push( @values, @sf ); } - return $n - ? $values[$n-1] - : @values; + if ( @values and @$field_numbers ) { + @values = map { $_ <= @values ? $values[ $_ - 1 ] : () } @$field_numbers; + } + + return @values; } =head2 field_exists - $bool = field_exists( $record, $fieldName[, $subfieldName ]); + @field_numbers = field_exists( $record, $fieldName[, $subfieldName ]); - Returns true if the field exits, false otherwise. + Returns the field numbers or an empty array. =cut @@ -314,28 +328,30 @@ sub field_exists { if ( ! $record ) { return; } - my $return = 0; - if ( $fieldName && $subfieldName ) { - $return = $record->field( $fieldName ) && $record->subfield( $fieldName, $subfieldName ); - } elsif ( $fieldName ) { - $return = $record->field( $fieldName ) && 1; + my @field_numbers = (); + my $current_field_number = 1; + for my $field ( $record->field( $fieldName ) ) { + if ( $subfieldName ) { + push @field_numbers, $current_field_number + if $field->subfield( $subfieldName ); + } else { + push @field_numbers, $current_field_number; + } + $current_field_number++; } - return $return; + return \@field_numbers; } =head2 field_equals - $bool = field_equals( $record, $value, $fieldName[, $subfieldName[, $regex [, $n ] ] ]); + $bool = field_equals( $record, $value, $fieldName[, $subfieldName[, $regex ] ]); Returns true if the field equals the given value, false otherwise. If a regular expression ( $regex ) is supplied, the value will be compared using the given regex. Example: $regex = 'sought_text' - If $n is passed, the Nth field of a repeatable series will be used for comparison. - Set $n to 1 or leave empty for a non-repeatable field. - =cut sub field_equals { @@ -345,19 +361,32 @@ sub field_equals { my $fieldName = $params->{field}; my $subfieldName = $params->{subfield}; my $regex = $params->{regex}; - my $n = $params->{n}; - $n = 1 unless ( $n ); ## $n defaults to first field of a repeatable field series if ( ! $record ) { return; } - my @field_values = read_field({ record => $record, field => $fieldName, subfield => $subfieldName, n => $n }); - my $field_value = $field_values[$n-1]; - - if ( $regex ) { - return $field_value =~ m/$value/; - } else { - return $field_value eq $value; + my @field_numbers = (); + my $current_field_number = 1; + FIELDS: for my $field ( $record->field( $fieldName ) ) { + my @subfield_values = $subfieldName + ? $field->subfield( $subfieldName ) + : map { $_->[1] } $field->subfields; + + SUBFIELDS: for my $subfield_value ( @subfield_values ) { + if ( + ( + $regex and $subfield_value =~ m/$value/ + ) or ( + $subfield_value eq $value + ) + ) { + push @field_numbers, $current_field_number; + last SUBFIELDS; + } + } + $current_field_number++; } + + return \@field_numbers; } =head2 move_field @@ -381,7 +410,7 @@ sub move_field { my $toFieldName = $params->{to_field}; my $toSubfieldName = $params->{to_subfield}; my $regex = $params->{regex}; - my $n = $params->{n}; + my $field_numbers = $params->{field_numbers} // []; if ( not $fromSubfieldName or $fromSubfieldName eq '' or not $toSubfieldName or $toSubfieldName eq '' @@ -391,7 +420,7 @@ sub move_field { from_field => $fromFieldName, to_field => $toFieldName, regex => $regex, - n => $n, + field_numbers => $field_numbers, }); } else { _move_subfield({ @@ -401,7 +430,7 @@ sub move_field { to_field => $toFieldName, to_subfield => $toSubfieldName, regex => $regex, - n => $n, + field_numbers => $field_numbers, }); } } @@ -412,13 +441,14 @@ sub _move_field { my $fromFieldName = $params->{from_field}; my $toFieldName = $params->{to_field}; my $regex = $params->{regex}; - my $n = $params->{n}; + my $field_numbers = $params->{field_numbers} // []; + _copy_move_field({ record => $record, from_field => $fromFieldName, to_field => $toFieldName, regex => $regex, - n => $n, + field_numbers => $field_numbers, action => 'move', }); } @@ -431,11 +461,13 @@ sub _move_subfield { my $toFieldName = $params->{to_field}; my $toSubfieldName = $params->{to_subfield}; my $regex = $params->{regex}; - my $n = $params->{n}; + my $field_numbers = $params->{field_numbers} // []; # Copy my @values = read_field({ record => $record, field => $fromFieldName, subfield => $fromSubfieldName }); - @values = ( $values[$n-1] ) if $n; + if ( @$field_numbers ) { + @values = map { $_ <= @values ? $values[ $_ - 1 ] : () } @$field_numbers; + } _modify_values({ values => \@values, regex => $regex }); _update_subfield({ record => $record, field => $toFieldName, subfield => $toSubfieldName, dont_erase => 1, values => \@values }); @@ -444,7 +476,7 @@ sub _move_subfield { record => $record, field => $fromFieldName, subfield => $fromSubfieldName, - n => $n, + field_numbers => $field_numbers, }); } @@ -464,12 +496,12 @@ sub delete_field { my $record = $params->{record}; my $fieldName = $params->{field}; my $subfieldName = $params->{subfield}; - my $n = $params->{n}; + my $field_numbers = $params->{field_numbers} // []; if ( not $subfieldName or $subfieldName eq '' ) { - _delete_field({ record => $record, field => $fieldName, n => $n }); + _delete_field({ record => $record, field => $fieldName, field_numbers => $field_numbers }); } else { - _delete_subfield({ record => $record, field => $fieldName, subfield => $subfieldName, n => $n }); + _delete_subfield({ record => $record, field => $fieldName, subfield => $subfieldName, field_numbers => $field_numbers }); } } @@ -477,11 +509,13 @@ sub _delete_field { my ( $params ) = @_; my $record = $params->{record}; my $fieldName = $params->{field}; - my $n = $params->{n}; + my $field_numbers = $params->{field_numbers} // []; my @fields = $record->field( $fieldName ); - @fields = ( $fields[$n-1] ) if ( $n ); + if ( @$field_numbers ) { + @fields = map { $_ <= @fields ? $fields[ $_ - 1 ] : () } @$field_numbers; + } foreach my $field ( @fields ) { $record->delete_field( $field ); } @@ -492,11 +526,13 @@ sub _delete_subfield { my $record = $params->{record}; my $fieldName = $params->{field}; my $subfieldName = $params->{subfield}; - my $n = $params->{n}; + my $field_numbers = $params->{field_numbers} // []; my @fields = $record->field( $fieldName ); - @fields = ( $fields[$n-1] ) if ( $n ); + if ( @$field_numbers ) { + @fields = map { $_ <= @fields ? $fields[ $_ - 1 ] : () } @$field_numbers; + } foreach my $field ( @fields ) { $field->delete_subfield( code => $subfieldName ); @@ -510,12 +546,12 @@ sub _copy_move_field { my $fromFieldName = $params->{from_field}; my $toFieldName = $params->{to_field}; my $regex = $params->{regex}; - my $n = $params->{n}; + my $field_numbers = $params->{field_numbers}; my $action = $params->{action} || 'copy'; my @fields = $record->field( $fromFieldName ); - if ( $n and $n <= scalar( @fields ) ) { - @fields = ( $fields[$n - 1] ); + if ( @$field_numbers ) { + @fields = map { $_ <= @fields ? $fields[ $_ - 1 ] : () } @$field_numbers; } for my $field ( @fields ) { diff --git a/t/SimpleMARC.t b/t/SimpleMARC.t index fc6ad5f172..6b2a316973 100644 --- a/t/SimpleMARC.t +++ b/t/SimpleMARC.t @@ -41,14 +41,14 @@ sub new_record { subtest 'field_exists' => sub { plan tests => 3; my $record = new_record; - is( + is_deeply( field_exists( { record => $record, field => '650', subfield => 'a' } ), - 'Computer programming.', + [1], '650$a exists' ); - is( + is_deeply( field_exists( { record => $record, field => '650', subfield => 'b' } ), - undef, + [], '650$b does not exist' ); @@ -60,10 +60,10 @@ subtest 'field_exists' => sub { ) ); - is( + is_deeply( field_exists( { record => $record, field => '650', subfield => 'a' } ), - 'Computer programming.', - '650$a exists, field_exists returns the first one' + [ 1, 2 ], + '650$a exists, field_exists returns the 2 field numbers' ); }; @@ -83,42 +83,48 @@ subtest 'read_field' => sub { my @fields_650a = read_field( { record => $record, field => '650', subfield => 'a' } ); - is( $fields_650a[0], 'Computer programming.', 'first 650$a' ); - is( $fields_650a[1], 'Computer algorithms.', 'second 650$a' ); - is( - read_field( - { - record => $record, - field => '650', - subfield => 'a', - n => 1 - } - ), - 'Computer programming.', + is_deeply( $fields_650a[0], 'Computer programming.', 'first 650$a' ); + is_deeply( $fields_650a[1], 'Computer algorithms.', 'second 650$a' ); + is_deeply( + [ + read_field( + { + record => $record, + field => '650', + subfield => 'a', + field_numbers => [1] + } + ) + ], + ['Computer programming.'], 'first 650$a bis' ); - is( - read_field( - { - record => $record, - field => '650', - subfield => 'a', - n => 2 - } - ), - 'Computer algorithms.', + is_deeply( + [ + read_field( + { + record => $record, + field => '650', + subfield => 'a', + field_numbers => [2] + } + ) + ], + ['Computer algorithms.'], 'second 650$a bis' ); - is( - read_field( - { - record => $record, - field => '650', - subfield => 'a', - n => 3 - } - ), - undef, + is_deeply( + [ + read_field( + { + record => $record, + field => '650', + subfield => 'a', + field_numbers => [3] + } + ) + ], + [], 'There is no 3 650$a' ); }; @@ -148,9 +154,9 @@ subtest 'read_field' => sub { [ read_field( { - record => $record, - field => '650', - n => 1 + record => $record, + field => '650', + field_numbers => [1] } ) ], @@ -158,12 +164,20 @@ subtest 'read_field' => sub { 'Get the all subfield values for the first field 650' ); is_deeply( - [ read_field( { record => $record, field => '650', n => 2 } ) ], + [ + read_field( + { record => $record, field => '650', field_numbers => [2] } + ) + ], [ 'Computer algorithms.', '463' ], 'Get the all subfield values for the second field 650' ); is_deeply( - [ read_field( { record => $record, field => '650', n => 3 } ) ], + [ + read_field( + { record => $record, field => '650', field_numbers => [3] } + ) + ], [], 'Get the all subfield values for the third field 650 which does not exist' ); @@ -186,10 +200,12 @@ subtest 'update_field' => sub { } ); is_deeply( - read_field( - { record => $record, field => '952', subfield => 'p' } - ), - '3010023918', + [ + read_field( + { record => $record, field => '952', subfield => 'p' } + ) + ], + ['3010023918'], 'update existing subfield 952$p' ); delete_field( { record => $record, field => '952' } ); @@ -210,10 +226,12 @@ subtest 'update_field' => sub { } ); is_deeply( - read_field( - { record => $record, field => '952', subfield => 'p' } - ), - '3010023918', + [ + read_field( + { record => $record, field => '952', subfield => 'p' } + ) + ], + ['3010023918'], 'create subfield 952$p' ); is_deeply( @@ -288,25 +306,29 @@ subtest 'copy_field' => sub { } ); is_deeply( - read_field( - { record => $record, field => '245', subfield => 'a' } - ), - 'The art of computer programming', + [ + read_field( + { record => $record, field => '245', subfield => 'a' } + ) + ], + ['The art of computer programming'], 'After copy 245$a still exists' ); is_deeply( - read_field( - { record => $record, field => '246', subfield => 'a' } - ), - 'The art of computer programming', + [ + read_field( + { record => $record, field => '246', subfield => 'a' } + ) + ], + ['The art of computer programming'], '246$a is a new field' ); delete_field( { record => $record, field => '246' } ); - is( + is_deeply( field_exists( { record => $record, field => '246', subfield => 'a' } ), - undef, + [], '246$a does not exist anymore' ); @@ -335,14 +357,16 @@ subtest 'copy_field' => sub { from_subfield => 'a', to_field => '651', to_subfield => 'a', - n => 1 + field_numbers => [1] } ); is_deeply( - read_field( - { record => $record, field => '651', subfield => 'a' } - ), - 'Computer programming.', + [ + read_field( + { record => $record, field => '651', subfield => 'a' } + ) + ], + ['Computer programming.'], 'Copy first field 650$a' ); delete_field( { record => $record, field => '652' } ); @@ -354,14 +378,16 @@ subtest 'copy_field' => sub { from_subfield => 'a', to_field => '651', to_subfield => 'a', - n => 2 + field_numbers => [2] } ); is_deeply( - read_field( - { record => $record, field => '651', subfield => 'a' } - ), - 'Computer algorithms.', + [ + read_field( + { record => $record, field => '651', subfield => 'a' } + ) + ], + ['Computer algorithms.'], 'Copy second field 650$a' ); delete_field( { record => $record, field => '651' } ); @@ -430,7 +456,7 @@ subtest 'copy_field' => sub { to_field => '651', to_subfield => 'a', regex => { search => 'Computer', replace => 'The art of' }, - n => 1 + field_numbers => [1] } ); @fields_651a = @@ -574,10 +600,12 @@ subtest 'copy_field' => sub { } ); is_deeply( - read_field( - { record => $record, field => '245', subfield => 'a' } - ), - 'BEGIN The art of computer programming', + [ + read_field( + { record => $record, field => '245', subfield => 'a' } + ) + ], + ['BEGIN The art of computer programming'], 'Update a subfield: add a string at the beginning' ); @@ -593,10 +621,12 @@ subtest 'copy_field' => sub { } ); is_deeply( - read_field( - { record => $record, field => '245', subfield => 'a' } - ), - 'The art of computer programming END', + [ + read_field( + { record => $record, field => '245', subfield => 'a' } + ) + ], + ['The art of computer programming END'], 'Update a subfield: add a string at the end' ); @@ -618,22 +648,38 @@ subtest 'copy_field' => sub { { record => $record, from_field => '952', to_field => '953' } ); my @fields_952 = read_field( { record => $record, field => '952' } ); is_deeply( - [ read_field( { record => $record, field => '952', n => 1 } ) ], + [ + read_field( + { record => $record, field => '952', field_numbers => [1] } + ) + ], [ '3010023917', 'BK', 'GEN', '2001-06-25' ], "copy all: original first field still exists" ); is_deeply( - [ read_field( { record => $record, field => '952', n => 2 } ) ], + [ + read_field( + { record => $record, field => '952', field_numbers => [2] } + ) + ], [ '3010023918', 'CD' ], "copy all: original second field still exists" ); is_deeply( - [ read_field( { record => $record, field => '953', n => 1 } ) ], + [ + read_field( + { record => $record, field => '953', field_numbers => [1] } + ) + ], [ '3010023917', 'BK', 'GEN', '2001-06-25' ], "copy all: first original fields has been copied" ); is_deeply( - [ read_field( { record => $record, field => '953', n => 2 } ) ], + [ + read_field( + { record => $record, field => '953', field_numbers => [2] } + ) + ], [ '3010023918', 'CD' ], "copy all: second original fields has been copied" ); @@ -641,19 +687,27 @@ subtest 'copy_field' => sub { #- copy only the first field copy_field( { - record => $record, - from_field => '953', - to_field => '954', - n => 1 + record => $record, + from_field => '953', + to_field => '954', + field_numbers => [1] } ); is_deeply( - [ read_field( { record => $record, field => '953', n => 1 } ) ], + [ + read_field( + { record => $record, field => '953', field_numbers => [1] } + ) + ], [ '3010023917', 'BK', 'GEN', '2001-06-25' ], "copy first: first original fields has been copied" ); is_deeply( - [ read_field( { record => $record, field => '953', n => 2 } ) ], + [ + read_field( + { record => $record, field => '953', field_numbers => [2] } + ) + ], [ '3010023918', 'CD' ], "copy first: second original fields has been copied" ); @@ -682,22 +736,38 @@ subtest 'copy_field' => sub { } ); is_deeply( - [ read_field( { record => $record, field => '952', n => 1 } ) ], + [ + read_field( + { record => $record, field => '952', field_numbers => [1] } + ) + ], [ '3010023917', 'BK', 'GEN', '2001-06-25' ], "copy all with regex: original first field still exists" ); is_deeply( - [ read_field( { record => $record, field => '952', n => 2 } ) ], + [ + read_field( + { record => $record, field => '952', field_numbers => [2] } + ) + ], [ '3010023918', 'CD' ], "copy all with regex: original second field still exists" ); is_deeply( - [ read_field( { record => $record, field => '953', n => 1 } ) ], + [ + read_field( + { record => $record, field => '953', field_numbers => [1] } + ) + ], [ '4242423917', 'BK', 'GEN', '2001-06-25' ], "copy all wirh regex: first original fields has been copied" ); is_deeply( - [ read_field( { record => $record, field => '953', n => 2 } ) ], + [ + read_field( + { record => $record, field => '953', field_numbers => [2] } + ) + ], [ '4242423918', 'CD' ], "copy all with regex: second original fields has been copied" ); @@ -752,7 +822,7 @@ subtest 'move_field' => sub { from_subfield => 'p', to_field => '954', to_subfield => 'p', - n => 1 + field_numbers => [1] } ); # Move the first field my @fields_952p = @@ -825,12 +895,20 @@ subtest 'move_field' => sub { is_deeply( [ read_field( { record => $record, field => '952' } ) ], [], "original fields don't exist" ); is_deeply( - [ read_field( { record => $record, field => '953', n => 1 } ) ], + [ + read_field( + { record => $record, field => '953', field_numbers => [1] } + ) + ], [ '3010023917', 'BK', 'GEN', '2001-06-25' ], "first original fields has been copied" ); is_deeply( - [ read_field( { record => $record, field => '953', n => 2 } ) ], + [ + read_field( + { record => $record, field => '953', field_numbers => [2] } + ) + ], [ '3010023917', 'BK' ], "second original fields has been copied" ); @@ -838,10 +916,10 @@ subtest 'move_field' => sub { #- Move only the first field move_field( { - record => $record, - from_field => '953', - to_field => '954', - n => 1 + record => $record, + from_field => '953', + to_field => '954', + field_numbers => [1] } ); is_deeply( @@ -876,12 +954,20 @@ subtest 'move_field' => sub { is_deeply( [ read_field( { record => $record, field => '952' } ) ], [], "use a regex, original fields don't exist" ); is_deeply( - [ read_field( { record => $record, field => '953', n => 1 } ) ], + [ + read_field( + { record => $record, field => '953', field_numbers => [1] } + ) + ], [ '3010023917', 'DVD', 'GEN', '2001-06-25' ], "use a regex, first original fields has been copied" ); is_deeply( - [ read_field( { record => $record, field => '953', n => 2 } ) ], + [ + read_field( + { record => $record, field => '953', field_numbers => [2] } + ) + ], [ '3010023917', 'DVD' ], "use a regex, second original fields has been copied" ); @@ -916,7 +1002,13 @@ subtest 'delete_field' => sub { ); delete_field( - { record => $record, field => '952', subfield => 'p', n => 1 } ); + { + record => $record, + field => '952', + subfield => 'p', + field_numbers => [1] + } + ); my @fields_952p = read_field( { record => $record, field => '952', subfield => 'p' } ); is_deeply( \@fields_952p, ['3010023917'], 'Delete first 952$p' ); -- 2.39.5