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

(-)a/Koha/Patrons/Import.pm (-58 / +101 lines)
Lines 25-30 use Text::CSV; Link Here
25
use C4::Members;
25
use C4::Members;
26
use C4::Branch;
26
use C4::Branch;
27
use C4::Members::Attributes qw(:all);
27
use C4::Members::Attributes qw(:all);
28
use C4::Members::AttributeTypes;
28
29
29
use Koha::DateUtils;
30
use Koha::DateUtils;
30
31
Lines 52-68 Further pod documentation needed here. Link Here
52
53
53
=cut
54
=cut
54
55
55
has 'today_iso' => (
56
has 'today_iso' => ( is => 'ro', lazy => 1,
56
    is      => 'ro',
57
    default => sub { output_pref( { dt => dt_from_string(), dateonly => 1, dateformat => 'iso' } ); }, );
57
    lazy    => 1,
58
    default => sub { output_pref( { dt => dt_from_string(), dateonly => 1, dateformat => 'iso' } ); },
59
  );
60
58
61
has 'text_csv' => (
59
has 'text_csv' => ( is => 'rw', lazy => 1,
62
    is      => 'ro',
60
    default => sub { Text::CSV->new( { binary => 1, } ); },  );
63
    lazy    => 1,
64
    default => sub { Text::CSV->new( { binary => 1 } ), },
65
  );
66
61
67
sub import_patrons {
62
sub import_patrons {
68
    my ($self, $params) = @_;
63
    my ($self, $params) = @_;
Lines 77-83 sub import_patrons { Link Here
77
    my $extended             = C4::Context->preference('ExtendedPatronAttributes');
72
    my $extended             = C4::Context->preference('ExtendedPatronAttributes');
78
    my $set_messaging_prefs  = C4::Context->preference('EnhancedMessagingPreferences');
73
    my $set_messaging_prefs  = C4::Context->preference('EnhancedMessagingPreferences');
79
74
80
    my @columnkeys = set_column_keys($extended);
75
    my @columnkeys = $self->set_column_keys($extended);
81
    my @feedback;
76
    my @feedback;
82
    my @errors;
77
    my @errors;
83
78
Lines 85-112 sub import_patrons { Link Here
85
    my $alreadyindb = 0;
80
    my $alreadyindb = 0;
86
    my $overwritten = 0;
81
    my $overwritten = 0;
87
    my $invalid     = 0;
82
    my $invalid     = 0;
88
    my $matchpoint_attr_type;
83
    my $matchpoint_attr_type = $self->set_attribute_types({ extended => $extended, matchpoint => $matchpoint, });
89
84
90
    # use header line to construct key to column map
85
    # Use header line to construct key to column map
91
    my $borrowerline = <$handle>;
92
    my $status       = $self->text_csv->parse($borrowerline);
93
    ($status) or push @errors, { badheader => 1, line => $., lineraw => $borrowerline };
94
95
    my @csvcolumns = $self->text_csv->fields();
96
    my %csvkeycol;
86
    my %csvkeycol;
97
    my $col = 0;
87
    my $borrowerline = <$handle>;
98
    foreach my $keycol (@csvcolumns) {
88
    my @csvcolumns   = $self->prepare_columns({headerrow => $borrowerline, keycol => \%csvkeycol, errors => \@errors, });
99
89
    push(@feedback, { feedback => 1, name => 'headerrow', value => join( ', ', @csvcolumns ) });
100
        # columnkeys don't contain whitespace, but some stupid tools add it
101
        $keycol =~ s/ +//g;
102
        $csvkeycol{$keycol} = $col++;
103
    }
104
105
    if ($extended) {
106
        $matchpoint_attr_type = C4::Members::AttributeTypes->fetch($matchpoint);
107
    }
108
90
109
    push @feedback, { feedback => 1, name => 'headerrow', value => join( ', ', @csvcolumns ) };
110
    my @criticals = qw( surname );    # there probably should be others - rm branchcode && categorycode
91
    my @criticals = qw( surname );    # there probably should be others - rm branchcode && categorycode
111
  LINE: while ( my $borrowerline = <$handle> ) {
92
  LINE: while ( my $borrowerline = <$handle> ) {
112
        my $line_number = $.;
93
        my $line_number = $.;
Lines 116-122 sub import_patrons { Link Here
116
        my $status  = $self->text_csv->parse($borrowerline);
97
        my $status  = $self->text_csv->parse($borrowerline);
117
        my @columns = $self->text_csv->fields();
98
        my @columns = $self->text_csv->fields();
118
        if ( !$status ) {
99
        if ( !$status ) {
119
            push @missing_criticals, { badparse => 1, line => $., lineraw => $borrowerline };
100
            push @missing_criticals, { badparse => 1, line => $line_number, lineraw => $borrowerline };
120
        }
101
        }
121
        elsif ( @columns == @columnkeys ) {
102
        elsif ( @columns == @columnkeys ) {
122
            @borrower{@columnkeys} = @columns;
103
            @borrower{@columnkeys} = @columns;
Lines 148-158 sub import_patrons { Link Here
148
            }
129
            }
149
        }
130
        }
150
131
151
        # Checking if borrower category code exists and if it matches to a known category. Pushing error to missing_criticals otherwise.
132
        # Check if borrower category code exists and if it matches to a known category. Pushing error to missing_criticals otherwise.
152
        check_borrower_category($borrower{categorycode}, $borrowerline, $line_number, \@missing_criticals);
133
        $self->check_borrower_category($borrower{categorycode}, $borrowerline, $line_number, \@missing_criticals);
134
135
        # Check if branch code exists and if it matches to a branch name. Pushing error to missing_criticals otherwise.
136
        $self->check_branch_code($borrower{branchcode}, $borrowerline, $line_number, \@missing_criticals);
137
138
        # Popular spreadsheet applications make it difficult to force date outputs to be zero-padded, but we require it.
139
        $self->format_dates({borrower => \%borrower, lineraw => $borrowerline, line => $line_number, missing_criticals => \@missing_criticals, });
153
140
154
        # Checking if branch code exists and if it matches to a branch name. Pushing error to missing_criticals otherwise.
155
        check_branch_code($borrower{branchcode}, $borrowerline, $line_number, \@missing_criticals);
156
141
157
        if (@missing_criticals) {
142
        if (@missing_criticals) {
158
            foreach (@missing_criticals) {
143
            foreach (@missing_criticals) {
Lines 166-190 sub import_patrons { Link Here
166
            next LINE;
151
            next LINE;
167
        }
152
        }
168
153
169
        # Setting patron attributes if extended.
154
        # Set patron attributes if extended.
170
        my $patron_attributes = set_patron_attributes($extended, $borrower{patron_attributes}, \@feedback);
155
        my $patron_attributes = $self->set_patron_attributes($extended, $borrower{patron_attributes}, \@feedback);
171
156
        if( $extended ) { delete $borrower{patron_attributes}; } # Not really a field in borrowers.
172
        # Not really a field in borrowers, so we don't want to pass it to ModMember.
173
        if ($extended) { delete $borrower{patron_attributes}; }
174
157
175
        # Popular spreadsheet applications make it difficult to force date outputs to be zero-padded, but we require it.
158
        # Default date enrolled and date expiry if not already set.
176
        foreach (qw(dateofbirth dateenrolled dateexpiry)) {
177
            my $tempdate = $borrower{$_} or next;
178
179
            $tempdate = eval { output_pref( { dt => dt_from_string( $tempdate ), dateonly => 1, dateformat => 'iso' } ); };
180
181
            if ($tempdate) {
182
                $borrower{$_} = $tempdate;
183
            } else {
184
                $borrower{$_} = '';
185
                push @missing_criticals, { key => $_, line => $line_number, lineraw => $borrowerline, bad_date => 1 };
186
            }
187
        }
188
        $borrower{dateenrolled} = $self->today_iso() unless $borrower{dateenrolled};
159
        $borrower{dateenrolled} = $self->today_iso() unless $borrower{dateenrolled};
189
        $borrower{dateexpiry} = GetExpiryDate( $borrower{categorycode}, $borrower{dateenrolled} ) unless $borrower{dateexpiry};
160
        $borrower{dateexpiry} = GetExpiryDate( $borrower{categorycode}, $borrower{dateenrolled} ) unless $borrower{dateexpiry};
190
161
Lines 370-375 sub import_patrons { Link Here
370
    };
341
    };
371
}
342
}
372
343
344
=head2 prepare_columns
345
346
 my @csvcolumns = $self->prepare_columns({headerrow => $borrowerline, keycol => \%csvkeycol, errors => \@errors, });
347
348
Returns an array of all column key and populates a hash of colunm key positions.
349
350
=cut
351
352
sub prepare_columns {
353
    my ($self, $params) = @_;
354
355
    my $status = $self->text_csv->parse($params->{headerrow});
356
    unless( $status ) {
357
        push( @{$params->{errors}}, { badheader => 1, line => 1, lineraw => $params->{headerrow} });
358
        return;
359
    }
360
361
    my @csvcolumns = $self->text_csv->fields();
362
    my $col = 0;
363
    foreach my $keycol (@csvcolumns) {
364
        # columnkeys don't contain whitespace, but some stupid tools add it
365
        $keycol =~ s/ +//g;
366
        $params->{keycol}->{$keycol} = $col++;
367
    }
368
369
    return @csvcolumns;
370
}
371
372
=head2 set_attribute_types
373
374
 my $matchpoint_attr_type = $self->set_attribute_types({ extended => $extended, matchpoint => $matchpoint, });
375
376
Returns an attribute type based on matchpoint parameter.
377
378
=cut
379
380
sub set_attribute_types {
381
    my ($self, $params) = @_;
382
383
    my $attribute_types;
384
    if( $params->{extended} ) {
385
        $attribute_types = C4::Members::AttributeTypes->fetch($params->{matchpoint});
386
    }
387
388
    return $attribute_types;
389
}
390
373
=head2 set_column_keys
391
=head2 set_column_keys
374
392
375
 my @columnkeys = set_column_keys($extended);
393
 my @columnkeys = set_column_keys($extended);
Lines 379-385 Returns an array of borrowers' table columns. Link Here
379
=cut
397
=cut
380
398
381
sub set_column_keys {
399
sub set_column_keys {
382
    my ($extended) = @_;
400
    my ($self, $extended) = @_;
383
401
384
    my @columnkeys = map { $_ ne 'borrowernumber' ? $_ : () } C4::Members::columns();
402
    my @columnkeys = map { $_ ne 'borrowernumber' ? $_ : () } C4::Members::columns();
385
    push( @columnkeys, 'patron_attributes' ) if $extended;
403
    push( @columnkeys, 'patron_attributes' ) if $extended;
Lines 396-404 Returns a reference to array of hashrefs data structure as expected by SetBorrow Link Here
396
=cut
414
=cut
397
415
398
sub set_patron_attributes {
416
sub set_patron_attributes {
399
    my ($extended, $patron_attributes, $feedback) = @_;
417
    my ($self, $extended, $patron_attributes, $feedback) = @_;
400
418
401
    unless($extended) { return; }
419
    unless( $extended ) { return; }
402
    unless( defined($patron_attributes) ) { return; }
420
    unless( defined($patron_attributes) ) { return; }
403
421
404
    # Fixup double quotes in case we are passed smart quotes
422
    # Fixup double quotes in case we are passed smart quotes
Lines 421-427 Pushes a 'missing_criticals' error entry if no branch code or branch code does n Link Here
421
=cut
439
=cut
422
440
423
sub check_branch_code {
441
sub check_branch_code {
424
    my ($branchcode, $borrowerline, $line_number, $missing_criticals) = @_;
442
    my ($self, $branchcode, $borrowerline, $line_number, $missing_criticals) = @_;
425
443
426
    # No branch code
444
    # No branch code
427
    unless( $branchcode ) {
445
    unless( $branchcode ) {
Lines 446-452 Pushes a 'missing_criticals' error entry if no category code or category code do Link Here
446
=cut
464
=cut
447
465
448
sub check_borrower_category {
466
sub check_borrower_category {
449
    my ($categorycode, $borrowerline, $line_number, $missing_criticals) = @_;
467
    my ($self, $categorycode, $borrowerline, $line_number, $missing_criticals) = @_;
450
468
451
    # No branch code
469
    # No branch code
452
    unless( $categorycode ) {
470
    unless( $categorycode ) {
Lines 462-467 sub check_borrower_category { Link Here
462
    }
480
    }
463
}
481
}
464
482
483
=head2 format_dates
484
485
 format_dates({borrower => \%borrower, lineraw => $lineraw, line => $line_number, missing_criticals => \@missing_criticals, });
486
487
Pushes a 'missing_criticals' error entry for each of the 3 date types dateofbirth, dateenrolled and dateexpiry if it can not
488
be formatted to the chosen date format. Populates the correctly formatted date otherwise.
489
490
=cut
491
492
sub format_dates {
493
    my ($self, $params) = @_;
494
495
    foreach my $date_type (qw(dateofbirth dateenrolled dateexpiry)) {
496
        my $tempdate = $params->{borrower}->{$date_type} or next();
497
        my $formatted_date = eval { output_pref( { dt => dt_from_string( $tempdate ), dateonly => 1, dateformat => 'iso' } ); };
498
499
        if ($formatted_date) {
500
            $params->{borrower}->{$date_type} = $formatted_date;
501
        } else {
502
            $params->{borrower}->{$date_type} = '';
503
            push (@{$params->{missing_criticals}}, { key => $date_type, line => $params->{line}, lineraw => $params->{lineraw}, bad_date => 1 });
504
        }
505
    }
506
}
507
465
1;
508
1;
466
509
467
=head1 AUTHOR
510
=head1 AUTHOR
(-)a/t/db_dependent/Koha/Patrons/Import.t (-41 / +219 lines)
Lines 18-24 Link Here
18
# along with Koha; if not, see <http://www.gnu.org/licenses>.
18
# along with Koha; if not, see <http://www.gnu.org/licenses>.
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
use Test::More tests => 98;
21
use Test::More tests => 122;
22
use Test::MockModule;
22
use Test::MockModule;
23
23
24
use Koha::Database;
24
use Koha::Database;
Lines 42-48 subtest 'test_methods' => sub { Link Here
42
    plan tests => 1;
42
    plan tests => 1;
43
43
44
    # Given ... we can reach the method(s)
44
    # Given ... we can reach the method(s)
45
    my @methods = ('import_patrons', 'set_column_keys', 'set_patron_attributes', 'check_branch_code');
45
    my @methods = ('import_patrons',
46
                   'set_attribute_types',
47
                   'prepare_columns',
48
                   'set_column_keys',
49
                   'set_patron_attributes',
50
                   'check_branch_code',
51
                   'format_dates',
52
                  );
46
    can_ok('Koha::Patrons::Import', @methods);
53
    can_ok('Koha::Patrons::Import', @methods);
47
};
54
};
48
55
Lines 66-73 my $result_0 = $patrons_import->import_patrons($params_0); Link Here
66
is($result_0, undef, 'Got the expected undef from import_patrons with no file handle');
73
is($result_0, undef, 'Got the expected undef from import_patrons with no file handle');
67
74
68
# Given ... a file handle to file with headers only.
75
# Given ... a file handle to file with headers only.
69
my $csv_headers = 'cardnumber,surname,firstname,title,othernames,initials,streetnumber,streettype,address,address2,city,state,zipcode,country,email,phone,mobile,fax,dateofbirth,branchcode,categorycode,dateenrolled,dateexpiry,userid,password';
76
my $csv_headers  = 'cardnumber,surname,firstname,title,othernames,initials,streetnumber,streettype,address,address2,city,state,zipcode,country,email,phone,mobile,fax,dateofbirth,branchcode,categorycode,dateenrolled,dateexpiry,userid,password';
70
my $csv_one_line = '1000,Nancy,Jenkins,Dr,,NJ,78,Circle,Bunting,El Paso,Henderson,Texas,79984,United States,ajenkins0@sourceforge.net,7-(388)559-6763,3-(373)151-4471,8-(509)286-4001,16/10/1965,CPL,PT,28/12/2014,01/07/2015,jjenkins0,DPQILy';
77
my $res_header   = 'cardnumber, surname, firstname, title, othernames, initials, streetnumber, streettype, address, address2, city, state, zipcode, country, email, phone, mobile, fax, dateofbirth, branchcode, categorycode, dateenrolled, dateexpiry, userid, password';
78
my $csv_one_line = '1000,Nancy,Jenkins,Dr,,NJ,78,Circle,Bunting,El Paso,Henderson,Texas,79984,United States,ajenkins0@sourceforge.net,7-(388)559-6763,3-(373)151-4471,8-(509)286-4001,10/16/1965,CPL,PT,12/28/2014,07/01/2015,jjenkins0,DPQILy';
71
79
72
my $filename_1 = make_csv($temp_dir, $csv_headers, $csv_one_line);
80
my $filename_1 = make_csv($temp_dir, $csv_headers, $csv_one_line);
73
open(my $handle_1, "<", $filename_1) or die "cannot open < $filename_1: $!";
81
open(my $handle_1, "<", $filename_1) or die "cannot open < $filename_1: $!";
Lines 82-89 is(scalar @{$result_1->{errors}}, 0, 'Got the expected 0 size error array from i Link Here
82
90
83
is($result_1->{feedback}->[0]->{feedback}, 1, 'Got the expected 1 feedback from import_patrons with no matchpoint defined');
91
is($result_1->{feedback}->[0]->{feedback}, 1, 'Got the expected 1 feedback from import_patrons with no matchpoint defined');
84
is($result_1->{feedback}->[0]->{name}, 'headerrow', 'Got the expected header row name from import_patrons with no matchpoint defined');
92
is($result_1->{feedback}->[0]->{name}, 'headerrow', 'Got the expected header row name from import_patrons with no matchpoint defined');
85
is($result_1->{feedback}->[0]->{value}, 'cardnumber, surname, firstname, title, othernames, initials, streetnumber, streettype, address, address2, city, state, zipcode, country, email, phone, mobile, fax, dateofbirth, branchcode, categorycode, dateenrolled, dateexpiry, userid, password',
93
is($result_1->{feedback}->[0]->{value}, $res_header, 'Got the expected header row value from import_patrons with no matchpoint defined');
86
                                        'Got the expected header row value from import_patrons with no matchpoint defined');
87
94
88
is($result_1->{feedback}->[1]->{feedback}, 1, 'Got the expected second feedback from import_patrons with no matchpoint defined');
95
is($result_1->{feedback}->[1]->{feedback}, 1, 'Got the expected second feedback from import_patrons with no matchpoint defined');
89
is($result_1->{feedback}->[1]->{name}, 'lastimported', 'Got the expected last imported name from import_patrons with no matchpoint defined');
96
is($result_1->{feedback}->[1]->{name}, 'lastimported', 'Got the expected last imported name from import_patrons with no matchpoint defined');
Lines 109-116 is($result_2->{errors}->[0]->{invalid_cardnumber}, 1, 'Got the expected invalid Link Here
109
116
110
is($result_2->{feedback}->[0]->{feedback}, 1, 'Got the expected 1 feedback from import_patrons with invalid card number');
117
is($result_2->{feedback}->[0]->{feedback}, 1, 'Got the expected 1 feedback from import_patrons with invalid card number');
111
is($result_2->{feedback}->[0]->{name}, 'headerrow', 'Got the expected header row name from import_patrons with invalid card number');
118
is($result_2->{feedback}->[0]->{name}, 'headerrow', 'Got the expected header row name from import_patrons with invalid card number');
112
is($result_2->{feedback}->[0]->{value}, 'cardnumber, surname, firstname, title, othernames, initials, streetnumber, streettype, address, address2, city, state, zipcode, country, email, phone, mobile, fax, dateofbirth, branchcode, categorycode, dateenrolled, dateexpiry, userid, password',
119
is($result_2->{feedback}->[0]->{value}, $res_header, 'Got the expected header row value from import_patrons with invalid card number');
113
                                        'Got the expected header row value from import_patrons with invalid card number');
114
120
115
is($result_2->{imported}, 0, 'Got the expected 0 imported result from import_patrons with invalid card number');
121
is($result_2->{imported}, 0, 'Got the expected 0 imported result from import_patrons with invalid card number');
116
is($result_2->{invalid}, 1, 'Got the expected 1 invalid result from import_patrons with invalid card number');
122
is($result_2->{invalid}, 1, 'Got the expected 1 invalid result from import_patrons with invalid card number');
Lines 131-138 is($result_3->{errors}->[0]->{userid}, 'jjenkins0', 'Got the expected userid err Link Here
131
137
132
is($result_3->{feedback}->[0]->{feedback}, 1, 'Got the expected 1 feedback from import_patrons with duplicate userid');
138
is($result_3->{feedback}->[0]->{feedback}, 1, 'Got the expected 1 feedback from import_patrons with duplicate userid');
133
is($result_3->{feedback}->[0]->{name}, 'headerrow', 'Got the expected header row name from import_patrons with duplicate userid');
139
is($result_3->{feedback}->[0]->{name}, 'headerrow', 'Got the expected header row name from import_patrons with duplicate userid');
134
is($result_3->{feedback}->[0]->{value}, 'cardnumber, surname, firstname, title, othernames, initials, streetnumber, streettype, address, address2, city, state, zipcode, country, email, phone, mobile, fax, dateofbirth, branchcode, categorycode, dateenrolled, dateexpiry, userid, password',
140
is($result_3->{feedback}->[0]->{value}, $res_header, 'Got the expected header row value from import_patrons with duplicate userid');
135
                                        'Got the expected header row value from import_patrons with duplicate userid');
136
141
137
is($result_3->{imported}, 0, 'Got the expected 0 imported result from import_patrons with duplicate userid');
142
is($result_3->{imported}, 0, 'Got the expected 0 imported result from import_patrons with duplicate userid');
138
is($result_3->{invalid}, 1, 'Got the expected 1 invalid result from import_patrons with duplicate userid');
143
is($result_3->{invalid}, 1, 'Got the expected 1 invalid result from import_patrons with duplicate userid');
Lines 140-151 is($result_3->{overwritten}, 0, 'Got the expected 0 overwritten result from impo Link Here
140
145
141
# Given ... a new input and mocked C4::Context
146
# Given ... a new input and mocked C4::Context
142
my $context = Test::MockModule->new('C4::Context');
147
my $context = Test::MockModule->new('C4::Context');
143
$context->mock('preference', sub { my ($mod, $meth) = @_; if ( $meth eq 'ExtendedPatronAttributes' ) { return 1; } });
148
$context->mock('preference', sub { my ($mod, $meth) = @_;
149
                                    if ( $meth eq 'ExtendedPatronAttributes' ) { return 1; }
150
                                    if ( $meth eq 'dateformat' ) { return 'us'; }
151
                                });
144
152
145
my $new_input_line = '1001,Donna,Sullivan,Mrs,Henry,DS,59,Court,Burrows,Reading,Salt Lake City,Pennsylvania,19605,United States,hsullivan1@purevolume.com,3-(864)009-3006,7-(291)885-8423,1-(879)095-5038,19/09/1970,LPL,PT,04/03/2015,01/07/2015,hsullivan1,8j6P6Dmap';
153
my $new_input_line = '1001,Donna,Sullivan,Mrs,Henry,DS,59,Court,Burrows,Reading,Salt Lake City,Pennsylvania,19605,United States,hsullivan1@purevolume.com,3-(864)009-3006,7-(291)885-8423,1-(879)095-5038,09/19/1970,LPL,PT,03/04/2015,07/01/2015,hsullivan1,8j6P6Dmap';
146
my $filename_4 = make_csv($temp_dir, $csv_headers, $new_input_line);
154
my $filename_4 = make_csv($temp_dir, $csv_headers, $new_input_line);
147
open(my $handle_4, "<", $filename_4) or die "cannot open < $filename_4: $!";
155
open(my $handle_4, "<", $filename_4) or die "cannot open < $filename_4: $!";
148
my $params_4 = { file => $handle_4, matchpoint => 'cardnumber', };
156
my $params_4 = { file => $handle_4, matchpoint => 'SHOW_BCODE', };
149
157
150
# When ...
158
# When ...
151
my $result_4 = $patrons_import->import_patrons($params_4);
159
my $result_4 = $patrons_import->import_patrons($params_4);
Lines 156-163 is(scalar @{$result_4->{errors}}, 0, 'Got the expected 0 size error array from i Link Here
156
164
157
is($result_4->{feedback}->[0]->{feedback}, 1, 'Got the expected 1 feedback from import_patrons with extended user');
165
is($result_4->{feedback}->[0]->{feedback}, 1, 'Got the expected 1 feedback from import_patrons with extended user');
158
is($result_4->{feedback}->[0]->{name}, 'headerrow', 'Got the expected header row name from import_patrons with extended user');
166
is($result_4->{feedback}->[0]->{name}, 'headerrow', 'Got the expected header row name from import_patrons with extended user');
159
is($result_4->{feedback}->[0]->{value}, 'cardnumber, surname, firstname, title, othernames, initials, streetnumber, streettype, address, address2, city, state, zipcode, country, email, phone, mobile, fax, dateofbirth, branchcode, categorycode, dateenrolled, dateexpiry, userid, password',
167
is($result_4->{feedback}->[0]->{value}, $res_header, 'Got the expected header row value from import_patrons with extended user');
160
                                        'Got the expected header row value from import_patrons with extended user');
161
168
162
is($result_4->{feedback}->[1]->{feedback}, 1, 'Got the expected second feedback from import_patrons with extended user');
169
is($result_4->{feedback}->[1]->{feedback}, 1, 'Got the expected second feedback from import_patrons with extended user');
163
is($result_4->{feedback}->[1]->{name}, 'attribute string', 'Got the expected attribute string from import_patrons with extended user');
170
is($result_4->{feedback}->[1]->{name}, 'attribute string', 'Got the expected attribute string from import_patrons with extended user');
Lines 174-182 is($result_4->{overwritten}, 0, 'Got the expected 0 overwritten result from impo Link Here
174
$context->unmock('preference');
181
$context->unmock('preference');
175
182
176
# Given ... 3 new inputs. One with no branch code, one with unexpected branch code.
183
# Given ... 3 new inputs. One with no branch code, one with unexpected branch code.
177
my $input_no_branch   = '1002,Johnny,Reynolds,Mr,Patricia,JR,12,Hill,Kennedy,Saint Louis,Colorado Springs,Missouri,63131,United States,preynolds2@washington.edu,7-(925)314-9514,0-(315)973-8956,4-(510)556-2323,18/09/1967,,PT,07/05/2015,01/07/2015,preynolds2,K3HiDzl';
184
my $input_no_branch   = '1002,Johnny,Reynolds,Mr,Patricia,JR,12,Hill,Kennedy,Saint Louis,Colorado Springs,Missouri,63131,United States,preynolds2@washington.edu,7-(925)314-9514,0-(315)973-8956,4-(510)556-2323,09/18/1967,,PT,05/07/2015,07/01/2015,preynolds2,K3HiDzl';
178
my $input_good_branch = '1003,Linda,Richardson,Mr,Kimberly,LR,90,Place,Bayside,Atlanta,Erie,Georgia,31190,United States,krichardson3@pcworld.com,8-(035)185-0387,4-(796)518-3676,3-(644)960-3789,13/04/1954,RPL,PT,06/06/2015,01/07/2015,krichardson3,P3EO0MVRPXbM';
185
my $input_good_branch = '1003,Linda,Richardson,Mr,Kimberly,LR,90,Place,Bayside,Atlanta,Erie,Georgia,31190,United States,krichardson3@pcworld.com,8-(035)185-0387,4-(796)518-3676,3-(644)960-3789,04/13/1954,RPL,PT,06/06/2015,07/01/2015,krichardson3,P3EO0MVRPXbM';
179
my $input_na_branch   = '1005,Ruth,Greene,Mr,Michael,RG,3,Avenue,Grim,Peoria,Jacksonville,Illinois,61614,United States,mgreene5@seesaa.net,3-(941)565-5752,1-(483)885-8138,4-(979)577-6908,09/02/1957,ZZZ,ST,02/04/2015,01/07/2015,mgreene5,or4ORT6JH';
186
my $input_na_branch   = '1005,Ruth,Greene,Mr,Michael,RG,3,Avenue,Grim,Peoria,Jacksonville,Illinois,61614,United States,mgreene5@seesaa.net,3-(941)565-5752,1-(483)885-8138,4-(979)577-6908,02/09/1957,ZZZ,ST,04/02/2015,07/01/2015,mgreene5,or4ORT6JH';
180
187
181
my $filename_5 = make_csv($temp_dir, $csv_headers, $input_no_branch, $input_good_branch, $input_na_branch);
188
my $filename_5 = make_csv($temp_dir, $csv_headers, $input_no_branch, $input_good_branch, $input_na_branch);
182
open(my $handle_5, "<", $filename_5) or die "cannot open < $filename_5: $!";
189
open(my $handle_5, "<", $filename_5) or die "cannot open < $filename_5: $!";
Lines 204-211 is($result_5->{errors}->[1]->{missing_criticals}->[0]->{value}, 'ZZZ', 'Got the Link Here
204
211
205
is($result_5->{feedback}->[0]->{feedback}, 1, 'Got the expected 1 feedback from import_patrons for branch tests');
212
is($result_5->{feedback}->[0]->{feedback}, 1, 'Got the expected 1 feedback from import_patrons for branch tests');
206
is($result_5->{feedback}->[0]->{name}, 'headerrow', 'Got the expected header row name from import_patrons for branch tests');
213
is($result_5->{feedback}->[0]->{name}, 'headerrow', 'Got the expected header row name from import_patrons for branch tests');
207
is($result_5->{feedback}->[0]->{value}, 'cardnumber, surname, firstname, title, othernames, initials, streetnumber, streettype, address, address2, city, state, zipcode, country, email, phone, mobile, fax, dateofbirth, branchcode, categorycode, dateenrolled, dateexpiry, userid, password',
214
is($result_5->{feedback}->[0]->{value}, $res_header, 'Got the expected header row value from import_patrons for branch tests');
208
                                        'Got the expected header row value from import_patrons for branch tests');
209
215
210
is($result_5->{feedback}->[1]->{feedback}, 1, 'Got the expected 1 feedback from import_patrons for branch tests');
216
is($result_5->{feedback}->[1]->{feedback}, 1, 'Got the expected 1 feedback from import_patrons for branch tests');
211
is($result_5->{feedback}->[1]->{name}, 'lastimported', 'Got the expected lastimported name from import_patrons for branch tests');
217
is($result_5->{feedback}->[1]->{name}, 'lastimported', 'Got the expected lastimported name from import_patrons for branch tests');
Lines 216-224 is($result_5->{invalid}, 2, 'Got the expected 2 invalid result from import patro Link Here
216
is($result_5->{overwritten}, 0, 'Got the expected 0 overwritten result from import patrons for branch tests');
222
is($result_5->{overwritten}, 0, 'Got the expected 0 overwritten result from import patrons for branch tests');
217
223
218
# Given ... 3 new inputs. One with no category code, one with unexpected category code.
224
# Given ... 3 new inputs. One with no category code, one with unexpected category code.
219
my $input_no_category   = '1006,Christina,Olson,Rev,Kimberly,CO,8,Avenue,Northridge,Lexington,Wilmington,Kentucky,40510,United States,kolson6@dropbox.com,7-(810)636-6048,1-(052)012-8984,8-(567)232-7818,26/03/1952,FFL,,07/09/2014,01/07/2015,kolson6,x5D3qGbLlptx';
225
my $input_no_category   = '1006,Christina,Olson,Rev,Kimberly,CO,8,Avenue,Northridge,Lexington,Wilmington,Kentucky,40510,United States,kolson6@dropbox.com,7-(810)636-6048,1-(052)012-8984,8-(567)232-7818,03/26/1952,FFL,,09/07/2014,01/07/2015,kolson6,x5D3qGbLlptx';
220
my $input_good_category = '1007,Peter,Peters,Mrs,Lawrence,PP,6,Trail,South,Oklahoma City,Topeka,Oklahoma,73135,United States,lpeters7@bandcamp.com,5-(992)205-9318,0-(732)586-9365,3-(448)146-7936,16/08/1983,PVL,T,24/03/2015,01/07/2015,lpeters7,Z19BrQ4';
226
my $input_good_category = '1007,Peter,Peters,Mrs,Lawrence,PP,6,Trail,South,Oklahoma City,Topeka,Oklahoma,73135,United States,lpeters7@bandcamp.com,5-(992)205-9318,0-(732)586-9365,3-(448)146-7936,08/16/1983,PVL,T,03/24/2015,07/01/2015,lpeters7,Z19BrQ4';
221
my $input_na_category   = '1008,Emily,Richards,Ms,Judy,ER,73,Way,Kedzie,Fort Wayne,Phoenix,Indiana,46825,United States,jrichards8@arstechnica.com,5-(266)658-8957,3-(550)500-9107,7-(816)675-9822,09/08/1984,FFL,ZZ,09/11/2014,01/07/2015,jrichards8,D5PvU6H2R';
227
my $input_na_category   = '1008,Emily,Richards,Ms,Judy,ER,73,Way,Kedzie,Fort Wayne,Phoenix,Indiana,46825,United States,jrichards8@arstechnica.com,5-(266)658-8957,3-(550)500-9107,7-(816)675-9822,08/09/1984,FFL,ZZ,11/09/2014,07/01/2015,jrichards8,D5PvU6H2R';
222
228
223
my $filename_6 = make_csv($temp_dir, $csv_headers, $input_no_category, $input_good_category, $input_na_category);
229
my $filename_6 = make_csv($temp_dir, $csv_headers, $input_no_category, $input_good_category, $input_na_category);
224
open(my $handle_6, "<", $filename_6) or die "cannot open < $filename_6: $!";
230
open(my $handle_6, "<", $filename_6) or die "cannot open < $filename_6: $!";
Lines 246-253 is($result_6->{errors}->[1]->{missing_criticals}->[0]->{value}, 'ZZ', 'Got the e Link Here
246
252
247
is($result_6->{feedback}->[0]->{feedback}, 1, 'Got the expected 1 feedback from import_patrons for category tests');
253
is($result_6->{feedback}->[0]->{feedback}, 1, 'Got the expected 1 feedback from import_patrons for category tests');
248
is($result_6->{feedback}->[0]->{name}, 'headerrow', 'Got the expected header row name from import_patrons for category tests');
254
is($result_6->{feedback}->[0]->{name}, 'headerrow', 'Got the expected header row name from import_patrons for category tests');
249
is($result_6->{feedback}->[0]->{value}, 'cardnumber, surname, firstname, title, othernames, initials, streetnumber, streettype, address, address2, city, state, zipcode, country, email, phone, mobile, fax, dateofbirth, branchcode, categorycode, dateenrolled, dateexpiry, userid, password',
255
is($result_6->{feedback}->[0]->{value}, $res_header, 'Got the expected header row value from import_patrons for category tests');
250
                                        'Got the expected header row value from import_patrons for category tests');
251
256
252
is($result_6->{feedback}->[1]->{feedback}, 1, 'Got the expected 1 feedback from import_patrons for category tests');
257
is($result_6->{feedback}->[1]->{feedback}, 1, 'Got the expected 1 feedback from import_patrons for category tests');
253
is($result_6->{feedback}->[1]->{name}, 'lastimported', 'Got the expected lastimported name from import_patrons for category tests');
258
is($result_6->{feedback}->[1]->{name}, 'lastimported', 'Got the expected lastimported name from import_patrons for category tests');
Lines 258-284 is($result_6->{invalid}, 2, 'Got the expected 2 invalid result from import patro Link Here
258
is($result_6->{overwritten}, 0, 'Got the expected 0 overwritten result from import patrons for category tests');
263
is($result_6->{overwritten}, 0, 'Got the expected 0 overwritten result from import patrons for category tests');
259
264
260
# Given ... 2 new inputs. One without dateofbirth, dateenrolled and dateexpiry values.
265
# Given ... 2 new inputs. One without dateofbirth, dateenrolled and dateexpiry values.
261
my $input_complete = '1009,Christina,Harris,Dr,Philip,CH,99,Street,Grayhawk,Baton Rouge,Dallas,Louisiana,70810,United States,pharris9@hp.com,9-(317)603-5513,7-(005)062-7593,8-(349)134-1627,19/06/1969,IPT,PT,09/04/2015,01/07/2015,pharris9,NcAhcvvnB';
266
my $input_complete = '1009,Christina,Harris,Dr,Philip,CH,99,Street,Grayhawk,Baton Rouge,Dallas,Louisiana,70810,United States,pharris9@hp.com,9-(317)603-5513,7-(005)062-7593,8-(349)134-1627,06/19/1969,IPT,PT,04/09/2015,07/01/2015,pharris9,NcAhcvvnB';
262
my $input_no_date  = '1010,Ralph,Warren,Ms,Linda,RW,6,Way,Barby,Orlando,Albany,Florida,32803,United States,lwarrena@multiply.com,7-(579)753-7752,6-(847)086-7566,9-(122)729-8226,,LPL,T,,,lwarrena,tJ56RD4uV';
267
my $input_no_date  = '1010,Ralph,Warren,Ms,Linda,RW,6,Way,Barby,Orlando,Albany,Florida,32803,United States,lwarrena@multiply.com,7-(579)753-7752,6-(847)086-7566,9-(122)729-8226,26/01/2001,LPL,T,25/01/2001,24/01/2001,lwarrena,tJ56RD4uV';
263
268
264
my $filename_7 = make_csv($temp_dir, $csv_headers, $input_complete, $input_no_date);
269
my $filename_7 = make_csv($temp_dir, $csv_headers, $input_complete, $input_no_date);
265
open(my $handle_7, "<", $filename_7) or die "cannot open < $filename_7: $!";
270
open(my $handle_7, "<", $filename_7) or die "cannot open < $filename_7: $!";
266
my $params_7 = { file => $handle_7, matchpoint => 'cardnumber', };
271
my $params_7 = { file => $handle_7, matchpoint => 'cardnumber', };
267
272
268
# Need upgrade to Moo
273
# When ...
274
my $result_7 = $patrons_import->import_patrons($params_7);
275
276
# Then ...
277
is($result_7->{already_in_db}, 0, 'Got the expected 0 already_in_db from import_patrons for dates tests');
278
is(scalar @{$result_7->{errors}}, 1, 'Got the expected 1 error array size from import_patrons for dates tests');
279
is(scalar @{$result_7->{errors}->[0]->{missing_criticals}}, 3, 'Got the expected 3 missing critical errors from import_patrons for dates tests');
280
281
is($result_7->{errors}->[0]->{missing_criticals}->[0]->{bad_date}, 1, 'Got the expected 1 bad_date error from import patrons for dates tests');
282
is($result_7->{errors}->[0]->{missing_criticals}->[0]->{borrowernumber}, 'UNDEF', 'Got the expected undef borrower number error from import patrons for dates tests');
283
is($result_7->{errors}->[0]->{missing_criticals}->[0]->{key}, 'dateofbirth', 'Got the expected dateofbirth key from import patrons for dates tests');
284
is($result_7->{errors}->[0]->{missing_criticals}->[0]->{line}, 3, 'Got the expected 2 line number error from import patrons for dates tests');
285
is($result_7->{errors}->[0]->{missing_criticals}->[0]->{lineraw}, $input_no_date."\r\n", 'Got the expected lineraw error from import patrons for dates tests');
286
is($result_7->{errors}->[0]->{missing_criticals}->[0]->{surname}, 'Ralph', 'Got the expected surname error from import patrons for dates tests');
287
288
is($result_7->{errors}->[0]->{missing_criticals}->[1]->{key}, 'dateenrolled', 'Got the expected dateenrolled key from import patrons for dates tests');
289
is($result_7->{errors}->[0]->{missing_criticals}->[2]->{key}, 'dateexpiry', 'Got the expected dateexpiry key from import patrons for dates tests');
290
291
is(scalar @{$result_7->{feedback}}, 2, 'Got the expected 2 feedback from import patrons for dates tests');
292
is($result_7->{feedback}->[0]->{feedback}, 1, 'Got the expected 1 feedback from import_patrons for dates tests');
293
is($result_7->{feedback}->[0]->{name}, 'headerrow', 'Got the expected header row name from import_patrons for dates tests');
294
is($result_7->{feedback}->[0]->{value}, $res_header, 'Got the expected header row value from import_patrons for dates tests');
295
296
is($result_7->{feedback}->[1]->{feedback}, 1, 'Got the expected 1 feedback from import_patrons for dates tests');
297
is($result_7->{feedback}->[1]->{name}, 'lastimported', 'Got the expected lastimported from import_patrons for dates tests');
298
like($result_7->{feedback}->[1]->{value}, qr/^Christina \/ \d+/, 'Got the expected lastimported value from import_patrons for dates tests');
299
300
is($result_7->{imported}, 1, 'Got the expected 1 imported result from import patrons for dates tests');
301
is($result_7->{invalid}, 1, 'Got the expected 1 invalid result from import patrons for dates tests');
302
is($result_7->{overwritten}, 0, 'Got the expected 0 overwritten result from import patrons for dates tests');
303
304
subtest 'test_prepare_columns' => sub {
305
    plan tests => 16;
306
307
    # Given ... no header row
308
    my $headerrow_0;
309
    my %csvkeycol_0;
310
    my @errors_0;
311
312
    # When ...
313
    my @csvcolumns_0 = $patrons_import->prepare_columns({headerrow => undef, keycol => \%csvkeycol_0, errors => \@errors_0, });
314
315
    # Then ...
316
    is(scalar @csvcolumns_0, 0, 'Got the expected empty column array from prepare columns with no header row');
317
318
    is(scalar @errors_0, 1, 'Got the expected 1 entry in error array from prepare columns with no header row');
319
    is($errors_0[0]->{badheader}, 1, 'Got the expected 1 badheader from prepare columns with no header row');
320
    is($errors_0[0]->{line}, 1, 'Got the expected 1 line from prepare columns with no header row');
321
    is($errors_0[0]->{lineraw}, undef, 'Got the expected undef lineraw from prepare columns with no header row');
322
323
    # Given ... a good header row with plenty of whitespaces
324
    my $headerrow_1 = 'a,    b ,        c,  ,   d';
325
    my %csvkeycol_1;
326
    my @errors_1;
327
328
    # When ...
329
    my @csvcolumns_1 = $patrons_import->prepare_columns({headerrow => $headerrow_1, keycol => \%csvkeycol_1, errors => \@errors_1, });
330
331
    # Then ...
332
    is(scalar @csvcolumns_1, 5, 'Got the expected 5 column array from prepare columns');
333
    is($csvcolumns_1[0], 'a', 'Got the expected a header from prepare columns');
334
    is($csvcolumns_1[1], 'b', 'Got the expected b header from prepare columns');
335
    is($csvcolumns_1[2], 'c', 'Got the expected c header from prepare columns');
336
    is($csvcolumns_1[3], '', 'Got the expected empty header from prepare columns');
337
    is($csvcolumns_1[4], 'd', 'Got the expected d header from prepare columns');
338
339
    is($csvkeycol_1{a}, 0, 'Got the expected 0 value for key a from prepare columns hash');
340
    is($csvkeycol_1{b}, 1, 'Got the expected 1 value for key b from prepare columns hash');
341
    is($csvkeycol_1{c}, 2, 'Got the expected 2 value for key c from prepare columns hash');
342
    is($csvkeycol_1{''}, 3, 'Got the expected 3 value for empty string key from prepare columns hash');
343
    is($csvkeycol_1{d}, 4, 'Got the expected 4 value for key d from prepare columns hash');
344
};
345
346
subtest 'test_set_column_keys' => sub {
347
    plan tests => 5;
348
349
    # Given ... nothing at all
350
    # When ... Then ...
351
    my $attr_type_0 = $patrons_import->set_attribute_types(undef);
352
    is($attr_type_0, undef, 'Got the expected undef attribute type from set attribute types with nothing');
353
354
    # Given ... extended but not matchpoint
355
    my $params_1 = { extended => 1, matchpoint => undef, };
356
357
    # When ... Then ...
358
    my $attr_type_1 = $patrons_import->set_attribute_types($params_1);
359
    is($attr_type_1, undef, 'Got the expected undef attribute type from set attribute types with no matchpoint');
360
361
    # Given ... extended and unexpected matchpoint
362
    my $params_2 = { extended => 1, matchpoint => 'unexpected', };
363
364
    # When ... Then ...
365
    my $attr_type_2 = $patrons_import->set_attribute_types($params_2);
366
    is($attr_type_2, undef, 'Got the expected undef attribute type from set attribute types with unexpected matchpoint');
367
368
    # Given ...
369
    my $code_3   = 'SHOW_BCODE';
370
    my $params_3 = { extended => 1, matchpoint => $code_3, };
371
372
    # When ...
373
    my $attr_type_3 = $patrons_import->set_attribute_types($params_3);
374
375
    # Then ...
376
    isa_ok($attr_type_3, 'C4::Members::AttributeTypes');
377
    is($attr_type_3->{code}, $code_3, 'Got the expected code attribute type from set attribute types');
378
};
379
269
subtest 'test_set_column_keys' => sub {
380
subtest 'test_set_column_keys' => sub {
270
    plan tests => 2;
381
    plan tests => 2;
271
382
272
    # Given ... nothing at all
383
    # Given ... nothing at all
273
    # When ... Then ...
384
    # When ... Then ...
274
    my @columnkeys_0 = Koha::Patrons::Import::set_column_keys(undef);
385
    my @columnkeys_0 = $patrons_import->set_column_keys(undef);
275
    is(scalar @columnkeys_0, 66, 'Got the expected array size from set column keys with undef extended');
386
    is(scalar @columnkeys_0, 66, 'Got the expected array size from set column keys with undef extended');
276
387
277
    # Given ... extended.
388
    # Given ... extended.
278
    my $extended = 1;
389
    my $extended = 1;
279
390
280
    # When ... Then ...
391
    # When ... Then ...
281
    my @columnkeys_1 = Koha::Patrons::Import::set_column_keys($extended);
392
    my @columnkeys_1 = $patrons_import->set_column_keys($extended);
282
    is(scalar @columnkeys_1, 67, 'Got the expected array size from set column keys with extended');
393
    is(scalar @columnkeys_1, 67, 'Got the expected array size from set column keys with extended');
283
};
394
};
284
395
Lines 287-300 subtest 'test_set_patron_attributes' => sub { Link Here
287
398
288
    # Given ... nothing at all
399
    # Given ... nothing at all
289
    # When ... Then ...
400
    # When ... Then ...
290
    my $result_0 = Koha::Patrons::Import::set_patron_attributes(undef, undef, undef);
401
    my $result_0 = $patrons_import->set_patron_attributes(undef, undef, undef);
291
    is($result_0, undef, 'Got the expected undef from set patron attributes with nothing');
402
    is($result_0, undef, 'Got the expected undef from set patron attributes with nothing');
292
403
293
    # Given ... not extended.
404
    # Given ... not extended.
294
    my $extended_1 = 0;
405
    my $extended_1 = 0;
295
406
296
    # When ... Then ...
407
    # When ... Then ...
297
    my $result_1 = Koha::Patrons::Import::set_patron_attributes($extended_1, undef, undef);
408
    my $result_1 = $patrons_import->set_patron_attributes($extended_1, undef, undef);
298
    is($result_1, undef, 'Got the expected undef from set patron attributes with not extended');
409
    is($result_1, undef, 'Got the expected undef from set patron attributes with not extended');
299
410
300
    # Given ... NO patrons attributes
411
    # Given ... NO patrons attributes
Lines 303-309 subtest 'test_set_patron_attributes' => sub { Link Here
303
    my @feedback_2;
414
    my @feedback_2;
304
415
305
    # When ...
416
    # When ...
306
    my $result_2 = Koha::Patrons::Import::set_patron_attributes($extended_2, $patron_attributes_2, \@feedback_2);
417
    my $result_2 = $patrons_import->set_patron_attributes($extended_2, $patron_attributes_2, \@feedback_2);
307
418
308
    # Then ...
419
    # Then ...
309
    is($result_2, undef, 'Got the expected undef from set patron attributes with no patrons attributes');
420
    is($result_2, undef, 'Got the expected undef from set patron attributes with no patrons attributes');
Lines 314-320 subtest 'test_set_patron_attributes' => sub { Link Here
314
    my @feedback_3;
425
    my @feedback_3;
315
426
316
    # When ...
427
    # When ...
317
    my $result_3 = Koha::Patrons::Import::set_patron_attributes($extended_2, $patron_attributes_3, \@feedback_3);
428
    my $result_3 = $patrons_import->set_patron_attributes($extended_2, $patron_attributes_3, \@feedback_3);
318
429
319
    # Then ...
430
    # Then ...
320
    ok($result_3, 'Got some data back from set patron attributes');
431
    ok($result_3, 'Got some data back from set patron attributes');
Lines 339-345 subtest 'test_check_branch_code' => sub { Link Here
339
    my @missing_criticals = ();
450
    my @missing_criticals = ();
340
451
341
    # When ...
452
    # When ...
342
    Koha::Patrons::Import::check_branch_code(undef, $borrowerline, $line_number, \@missing_criticals);
453
    $patrons_import->check_branch_code(undef, $borrowerline, $line_number, \@missing_criticals);
343
454
344
    # Then ...
455
    # Then ...
345
    is(scalar @missing_criticals, 1, 'Got the expected missing critical array size of 1 from check_branch_code with no branch code');
456
    is(scalar @missing_criticals, 1, 'Got the expected missing critical array size of 1 from check_branch_code with no branch code');
Lines 355-361 subtest 'test_check_branch_code' => sub { Link Here
355
    my @missing_criticals_1 = ();
466
    my @missing_criticals_1 = ();
356
467
357
    # When ...
468
    # When ...
358
    Koha::Patrons::Import::check_branch_code($branchcode_1, $borrowerline_1, $line_number_1, \@missing_criticals_1);
469
    $patrons_import->check_branch_code($branchcode_1, $borrowerline_1, $line_number_1, \@missing_criticals_1);
359
470
360
    # Then ...
471
    # Then ...
361
    is(scalar @missing_criticals_1, 1, 'Got the expected missing critical array size of 1 from check_branch_code with unexpected branch code');
472
    is(scalar @missing_criticals_1, 1, 'Got the expected missing critical array size of 1 from check_branch_code with unexpected branch code');
Lines 373-379 subtest 'test_check_branch_code' => sub { Link Here
373
    my @missing_criticals_2 = ();
484
    my @missing_criticals_2 = ();
374
485
375
    # When ...
486
    # When ...
376
    Koha::Patrons::Import::check_branch_code($branchcode_2, $borrowerline_2, $line_number_2, \@missing_criticals_2);
487
    $patrons_import->check_branch_code($branchcode_2, $borrowerline_2, $line_number_2, \@missing_criticals_2);
377
488
378
    # Then ...
489
    # Then ...
379
    is(scalar @missing_criticals_2, 0, 'Got the expected missing critical array size of 0 from check_branch_code');
490
    is(scalar @missing_criticals_2, 0, 'Got the expected missing critical array size of 0 from check_branch_code');
Lines 388-394 subtest 'test_check_borrower_category' => sub { Link Here
388
    my @missing_criticals = ();
499
    my @missing_criticals = ();
389
500
390
    # When ...
501
    # When ...
391
    Koha::Patrons::Import::check_borrower_category(undef, $borrowerline, $line_number, \@missing_criticals);
502
    $patrons_import->check_borrower_category(undef, $borrowerline, $line_number, \@missing_criticals);
392
503
393
    # Then ...
504
    # Then ...
394
    is(scalar @missing_criticals, 1, 'Got the expected missing critical array size of 1 from check_branch_code with no category code');
505
    is(scalar @missing_criticals, 1, 'Got the expected missing critical array size of 1 from check_branch_code with no category code');
Lines 404-410 subtest 'test_check_borrower_category' => sub { Link Here
404
    my @missing_criticals_1 = ();
515
    my @missing_criticals_1 = ();
405
516
406
    # When ...
517
    # When ...
407
    Koha::Patrons::Import::check_borrower_category($categorycode_1, $borrowerline_1, $line_number_1, \@missing_criticals_1);
518
    $patrons_import->check_borrower_category($categorycode_1, $borrowerline_1, $line_number_1, \@missing_criticals_1);
408
519
409
    # Then ...
520
    # Then ...
410
    is(scalar @missing_criticals_1, 1, 'Got the expected missing critical array size of 1 from check_branch_code with unexpected category code');
521
    is(scalar @missing_criticals_1, 1, 'Got the expected missing critical array size of 1 from check_branch_code with unexpected category code');
Lines 422-432 subtest 'test_check_borrower_category' => sub { Link Here
422
    my @missing_criticals_2 = ();
533
    my @missing_criticals_2 = ();
423
534
424
    # When ...
535
    # When ...
425
    Koha::Patrons::Import::check_borrower_category($categorycode_2, $borrowerline_2, $line_number_2, \@missing_criticals_2);
536
    $patrons_import->check_borrower_category($categorycode_2, $borrowerline_2, $line_number_2, \@missing_criticals_2);
426
537
427
    # Then ...
538
    # Then ...
428
    is(scalar @missing_criticals_2, 0, 'Got the expected missing critical array size of 0 from check_branch_code');
539
    is(scalar @missing_criticals_2, 0, 'Got the expected missing critical array size of 0 from check_branch_code');
429
};
540
};
541
542
subtest 'test_format_dates' => sub {
543
    plan tests => 22;
544
545
    # Given ... no borrower data.
546
    my $borrowerline      = 'another line';
547
    my $line_number       = 987;
548
    my @missing_criticals = ();
549
    my %borrower;
550
    my $params = {borrower => \%borrower, lineraw => $borrowerline, line => $line_number, missing_criticals => \@missing_criticals, };
551
552
    # When ...
553
    $patrons_import->format_dates($params);
554
555
    # Then ...
556
    ok( not(%borrower), 'Got the expected no borrower from format_dates with no dates');
557
    is(scalar @missing_criticals, 0, 'Got the expected missing critical array size of 0 from format_dates with no dates');
558
559
    # Given ... some good dates
560
    my @missing_criticals_1 = ();
561
    my $dateofbirth_1  = '2016-05-03';
562
    my $dateenrolled_1 = '2016-05-04';
563
    my $dateexpiry_1   = '2016-05-06';
564
    my $borrower_1     = { dateofbirth => $dateofbirth_1, dateenrolled => $dateenrolled_1, dateexpiry => $dateexpiry_1, };
565
    my $params_1       = {borrower => $borrower_1, lineraw => $borrowerline, line => $line_number, missing_criticals => \@missing_criticals_1, };
566
567
    # When ...
568
    $patrons_import->format_dates($params_1);
569
570
    # Then ...
571
    is($borrower_1->{dateofbirth}, $dateofbirth_1, 'Got the expected date of birth from format_dates with good dates');
572
    is($borrower_1->{dateenrolled}, $dateenrolled_1, 'Got the expected date of birth from format_dates with good dates');
573
    is($borrower_1->{dateexpiry}, $dateexpiry_1, 'Got the expected date of birth from format_dates with good dates');
574
    is(scalar @missing_criticals_1, 0, 'Got the expected missing critical array size of 0 from check_branch_code with good dates');
575
576
    # Given ... some very bad dates
577
    my @missing_criticals_2 = ();
578
    my $dateofbirth_2  = '03-2016-05';
579
    my $dateenrolled_2 = '04-2016-05';
580
    my $dateexpiry_2   = '06-2016-05';
581
    my $borrower_2     = { dateofbirth => $dateofbirth_2, dateenrolled => $dateenrolled_2, dateexpiry => $dateexpiry_2, };
582
    my $params_2       = {borrower => $borrower_2, lineraw => $borrowerline, line => $line_number, missing_criticals => \@missing_criticals_2, };
583
584
    # When ...
585
    $patrons_import->format_dates($params_2);
586
587
    # Then ...
588
    is($borrower_2->{dateofbirth}, '', 'Got the expected empty date of birth from format_dates with bad dates');
589
    is($borrower_2->{dateenrolled}, '', 'Got the expected emptydate of birth from format_dates with bad dates');
590
    is($borrower_2->{dateexpiry}, '', 'Got the expected empty date of birth from format_dates with bad dates');
591
592
    is(scalar @missing_criticals_2, 3, 'Got the expected missing critical array size of 3 from check_branch_code with bad dates');
593
    is($missing_criticals_2[0]->{bad_date}, 1, 'Got the expected first bad date flag from check_branch_code with bad dates');
594
    is($missing_criticals_2[0]->{key}, 'dateofbirth', 'Got the expected dateofbirth key from check_branch_code with bad dates');
595
    is($missing_criticals_2[0]->{line}, $line_number, 'Got the expected first line from check_branch_code with bad dates');
596
    is($missing_criticals_2[0]->{lineraw}, $borrowerline, 'Got the expected first lineraw from check_branch_code with bad dates');
597
598
    is($missing_criticals_2[1]->{bad_date}, 1, 'Got the expected second bad date flag from check_branch_code with bad dates');
599
    is($missing_criticals_2[1]->{key}, 'dateenrolled', 'Got the expected dateenrolled key from check_branch_code with bad dates');
600
    is($missing_criticals_2[1]->{line}, $line_number, 'Got the expected second line from check_branch_code with bad dates');
601
    is($missing_criticals_2[1]->{lineraw}, $borrowerline, 'Got the expected second lineraw from check_branch_code with bad dates');
602
603
    is($missing_criticals_2[2]->{bad_date}, 1, 'Got the expected third bad date flag from check_branch_code with bad dates');
604
    is($missing_criticals_2[2]->{key}, 'dateexpiry', 'Got the expected dateexpiry key from check_branch_code with bad dates');
605
    is($missing_criticals_2[2]->{line}, $line_number, 'Got the expected third line from check_branch_code with bad dates');
606
    is($missing_criticals_2[2]->{lineraw}, $borrowerline, 'Got the expected third lineraw from check_branch_code with bad dates');
607
};
608
430
# ###### Test utility ###########
609
# ###### Test utility ###########
431
sub make_csv {
610
sub make_csv {
432
    my ($temp_dir, @lines) = @_;
611
    my ($temp_dir, @lines) = @_;
433
- 

Return to bug 12598