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

(-)a/C4/MarcModificationTemplates.pm (-2 / +15 lines)
Lines 23-28 use DateTime; Link Here
23
23
24
use C4::Context;
24
use C4::Context;
25
use Koha::SimpleMARC;
25
use Koha::SimpleMARC;
26
use Koha::MoreUtils;
26
27
27
use vars qw(@ISA @EXPORT);
28
use vars qw(@ISA @EXPORT);
28
29
Lines 567-575 sub ModifyRecordWithTemplate { Link Here
567
                    subfield => $conditional_subfield,
568
                    subfield => $conditional_subfield,
568
                    is_regex => $conditional_regex,
569
                    is_regex => $conditional_regex,
569
                });
570
                });
571
                my $all_fields = [
572
                    1 .. scalar @{
573
                        field_exists(
574
                            {
575
                                record   => $record,
576
                                field    => $conditional_field,
577
                                subfield => $conditional_subfield
578
                            }
579
                        )
580
                    }
581
                ];
582
                $field_numbers = [Koha::MoreUtils::singleton ( @$field_numbers, @$all_fields ) ];
570
                $do = $conditional eq 'if'
583
                $do = $conditional eq 'if'
571
                    ? not @$field_numbers
584
                    ? @$field_numbers
572
                    : @$field_numbers;
585
                    : not @$field_numbers;
573
            }
586
            }
574
        }
587
        }
575
588
(-)a/Koha/MoreUtils.pm (+15 lines)
Line 0 Link Here
1
package Koha::MoreUtils;
2
3
use Modern::Perl;
4
5
# From  List::MoreUtils v4.0
6
sub singleton (@)
7
{
8
    my %seen = ();
9
    my $k;
10
    my $seen_undef;
11
    grep { 1 == ( defined $_ ? $seen{ $k = $_ } : $seen_undef ) }
12
      grep { defined $_ ? not $seen{ $k = $_ }++ : not $seen_undef++ } @_;
13
}
14
15
1;
(-)a/t/db_dependent/MarcModificationTemplates.t (-15 / +43 lines)
Lines 1-6 Link Here
1
use Modern::Perl;
1
use Modern::Perl;
2
2
3
use Test::More tests => 95;
3
use Test::More tests => 96;
4
4
5
use Koha::SimpleMARC;
5
use Koha::SimpleMARC;
6
6
Lines 346-351 subtest 'GetModificationTemplates' => sub { Link Here
346
    is_deeply( [map{$_->{name}} @templates], ['aaa', 'mmm', 'zzz'] );
346
    is_deeply( [map{$_->{name}} @templates], ['aaa', 'mmm', 'zzz'] );
347
};
347
};
348
348
349
subtest "not_equals" => sub {
350
    plan tests => 2;
351
    $dbh->do(q|DELETE FROM marc_modification_templates|);
352
    my $template_id = AddModificationTemplate("template_name");
353
    AddModificationTemplateAction(
354
        $template_id, 'move_field', 0,
355
        '650', '', '', '651', '',
356
        '', '', '',
357
        'if', '650', '9', 'not_equals', '499', '',
358
        'Move field 650 to 651 if 650$9 != 499'
359
    );
360
    my $record = new_record();
361
    ModifyRecordWithTemplate( $template_id, $record );
362
    my $expected_record = expected_record_2();
363
    is_deeply( $record, $expected_record, '650 has been moved to 651 when 650$9 != 499' );
364
365
    $dbh->do(q|DELETE FROM marc_modification_templates|);
366
    $template_id = AddModificationTemplate("template_name");
367
    AddModificationTemplateAction(
368
        $template_id, 'move_field', 0,
369
        '650', '', '', '651', '',
370
        '', '', '',
371
        'if', '650', 'b', 'not_equals', '499', '',
372
        'Move field 650 to 651 if 650$b != 499'
373
    );
374
    $record = new_record();
375
    ModifyRecordWithTemplate( $template_id, $record );
376
    $expected_record = new_record();
377
    is_deeply( $record, $expected_record, 'None 650 have been moved, no $650$b exists' );
378
};
379
349
sub new_record {
380
sub new_record {
350
    my $record = MARC::Record->new;
381
    my $record = MARC::Record->new;
351
    $record->leader('03174nam a2200445 a 4500');
382
    $record->leader('03174nam a2200445 a 4500');
Lines 445-472 sub expected_record_2 { Link Here
445
            c => 'Donald E. Knuth.',
476
            c => 'Donald E. Knuth.',
446
        ),
477
        ),
447
        MARC::Field->new(
478
        MARC::Field->new(
479
            245, '1', '4',
480
            a => 'Bad title',
481
            c => 'Donald E. Knuth.',
482
        ),
483
        MARC::Field->new(
448
            650, ' ', '0',
484
            650, ' ', '0',
449
            9 => '462',
485
            a => 'Computer programming.',
486
            9 => '499',
450
        ),
487
        ),
451
        MARC::Field->new(
488
        MARC::Field->new(
452
            952, ' ', ' ',
489
            952, ' ', ' ',
453
            p => '3010023917_updated',
490
            p => '3010023917',
454
            y => 'BK',
491
            y => 'BK',
455
            c => 'GEN',
492
            c => 'GEN',
456
            e => '2001-06-25',
493
            d => '2001-06-25',
457
        ),
458
        MARC::Field->new(
459
            246, '', ' ',
460
            a => 'The art of computer programming',
461
        ),
494
        ),
462
        MARC::Field->new(
495
        MARC::Field->new(
463
            651, ' ', '0',
496
            651, ' ', '0',
464
            a => 'Computer algorithms.',
497
            a => 'Computer programming.',
465
            9 => '499',
498
            9 => '462',
466
        ),
467
        MARC::Field->new(
468
            999, ' ', ' ',
469
            a => 'non existent.',
470
        ),
499
        ),
471
    );
500
    );
472
    $record->append_fields(@fields);
501
    $record->append_fields(@fields);
473
- 

Return to bug 19069