View | Details | Raw Unified | Return to bug 11395
Collapse All | Expand All

(-)a/C4/MarcModificationTemplates.pm (-6 / +28 lines)
Lines 504-510 sub ModifyRecordWithTemplate { Link Here
504
504
505
    foreach my $a ( @actions ) {
505
    foreach my $a ( @actions ) {
506
        my $action = $a->{'action'};
506
        my $action = $a->{'action'};
507
        my $field_number = $a->{'field_number'};
507
        my $field_number = $a->{'field_number'} // 1;
508
        my $from_field = $a->{'from_field'};
508
        my $from_field = $a->{'from_field'};
509
        my $from_subfield = $a->{'from_subfield'};
509
        my $from_subfield = $a->{'from_subfield'};
510
        my $field_value = $a->{'field_value'};
510
        my $field_value = $a->{'field_value'};
Lines 526-532 sub ModifyRecordWithTemplate { Link Here
526
        }
526
        }
527
527
528
        my $do = 1;
528
        my $do = 1;
529
        my $field_numbers = [ $field_number ];
529
        my $field_numbers = [];
530
        if ( $conditional ) {
530
        if ( $conditional ) {
531
            if ( $conditional_comparison eq 'exists' ) {
531
            if ( $conditional_comparison eq 'exists' ) {
532
                $field_numbers = field_exists({
532
                $field_numbers = field_exists({
Lines 554-559 sub ModifyRecordWithTemplate { Link Here
554
                    value => $conditional_value,
554
                    value => $conditional_value,
555
                    field => $conditional_field,
555
                    field => $conditional_field,
556
                    subfield => $conditional_subfield,
556
                    subfield => $conditional_subfield,
557
                    is_regex => $conditional_regex,
557
                });
558
                });
558
                $do = $conditional eq 'if'
559
                $do = $conditional eq 'if'
559
                    ? @$field_numbers
560
                    ? @$field_numbers
Lines 562-569 sub ModifyRecordWithTemplate { Link Here
562
            elsif ( $conditional_comparison eq 'not_equals' ) {
563
            elsif ( $conditional_comparison eq 'not_equals' ) {
563
                $field_numbers = field_equals({
564
                $field_numbers = field_equals({
564
                    record => $record,
565
                    record => $record,
566
                    value => $conditional_value,
565
                    field => $conditional_field,
567
                    field => $conditional_field,
566
                    subfield => $conditional_subfield
568
                    subfield => $conditional_subfield,
569
                    is_regex => $conditional_regex,
567
                });
570
                });
568
                $do = $conditional eq 'if'
571
                $do = $conditional eq 'if'
569
                    ? not @$field_numbers
572
                    ? not @$field_numbers
Lines 572-580 sub ModifyRecordWithTemplate { Link Here
572
        }
575
        }
573
576
574
        if ( $do ) {
577
        if ( $do ) {
575
            @$field_numbers = ( $field_number )
578
576
                if $from_field ne $to_subfield
579
            # field_number == 0 if all field need to be updated
577
                    and $field_number;
580
            # or 1 if only the first field need to be updated
581
582
            # A condition has been given
583
            if ( @$field_numbers > 0 ) {
584
                if ( $field_number == 1 ) {
585
                    # We want only the first matching
586
                    $field_numbers = [ $field_numbers->[0] ];
587
                }
588
            }
589
            # There was no condition
590
            else {
591
                if ( $field_number == 1 ) {
592
                    # We want to process the first field
593
                    $field_numbers = [ 1 ];
594
                } elsif ( $to_field and $from_field ne $to_field ) {
595
                    # If the from and to fields are not the same, we only process the first field.
596
                    $field_numbers = [ 1 ];
597
                }
598
            }
599
578
            if ( $action eq 'copy_field' ) {
600
            if ( $action eq 'copy_field' ) {
579
                copy_field({
601
                copy_field({
580
                    record => $record,
602
                    record => $record,
(-)a/Koha/SimpleMARC.pm (-3 / +3 lines)
Lines 360-366 sub field_equals { Link Here
360
  my $value = $params->{value};
360
  my $value = $params->{value};
361
  my $fieldName = $params->{field};
361
  my $fieldName = $params->{field};
362
  my $subfieldName = $params->{subfield};
362
  my $subfieldName = $params->{subfield};
363
  my $regex = $params->{regex};
363
  my $is_regex = $params->{is_regex};
364
364
365
  if ( ! $record ) { return; }
365
  if ( ! $record ) { return; }
366
366
Lines 374-380 sub field_equals { Link Here
374
    SUBFIELDS: for my $subfield_value ( @subfield_values ) {
374
    SUBFIELDS: for my $subfield_value ( @subfield_values ) {
375
      if (
375
      if (
376
          (
376
          (
377
              $regex and $subfield_value =~ m/$value/
377
              $is_regex and $subfield_value =~ m/$value/
378
          ) or (
378
          ) or (
379
              $subfield_value eq $value
379
              $subfield_value eq $value
380
          )
380
          )
Lines 546-552 sub _copy_move_field { Link Here
546
    my $fromFieldName = $params->{from_field};
546
    my $fromFieldName = $params->{from_field};
547
    my $toFieldName = $params->{to_field};
547
    my $toFieldName = $params->{to_field};
548
    my $regex = $params->{regex};
548
    my $regex = $params->{regex};
549
    my $field_numbers = $params->{field_numbers};
549
    my $field_numbers = $params->{field_numbers} // [];
550
    my $action = $params->{action} || 'copy';
550
    my $action = $params->{action} || 'copy';
551
551
552
    my @fields = $record->field( $fromFieldName );
552
    my @fields = $record->field( $fromFieldName );
(-)a/t/db_dependent/MarcModificationTemplates.t (-11 / +179 lines)
Lines 1-6 Link Here
1
use Modern::Perl;
1
use Modern::Perl;
2
2
3
use Test::More tests => 78;
3
use Test::More tests => 94;
4
5
use Koha::SimpleMARC;
4
6
5
use_ok("MARC::Field");
7
use_ok("MARC::Field");
6
use_ok("MARC::Record");
8
use_ok("MARC::Record");
Lines 151-157 like( $template_id, qr|^\d+$|, "new template returns an id" ); Link Here
151
153
152
is( AddModificationTemplateAction(
154
is( AddModificationTemplateAction(
153
    $template_id, 'delete_field', 0,
155
    $template_id, 'delete_field', 0,
154
    '245', '', '', '', '', '',
156
    '245', '', '', '', '',
155
    '', '', '',
157
    '', '', '',
156
    'if', '245', 'a', 'equals', 'Bad title', '',
158
    'if', '245', 'a', 'equals', 'Bad title', '',
157
    'Delete field 245 if 245$a eq "Bad title"'
159
    'Delete field 245 if 245$a eq "Bad title"'
Lines 183-197 is( AddModificationTemplateAction( Link Here
183
185
184
is( AddModificationTemplateAction(
186
is( AddModificationTemplateAction(
185
    $template_id, 'move_field', 0,
187
    $template_id, 'move_field', 0,
186
    '952', 'd', '', '952', 'e', '',
188
    '952', 'd', '', '952', 'e',
187
    '', '', '',
189
    '', '', '',
188
    'if', '952', 'c', 'equals', '^GEN', '1',
190
    'if', '952', 'c', 'equals', '^GEN', '1',
189
    'Move field 952$d to 952$e if 952$c =~ /^GE/'
191
    'Move field 952$d to 952$e if 952$c =~ /^GEN/'
190
), 1, 'Add fifth action: move field 952$d to 952$e if 952$c =~ /^GE/');
192
), 1, 'Add fifth action: move field 952$d to 952$e if 952$c =~ /^GEN/');
191
193
192
is( AddModificationTemplateAction(
194
is( AddModificationTemplateAction(
193
    $template_id, 'update_field', 0,
195
    $template_id, 'update_field', 0,
194
    '650', 'a', 'Computer algorithms.', '', '', '',
196
    '650', 'a', 'Computer algorithms.', '', '',
195
    '', '', '',
197
    '', '', '',
196
    'if', '650', '9', 'equals', '499', '',
198
    'if', '650', '9', 'equals', '499', '',
197
    'Update field 650$a with "Computer algorithms." to 651 if 650$9 == 499'
199
    'Update field 650$a with "Computer algorithms." to 651 if 650$9 == 499'
Lines 199-218 is( AddModificationTemplateAction( Link Here
199
201
200
is( AddModificationTemplateAction(
202
is( AddModificationTemplateAction(
201
    $template_id, 'move_field', 0,
203
    $template_id, 'move_field', 0,
202
    '650', '', '', '651', '', '',
204
    '650', '', '', '651', '',
203
    '', '', '',
205
    '', '', '',
204
    'if', '650', '9', 'equals', '499', '',
206
    'if', '650', '9', 'equals', '499', '',
205
    'Move field 650 to 651 if 650$9 == 499'
207
    'Move field 650 to 651 if 650$9 == 499'
206
), 1, 'Add seventh action: move field 650 to 651 if 650$9 == 499');
208
), 1, 'Add seventh action: move field 650 to 651 if 650$9 == 499');
207
209
210
is( AddModificationTemplateAction(
211
    $template_id, 'update_field', 0,
212
    '999', 'a', 'non existent.', '', '',
213
    '', '', '',
214
    '', '', '', '', '', '',
215
    'Update non existent field 999$a with "non existent"'
216
), 1, 'Add eighth action: update field non existent 999$a with "non existent."');
208
217
209
my $record = new_record();
218
my $record = new_record();
210
219
211
is( ModifyRecordWithTemplate( $template_id, $record ), undef, "The ModifyRecordWithTemplate returns undef" );
220
is( ModifyRecordWithTemplate( $template_id, $record ), undef, "The ModifyRecordWithTemplate returns undef" );
212
221
213
my $expected_record = expected_record();
222
my $expected_record = expected_record_1();
214
is_deeply( $record, $expected_record, "Record modification as expected");
223
is_deeply( $record, $expected_record, "Record modification as expected");
215
224
225
$template_id = AddModificationTemplate("another_template_test");
226
227
# Duplicate 245 => 3x245
228
is( AddModificationTemplateAction(
229
    $template_id, 'copy_field', 0,
230
    '245', '', '', '245', '',
231
    '', '', '',
232
    'if', '245', 'a', 'equals', 'Bad title', '',
233
    'Copy field 245 if 245$a eq "Bad title"'
234
), 1, 'Add action: copy field 245 if 245$a eq "Bad title"');
235
236
$record = new_record();
237
is( ModifyRecordWithTemplate( $template_id, $record ), undef, "The ModifyRecordWithTemplate returns undef" );
238
239
my @fields_245a = Koha::SimpleMARC::read_field({
240
    record => $record,
241
    field => '245',
242
    subfield => 'a',
243
});
244
is_deeply( \@fields_245a, [
245
        'The art of computer programming',
246
        'Bad title',
247
        'Bad title',
248
    ], 'Copy field has copied the "Bad title"' );
249
250
# Update first "Bad title"
251
is( AddModificationTemplateAction(
252
    $template_id, 'update_field', 1,
253
    '245', 'a', 'Bad title updated', '', '',
254
    '', '', '',
255
    'if', '245', 'a', 'equals', 'Bad title', '',
256
    'Update first 245$a matching "Bad title" with "Bad title updated"'
257
), 1, 'Add action: update field 245$a matching "Bad title" with "Bad title updated');
258
259
$record = new_record();
260
is( ModifyRecordWithTemplate( $template_id, $record ), undef, "The ModifyRecordWithTemplate returns undef" );
261
262
@fields_245a = Koha::SimpleMARC::read_field({
263
    record => $record,
264
    field => '245',
265
    subfield => 'a',
266
});
267
is_deeply( \@fields_245a, [
268
        'The art of computer programming',
269
        'Bad title updated',
270
        'Bad title',
271
    ], 'update_field has update first the "Bad title"' );
272
273
# Duplicate first 245 => 3x245
274
is( AddModificationTemplateAction(
275
    $template_id, 'copy_field', 1,
276
    '245', '', '', '245', '',
277
    '', '', '',
278
    'if', '245', 'a', 'equals', '^Bad title', '1',
279
    'Copy field 245 if 245$a =~ "^Bad title"'
280
), 1, 'Add action: copy field 245 if 245$a =~ "^Bad title"');
281
282
$record = new_record();
283
is( ModifyRecordWithTemplate( $template_id, $record ), undef, "The ModifyRecordWithTemplate returns undef" );
284
285
@fields_245a = Koha::SimpleMARC::read_field({
286
    record => $record,
287
    field => '245',
288
    subfield => 'a',
289
});
290
is_deeply( \@fields_245a, [
291
        'The art of computer programming',
292
        'Bad title updated',
293
        'Bad title',
294
        'Bad title updated',
295
    ], 'Copy field has copied first "^Bad title"' );
296
297
# Delete first ^Bad title
298
is( AddModificationTemplateAction(
299
    $template_id, 'delete_field', 1,
300
    '245', '', '', '', '',
301
    '', '', '',
302
    'if', '245', 'a', 'equals', '^Bad title', '1',
303
    'Delete first 245$a mathing ^Bad title'
304
), 1, 'Delete first 245$a mathing ^Bad title');
305
306
$record = new_record();
307
is( ModifyRecordWithTemplate( $template_id, $record ), undef, "The ModifyRecordWithTemplate returns undef" );
308
@fields_245a = Koha::SimpleMARC::read_field({
309
    record => $record,
310
    field => '245',
311
    subfield => 'a',
312
});
313
is_deeply( \@fields_245a, [
314
        'The art of computer programming',
315
        'Bad title',
316
        'Bad title updated',
317
    ], 'delete field has been deleted the right field"' );
318
319
is( AddModificationTemplateAction(
320
    $template_id, 'delete_field', 0,
321
    '245', '', '', '', '',
322
    '', '', '',
323
    'if', '245', 'a', 'equals', 'updated$', '1',
324
    'Delete first 245$a mathing updated$'
325
), 1, 'Delete first 245$a mathing updated$');
326
327
$record = new_record();
328
is( ModifyRecordWithTemplate( $template_id, $record ), undef, "The ModifyRecordWithTemplate returns undef" );
329
@fields_245a = Koha::SimpleMARC::read_field({
330
    record => $record,
331
    field => '245',
332
    subfield => 'a',
333
});
334
is_deeply( \@fields_245a, [
335
        'The art of computer programming',
336
        'Bad title'
337
    ], 'delete field has been deleted the right field"' );
338
216
sub new_record {
339
sub new_record {
217
    my $record = MARC::Record->new;
340
    my $record = MARC::Record->new;
218
    $record->leader('03174nam a2200445 a 4500');
341
    $record->leader('03174nam a2200445 a 4500');
Lines 229-235 sub new_record { Link Here
229
        ),
352
        ),
230
        MARC::Field->new(
353
        MARC::Field->new(
231
            245, '1', '4',
354
            245, '1', '4',
232
            a => 'field to remove',
355
            a => 'Bad title',
233
            c => 'Donald E. Knuth.',
356
            c => 'Donald E. Knuth.',
234
        ),
357
        ),
235
        MARC::Field->new(
358
        MARC::Field->new(
Lines 254-260 sub new_record { Link Here
254
    return $record;
377
    return $record;
255
}
378
}
256
379
257
sub expected_record {
380
sub expected_record_1 {
258
    my $record = MARC::Record->new;
381
    my $record = MARC::Record->new;
259
    $record->leader('03174nam a2200445 a 4500');
382
    $record->leader('03174nam a2200445 a 4500');
260
    my @fields = (
383
    my @fields = (
Lines 288-298 sub expected_record { Link Here
288
            a => 'Computer algorithms.',
411
            a => 'Computer algorithms.',
289
            9 => '499',
412
            9 => '499',
290
        ),
413
        ),
414
        MARC::Field->new(
415
            999, ' ', ' ',
416
            a => 'non existent.',
417
        ),
291
    );
418
    );
292
    $record->append_fields(@fields);
419
    $record->append_fields(@fields);
293
    return $record;
420
    return $record;
294
}
421
}
295
422
423
sub expected_record_2 {
424
    my $record = MARC::Record->new;
425
    $record->leader('03174nam a2200445 a 4500');
426
    my @fields = (
427
        MARC::Field->new(
428
            100, '1', ' ',
429
            a => 'Knuth, Donald Ervin',
430
            d => '1938',
431
        ),
432
        MARC::Field->new(
433
            245, '1', '4',
434
            a => 'The art of computer programming',
435
            c => 'Donald E. Knuth.',
436
        ),
437
        MARC::Field->new(
438
            650, ' ', '0',
439
            9 => '462',
440
        ),
441
        MARC::Field->new(
442
            952, ' ', ' ',
443
            p => '3010023917_updated',
444
            y => 'BK',
445
            c => 'GEN',
446
            e => '2001-06-25',
447
        ),
448
        MARC::Field->new(
449
            246, '', ' ',
450
            a => 'The art of computer programming',
451
        ),
452
        MARC::Field->new(
453
            651, ' ', '0',
454
            a => 'Computer algorithms.',
455
            9 => '499',
456
        ),
457
        MARC::Field->new(
458
            999, ' ', ' ',
459
            a => 'non existent.',
460
        ),
461
    );
462
    $record->append_fields(@fields);
463
    return $record;
464
}
296
465
297
# C4::Context->userenv
466
# C4::Context->userenv
298
sub Mock_userenv {
467
sub Mock_userenv {
299
- 

Return to bug 11395