3 use Test::More tests => 9;
6 use_ok("MARC::Record");
7 use_ok("Koha::SimpleMARC");
10 my $record = MARC::Record->new;
11 $record->leader('03174nam a2200445 a 4500');
15 a => 'Knuth, Donald Ervin',
20 a => 'The art of computer programming',
21 c => 'Donald E. Knuth.',
25 a => 'Computer programming.',
36 $record->append_fields(@fields);
41 subtest 'field_exists' => sub {
43 my $record = new_record;
45 field_exists( { record => $record, field => '650', subfield => 'a' } ),
50 field_exists( { record => $record, field => '650', subfield => 'b' } ),
52 '650$b does not exist'
55 $record->append_fields(
58 a => 'Computer algorithms.',
64 field_exists( { record => $record, field => '650', subfield => 'a' } ),
66 '650$a exists, field_exists returns the 2 field numbers'
71 subtest 'read_field' => sub {
73 subtest 'read subfield' => sub {
75 my $record = new_record;
76 $record->append_fields(
79 a => 'Computer algorithms.',
85 read_field( { record => $record, field => '650', subfield => 'a' } );
86 is_deeply( $fields_650a[0], 'Computer programming.', 'first 650$a' );
87 is_deeply( $fields_650a[1], 'Computer algorithms.', 'second 650$a' );
99 ['Computer programming.'],
113 ['Computer algorithms.'],
128 'There is no 3 650$a'
131 subtest 'read field' => sub {
133 my $record = new_record;
134 $record->append_fields(
137 a => 'Computer algorithms.',
150 [ 'Computer programming.', '462', 'Computer algorithms.', '463' ],
151 'Get the all subfield values for field 650'
163 [ 'Computer programming.', '462' ],
164 'Get the all subfield values for the first field 650'
169 { record => $record, field => '650', field_numbers => [2] }
172 [ 'Computer algorithms.', '463' ],
173 'Get the all subfield values for the second field 650'
178 { record => $record, field => '650', field_numbers => [3] }
182 'Get the all subfield values for the third field 650 which does not exist'
188 subtest 'update_field' => sub {
190 subtest 'update subfield' => sub {
192 my $record = new_record;
199 values => ['3010023918']
205 { record => $record, field => '952', subfield => 'p' }
209 'update existing subfield 952$p'
211 delete_field( { record => $record, field => '952' } );
217 values => ['3010023918']
231 { record => $record, field => '952', subfield => 'p' }
235 'create subfield 952$p'
239 { record => $record, field => '952', subfield => 'y' }
242 'create subfield 952$k on existing 952 field'
245 $record->append_fields(
257 values => ['3010023919']
261 read_field( { record => $record, field => '952', subfield => 'p' } );
264 [ '3010023919', '3010023919' ],
265 'update all subfields 952$p with the same value'
273 values => [ '3010023917', '3010023918' ]
277 read_field( { record => $record, field => '952', subfield => 'p' } );
280 [ '3010023917', '3010023918' ],
281 'update all subfields 952$p with the different values'
286 # copy_field - subfield
287 subtest 'copy_field' => sub {
289 subtest 'copy subfield' => sub {
291 my $record = new_record;
292 $record->append_fields(
295 a => 'Computer algorithms.',
303 from_subfield => 'a',
311 { record => $record, field => '245', subfield => 'a' }
314 ['The art of computer programming'],
315 'After copy 245$a still exists'
320 { record => $record, field => '246', subfield => 'a' }
323 ['The art of computer programming'],
324 '246$a is a new field'
326 delete_field( { record => $record, field => '246' } );
329 { record => $record, field => '246', subfield => 'a' }
332 '246$a does not exist anymore'
339 from_subfield => 'a',
345 read_field( { record => $record, field => '651', subfield => 'a' } );
348 [ 'Computer programming.', 'Computer algorithms.' ],
349 'Copy multivalued field'
351 delete_field( { record => $record, field => '651' } );
357 from_subfield => 'a',
366 { record => $record, field => '651', subfield => 'a' }
369 ['Computer programming.'],
370 'Copy first field 650$a'
372 delete_field( { record => $record, field => '652' } );
378 from_subfield => 'a',
387 { record => $record, field => '651', subfield => 'a' }
390 ['Computer algorithms.'],
391 'Copy second field 650$a'
393 delete_field( { record => $record, field => '651' } );
399 from_subfield => 'a',
402 regex => { search => 'Computer', replace => 'The art of' }
406 read_field( { record => $record, field => '651', subfield => 'a' } );
409 [ 'The art of programming.', 'The art of algorithms.' ],
410 'Copy field using regex'
417 from_subfield => 'a',
420 regex => { search => 'Computer', replace => 'The mistake of' }
424 read_field( { record => $record, field => '651', subfield => 'a' } );
427 [ 'The mistake of programming.', 'The mistake of algorithms.' ],
428 'Copy fields using regex on existing fields'
430 delete_field( { record => $record, field => '651' } );
436 from_subfield => 'a',
439 regex => { search => 'Computer', replace => 'The art of' }
443 read_field( { record => $record, field => '651', subfield => 'a' } );
446 [ 'The art of programming.', 'The art of algorithms.', ],
447 'Copy all fields using regex'
449 delete_field( { record => $record, field => '651' } );
455 from_subfield => 'a',
458 regex => { search => 'Computer', replace => 'The art of' },
463 read_field( { record => $record, field => '651', subfield => 'a' } );
466 [ 'The art of programming.', ],
467 'Copy first field using regex'
469 delete_field( { record => $record, field => '651' } );
471 # Copy with regex modifiers
472 $record = new_record;
473 $record->append_fields(
476 a => 'Computer algorithms.',
484 from_subfield => 'a',
487 regex => { search => 'o', replace => 'foo' }
491 read_field( { record => $record, field => '652', subfield => 'a' } );
494 [ 'Cfoomputer programming.', 'Cfoomputer algorithms.' ],
495 'Copy field using regex'
502 from_subfield => 'a',
505 regex => { search => 'o', replace => 'foo', modifiers => 'g' }
509 read_field( { record => $record, field => '653', subfield => 'a' } );
512 [ 'Cfoomputer prfoogramming.', 'Cfoomputer algfoorithms.' ],
513 'Copy field using regex'
520 from_subfield => 'a',
523 regex => { search => 'O', replace => 'foo', modifiers => 'i' }
527 read_field( { record => $record, field => '654', subfield => 'a' } );
530 [ 'Cfoomputer programming.', 'Cfoomputer algorithms.' ],
531 'Copy field using regex'
538 from_subfield => 'a',
541 regex => { search => 'O', replace => 'foo', modifiers => 'gi' }
545 read_field( { record => $record, field => '655', subfield => 'a' } );
548 [ 'Cfoomputer prfoogramming.', 'Cfoomputer algfoorithms.' ],
549 'Copy field using regex'
552 $record->append_fields(
564 from_subfield => 'd',
570 read_field( { record => $record, field => '952', subfield => 'd' } );
573 [ '2001-06-25', '2001-06-25' ],
574 'copy 952$d into others 952 field'
581 from_subfield => '1',
587 read_field( { record => $record, field => '999', subfield => '9' } );
588 is_deeply( \@fields_9999, [],
589 'copy a nonexistent subfield does not create a new one' );
591 $record = new_record;
596 from_subfield => 'a',
599 regex => { search => '^', replace => 'BEGIN ' }
605 { record => $record, field => '245', subfield => 'a' }
608 ['BEGIN The art of computer programming'],
609 'Update a subfield: add a string at the beginning'
612 $record = new_record;
617 from_subfield => 'a',
620 regex => { search => '$', replace => ' END' }
626 { record => $record, field => '245', subfield => 'a' }
629 ['The art of computer programming END'],
630 'Update a subfield: add a string at the end'
635 subtest 'copy field' => sub {
637 my $record = new_record;
638 $record->append_fields(
648 { record => $record, from_field => '952', to_field => '953' } );
649 my @fields_952 = read_field( { record => $record, field => '952' } );
653 { record => $record, field => '952', field_numbers => [1] }
656 [ '3010023917', 'BK', 'GEN', '2001-06-25' ],
657 "copy all: original first field still exists"
662 { record => $record, field => '952', field_numbers => [2] }
665 [ '3010023918', 'CD' ],
666 "copy all: original second field still exists"
671 { record => $record, field => '953', field_numbers => [1] }
674 [ '3010023917', 'BK', 'GEN', '2001-06-25' ],
675 "copy all: first original fields has been copied"
680 { record => $record, field => '953', field_numbers => [2] }
683 [ '3010023918', 'CD' ],
684 "copy all: second original fields has been copied"
687 #- copy only the first field
699 { record => $record, field => '953', field_numbers => [1] }
702 [ '3010023917', 'BK', 'GEN', '2001-06-25' ],
703 "copy first: first original fields has been copied"
708 { record => $record, field => '953', field_numbers => [2] }
711 [ '3010023918', 'CD' ],
712 "copy first: second original fields has been copied"
715 [ read_field( { record => $record, field => '954' } ) ],
716 [ '3010023917', 'BK', 'GEN', '2001-06-25' ],
717 "copy first: only first, first 953 has been copied"
720 $record = new_record;
721 $record->append_fields(
729 #- copy all fields and modify values using a regex
735 regex => { search => '30100', replace => '42424' }
741 { record => $record, field => '952', field_numbers => [1] }
744 [ '3010023917', 'BK', 'GEN', '2001-06-25' ],
745 "copy all with regex: original first field still exists"
750 { record => $record, field => '952', field_numbers => [2] }
753 [ '3010023918', 'CD' ],
754 "copy all with regex: original second field still exists"
759 { record => $record, field => '953', field_numbers => [1] }
762 [ '4242423917', 'BK', 'GEN', '2001-06-25' ],
763 "copy all wirh regex: first original fields has been copied"
768 { record => $record, field => '953', field_numbers => [2] }
771 [ '4242423918', 'CD' ],
772 "copy all with regex: second original fields has been copied"
782 read_field( { record => $record, field => '999', subfield => '9' } );
783 is_deeply( \@fields_9999, [],
784 'copy a nonexistent field does not create a new one' );
788 # move_field - subfields
789 subtest 'move_field' => sub {
791 subtest 'move subfield' => sub {
793 my $record = new_record;
794 my ( @fields_952d, @fields_952c, @fields_954c, @fields_954p );
795 $record->append_fields(
806 from_subfield => 'c',
812 read_field( { record => $record, field => '952', subfield => 'c' } );
814 read_field( { record => $record, field => '954', subfield => 'c' } );
815 is_deeply( \@fields_952c, [], 'The 952$c has moved' );
816 is_deeply( \@fields_954c, ['GEN'], 'Now 954$c exists' );
822 from_subfield => 'p',
827 ); # Move the first field
829 read_field( { record => $record, field => '952', subfield => 'p' } );
831 read_field( { record => $record, field => '954', subfield => 'p' } );
832 is_deeply( \@fields_952p, ['3010023917'], 'One of 952$p has moved' );
833 is_deeply( \@fields_954p, ['3010023917'], 'Now 954$p exists' );
835 $record = new_record;
836 $record->append_fields(
848 from_subfield => 'p',
854 read_field( { record => $record, field => '952', subfield => 'p' } );
856 read_field( { record => $record, field => '954', subfield => 'p' } );
857 is_deeply( \@fields_952p, [], 'All 952$p have moved' );
860 [ '3010023917', '3010023917' ],
868 from_subfield => '1',
874 read_field( { record => $record, field => '999', subfield => '9' } );
875 is_deeply( \@fields_9999, [],
876 'move a nonexistent subfield does not create a new one' );
879 subtest 'move field' => sub {
882 # move_field - fields
883 my $record = new_record;
884 $record->append_fields(
894 { record => $record, from_field => '952', to_field => '953' } );
895 is_deeply( [ read_field( { record => $record, field => '952' } ) ],
896 [], "original fields don't exist" );
900 { record => $record, field => '953', field_numbers => [1] }
903 [ '3010023917', 'BK', 'GEN', '2001-06-25' ],
904 "first original fields has been copied"
909 { record => $record, field => '953', field_numbers => [2] }
912 [ '3010023917', 'BK' ],
913 "second original fields has been copied"
916 #- Move only the first field
926 [ read_field( { record => $record, field => '953' } ) ],
927 [ '3010023917', 'BK' ],
928 "only first, the second 953 still exists"
931 [ read_field( { record => $record, field => '954' } ) ],
932 [ '3010023917', 'BK', 'GEN', '2001-06-25' ],
933 "only first, first 953 has been copied"
936 $record = new_record;
937 $record->append_fields(
945 #- Move all fields and modify values using a regex
951 regex => { search => 'BK', replace => 'DVD' }
954 is_deeply( [ read_field( { record => $record, field => '952' } ) ],
955 [], "use a regex, original fields don't exist" );
959 { record => $record, field => '953', field_numbers => [1] }
962 [ '3010023917', 'DVD', 'GEN', '2001-06-25' ],
963 "use a regex, first original fields has been copied"
968 { record => $record, field => '953', field_numbers => [2] }
971 [ '3010023917', 'DVD' ],
972 "use a regex, second original fields has been copied"
983 read_field( { record => $record, field => '999', subfield => '9' } );
984 is_deeply( \@fields_9999, [],
985 'move a nonexistent field does not create a new one' );
991 subtest 'delete_field' => sub {
993 subtest 'delete subfield' => sub {
995 my $record = new_record;
996 $record->append_fields(
1009 field_numbers => [1]
1013 read_field( { record => $record, field => '952', subfield => 'p' } );
1014 is_deeply( \@fields_952p, ['3010023917'], 'Delete first 952$p' );
1016 $record = new_record;
1017 $record->append_fields(
1024 delete_field( { record => $record, field => '952', subfield => 'p' } );
1026 read_field( { record => $record, field => '952', subfield => 'p' } );
1027 is_deeply( \@fields_952p, [], 'Delete all 952$p' );
1030 subtest 'delete field' => sub {
1032 my $record = new_record;
1033 delete_field( { record => $record, field => '952' } );
1034 my @fields_952 = read_field( { record => $record, field => '952' } );
1035 is_deeply( \@fields_952, [], 'Delete all 952, 1 deleted' );
1037 $record = new_record;
1038 $record->append_fields(
1045 delete_field( { record => $record, field => '952' } );
1046 @fields_952 = read_field( { record => $record, field => '952' } );
1047 is_deeply( \@fields_952, [], 'Delete all 952, 2 deleted' );