From 63cbcbff417a190c3719449eb8cb33099be78350 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Fri, 29 Mar 2013 17:29:27 +0100 Subject: [PATCH] Bug 8015: Add unit tests for SimpleMARC and MarcModificationTemplates routines Signed-off-by: Kyle M Hall --- C4/Koha.pm | 2 +- Koha/SimpleMARC.pm | 51 +------- t/SimpleMARC.t | 188 ++++++++++++++++++++++++++++ t/db_dependent/MarcModificationTemplates.t | 125 ++++++++++++++++++ tools/marc_modification_templates.pl | 2 +- 5 files changed, 320 insertions(+), 48 deletions(-) create mode 100644 t/SimpleMARC.t create mode 100644 t/db_dependent/MarcModificationTemplates.t diff --git a/C4/Koha.pm b/C4/Koha.pm index ddc39ce..e2ca4a4 100644 --- a/C4/Koha.pm +++ b/C4/Koha.pm @@ -1480,7 +1480,7 @@ sub _isbn_cleanup { sub Log { my ($data) = @_; warn $data; - open my $fh, '>>/tmp/koha.log'; + open my $fh, '>>', '/tmp/koha.log'; print $fh "$data\n"; close $fh; } diff --git a/Koha/SimpleMARC.pm b/Koha/SimpleMARC.pm index f72fc2f..ef40694 100644 --- a/Koha/SimpleMARC.pm +++ b/Koha/SimpleMARC.pm @@ -114,16 +114,13 @@ sub update_field { if ( ! ( $record && $fieldName ) ) { return; } - if ( @values eq 1 ) { - _update_repeatable_field_with_single_value( $record, $fieldName, $subfieldName, @values ); - return; - } - my $i = 0; my $field; if ( $subfieldName ) { if ( my @fields = $record->field( $fieldName ) ) { unless ( $dont_erase ) { + @values = ($values[0]) x scalar( @fields ) + if @values == 1; foreach my $field ( @fields ) { $field->update( "$subfieldName" => $values[$i++] ); } @@ -144,6 +141,8 @@ sub update_field { } } else { ## No subfield if ( my @fields = $record->field( $fieldName ) ) { + @values = ($values[0]) x scalar( @fields ) + if @values == 1; foreach my $field ( @fields ) { $field->update( $values[$i++] ); } @@ -262,7 +261,7 @@ sub field_equals { sub move_field { my ( $record, $fromFieldName, $fromSubfieldName, $toFieldName, $toSubfieldName, $regex, $n ) = @_; C4::Koha::Log( "C4::SimpleMARC::move_field( '$record', '$fromFieldName', '$fromSubfieldName', '$toFieldName', '$toSubfieldName', '$regex', '$n' )" ) if $debug; - copy_field( $record, $fromFieldName, $fromSubfieldName, $toFieldName, $toSubfieldName, $regex, $n , 1); + copy_field( $record, $fromFieldName, $fromSubfieldName, $toFieldName, $toSubfieldName, $regex, $n , 'dont_erase' ); delete_field( $record, $fromFieldName, $fromSubfieldName, $n ); } @@ -296,45 +295,5 @@ sub delete_field { } } -=head2 _update_repeatable_field_with_single_value - - _update_repeatable_field_with_single_value( $record, $fieldName, $subfieldName, $value ); - - Updates a repeatable field, giving all existing copies of that field the given value. - - This is an internal function, and thus is not exported. - -=cut - -sub _update_repeatable_field_with_single_value { - my ( $record, $fieldName, $subfieldName, $value ) = @_; - C4::Koha::Log( "C4::SimpleMARC::_update_repeatable_field_with_single_value( $record, $fieldName, $subfieldName, $value )" ) if $debug; - - if ( ! ( $record && $fieldName ) ) { return; } - - my $field; - if ( $subfieldName ) { - if ( my @fields = $record->field( $fieldName ) ) { - foreach my $field ( @fields ) { - $field->update( $subfieldName => $value ); - } - } else { - ## Field does not exist, create it. - $field = MARC::Field->new( $fieldName, undef, undef, $subfieldName => $value ); - $record->append_fields( $field ); - } - } else { ## No subfield - if ( my @fields = $record->field( $fieldName ) ) { - foreach my $field ( @fields ) { - $field->update( $value ); - } - } else { - ## Field does not exists, create it - $field = MARC::Field->new( $fieldName, $value ); - $record->append_fields( $field ); - } - } -} - 1; __END__ diff --git a/t/SimpleMARC.t b/t/SimpleMARC.t new file mode 100644 index 0000000..cfba615 --- /dev/null +++ b/t/SimpleMARC.t @@ -0,0 +1,188 @@ +use Modern::Perl; + +use Test::More;#tests => 1; + +use Data::Dumper; + +use_ok("MARC::Field"); +use_ok("MARC::Record"); +use_ok("Koha::SimpleMARC"); + + +sub new_record { + my $record = MARC::Record->new; + $record->leader('03174nam a2200445 a 4500'); + my @fields = ( + MARC::Field->new( + 100, '1', ' ', + a => 'Knuth, Donald Ervin', + d => '1938', + ), + MARC::Field->new( + 245, '1', '4', + a => 'The art of computer programming', + c => 'Donald E. Knuth.', + ), + MARC::Field->new( + 650, ' ', '0', + a => 'Computer programming.', + 9 => '462', + ), + MARC::Field->new( + 952, ' ', ' ', + p => '3010023917', + y => 'BK', + c => 'GEN', + d => '2001-06-25', + ), + ); + $record->append_fields(@fields); + return $record; +} + +my $record = new_record; + +# field_exists +is( field_exists( $record, '650', 'a'), 'Computer programming.', '650$a exists' ); +is( field_exists( $record, '650', 'b'), undef, '650$b does not exist' ); + +$record->append_fields( + MARC::Field->new( + 650, ' ', '0', + a => 'Computer algorithms.', + 9 => '463', + ) +); + +is( field_exists( $record, '650', 'a'), 'Computer programming.', '650$a exists, field_exists returns the first one' ); + +# read_field +my @fields_650a = read_field( $record, '650', 'a'); +is( $fields_650a[0], 'Computer programming.', 'first 650$a' ); +is( $fields_650a[1], 'Computer algorithms.', 'second 650$a' ); +is( read_field( $record, '650', 'a', 1 ), 'Computer programming.', 'first 650$a bis' ); +is( read_field( $record, '650', 'a', 2 ), 'Computer algorithms.', 'second 650$a bis' ); +is( read_field( $record, '650', 'a', 3 ), undef, 'There is no 3 650$a' ); + +# copy_field +copy_field( $record, '245', 'a', '246', 'a' ); +is_deeply( read_field( $record, '245', 'a' ), 'The art of computer programming', 'After copy 245$a still exists' ); +is_deeply( read_field( $record, '246', 'a' ), 'The art of computer programming', '246$a is a new field' ); +delete_field( $record, '246' ); +is( field_exists( $record, '246', 'a', '246$a does not exist anymore' ), undef ); + +copy_field( $record, '650', 'a', '651', 'a' ); +my @fields_651a = read_field( $record, '651', 'a' ); +is_deeply( \@fields_651a, ['Computer programming.', 'Computer algorithms.'], 'Copy multivalued field' ); +delete_field( $record, '651' ); + +copy_field( $record, '650', 'a', '651', 'a', undef, 1 ); +@fields_651a = read_field( $record, '651', 'a' ); +is_deeply( read_field( $record, '651', 'a' ), 'Computer programming.', 'Copy first field 650$a' ); +delete_field( $record, '651' ); + +copy_field( $record, '650', 'a', '651', 'a', undef, 2 ); +@fields_651a = read_field( $record, '651', 'a' ); +is_deeply( read_field( $record, '651', 'a' ), 'Computer algorithms.', 'Copy second field 650$a' ); +delete_field( $record, '651' ); + +copy_field( $record, '650', 'a', '651', 'a', '/Computer/The art of/' ); +@fields_651a = read_field( $record, '651', 'a' ); +is_deeply( \@fields_651a, ['The art of programming.', 'The art of algorithms.'], 'Copy field using regex' ); + +copy_field( $record, '650', 'a', '651', 'a', '/Computer/The mistake of/' ); +@fields_651a = read_field( $record, '651', 'a' ); +is_deeply( \@fields_651a, ['The mistake of programming.', 'The mistake of algorithms.'], 'Copy fields using regex on existing fields' ); +delete_field( $record, '651' ); + +copy_field( $record, '650', 'a', '651', 'a', '/Computer/The art of/' ); +copy_field( $record, '650', 'a', '651', 'a', '/Computer/The mistake of/', 1, "dont_erase" ); +@fields_651a = read_field( $record, '651', 'a' ); +is_deeply( \@fields_651a, [ + 'The art of programming.', + 'The mistake of programming.', + 'The art of algorithms.', + 'The mistake of programming.' +], 'Copy first field using regex on existing fields without erase existing values' ); +delete_field( $record, '651' ); + +copy_field( $record, '650', 'a', '651', 'a', '/Computer/The art of/' ); +copy_field( $record, '650', 'a', '651', 'a', '/Computer/The mistake of/', undef , "dont_erase" ); +@fields_651a = read_field( $record, '651', 'a' ); +is_deeply( \@fields_651a, [ + 'The art of programming.', + 'The mistake of programming.', + 'The mistake of algorithms.', + 'The art of algorithms.', + 'The mistake of programming.', + 'The mistake of algorithms.' +], 'Copy fields using regex on existing fields without erase existing values' ); +delete_field( $record, '651' ); + + +# update_field +update_field( $record, '952', 'p', undef, '3010023918' ); +is_deeply( read_field( $record, '952', 'p' ), '3010023918', 'update existing subfield 952$p' ); +delete_field( $record, '952' ); +update_field( $record, '952', 'p', undef, '3010023918' ); +update_field( $record, '952', 'y', undef, 'BK' ); +is_deeply( read_field( $record, '952', 'p' ), '3010023918', 'create subfield 952$p' ); +is_deeply( read_field( $record, '952', 'y' ), 'BK', 'create subfield 952$k on existing 952 field' ); +$record->append_fields( + MARC::Field->new( + 952, ' ', ' ', + p => '3010023917', + y => 'BK', + ), +); +update_field( $record, '952', 'p', undef, '3010023919' ); +my @fields_952p = read_field( $record, '952', 'p' ); +is_deeply( \@fields_952p, ['3010023919', '3010023919'], 'update all subfields 952$p with the same value' ); + +update_field( $record, '952', 'p', undef, ('3010023917', '3010023918') ); +@fields_952p = read_field( $record, '952', 'p' ); +is_deeply( \@fields_952p, ['3010023917', '3010023918'], 'update all subfields 952$p with the different values' ); + +# move_field +$record = new_record; +my ( @fields_952d, @fields_952c, @fields_954c, @fields_954p); +$record->append_fields( + MARC::Field->new( + 952, ' ', ' ', + p => '3010023917', + y => 'BK', + ), +); +copy_field( $record, '952', 'd', '952', 'd' ); +@fields_952d = read_field( $record, '952', 'd' ); +is_deeply( \@fields_952d, ['2001-06-25', '2001-06-25'], 'copy 952$d into others 952 field' ); + +move_field( $record, '952', 'c', '954', 'c' ); +@fields_952c = read_field( $record, '952', 'c' ); +@fields_954c = read_field( $record, '954', 'c' ); +is_deeply( \@fields_952c, [], 'The 952$c has moved' ); +is_deeply( \@fields_954c, ['GEN'], 'Now 954$c exists' ); + +move_field( $record, '952', 'p', '954', 'p', undef, 1 ); # Move the first field +@fields_952p = read_field( $record, '952', 'p' ); +@fields_954p = read_field( $record, '954', 'p' ); +is_deeply( \@fields_952p, ['3010023917'], 'One of 952$p has moved' ); +is_deeply( \@fields_954p, ['3010023917'], 'Now 954$p exists' ); + +$record = new_record; +$record->append_fields( + MARC::Field->new( + 952, ' ', ' ', + p => '3010023917', + y => 'BK', + ), +); + +move_field( $record, '952', 'p', '954', 'p' ); # Move all field +@fields_952p = read_field( $record, '952', 'p' ); +@fields_954p = read_field( $record, '954', 'p' ); +is_deeply( \@fields_952p, [], 'All 952$p have moved' ); +is_deeply( \@fields_954p, ['3010023917', '3010023917'], 'Now 2 954$p exist' ); + + +done_testing; diff --git a/t/db_dependent/MarcModificationTemplates.t b/t/db_dependent/MarcModificationTemplates.t new file mode 100644 index 0000000..d253b82 --- /dev/null +++ b/t/db_dependent/MarcModificationTemplates.t @@ -0,0 +1,125 @@ +use Modern::Perl; + +use Test::More tests => 66; + +use_ok("C4::MarcModificationTemplates"); + +# Creation +my $template_id = AddModificationTemplate("template_name"); +like( $template_id, qr|^\d+$|, "new template returns an id" ); + +is( AddModificationTemplateAction( + $template_id, 'move_field', 1, + '464', 'u', '', + '464', '3', '', + '', '', '', '', '', '', '', + 'move first 464$u to 464$3' +), 1, "Add first action"); + +is( AddModificationTemplateAction( + $template_id, 'update_field', 0, + '099', 't', 'LIV', + '', '', '', + 'if', '200', 'b', 'equals', 'Text', '', + 'Update field 099$t with value LIV if 200$b matches "Text"' +), 1, "Add second action"); + +is( AddModificationTemplateAction( + $template_id, 'copy_field', 0, + '606', 'a', '', + '607', 'a', '', + 'unless', '606', 'a', 'not_equals', '^AJAX', '1', + 'Copy field 606$a to 607$a unless 606$a matches RegEx m^AJAX' +), 1, "Add third action"); + +# Getter +my @actions = GetModificationTemplateActions( $template_id ); +is( @actions, 3, "3 actions are insered"); + +for my $action ( @actions ) { + isnt( GetModificationTemplateAction( $action->{mmta_id} ), undef, "action with id $action->{mmta_id} exists" ); +} + +my $first_action = $actions[0]; +is( $first_action->{ordering}, 1, "test ordering for first action" ); +is( $first_action->{action}, 'move_field', "test action for first action" ); +is( $first_action->{from_field}, '464', "test from_field for first action" ); +is( $first_action->{from_subfield}, 'u', "test from_subfield for first action" ); +is( $first_action->{to_field}, '464', "test to_field for first action" ); +is( $first_action->{to_subfield}, '3', "test to_subfield for first action" ); + +my $second_action = $actions[1]; +is( $second_action->{ordering}, 2, "test ordering for second action" ); +is( $second_action->{action}, 'update_field', "test action for second action" ); +is( $second_action->{from_field}, '099',"test from_field for second action" ); +is( $second_action->{from_subfield}, 't', "test from_subfield for second action" ); +is( $second_action->{field_value}, 'LIV', "test firld_value for second action" ); +is( $second_action->{to_field}, '', "test to_field for second action" ); +is( $second_action->{to_subfield}, '', "test to_subfield for second action" ); +is( $second_action->{conditional}, 'if', "test conditional for second action" ); +is( $second_action->{conditional_field}, '200', "test conditional_field for second action" ); +is( $second_action->{conditional_subfield}, 'b', "test conditional_subfield for second action" ); +is( $second_action->{conditional_comparison}, 'equals', "test conditional_comparison for second action" ); + +my $third_action = $actions[2]; +is( $third_action->{ordering}, 3, "test ordering for third action" ); +is( $third_action->{action}, 'copy_field', "test factionor third action" ); +is( $third_action->{from_field}, '606', "test from_field for third action" ); +is( $third_action->{from_subfield}, 'a', "test from_subfield for third action" ); +is( $third_action->{to_field}, '607', "test to_field for third action" ); +is( $third_action->{to_subfield}, 'a', "test to_subfield for third action" ); +is( $third_action->{conditional}, 'unless', "test conditional for third action" ); +is( $third_action->{conditional_field}, '606', "test conditional_field for third action" ); +is( $third_action->{conditional_subfield}, 'a', "test conditional_subfield for third action" ); +is( $third_action->{conditional_comparison}, 'not_equals', "test conditional_comparison for third action" ); +is( $third_action->{conditional_value}, '^AJAX', "test conditional_value for third action" ); + + +# Modifications +is( ModModificationTemplateAction( + $actions[1]->{mmta_id}, 'update_field', 0, + '100', 'u', 'LIV', + '', '', '', + 'if', '200', 'c', 'equals', 'Text', '', + 'Update field 099$t with value LIV if 200$b matches "Text"' +), 1, "Modify second action"); + +$second_action = GetModificationTemplateAction( $actions[1]->{mmta_id} ); +is( $second_action->{ordering}, 2, "test ordering for second action modified" ); +is( $second_action->{action}, 'update_field', "test action for second action modified" ); +is( $second_action->{from_field}, '100',"test from_field for second action modified" ); +is( $second_action->{from_subfield}, 'u', "test from_subfield for second action modified" ); +is( $second_action->{field_value}, 'LIV', "test firld_value for second action modified" ); +is( $second_action->{to_field}, '', "test to_field for second action modified" ); +is( $second_action->{to_subfield}, '', "test to_subfield for second action modified" ); +is( $second_action->{conditional}, 'if', "test conditional for second action modified" ); +is( $second_action->{conditional_field}, '200', "test conditional_field for second action modified" ); +is( $second_action->{conditional_subfield}, 'c', "test conditional_subfield for second action modified" ); +is( $second_action->{conditional_comparison}, 'equals', "test conditional_comparison for second action modified" ); + +# Up and down +is( MoveModificationTemplateAction( $actions[2]->{mmta_id}, 'top' ), '1', 'Move the third action on top' ); +is( MoveModificationTemplateAction( $actions[0]->{mmta_id}, 'bottom' ), '1', 'Move the first action on bottom' ); + +is( GetModificationTemplateAction( $actions[0]->{mmta_id} )->{ordering}, '3', 'First becomes third' ); +is( GetModificationTemplateAction( $actions[1]->{mmta_id} )->{ordering}, '2', 'Second stays second' ); +is( GetModificationTemplateAction( $actions[2]->{mmta_id} )->{ordering}, '1', 'Third becomes first' ); + +is( MoveModificationTemplateAction( $actions[0]->{mmta_id}, 'up' ), '1', 'Move up the first action (was third)' ); +is( MoveModificationTemplateAction( $actions[0]->{mmta_id}, 'up' ), '1', 'Move up the first action (was second)' ); +is( MoveModificationTemplateAction( $actions[2]->{mmta_id}, 'down' ), '1', 'Move down the third action (was second)' ); + +is( GetModificationTemplateAction( $actions[0]->{mmta_id} )->{ordering}, '1', 'First becomes again first' ); +is( GetModificationTemplateAction( $actions[1]->{mmta_id} )->{ordering}, '2', 'Second stays again second' ); +is( GetModificationTemplateAction( $actions[2]->{mmta_id} )->{ordering}, '3', 'Third becomes again third' ); + +# Cleaning +is( DelModificationTemplateAction( $actions[0]->{mmta_id} ), 2, "Delete the first action, 2 others are reordered" ); +is( GetModificationTemplateAction( $actions[0]->{mmta_id} ), undef, "first action does not exist anymore" ); + +is( DelModificationTemplate( $template_id ), 2, "2 actions are deleted" ); + +is( GetModificationTemplateAction( $actions[1]->{mmta_id} ), undef, "second action does not exist anymore" ); +is( GetModificationTemplateAction( $actions[2]->{mmta_id} ), undef, "third action does not exist anymore" ); + +is( GetModificationTemplateActions( $template_id ), 0, "There is no action for deleted template" ); diff --git a/tools/marc_modification_templates.pl b/tools/marc_modification_templates.pl index dde2476..4940a44 100755 --- a/tools/marc_modification_templates.pl +++ b/tools/marc_modification_templates.pl @@ -28,7 +28,7 @@ use C4::MarcModificationTemplates; my $cgi = new CGI; -my $op = $cgi->param('op'); +my $op = $cgi->param('op') || q{}; my $template_id = $cgi->param('template_id'); my ($template, $loggedinuser, $cookie) -- 1.7.2.5