3 use Test::More tests => 37;
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);
40 my $record = new_record;
43 is( field_exists( $record, '650', 'a'), 'Computer programming.', '650$a exists' );
44 is( field_exists( $record, '650', 'b'), undef, '650$b does not exist' );
46 $record->append_fields(
49 a => 'Computer algorithms.',
54 is( field_exists( $record, '650', 'a'), 'Computer programming.', '650$a exists, field_exists returns the first one' );
57 my @fields_650a = read_field( $record, '650', 'a');
58 is( $fields_650a[0], 'Computer programming.', 'first 650$a' );
59 is( $fields_650a[1], 'Computer algorithms.', 'second 650$a' );
60 is( read_field( $record, '650', 'a', 1 ), 'Computer programming.', 'first 650$a bis' );
61 is( read_field( $record, '650', 'a', 2 ), 'Computer algorithms.', 'second 650$a bis' );
62 is( read_field( $record, '650', 'a', 3 ), undef, 'There is no 3 650$a' );
65 copy_field( $record, '245', 'a', '246', 'a' );
66 is_deeply( read_field( $record, '245', 'a' ), 'The art of computer programming', 'After copy 245$a still exists' );
67 is_deeply( read_field( $record, '246', 'a' ), 'The art of computer programming', '246$a is a new field' );
68 delete_field( $record, '246' );
69 is( field_exists( $record, '246', 'a', '246$a does not exist anymore' ), undef );
71 copy_field( $record, '650', 'a', '651', 'a' );
72 my @fields_651a = read_field( $record, '651', 'a' );
73 is_deeply( \@fields_651a, ['Computer programming.', 'Computer algorithms.'], 'Copy multivalued field' );
74 delete_field( $record, '651' );
76 copy_field( $record, '650', 'a', '651', 'a', undef, 1 );
77 @fields_651a = read_field( $record, '651', 'a' );
78 is_deeply( read_field( $record, '651', 'a' ), 'Computer programming.', 'Copy first field 650$a' );
79 delete_field( $record, '651' );
81 copy_field( $record, '650', 'a', '651', 'a', undef, 2 );
82 @fields_651a = read_field( $record, '651', 'a' );
83 is_deeply( read_field( $record, '651', 'a' ), 'Computer algorithms.', 'Copy second field 650$a' );
84 delete_field( $record, '651' );
86 copy_field( $record, '650', 'a', '651', 'a', { search => 'Computer', replace => 'The art of' } );
87 @fields_651a = read_field( $record, '651', 'a' );
88 is_deeply( \@fields_651a, ['The art of programming.', 'The art of algorithms.'], 'Copy field using regex' );
90 copy_field( $record, '650', 'a', '651', 'a', { search => 'Computer', replace => 'The mistake of' } );
91 @fields_651a = read_field( $record, '651', 'a' );
92 is_deeply( \@fields_651a, ['The mistake of programming.', 'The mistake of algorithms.'], 'Copy fields using regex on existing fields' );
93 delete_field( $record, '651' );
95 copy_field( $record, '650', 'a', '651', 'a', { search => 'Computer', replace => 'The art of' } );
96 copy_field( $record, '650', 'a', '651', 'a', { search => 'Computer', replace => 'The mistake of' }, 1, "dont_erase" );
97 @fields_651a = read_field( $record, '651', 'a' );
98 is_deeply( \@fields_651a, [
99 'The art of programming.',
100 'The mistake of programming.',
101 'The art of algorithms.',
102 'The mistake of programming.'
103 ], 'Copy first field using regex on existing fields without erase existing values' );
104 delete_field( $record, '651' );
106 copy_field( $record, '650', 'a', '651', 'a', { search => 'Computer', replace => 'The art of' } );
107 copy_field( $record, '650', 'a', '651', 'a', { search => 'Computer', replace => 'The mistake of' }, undef , "dont_erase" );
108 @fields_651a = read_field( $record, '651', 'a' );
109 is_deeply( \@fields_651a, [
110 'The art of programming.',
111 'The mistake of programming.',
112 'The mistake of algorithms.',
113 'The art of algorithms.',
114 'The mistake of programming.',
115 'The mistake of algorithms.'
116 ], 'Copy fields using regex on existing fields without erase existing values' );
117 delete_field( $record, '651' );
119 # Copy with regex modifiers
120 copy_field( $record, '650', 'a', '652', 'a', { search => 'o', replace => 'foo' } );
121 my @fields_652a = read_field( $record, '652', 'a' );
122 is_deeply( \@fields_652a, ['Cfoomputer programming.', 'Cfoomputer algorithms.'], 'Copy field using regex' );
124 copy_field( $record, '650', 'a', '653', 'a', { search => 'o', replace => 'foo', modifiers => 'g' } );
125 my @fields_653a = read_field( $record, '653', 'a' );
126 is_deeply( \@fields_653a, ['Cfoomputer prfoogramming.', 'Cfoomputer algfoorithms.'], 'Copy field using regex' );
128 copy_field( $record, '650', 'a', '654', 'a', { search => 'O', replace => 'foo', modifiers => 'i' } );
129 my @fields_654a = read_field( $record, '654', 'a' );
130 is_deeply( \@fields_654a, ['Cfoomputer programming.', 'Cfoomputer algorithms.'], 'Copy field using regex' );
132 copy_field( $record, '650', 'a', '655', 'a', { search => 'O', replace => 'foo', modifiers => 'gi' } );
133 my @fields_655a = read_field( $record, '655', 'a' );
134 is_deeply( \@fields_655a, ['Cfoomputer prfoogramming.', 'Cfoomputer algfoorithms.'], 'Copy field using regex' );
137 update_field( $record, '952', 'p', undef, '3010023918' );
138 is_deeply( read_field( $record, '952', 'p' ), '3010023918', 'update existing subfield 952$p' );
139 delete_field( $record, '952' );
140 update_field( $record, '952', 'p', undef, '3010023918' );
141 update_field( $record, '952', 'y', undef, 'BK' );
142 is_deeply( read_field( $record, '952', 'p' ), '3010023918', 'create subfield 952$p' );
143 is_deeply( read_field( $record, '952', 'y' ), 'BK', 'create subfield 952$k on existing 952 field' );
144 $record->append_fields(
151 update_field( $record, '952', 'p', undef, '3010023919' );
152 my @fields_952p = read_field( $record, '952', 'p' );
153 is_deeply( \@fields_952p, ['3010023919', '3010023919'], 'update all subfields 952$p with the same value' );
155 update_field( $record, '952', 'p', undef, ('3010023917', '3010023918') );
156 @fields_952p = read_field( $record, '952', 'p' );
157 is_deeply( \@fields_952p, ['3010023917', '3010023918'], 'update all subfields 952$p with the different values' );
160 $record = new_record;
161 my ( @fields_952d, @fields_952c, @fields_954c, @fields_954p);
162 $record->append_fields(
169 copy_field( $record, '952', 'd', '952', 'd' );
170 @fields_952d = read_field( $record, '952', 'd' );
171 is_deeply( \@fields_952d, ['2001-06-25', '2001-06-25'], 'copy 952$d into others 952 field' );
173 move_field( $record, '952', 'c', '954', 'c' );
174 @fields_952c = read_field( $record, '952', 'c' );
175 @fields_954c = read_field( $record, '954', 'c' );
176 is_deeply( \@fields_952c, [], 'The 952$c has moved' );
177 is_deeply( \@fields_954c, ['GEN'], 'Now 954$c exists' );
179 move_field( $record, '952', 'p', '954', 'p', undef, 1 ); # Move the first field
180 @fields_952p = read_field( $record, '952', 'p' );
181 @fields_954p = read_field( $record, '954', 'p' );
182 is_deeply( \@fields_952p, ['3010023917'], 'One of 952$p has moved' );
183 is_deeply( \@fields_954p, ['3010023917'], 'Now 954$p exists' );
185 $record = new_record;
186 $record->append_fields(
194 move_field( $record, '952', 'p', '954', 'p' ); # Move all field
195 @fields_952p = read_field( $record, '952', 'p' );
196 @fields_954p = read_field( $record, '954', 'p' );
197 is_deeply( \@fields_952p, [], 'All 952$p have moved' );
198 is_deeply( \@fields_954p, ['3010023917', '3010023917'], 'Now 2 954$p exist' );