Bug 34621: implement Patron import option to 'Renew existing patrons' 'from the curre...
[koha.git] / t / db_dependent / Koha / Patrons / Import.t
1 #!/usr/bin/perl
2
3 # Copyright 2015 Koha Development team
4 #
5 # This file is part of Koha
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use Modern::Perl;
21 use Test::More tests => 180;
22 use Test::Warn;
23 use Test::Exception;
24 use Encode qw( encode_utf8 );
25 use utf8;
26
27 # To be replaced by t::lib::Mock
28 use Test::MockModule;
29 use Koha::Database;
30 use Koha::Patron::Relationships;
31 use Koha::DateUtils qw(dt_from_string);
32
33 use File::Temp qw(tempfile tempdir);
34 my $temp_dir = tempdir('Koha_patrons_import_test_XXXX', CLEANUP => 1, TMPDIR => 1);
35
36 use t::lib::Mocks;
37 use t::lib::TestBuilder;
38 my $builder = t::lib::TestBuilder->new;
39
40 my $schema = Koha::Database->new->schema;
41 $schema->storage->txn_begin;
42
43 # ########## Tests start here #############################
44 # Given ... we can use the module
45 BEGIN { use_ok('Koha::Patrons::Import'); }
46
47 my $patrons_import = new_ok('Koha::Patrons::Import');
48
49 subtest 'test_methods' => sub {
50     plan tests => 1;
51
52     # Given ... we can reach the method(s)
53     my @methods = ('import_patrons',
54                    'set_attribute_types',
55                    'prepare_columns',
56                    'set_column_keys',
57                    'generate_patron_attributes',
58                    'check_branch_code',
59                    'format_dates',
60                   );
61     can_ok('Koha::Patrons::Import', @methods);
62 };
63
64 subtest 'test_attributes' => sub {
65     plan tests => 1;
66
67     my @attributes = ('today_iso', 'text_csv');
68     can_ok('Koha::Patrons::Import', @attributes);
69 };
70
71 # Tests for Koha::Patrons::Import::import_patrons()
72 # Given ... nothing much. When ... Then ...
73 my $result;
74 warning_is { $result = $patrons_import->import_patrons(undef) }
75            { carped => 'No file handle passed in!' },
76            " Koha::Patrons::Import->import_patrons carps if no file handle is passed";
77 is($result, undef, 'Got the expected undef from import_patrons with nothing much');
78
79 # Given ... some params but no file handle.
80 my $params_0 = { some_stuff => 'random stuff', };
81
82 # When ... Then ...
83 my $result_0;
84 warning_is { $result_0 = $patrons_import->import_patrons($params_0) }
85            { carped => 'No file handle passed in!' },
86            " Koha::Patrons::Import->import_patrons carps if no file handle is passed";
87 is($result_0, undef, 'Got the expected undef from import_patrons with no file handle');
88
89 # Given ... a file handle to file with headers only.
90 t::lib::Mocks::mock_preference('ExtendedPatronAttributes', 0);
91 t::lib::Mocks::mock_preference('dateformat', 'us');
92
93 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';
94 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';
95 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';
96 my $csv_one_line_a = '1001,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';
97 my $csv_one_line_b = '1000,Nancy2,Jenkins2,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';
98
99 my $filename_1 = make_csv($temp_dir, $csv_headers, $csv_one_line);
100 open(my $handle_1, "<", $filename_1) or die "cannot open < $filename_1: $!";
101 my $params_1 = { file => $handle_1, };
102
103 # When ...
104 my $result_1 = $patrons_import->import_patrons($params_1);
105
106 # Then ...
107 is($result_1->{already_in_db}, 0, 'Got the expected 0 already_in_db from import_patrons with no matchpoint defined');
108 is(scalar @{$result_1->{errors}}, 0, 'Got the expected 0 size error array from import_patrons with no matchpoint defined');
109
110 is($result_1->{feedback}->[0]->{feedback}, 1, 'Got the expected 1 feedback from import_patrons with no matchpoint defined');
111 is($result_1->{feedback}->[0]->{name}, 'headerrow', 'Got the expected header row name from import_patrons with no matchpoint defined');
112 is($result_1->{feedback}->[0]->{value}, $res_header, 'Got the expected header row value from import_patrons with no matchpoint defined');
113
114 is($result_1->{feedback}->[1]->{feedback}, 1, 'Got the expected second feedback from import_patrons with no matchpoint defined');
115 is($result_1->{feedback}->[1]->{name}, 'lastimported', 'Got the expected last imported name from import_patrons with no matchpoint defined');
116 like($result_1->{feedback}->[1]->{value}, qr/^Nancy \/ \d+/, 'Got the expected second header row value from import_patrons with no matchpoint defined');
117
118 is($result_1->{imported}, 1, 'Got the expected 1 imported result from import_patrons with no matchpoint defined');
119 is($result_1->{invalid}, 0, 'Got the expected 0 invalid result from import_patrons with no matchpoint defined');
120 is($result_1->{overwritten}, 0, 'Got the expected 0 overwritten result from import_patrons with no matchpoint defined');
121
122 # Given ... a valid file handle, a bad matchpoint resulting in invalid card number
123 my $filename_2 = make_csv($temp_dir, $csv_headers, $csv_one_line);
124 open(my $handle_2, "<", $filename_2) or die "cannot open < $filename_2: $!";
125 my $params_2 = { file => $handle_2, matchpoint => 'SHOW_BCODE', };
126
127 # When ...
128 my $result_2 = $patrons_import->import_patrons($params_2);
129
130 # Then ...
131 is($result_2->{already_in_db}, 0, 'Got the expected 0 already_in_db from import_patrons with invalid card number');
132 is($result_2->{errors}->[0]->{borrowernumber}, undef, 'Got the expected undef borrower number from import patrons with invalid card number');
133 is($result_2->{errors}->[0]->{cardnumber}, 1000, 'Got the expected 1000 card number from import patrons with invalid card number');
134 is($result_2->{errors}->[0]->{invalid_cardnumber}, 1, 'Got the expected invalid card number from import patrons with invalid card number');
135
136 is($result_2->{feedback}->[0]->{feedback}, 1, 'Got the expected 1 feedback from import_patrons with invalid card number');
137 is($result_2->{feedback}->[0]->{name}, 'headerrow', 'Got the expected header row name from import_patrons with invalid card number');
138 is($result_2->{feedback}->[0]->{value}, $res_header, 'Got the expected header row value from import_patrons with invalid card number');
139
140 is($result_2->{imported}, 0, 'Got the expected 0 imported result from import_patrons with invalid card number');
141 is($result_2->{invalid}, 1, 'Got the expected 1 invalid result from import_patrons with invalid card number');
142 is($result_2->{overwritten}, 0, 'Got the expected 0 overwritten result from import_patrons with invalid card number');
143
144 # Given ... valid file handle, good matchpoint that matches should not overwrite when not set.
145 my $filename_3 = make_csv($temp_dir, $csv_headers, $csv_one_line);
146 open(my $handle_3, "<", $filename_3) or die "cannot open < $filename_3: $!";
147 my $params_3 = { file => $handle_3, matchpoint => 'cardnumber', };
148
149 # When ...
150 my $result_3 = $patrons_import->import_patrons($params_3);
151
152 # Then ...
153 is($result_3->{already_in_db}, 1, 'Got the expected 1 already_in_db from import_patrons with duplicate userid');
154 is($result_3->{errors}->[0]->{duplicate_userid}, undef, 'No duplicate userid error from import patrons with duplicate userid (it is our own)');
155 is($result_3->{errors}->[0]->{userid}, undef, 'No duplicate userid error from import patrons with duplicate userid (it is our own)');
156
157 is($result_3->{feedback}->[0]->{feedback}, 1, 'Got 1 expected feedback from import_patrons that matched but not overwritten');
158 is($result_3->{feedback}->[0]->{name}, 'headerrow', 'Got the expected header row name from import_patrons with duplicate userid');
159 is($result_3->{feedback}->[0]->{value}, $res_header, 'Got the expected header row value from import_patrons with duplicate userid');
160
161 is($result_3->{imported}, 0, 'Got the expected 0 imported result from import_patrons');
162 is($result_3->{invalid}, 0, 'Got the expected 0 invalid result from import_patrons');
163 is($result_3->{overwritten}, 0, 'Got the expected 0 overwritten result from import_patrons that matched');
164
165 # Given ... valid file handle, good matchpoint that matches should overwrite when set.
166 my $filename_3a = make_csv($temp_dir, $csv_headers, $csv_one_line);
167 open(my $handle_3a, "<", $filename_3a) or die "cannot open < $filename_3: $!";
168 my $params_3a = { file => $handle_3a, matchpoint => 'cardnumber', overwrite_cardnumber => 1};
169
170 # When ...
171 my $result_3a;
172 warning_is { $result_3a = $patrons_import->import_patrons($params_3a) }
173            undef,
174            "No warning raised by import_patrons";
175
176 # Then ...
177 is($result_3a->{already_in_db}, 0, 'Got the expected 0 already_in_db from import_patrons when matched and overwrite set');
178 is($result_3a->{errors}->[0]->{duplicate_userid}, undef, 'No duplicate userid error from import patrons with duplicate userid (it is our own)');
179 is($result_3a->{errors}->[0]->{userid}, undef, 'No duplicate userid error from import patrons with duplicate userid (it is our own)');
180
181 is($result_3a->{feedback}->[0]->{feedback}, 1, 'Got 1 expected feedback from import_patrons that matched and overwritten');
182 is($result_3a->{feedback}->[0]->{name}, 'headerrow', 'Got the expected header row name from import_patrons with duplicate userid');
183 is($result_3a->{feedback}->[0]->{value}, $res_header, 'Got the expected header row value from import_patrons with duplicate userid');
184
185 is($result_3a->{imported}, 0, 'Got the expected 0 imported result from import_patrons');
186 is($result_3a->{invalid}, 0, 'Got the expected 0 invalid result from import_patrons');
187 is($result_3a->{overwritten}, 1, 'Got the expected 1 overwritten result from import_patrons that matched');
188
189 my $patron_3 = Koha::Patrons->find({ cardnumber => '1000' });
190 is( $patron_3->dateexpiry, '2015-07-01', "Expiration date is correct with update_dateexpiry = false" );
191
192 # Given ... valid file handle, good matchpoint that matches should overwrite when set, surname is protected from
193 # overwrite but firstname is not
194 my $filename_3c = make_csv($temp_dir, $csv_headers, $csv_one_line_b);
195 open(my $handle_3c, "<", $filename_3c) or die "cannot open < $filename_3: $!";
196 my $params_3c = { file => $handle_3c, matchpoint => 'cardnumber', overwrite_cardnumber => 1, preserve_fields => [ 'firstname' ], update_dateexpiry => 1, update_dateexpiry_from_today => 1 };
197
198 # When ...
199 my $result_3c;
200 warning_is { $result_3c = $patrons_import->import_patrons($params_3c) }
201     undef,
202     "No warning raised by import_patrons";
203
204 # Then ...
205 is($result_3c->{already_in_db}, 0, 'Got the expected 0 already_in_db from import_patrons when matched and overwrite set');
206 is($result_3c->{errors}->[0]->{duplicate_userid}, undef, 'No duplicate userid error from import patrons with duplicate userid (it is our own)');
207 is($result_3c->{errors}->[0]->{userid}, undef, 'No duplicate userid error from import patrons with duplicate userid (it is our own)');
208
209 is($result_3c->{feedback}->[0]->{feedback}, 1, 'Got 1 expected feedback from import_patrons that matched and overwritten');
210 is($result_3c->{feedback}->[0]->{name}, 'headerrow', 'Got the expected header row name from import_patrons with duplicate userid');
211 is($result_3c->{feedback}->[0]->{value}, $res_header, 'Got the expected header row value from import_patrons with duplicate userid');
212
213 is($result_3c->{imported}, 0, 'Got the expected 0 imported result from import_patrons');
214 is($result_3c->{invalid}, 0, 'Got the expected 0 invalid result from import_patrons');
215 is($result_3c->{overwritten}, 1, 'Got the expected 1 overwritten result from import_patrons that matched');
216
217 my $patron_3c = Koha::Patrons->find({ cardnumber => '1000' });
218 is( $patron_3c->dateexpiry, dt_from_string->add( months => 99, end_of_month => 'limit' )->ymd, "Expiration date is correct with update_dateexpiry = true" );
219
220 is( $patron_3c->surname, "Nancy2", "Surname field is preserved from original" );
221 is( $patron_3c->firstname, "Jenkins", "Firstname field is overwritten" );
222
223 # test update_dateexpiry_from_current
224 my $filename_3d = make_csv($temp_dir, $csv_headers, $csv_one_line_b);
225 open(my $handle_3d, "<", $filename_3d) or die "cannot open < $filename_3: $!";
226 $patron_3c->dateexpiry('1980-01-01')->store();
227 my $params_3d = { file => $handle_3d, matchpoint => 'cardnumber', overwrite_cardnumber => 1, preserve_fields => [ 'firstname' ], update_dateexpiry => 1, update_dateexpiry_from_existing => 1 };
228 warning_is { $patrons_import->import_patrons($params_3d) }
229     undef,
230     "No warning raised by import_patrons";
231 $patron_3c = Koha::Patrons->find({ cardnumber => '1000' });
232 is( $patron_3c->dateexpiry, '1988-04-01', "Expiration date is correct with update_dateexpiry_from_existing => 1" );
233 # /test update_dateexpiry_from_current
234
235 # Given ... valid file handle, good matchpoint that does not match and conflicting userid.
236 my $filename_3b = make_csv($temp_dir, $csv_headers, $csv_one_line_a);
237 open(my $handle_3b, "<", $filename_3b) or die "cannot open < $filename_3: $!";
238 my $params_3b = { file => $handle_3b, matchpoint => 'cardnumber', };
239
240 # When ...
241 my $result_3b = $patrons_import->import_patrons($params_3b);
242
243 # Then ...
244 is($result_3b->{already_in_db}, 0, 'Got the expected 0 already_in_db from import_patrons with duplicate userid');
245 is($result_3b->{errors}->[0]->{duplicate_userid}, 1, 'Got the expected duplicate userid error from import patrons with duplicate userid');
246 is($result_3b->{errors}->[0]->{userid}, 'jjenkins0', 'Got the expected userid error from import patrons with duplicate userid');
247
248 is($result_3b->{feedback}->[0]->{feedback}, 1, 'Got the expected 1 feedback from import_patrons with duplicate userid');
249 is($result_3b->{feedback}->[0]->{name}, 'headerrow', 'Got the expected header row name from import_patrons with duplicate userid');
250 is($result_3b->{feedback}->[0]->{value}, $res_header, 'Got the expected header row value from import_patrons with duplicate userid');
251
252 is($result_3b->{imported}, 0, 'Got the expected 0 imported result from import_patrons with duplicate userid');
253 is($result_3b->{invalid}, 1, 'Got the expected 1 invalid result from import_patrons with duplicate userid');
254 is($result_3b->{overwritten}, 0, 'Got the expected 0 overwritten result from import_patrons with duplicate userid');
255
256 # Given ... a new input and mocked C4::Context
257 t::lib::Mocks::mock_preference('ExtendedPatronAttributes', 1);
258 my $attribute = $builder->build({ source => "BorrowerAttributeType"});
259
260 my $csv_headers_a  = '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,patron_attributes';
261 my $res_header_a   = '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, patron_attributes';
262 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,'.$attribute->{code}.':1';
263 my $filename_4 = make_csv($temp_dir, $csv_headers_a, $new_input_line);
264 open(my $handle_4, "<", $filename_4) or die "cannot open < $filename_4: $!";
265 my $params_4 = { file => $handle_4, matchpoint => $attribute->{code}, };
266
267 # When ...
268 my $result_4 = $patrons_import->import_patrons($params_4);
269
270 # Then ...
271 is($result_4->{already_in_db}, 0, 'Got the expected 0 already_in_db from import_patrons with extended user');
272 is(scalar @{$result_4->{errors}}, 0, 'Got the expected 0 size error array from import_patrons with extended user');
273
274 is($result_4->{feedback}->[0]->{feedback}, 1, 'Got the expected 1 feedback from import_patrons with extended user');
275 is($result_4->{feedback}->[0]->{name}, 'headerrow', 'Got the expected header row name from import_patrons with extended user');
276 is($result_4->{feedback}->[0]->{value}, $res_header_a, 'Got the expected header row value from import_patrons with extended user');
277
278 is($result_4->{feedback}->[1]->{feedback}, 1, 'Got the expected second feedback from import_patrons with extended user');
279 is($result_4->{feedback}->[1]->{name}, 'attribute string', 'Got the expected attribute string from import_patrons with extended user');
280 is($result_4->{feedback}->[1]->{value}, $attribute->{code}.':1', 'Got the expected second feedback value from import_patrons with extended user');
281
282 is($result_4->{feedback}->[2]->{feedback}, 1, 'Got the expected third feedback from import_patrons with extended user');
283 is($result_4->{feedback}->[2]->{name}, 'lastimported', 'Got the expected last imported name from import_patrons with extended user');
284 like($result_4->{feedback}->[2]->{value}, qr/^Donna \/ \d+/, 'Got the expected third feedback value from import_patrons with extended user');
285
286 is($result_4->{imported}, 1, 'Got the expected 1 imported result from import_patrons with extended user');
287 is($result_4->{invalid}, 0, 'Got the expected 0 invalid result from import_patrons with extended user');
288 is($result_4->{overwritten}, 0, 'Got the expected 0 overwritten result from import_patrons with extended user');
289
290 seek $handle_4,0,0; #Reset to verify finding a matched patron works
291 my $result_4a = $patrons_import->import_patrons($params_4);
292 is($result_4a->{already_in_db}, 1, 'Got the expected 1 already_in_db from import_patrons with extended user matched');
293 is(scalar @{$result_4->{errors}}, 0, 'Got the expected 0 size error array from import_patrons with extended user matched');
294
295 is($result_4a->{feedback}->[0]->{feedback}, 1, 'Got the expected 1 feedback from import_patrons with extended user matched');
296 is($result_4a->{feedback}->[0]->{name}, 'headerrow', 'Got the expected header row name from import_patrons with extended user matched');
297 is($result_4a->{feedback}->[0]->{value}, $res_header_a, 'Got the expected header row value from import_patrons with extended user matched');
298
299 is($result_4a->{feedback}->[1]->{feedback}, 1, 'Got the expected second feedback from import_patrons with extended user matched');
300 is($result_4a->{feedback}->[1]->{name}, 'attribute string', 'Got the expected attribute string from import_patrons with extended user matched');
301 is($result_4a->{feedback}->[1]->{value}, $attribute->{code}.':1', 'Got the expected second feedback value from import_patrons with extended user matched');
302
303 is($result_4a->{feedback}->[2]->{already_in_db}, '1', 'Got the expected already_in_db from import_patrons with extended user matched');
304 like($result_4a->{feedback}->[2]->{value}, qr/^Donna \/ \d+/, 'Got the expected third feedback value from import_patrons with extended user matched');
305
306 is($result_4a->{imported}, 0, 'Got the expected 0 imported result from import_patrons with extended user matched');
307 is($result_4a->{invalid}, 0, 'Got the expected 0 invalid result from import_patrons with extended user matched');
308 is($result_4a->{overwritten}, 0, 'Got the expected 0 overwritten result from import_patrons with extended user matched');
309
310 t::lib::Mocks::mock_preference('ExtendedPatronAttributes', 0);
311
312 my $surname ='Chloé❤';
313 # Given ... 3 new inputs. One with no branch code, one with unexpected branch code.
314 my $input_no_branch   = qq|1002,$surname,Reynolds,Mr,Patricia,JR,12,Hill,Kennedy,Saint Louis,Colorado Springs,Missouri,63131,United States,preynolds2i\@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|;
315 my $input_good_branch = qq|1003,$surname,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|;
316 my $input_na_branch   = qq|1005,$surname,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|;
317
318 my $filename_5 = make_csv($temp_dir, $csv_headers, encode_utf8($input_no_branch), encode_utf8($input_good_branch), encode_utf8($input_na_branch));
319 open(my $handle_5, "<", $filename_5) or die "cannot open < $filename_5: $!";
320 my $params_5 = { file => $handle_5, matchpoint => 'cardnumber', };
321
322 # When ...
323 my $result_5 = $patrons_import->import_patrons($params_5);
324
325 # Then ...
326 is($result_5->{already_in_db}, 0, 'Got the expected 0 already_in_db from import_patrons for branch tests');
327
328 is($result_5->{errors}->[0]->{missing_criticals}->[0]->{borrowernumber}, 'UNDEF', 'Got the expected undef borrower number error from import patrons for branch tests');
329 is($result_5->{errors}->[0]->{missing_criticals}->[0]->{key}, 'branchcode', 'Got the expected branch code key from import patrons for branch tests');
330 is($result_5->{errors}->[0]->{missing_criticals}->[0]->{line}, 2, 'Got the expected 2 line number error from import patrons for branch tests');
331 is($result_5->{errors}->[0]->{missing_criticals}->[0]->{lineraw}, $input_no_branch."\r\n", 'Got the expected lineraw error from import patrons for branch tests');
332 is($result_5->{errors}->[0]->{missing_criticals}->[0]->{surname}, $surname, 'Got the expected surname error from import patrons for branch tests');
333
334 is($result_5->{errors}->[1]->{missing_criticals}->[0]->{borrowernumber}, 'UNDEF', 'Got the expected undef borrower number error from import patrons for branch tests');
335 is($result_5->{errors}->[1]->{missing_criticals}->[0]->{branch_map}, 1, 'Got the expected 1 branchmap error from import patrons for branch tests');
336 is($result_5->{errors}->[1]->{missing_criticals}->[0]->{key}, 'branchcode', 'Got the expected branch code key from import patrons for branch tests');
337 is($result_5->{errors}->[1]->{missing_criticals}->[0]->{line}, 4, 'Got the expected 4 line number error from import patrons for branch tests');
338 is($result_5->{errors}->[1]->{missing_criticals}->[0]->{lineraw}, $input_na_branch."\r\n", 'Got the expected lineraw error from import patrons for branch tests');
339 is($result_5->{errors}->[1]->{missing_criticals}->[0]->{surname}, $surname, 'Got the expected surname error from import patrons for branch tests');
340 is($result_5->{errors}->[1]->{missing_criticals}->[0]->{value}, 'ZZZ', 'Got the expected ZZZ value error from import patrons for branch tests');
341
342 is($result_5->{feedback}->[0]->{feedback}, 1, 'Got the expected 1 feedback from import_patrons for branch tests');
343 is($result_5->{feedback}->[0]->{name}, 'headerrow', 'Got the expected header row name from import_patrons for branch tests');
344 is($result_5->{feedback}->[0]->{value}, $res_header, 'Got the expected header row value from import_patrons for branch tests');
345
346 is($result_5->{feedback}->[1]->{feedback}, 1, 'Got the expected 1 feedback from import_patrons for branch tests');
347 is($result_5->{feedback}->[1]->{name}, 'lastimported', 'Got the expected lastimported name from import_patrons for branch tests');
348 like($result_5->{feedback}->[1]->{value},  qr/^$surname \/ \d+/, 'Got the expected last imported value from import_patrons with for branch tests');
349
350 is($result_5->{imported}, 1, 'Got the expected 1 imported result from import patrons for branch tests');
351 is($result_5->{invalid}, 2, 'Got the expected 2 invalid result from import patrons for branch tests');
352 is($result_5->{overwritten}, 0, 'Got the expected 0 overwritten result from import patrons for branch tests');
353
354 # Given ... 3 new inputs. One with no category code, one with unexpected category code.
355 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';
356 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';
357 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';
358
359 my $filename_6 = make_csv($temp_dir, $csv_headers, $input_no_category, $input_good_category, $input_na_category);
360 open(my $handle_6, "<", $filename_6) or die "cannot open < $filename_6: $!";
361 my $params_6 = { file => $handle_6, matchpoint => 'cardnumber', };
362
363 # When ...
364 my $result_6 = $patrons_import->import_patrons($params_6);
365
366 # Then ...
367 is($result_6->{already_in_db}, 0, 'Got the expected 0 already_in_db from import_patrons for category tests');
368
369 is($result_6->{errors}->[0]->{missing_criticals}->[0]->{borrowernumber}, 'UNDEF', 'Got the expected undef borrower number error from import patrons for category tests');
370 is($result_6->{errors}->[0]->{missing_criticals}->[0]->{key}, 'categorycode', 'Got the expected category code key from import patrons for category tests');
371 is($result_6->{errors}->[0]->{missing_criticals}->[0]->{line}, 2, 'Got the expected 2 line number error from import patrons for category tests');
372 is($result_6->{errors}->[0]->{missing_criticals}->[0]->{lineraw}, $input_no_category."\r\n", 'Got the expected lineraw error from import patrons for category tests');
373 is($result_6->{errors}->[0]->{missing_criticals}->[0]->{surname}, 'Christina', 'Got the expected surname error from import patrons for category tests');
374
375 is($result_6->{errors}->[1]->{missing_criticals}->[0]->{borrowernumber}, 'UNDEF', 'Got the expected undef borrower number error from import patrons for category tests');
376 is($result_6->{errors}->[1]->{missing_criticals}->[0]->{category_map}, 1, 'Got the expected 1 category_map error from import patrons for category tests');
377 is($result_6->{errors}->[1]->{missing_criticals}->[0]->{key}, 'categorycode', 'Got the expected category code key from import patrons for category tests');
378 is($result_6->{errors}->[1]->{missing_criticals}->[0]->{line}, 4, 'Got the expected 4 line number error from import patrons for category tests');
379 is($result_6->{errors}->[1]->{missing_criticals}->[0]->{lineraw}, $input_na_category."\r\n", 'Got the expected lineraw error from import patrons for category tests');
380 is($result_6->{errors}->[1]->{missing_criticals}->[0]->{surname}, 'Emily', 'Got the expected surname error from import patrons for category tests');
381 is($result_6->{errors}->[1]->{missing_criticals}->[0]->{value}, 'ZZ', 'Got the expected ZZ value error from import patrons for category tests');
382
383 is($result_6->{feedback}->[0]->{feedback}, 1, 'Got the expected 1 feedback from import_patrons for category tests');
384 is($result_6->{feedback}->[0]->{name}, 'headerrow', 'Got the expected header row name from import_patrons for category tests');
385 is($result_6->{feedback}->[0]->{value}, $res_header, 'Got the expected header row value from import_patrons for category tests');
386
387 is($result_6->{feedback}->[1]->{feedback}, 1, 'Got the expected 1 feedback from import_patrons for category tests');
388 is($result_6->{feedback}->[1]->{name}, 'lastimported', 'Got the expected lastimported name from import_patrons for category tests');
389 like($result_6->{feedback}->[1]->{value},  qr/^Peter \/ \d+/, 'Got the expected last imported value from import_patrons with for category tests');
390
391 is($result_6->{imported}, 1, 'Got the expected 1 imported result from import patrons for category tests');
392 is($result_6->{invalid}, 2, 'Got the expected 2 invalid result from import patrons for category tests');
393 is($result_6->{overwritten}, 0, 'Got the expected 0 overwritten result from import patrons for category tests');
394
395 # Given ... 2 new inputs. One without dateofbirth, dateenrolled and dateexpiry values.
396 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';
397 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';
398
399 my $filename_7 = make_csv($temp_dir, $csv_headers, $input_complete, $input_no_date);
400 open(my $handle_7, "<", $filename_7) or die "cannot open < $filename_7: $!";
401 my $params_7 = { file => $handle_7, matchpoint => 'cardnumber', };
402
403 # When ...
404 my $result_7 = $patrons_import->import_patrons($params_7);
405
406 # Then ...
407 is($result_7->{already_in_db}, 0, 'Got the expected 0 already_in_db from import_patrons for dates tests');
408 is(scalar @{$result_7->{errors}}, 1, 'Got the expected 1 error array size from import_patrons for dates tests');
409 is(scalar @{$result_7->{errors}->[0]->{missing_criticals}}, 3, 'Got the expected 3 missing critical errors from import_patrons for dates tests');
410
411 is($result_7->{errors}->[0]->{missing_criticals}->[0]->{bad_date}, 1, 'Got the expected 1 bad_date error from import patrons for dates tests');
412 is($result_7->{errors}->[0]->{missing_criticals}->[0]->{borrowernumber}, 'UNDEF', 'Got the expected undef borrower number error from import patrons for dates tests');
413 is($result_7->{errors}->[0]->{missing_criticals}->[0]->{key}, 'dateofbirth', 'Got the expected dateofbirth key from import patrons for dates tests');
414 is($result_7->{errors}->[0]->{missing_criticals}->[0]->{line}, 3, 'Got the expected 2 line number error from import patrons for dates tests');
415 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');
416 is($result_7->{errors}->[0]->{missing_criticals}->[0]->{surname}, 'Ralph', 'Got the expected surname error from import patrons for dates tests');
417
418 is($result_7->{errors}->[0]->{missing_criticals}->[1]->{key}, 'dateenrolled', 'Got the expected dateenrolled key from import patrons for dates tests');
419 is($result_7->{errors}->[0]->{missing_criticals}->[2]->{key}, 'dateexpiry', 'Got the expected dateexpiry key from import patrons for dates tests');
420
421 is(scalar @{$result_7->{feedback}}, 2, 'Got the expected 2 feedback from import patrons for dates tests');
422 is($result_7->{feedback}->[0]->{feedback}, 1, 'Got the expected 1 feedback from import_patrons for dates tests');
423 is($result_7->{feedback}->[0]->{name}, 'headerrow', 'Got the expected header row name from import_patrons for dates tests');
424 is($result_7->{feedback}->[0]->{value}, $res_header, 'Got the expected header row value from import_patrons for dates tests');
425
426 is($result_7->{feedback}->[1]->{feedback}, 1, 'Got the expected 1 feedback from import_patrons for dates tests');
427 is($result_7->{feedback}->[1]->{name}, 'lastimported', 'Got the expected lastimported from import_patrons for dates tests');
428 like($result_7->{feedback}->[1]->{value}, qr/^Christina \/ \d+/, 'Got the expected lastimported value from import_patrons for dates tests');
429
430 is($result_7->{imported}, 1, 'Got the expected 1 imported result from import patrons for dates tests');
431 is($result_7->{invalid}, 1, 'Got the expected 1 invalid result from import patrons for dates tests');
432 is($result_7->{overwritten}, 0, 'Got the expected 0 overwritten result from import patrons for dates tests');
433
434 subtest 'test_import_without_cardnumber' => sub {
435     plan tests => 2;
436
437     #Remove possible existing user with a "" as cardnumber
438     my $blank_card = Koha::Patrons->find({ cardnumber => '' });
439     $blank_card->delete if $blank_card;
440
441     my $branchcode = $builder->build({ source => "Branch"})->{branchcode};
442     my $categorycode = $builder->build({ source => "Category"})->{categorycode};
443     my $csv_headers  = 'surname, branchcode, categorycode';
444     my $res_headers  = 'surname, branchcode, categorycode';
445     my $csv_nocard_1 = "Squarepants,$branchcode,$categorycode";
446     my $csv_nocard_2 = "Star,$branchcode,$categorycode";
447
448     my $filename_1 = make_csv($temp_dir, $csv_headers, $csv_nocard_1, $csv_nocard_2);
449     open(my $handle_1, "<", $filename_1) or die "cannot open < $filename_1: $!";
450     my $params_1 = { file => $handle_1, };
451
452     my $defaults = { cardnumber => "" }; #currently all the defaults come as "" if not filled
453
454     my $result = $patrons_import->import_patrons($params_1, $defaults);
455     like($result->{feedback}->[1]->{value}, qr/^Squarepants \/ \d+/, 'First borrower imported as expected');
456     like($result->{feedback}->[2]->{value}, qr/^Star \/ \d+/, 'Second borrower imported as expected');
457
458 };
459
460 subtest 'test_import_with_cardnumber_0' => sub {
461     plan tests => 2;
462
463     #Remove possible existing user with a "" as cardnumber
464     my $zero_card = Koha::Patrons->find({ cardnumber => 0 });
465     $zero_card->delete if $zero_card;
466
467     my $branchcode = $builder->build({ source => "Branch"})->{branchcode};
468     my $categorycode = $builder->build({ source => "Category"})->{categorycode};
469     my $csv_headers  = 'cardnumber,surname, branchcode, categorycode';
470     my $res_headers  = 'cardnumber,surname, branchcode, categorycode';
471     my $csv_nocard_1 = "0,Squarepants,$branchcode,$categorycode";
472
473     my $filename_1 = make_csv($temp_dir, $csv_headers, $csv_nocard_1);
474     open(my $handle_1, "<", $filename_1) or die "cannot open < $filename_1: $!";
475     my $params_1 = { file => $handle_1, };
476
477     my $defaults = { cardnumber => "" }; #currently all the defaults come as "" if not filled
478
479     my $result = $patrons_import->import_patrons($params_1, $defaults);
480     like($result->{feedback}->[1]->{value}, qr/^Squarepants \/ \d+/, 'First borrower imported as expected');
481     $zero_card = Koha::Patrons->find({ cardnumber => 0 });
482     is($zero_card->surname.$zero_card->branchcode.$zero_card->categorycode,'Squarepants'.$branchcode.$categorycode,"Patron with cardnumber 0 is the imported patron");
483
484 };
485
486 subtest 'Import patron with guarantor' => sub {
487     plan tests => 4;
488     t::lib::Mocks::mock_preference( 'borrowerRelationship', 'guarantor' );
489
490     my $category = $builder->build( { source => 'Category' } )->{categorycode};
491     my $branch = $builder->build( { source => 'Branch' } )->{branchcode};
492     my $guarantor = Koha::Patron->new(
493         {
494             surname      => 'Guarantor',
495             branchcode   => $branch,
496             categorycode => $category,
497         }
498     )->store();
499     my $guarantor_id = $guarantor->id;
500
501     my $branchcode = $builder->build( { source => "Branch" } )->{branchcode};
502     my $categorycode = $builder->build( { source => "Category" } )->{categorycode};
503     my $csv_headers = 'cardnumber,surname, branchcode, categorycode, guarantor_id, guarantor_relationship';
504     my $csv = "kylemhall,Hall,$branchcode,$categorycode,$guarantor_id,guarantor";
505
506     my $filename_1 = make_csv( $temp_dir, $csv_headers, $csv );
507     open( my $handle_1, "<", $filename_1 ) or die "cannot open < $filename_1: $!";
508     my $params_1 = { file => $handle_1, };
509
510     my $result = $patrons_import->import_patrons( $params_1 );
511     like( $result->{feedback}->[1]->{value}, qr/^Hall \/ \d+/, 'First borrower imported as expected' );
512     my $patron = Koha::Patrons->find( { cardnumber => 'kylemhall' } );
513     is( $patron->surname, "Hall", "Patron was created" );
514
515     my $r = Koha::Patron::Relationships->find( { guarantor_id => $guarantor_id } );
516     ok( $r, 'Found relationship' );
517     is( $r->guarantee->cardnumber, 'kylemhall', 'Found the correct guarantee' );
518 };
519
520 subtest 'test_import_with_password_overwrite' => sub {
521     plan tests => 8;
522
523     #Remove possible existing user to avoid clashes
524     my $ernest = Koha::Patrons->find({ userid => 'ErnestP' });
525     $ernest->delete if $ernest;
526
527     #Setup our info
528     my $branchcode = $builder->build({ source => "Branch"})->{branchcode};
529     my $categorycode = $builder->build({ source => "Category", value => { category_type => 'A'  } })->{categorycode};
530     my $staff_categorycode = $builder->build({ source => "Category", value => { category_type => 'S'  } })->{categorycode};
531     my $csv_headers  = 'surname,userid,branchcode,categorycode,password';
532     my $csv_password = "Worrell,ErnestP,$branchcode,$categorycode,Ernest11";
533     my $csv_password_change = "Worrell,ErnestP,$branchcode,$categorycode,Vern1234";
534     my $csv_blank_password = "Worel,ErnestP,$branchcode,$categorycode,";
535     my $defaults = { cardnumber => "" }; #currently all the defaults come as "" if not filled
536     my $csv_staff_password_change = "Worrell,ErnestP,$branchcode,$staff_categorycode,Vern1234";
537
538     #Make the test files for importing
539     my $filename_1 = make_csv($temp_dir, $csv_headers, $csv_password);
540     open(my $handle_1, "<", $filename_1) or die "cannot open < $filename_1: $!";
541     my $params_1 = { file => $handle_1, matchpoint => 'userid', overwrite_passwords => 1, overwrite_cardnumber => 1};
542     my $filename_2 = make_csv($temp_dir, $csv_headers, $csv_password_change);
543     open(my $handle_2, "<", $filename_2) or die "cannot open < $filename_2: $!";
544     my $params_2 = { file => $handle_2, matchpoint => 'userid', overwrite_passwords => 1, overwrite_cardnumber => 1};
545
546     my $filename_3 = make_csv($temp_dir, $csv_headers, $csv_blank_password);
547     open(my $handle_3, "<", $filename_3) or die "cannot open < $filename_3: $!";
548     my $params_3 = { file => $handle_3, matchpoint => 'userid', overwrite_passwords => 1, overwrite_cardnumber => 1};
549
550     my $filename_4 = make_csv($temp_dir, $csv_headers, $csv_staff_password_change);
551     open(my $handle_4, "<", $filename_4) or die "cannot open < $filename_4: $!";
552     my $params_4 = { file => $handle_4, matchpoint => 'userid', overwrite_passwords => 1, overwrite_cardnumber => 1};
553
554
555     my $result = $patrons_import->import_patrons($params_1, $defaults);
556     like($result->{feedback}->[1]->{value}, qr/^Worrell \/ \d+/, 'First borrower imported as expected');
557     $ernest = Koha::Patrons->find({ userid => 'ErnestP' });
558     isnt($ernest->password,'Ernest',"New patron is imported, password is encrypted");
559
560     #Save info to double check
561     my $orig_pass = $ernest->password;
562
563     $result = $patrons_import->import_patrons($params_2, $defaults);
564     $ernest = Koha::Patrons->find({ userid => 'ErnestP' });
565     isnt($ernest->password,$orig_pass,"New patron is overwritten, password is overwritten");
566     isnt($ernest->password,'Vern',"Password is overwritten and is encrypted from value provided");
567
568     #Save info to check not changed
569     $orig_pass = $ernest->password;
570
571     $result = $patrons_import->import_patrons($params_3, $defaults);
572     $ernest = Koha::Patrons->find({ userid => 'ErnestP' });
573     is($ernest->surname,'Worel',"Patron is overwritten, surname changed");
574     is($ernest->password,$orig_pass,"Patron was overwritten but password is not overwritten if blank");
575
576     $ernest->category($staff_categorycode);
577     $ernest->store;
578
579     $result = $patrons_import->import_patrons($params_4, $defaults);
580     $ernest = Koha::Patrons->find({ userid => 'ErnestP' });
581     is($ernest->surname,'Worrell',"Patron is overwritten, surname changed");
582     is($ernest->password,$orig_pass,"Patron is imported, password is not changed for staff");
583
584 };
585
586
587 subtest 'test_prepare_columns' => sub {
588     plan tests => 16;
589
590     # Given ... no header row
591     my %csvkeycol_0;
592     my @errors_0;
593
594     # When ...
595     my @csvcolumns_0 = $patrons_import->prepare_columns({headerrow => undef, keycol => \%csvkeycol_0, errors => \@errors_0, });
596
597     # Then ...
598     is(scalar @csvcolumns_0, 0, 'Got the expected empty column array from prepare columns with no header row');
599
600     is(scalar @errors_0, 1, 'Got the expected 1 entry in error array from prepare columns with no header row');
601     is($errors_0[0]->{badheader}, 1, 'Got the expected 1 badheader from prepare columns with no header row');
602     is($errors_0[0]->{line}, 1, 'Got the expected 1 line from prepare columns with no header row');
603     is($errors_0[0]->{lineraw}, undef, 'Got the expected undef lineraw from prepare columns with no header row');
604
605     # Given ... a good header row with plenty of whitespaces
606     my $headerrow_1 = 'a,    b ,        c,  ,   d';
607     my %csvkeycol_1;
608     my @errors_1;
609
610     # When ...
611     my @csvcolumns_1 = $patrons_import->prepare_columns({headerrow => $headerrow_1, keycol => \%csvkeycol_1, errors => \@errors_1, });
612
613     # Then ...
614     is(scalar @csvcolumns_1, 5, 'Got the expected 5 column array from prepare columns');
615     is($csvcolumns_1[0], 'a', 'Got the expected a header from prepare columns');
616     is($csvcolumns_1[1], 'b', 'Got the expected b header from prepare columns');
617     is($csvcolumns_1[2], 'c', 'Got the expected c header from prepare columns');
618     is($csvcolumns_1[3], '', 'Got the expected empty header from prepare columns');
619     is($csvcolumns_1[4], 'd', 'Got the expected d header from prepare columns');
620
621     is($csvkeycol_1{a}, 0, 'Got the expected 0 value for key a from prepare columns hash');
622     is($csvkeycol_1{b}, 1, 'Got the expected 1 value for key b from prepare columns hash');
623     is($csvkeycol_1{c}, 2, 'Got the expected 2 value for key c from prepare columns hash');
624     is($csvkeycol_1{''}, 3, 'Got the expected 3 value for empty string key from prepare columns hash');
625     is($csvkeycol_1{d}, 4, 'Got the expected 4 value for key d from prepare columns hash');
626 };
627
628 subtest 'test_set_column_keys' => sub {
629     plan tests => 5;
630
631     # Given ... nothing at all
632     # When ... Then ...
633     my $attr_type_0 = $patrons_import->set_attribute_types(undef);
634     is($attr_type_0, undef, 'Got the expected undef attribute type from set attribute types with nothing');
635
636     # Given ... extended but not matchpoint
637     my $params_1 = { extended => 1, matchpoint => undef, };
638
639     # When ... Then ...
640     my $attr_type_1 = $patrons_import->set_attribute_types($params_1);
641     is($attr_type_1, undef, 'Got the expected undef attribute type from set attribute types with no matchpoint');
642
643     # Given ... extended and unexpected matchpoint
644     my $params_2 = { extended => 1, matchpoint => 'unexpected', };
645
646     # When ... Then ...
647     my $attr_type_2 = $patrons_import->set_attribute_types($params_2);
648     is($attr_type_2, undef, 'Got the expected undef attribute type from set attribute types with unexpected matchpoint');
649
650     # Given ...
651     my $code_3   = 'SHOW_BCODE';
652     my $params_3 = { extended => 1, matchpoint => $code_3, };
653
654     # When ...
655     my $attr_type_3 = $patrons_import->set_attribute_types($params_3);
656
657     # Then ...
658     isa_ok($attr_type_3, 'Koha::Patron::Attribute::Type');
659     is($attr_type_3->code, $code_3, 'Got the expected code attribute type from set attribute types');
660 };
661
662 subtest 'test_set_column_keys' => sub {
663     plan tests => 2;
664
665     my @columns = Koha::Patrons->columns;
666     # Given ... nothing at all
667     # When ... Then ...
668     my @columnkeys_0 = $patrons_import->set_column_keys(undef);
669     # -1 because we do not want the borrowernumber column
670     # +2 for guarantor id and guarantor relationship
671     is(scalar @columnkeys_0, @columns - 1 + 2, 'Got the expected array size from set column keys with undef extended');
672
673     # Given ... extended.
674     my $extended = 1;
675
676     # When ... Then ...
677     my @columnkeys_1 = $patrons_import->set_column_keys($extended);
678     is(scalar @columnkeys_1, @columns - 1 + 2 + $extended, 'Got the expected array size from set column keys with extended');
679 };
680
681 subtest 'test_generate_patron_attributes' => sub {
682     plan tests => 13;
683
684     # Given ... nothing at all
685     # When ... Then ...
686     my $result_0 = $patrons_import->generate_patron_attributes(undef, undef, undef);
687     is($result_0, undef, 'Got the expected undef from set patron attributes with nothing');
688
689     # Given ... not extended.
690     my $extended_1 = 0;
691
692     # When ... Then ...
693     my $result_1 = $patrons_import->generate_patron_attributes($extended_1, undef, undef);
694     is($result_1, undef, 'Got the expected undef from set patron attributes with not extended');
695
696     # Given ... NO patrons attributes
697     my $extended_2          = 1;
698     my $patron_attributes_2 = undef;
699     my @feedback_2;
700
701     # When ...
702     my $result_2 = $patrons_import->generate_patron_attributes($extended_2, $patron_attributes_2, \@feedback_2);
703
704     # Then ...
705     is($result_2, undef, 'Got the expected undef from set patron attributes with no patrons attributes');
706     is(scalar @feedback_2, 0, 'Got the expected 0 size feedback array from set patron attributes with no patrons attributes');
707
708     # Given ... some patrons attributes
709     my $patron_attributes_3 = "homeroom:1150605,grade:01";
710     my @feedback_3;
711
712     # When ...
713     my $result_3 = $patrons_import->generate_patron_attributes($extended_2, $patron_attributes_3, \@feedback_3);
714
715     # Then ...
716     ok($result_3, 'Got some data back from set patron attributes');
717     is($result_3->[0]->{code}, 'grade', 'Got the expected first code from set patron attributes');
718     is($result_3->[0]->{attribute}, '01', 'Got the expected first value from set patron attributes');
719
720     is($result_3->[1]->{code}, 'homeroom', 'Got the expected second code from set patron attributes');
721     is($result_3->[1]->{attribute}, 1150605, 'Got the expected second value from set patron attributes');
722
723     is(scalar @feedback_3, 1, 'Got the expected 1 array size from set patron attributes with extended user');
724     is($feedback_3[0]->{feedback}, 1, 'Got the expected second feedback from set patron attributes with extended user');
725     is($feedback_3[0]->{name}, 'attribute string', 'Got the expected attribute string from set patron attributes with extended user');
726     is($feedback_3[0]->{value}, 'homeroom:1150605,grade:01', 'Got the expected feedback value from set patron attributes with extended user');
727 };
728
729 subtest 'test_check_branch_code' => sub {
730     plan tests => 11;
731
732     # Given ... no branch code.
733     my $borrowerline      = 'some, line';
734     my $line_number       = 78;
735     my @missing_criticals = ();
736
737     # When ...
738     $patrons_import->check_branch_code(undef, $borrowerline, $line_number, \@missing_criticals);
739
740     # Then ...
741     is(scalar @missing_criticals, 1, 'Got the expected missing critical array size of 1 from check_branch_code with no branch code');
742
743     is($missing_criticals[0]->{key}, 'branchcode', 'Got the expected branchcode key from check_branch_code with no branch code');
744     is($missing_criticals[0]->{line}, $line_number, 'Got the expected line number from check_branch_code with no branch code');
745     is($missing_criticals[0]->{lineraw}, $borrowerline, 'Got the expected lineraw value from check_branch_code with no branch code');
746
747     # Given ... unknown branch code
748     my $branchcode_1        = 'unexpected';
749     my $borrowerline_1      = 'some, line,'.$branchcode_1;
750     my $line_number_1       = 79;
751     my @missing_criticals_1 = ();
752
753     # When ...
754     $patrons_import->check_branch_code($branchcode_1, $borrowerline_1, $line_number_1, \@missing_criticals_1);
755
756     # Then ...
757     is(scalar @missing_criticals_1, 1, 'Got the expected missing critical array size of 1 from check_branch_code with unexpected branch code');
758
759     is($missing_criticals_1[0]->{branch_map}, 1, 'Got the expected 1 branch_map from check_branch_code with unexpected branch code');
760     is($missing_criticals_1[0]->{key}, 'branchcode', 'Got the expected branchcode key from check_branch_code with unexpected branch code');
761     is($missing_criticals_1[0]->{line}, $line_number_1, 'Got the expected line number from check_branch_code with unexpected branch code');
762     is($missing_criticals_1[0]->{lineraw}, $borrowerline_1, 'Got the expected lineraw value from check_branch_code with unexpected branch code');
763     is($missing_criticals_1[0]->{value}, $branchcode_1, 'Got the expected value from check_branch_code with unexpected branch code');
764
765     # Given ... a known branch code. Relies on database sample data
766     my $branchcode_2        = 'FFL';
767     my $borrowerline_2      = 'some, line,'.$branchcode_2;
768     my $line_number_2       = 80;
769     my @missing_criticals_2 = ();
770
771     # When ...
772     $patrons_import->check_branch_code($branchcode_2, $borrowerline_2, $line_number_2, \@missing_criticals_2);
773
774     # Then ...
775     is(scalar @missing_criticals_2, 0, 'Got the expected missing critical array size of 0 from check_branch_code');
776 };
777
778 subtest 'test_check_borrower_category' => sub {
779     plan tests => 11;
780
781     # Given ... no category code.
782     my $borrowerline      = 'some, line';
783     my $line_number       = 781;
784     my @missing_criticals = ();
785
786     # When ...
787     $patrons_import->check_borrower_category(undef, $borrowerline, $line_number, \@missing_criticals);
788
789     # Then ...
790     is(scalar @missing_criticals, 1, 'Got the expected missing critical array size of 1 from check_branch_code with no category code');
791
792     is($missing_criticals[0]->{key}, 'categorycode', 'Got the expected categorycode key from check_branch_code with no category code');
793     is($missing_criticals[0]->{line}, $line_number, 'Got the expected line number from check_branch_code with no category code');
794     is($missing_criticals[0]->{lineraw}, $borrowerline, 'Got the expected lineraw value from check_branch_code with no category code');
795
796     # Given ... unknown category code
797     my $categorycode_1      = 'unexpected';
798     my $borrowerline_1      = 'some, line, line, '.$categorycode_1;
799     my $line_number_1       = 791;
800     my @missing_criticals_1 = ();
801
802     # When ...
803     $patrons_import->check_borrower_category($categorycode_1, $borrowerline_1, $line_number_1, \@missing_criticals_1);
804
805     # Then ...
806     is(scalar @missing_criticals_1, 1, 'Got the expected missing critical array size of 1 from check_branch_code with unexpected category code');
807
808     is($missing_criticals_1[0]->{category_map}, 1, 'Got the expected 1 category_map from check_branch_code with unexpected category code');
809     is($missing_criticals_1[0]->{key}, 'categorycode', 'Got the expected branchcode key from check_branch_code with unexpected category code');
810     is($missing_criticals_1[0]->{line}, $line_number_1, 'Got the expected line number from check_branch_code with unexpected category code');
811     is($missing_criticals_1[0]->{lineraw}, $borrowerline_1, 'Got the expected lineraw value from check_branch_code with unexpected category code');
812     is($missing_criticals_1[0]->{value}, $categorycode_1, 'Got the expected value from check_branch_code with unexpected category code');
813
814     # Given ... a known category code. Relies on database sample data.
815     my $categorycode_2      = 'T';
816     my $borrowerline_2      = 'some, line,'.$categorycode_2;
817     my $line_number_2       = 801;
818     my @missing_criticals_2 = ();
819
820     # When ...
821     $patrons_import->check_borrower_category($categorycode_2, $borrowerline_2, $line_number_2, \@missing_criticals_2);
822
823     # Then ...
824     is(scalar @missing_criticals_2, 0, 'Got the expected missing critical array size of 0 from check_branch_code');
825 };
826
827 subtest 'test_format_dates' => sub {
828     plan tests => 22;
829
830     # Given ... no borrower data.
831     my $borrowerline      = 'another line';
832     my $line_number       = 987;
833     my @missing_criticals = ();
834     my %borrower;
835     my $params = {borrower => \%borrower, lineraw => $borrowerline, line => $line_number, missing_criticals => \@missing_criticals, };
836
837     # When ...
838     $patrons_import->format_dates($params);
839
840     # Then ...
841     ok( not(%borrower), 'Got the expected no borrower from format_dates with no dates');
842     is(scalar @missing_criticals, 0, 'Got the expected missing critical array size of 0 from format_dates with no dates');
843
844     # Given ... some good dates
845     my @missing_criticals_1 = ();
846     my $dateofbirth_1  = '2016-05-03';
847     my $dateenrolled_1 = '2016-05-04';
848     my $dateexpiry_1   = '2016-05-06';
849     my $borrower_1     = { dateofbirth => $dateofbirth_1, dateenrolled => $dateenrolled_1, dateexpiry => $dateexpiry_1, };
850     my $params_1       = {borrower => $borrower_1, lineraw => $borrowerline, line => $line_number, missing_criticals => \@missing_criticals_1, };
851
852     # When ...
853     $patrons_import->format_dates($params_1);
854
855     # Then ...
856     is($borrower_1->{dateofbirth}, $dateofbirth_1, 'Got the expected date of birth from format_dates with good dates');
857     is($borrower_1->{dateenrolled}, $dateenrolled_1, 'Got the expected date of birth from format_dates with good dates');
858     is($borrower_1->{dateexpiry}, $dateexpiry_1, 'Got the expected date of birth from format_dates with good dates');
859     is(scalar @missing_criticals_1, 0, 'Got the expected missing critical array size of 0 from check_branch_code with good dates');
860
861     # Given ... some very bad dates
862     my @missing_criticals_2 = ();
863     my $dateofbirth_2  = '03-2016-05';
864     my $dateenrolled_2 = '04-2016-05';
865     my $dateexpiry_2   = '06-2016-05';
866     my $borrower_2     = { dateofbirth => $dateofbirth_2, dateenrolled => $dateenrolled_2, dateexpiry => $dateexpiry_2, };
867     my $params_2       = {borrower => $borrower_2, lineraw => $borrowerline, line => $line_number, missing_criticals => \@missing_criticals_2, };
868
869     # When ...
870     $patrons_import->format_dates($params_2);
871
872     # Then ...
873     is($borrower_2->{dateofbirth}, '', 'Got the expected empty date of birth from format_dates with bad dates');
874     is($borrower_2->{dateenrolled}, '', 'Got the expected emptydate of birth from format_dates with bad dates');
875     is($borrower_2->{dateexpiry}, '', 'Got the expected empty date of birth from format_dates with bad dates');
876
877     is(scalar @missing_criticals_2, 3, 'Got the expected missing critical array size of 3 from check_branch_code with bad dates');
878     is($missing_criticals_2[0]->{bad_date}, 1, 'Got the expected first bad date flag from check_branch_code with bad dates');
879     is($missing_criticals_2[0]->{key}, 'dateofbirth', 'Got the expected dateofbirth key from check_branch_code with bad dates');
880     is($missing_criticals_2[0]->{line}, $line_number, 'Got the expected first line from check_branch_code with bad dates');
881     is($missing_criticals_2[0]->{lineraw}, $borrowerline, 'Got the expected first lineraw from check_branch_code with bad dates');
882
883     is($missing_criticals_2[1]->{bad_date}, 1, 'Got the expected second bad date flag from check_branch_code with bad dates');
884     is($missing_criticals_2[1]->{key}, 'dateenrolled', 'Got the expected dateenrolled key from check_branch_code with bad dates');
885     is($missing_criticals_2[1]->{line}, $line_number, 'Got the expected second line from check_branch_code with bad dates');
886     is($missing_criticals_2[1]->{lineraw}, $borrowerline, 'Got the expected second lineraw from check_branch_code with bad dates');
887
888     is($missing_criticals_2[2]->{bad_date}, 1, 'Got the expected third bad date flag from check_branch_code with bad dates');
889     is($missing_criticals_2[2]->{key}, 'dateexpiry', 'Got the expected dateexpiry key from check_branch_code with bad dates');
890     is($missing_criticals_2[2]->{line}, $line_number, 'Got the expected third line from check_branch_code with bad dates');
891     is($missing_criticals_2[2]->{lineraw}, $borrowerline, 'Got the expected third lineraw from check_branch_code with bad dates');
892 };
893
894 subtest 'patron_attributes' => sub {
895
896     plan tests => 17;
897
898     t::lib::Mocks::mock_preference('ExtendedPatronAttributes', 1);
899
900     my $unique_attribute_type = $builder->build_object(
901         {
902             class => 'Koha::Patron::Attribute::Types',
903             value => { unique_id=> 1, repeatable => 0 }
904         }
905     );
906     my $repeatable_attribute_type = $builder->build_object(
907         {
908             class => 'Koha::Patron::Attribute::Types',
909             value => { unique_id => 0, repeatable => 1 }
910         }
911     );
912     my $normal_attribute_type = $builder->build_object(
913         {
914             class => 'Koha::Patron::Attribute::Types',
915             value => { unique_id => 0, repeatable => 0 }
916         }
917     );
918     my $non_existent_attribute_type = $builder->build_object(
919         {
920             class => 'Koha::Patron::Attribute::Types',
921         }
922     );
923     my $non_existent_attribute_type_code = $non_existent_attribute_type->code;
924     $non_existent_attribute_type->delete;
925
926     our $cardnumber = "1042";
927
928     # attributes is { code => \@attributes }
929     sub build_csv {
930         my ($attributes) = @_;
931
932         my $csv_headers = 'cardnumber,surname,firstname,branchcode,categorycode,patron_attributes';
933         my @attributes_str = map { my $code = $_; map {  sprintf "%s:%s", $code, $_ } @{ $attributes->{$code} } } keys %$attributes;
934         my $attributes_str = join ',', @attributes_str;
935         my $csv_line = sprintf '%s,John,D,MPL,PT,"%s"', $cardnumber, $attributes_str;
936         my $filename = make_csv( $temp_dir, $csv_headers, $csv_line );
937         open( my $fh, "<:encoding(utf8)", $filename ) or die "cannot open $filename: $!";
938         return $fh;
939     }
940
941     { # Everything good, we create a patron with 3 attributes
942         my $attributes = {
943             $unique_attribute_type->code => ['my unique attribute 1'],
944             $repeatable_attribute_type->code => [ 'my repeatable attribute 1', 'my repeatable attribute 2' ],
945             $normal_attribute_type->code => ['my normal attribute 1'],
946         };
947         my $fh = build_csv({ %$attributes });
948         my $result = $patrons_import->import_patrons({file => $fh});
949
950         is( $result->{imported}, 1 );
951
952         my $patron = Koha::Patrons->find({cardnumber => $cardnumber});
953         compare_patron_attributes($patron->extended_attributes->unblessed, { %$attributes } );
954         $patron->delete;
955     }
956
957     { # UniqueIDConstraint
958         $builder->build_object(
959             {
960                 class => 'Koha::Patron::Attributes',
961                 value => { code => $unique_attribute_type->code, attribute => 'unique' }
962             }
963         );
964
965         my $attributes = {
966             $unique_attribute_type->code => ['unique'],
967             $normal_attribute_type->code => ['my normal attribute 1']
968         };
969         my $fh = build_csv({ %$attributes });
970
971         my $result = $patrons_import->import_patrons({file => $fh, matchpoint => 'cardnumber'});
972         my $error = $result->{errors}->[0];
973         is( $error->{patron_attribute_unique_id_constraint}, 1 );
974         is( $error->{patron_id}, $cardnumber );
975         is( $error->{attribute}->code, $unique_attribute_type->code );
976
977         my $patron = Koha::Patrons->find({cardnumber => $cardnumber});
978         is( $patron, undef, 'Patron is not created' );
979     }
980
981     { #InvalidType
982         my $attributes = {
983             $non_existent_attribute_type_code => ['my non-existent attribute'],
984             $normal_attribute_type->code      => ['my attribute 1'],
985         };
986         my $fh = build_csv({ %$attributes });
987
988         my $result = $patrons_import->import_patrons({file => $fh, matchpoint => 'cardnumber'});
989         is( $result->{imported}, 0 );
990
991         my $error = $result->{errors}->[0];
992         is( $error->{patron_attribute_invalid_type}, 1 );
993         is( $error->{patron_id}, $cardnumber );
994         is( $error->{attribute_type_code}, $non_existent_attribute_type_code );
995
996         my $patron = Koha::Patrons->find({cardnumber => $cardnumber});
997         is( $patron, undef );
998
999     }
1000
1001     { # NonRepeatable
1002         my $attributes = {
1003                 $repeatable_attribute_type->code => ['my repeatable attribute 1', 'my repeatable attribute 2'],
1004                 $normal_attribute_type->code     => ['my normal attribute 1', 'my normal attribute 2'],
1005             };
1006         my $fh = build_csv({ %$attributes });
1007         my $result = $patrons_import->import_patrons({file => $fh, matchpoint => 'cardnumber'});
1008         is( $result->{imported}, 0 );
1009
1010         my $error = $result->{errors}->[0];
1011         is( $error->{patron_attribute_non_repeatable}, 1 );
1012         is( $error->{patron_id}, $cardnumber );
1013         is( $error->{attribute}->code, $normal_attribute_type->code );
1014
1015         my $patron = Koha::Patrons->find({cardnumber => $cardnumber});
1016         is( $patron, undef );
1017     }
1018
1019     subtest 'update existing patron' => sub {
1020         plan tests => 19;
1021
1022         my $patron = $builder->build_object(
1023             {
1024                 class => 'Koha::Patrons',
1025                 value => { cardnumber => $cardnumber }
1026             }
1027         );
1028
1029         my $attributes = {
1030             $unique_attribute_type->code => ['my unique attribute 1'],
1031             $repeatable_attribute_type->code => [ 'my repeatable attribute 1', 'my repeatable attribute 2' ],
1032             $normal_attribute_type->code => ['my normal attribute 1'],
1033         };
1034         my $fh = build_csv({ %$attributes });
1035         my $result = $patrons_import->import_patrons(
1036             {
1037                 file                         => $fh,
1038                 matchpoint                   => 'cardnumber',
1039                 overwrite_cardnumber         => 1,
1040                 preserve_extended_attributes => 1
1041             }
1042         );
1043
1044         is( $result->{overwritten}, 1 );
1045
1046         compare_patron_attributes($patron->extended_attributes->unblessed, { %$attributes } );
1047
1048         # Adding a new non-repeatable attribute
1049         my $new_attributes = {
1050             $normal_attribute_type->code => ['my normal attribute 2'],
1051         };
1052         $fh = build_csv({ %$new_attributes });
1053         $result = $patrons_import->import_patrons(
1054             {
1055                 file                         => $fh,
1056                 matchpoint                   => 'cardnumber',
1057                 overwrite_cardnumber         => 1,
1058                 preserve_extended_attributes => 1
1059             }
1060         );
1061
1062         is( $result->{overwritten}, 1 );
1063
1064         # The normal_attribute_type has been replaced with 'my normal attribute 2'
1065         compare_patron_attributes($patron->extended_attributes->unblessed, { %$attributes, %$new_attributes } );
1066
1067         # UniqueIDConstraint
1068         $patron->extended_attributes->delete; # reset
1069         $builder->build_object(
1070             {
1071                 class => 'Koha::Patron::Attributes',
1072                 value => { code => $unique_attribute_type->code, attribute => 'unique' }
1073             }
1074         );
1075         $attributes = {
1076             $unique_attribute_type->code => ['unique'],
1077             $repeatable_attribute_type->code => [ 'my repeatable attribute 1', 'my repeatable attribute 2' ],
1078             $normal_attribute_type->code => ['my normal attribute 1'],
1079         };
1080         $fh = build_csv({ %$attributes });
1081         $result = $patrons_import->import_patrons(
1082             {
1083                 file                         => $fh,
1084                 matchpoint                   => 'cardnumber',
1085                 overwrite_cardnumber         => 1,
1086                 preserve_extended_attributes => 1
1087             }
1088         );
1089
1090         is( $result->{overwritten}, 0 );
1091         my $error = $result->{errors}->[0];
1092         is( $error->{patron_attribute_unique_id_constraint}, 1 );
1093         is( $error->{borrowernumber}, $patron->borrowernumber );
1094         is( $error->{attribute}->code, $unique_attribute_type->code );
1095
1096         compare_patron_attributes($patron->extended_attributes->unblessed, {},  );
1097
1098
1099         #InvalidType
1100         $attributes = {
1101             $non_existent_attribute_type_code => ['my non-existent attribute'],
1102             $normal_attribute_type->code      => ['my attribute 1'],
1103         };
1104         $fh = build_csv({ %$attributes });
1105
1106         $result = $patrons_import->import_patrons(
1107             {
1108                 file                         => $fh,
1109                 matchpoint                   => 'cardnumber',
1110                 overwrite_cardnumber         => 1,
1111                 preserve_extended_attributes => 1
1112             }
1113         );
1114         is( $result->{overwritten}, 0 );
1115
1116         $error = $result->{errors}->[0];
1117         is( $error->{patron_attribute_invalid_type}, 1 );
1118         is( $error->{borrowernumber}, $patron->borrowernumber );
1119         is( $error->{attribute_type_code}, $non_existent_attribute_type_code );
1120
1121         # NonRepeatable
1122         $attributes = {
1123                 $repeatable_attribute_type->code => ['my repeatable attribute 1', 'my repeatable attribute 2'],
1124                 $normal_attribute_type->code     => ['my normal attribute 1', 'my normal attribute 2'],
1125             };
1126         $fh = build_csv({ %$attributes });
1127         $result = $patrons_import->import_patrons(
1128             {
1129                 file                         => $fh,
1130                 matchpoint                   => 'cardnumber',
1131                 overwrite_cardnumber         => 1,
1132                 preserve_extended_attributes => 1
1133             }
1134         );
1135         is( $result->{overwritten}, 0 );
1136
1137         $error = $result->{errors}->[0];
1138         is( $error->{patron_attribute_non_repeatable}, 1 );
1139         is( $error->{borrowernumber}, $patron->borrowernumber );
1140         is( $error->{attribute}->code, $normal_attribute_type->code );
1141
1142         # Don't preserve existing attributes
1143         $attributes = {
1144                 $repeatable_attribute_type->code => ['my repeatable attribute 3', 'my repeatable attribute 4'],
1145                 $normal_attribute_type->code     => ['my normal attribute 1'],
1146             };
1147         $fh = build_csv({ %$attributes });
1148         $result = $patrons_import->import_patrons(
1149             {
1150                 file                         => $fh,
1151                 matchpoint                   => 'cardnumber',
1152                 overwrite_cardnumber         => 1,
1153                 preserve_extended_attributes => 1
1154             }
1155         );
1156         is( $result->{overwritten}, 1 );
1157
1158         compare_patron_attributes($patron->extended_attributes->unblessed, { %$attributes } );
1159
1160     };
1161
1162 };
1163
1164 subtest 'welcome_email' => sub {
1165
1166     plan tests => 3;
1167
1168     #Setup our info
1169     my $branchcode = $builder->build({ source => "Branch"})->{branchcode};
1170     my $categorycode = $builder->build({ source => "Category", value => { category_type => 'A'  } })->{categorycode};
1171     my $staff_categorycode = $builder->build({ source => "Category", value => { category_type => 'S'  } })->{categorycode};
1172     my $csv_headers  = 'surname,userid,branchcode,categorycode,password,email';
1173     my $csv_new      = "Spagobi,EldridgeS,$branchcode,$categorycode,H4ckR".',me@myemail.com';
1174     my $defaults = { cardnumber => "" }; #currently all the defaults come as "" if not filled
1175
1176     #Make the test files for importing
1177     my $filename_1 = make_csv($temp_dir, $csv_headers, $csv_new);
1178     open(my $handle_1, "<", $filename_1) or die "cannot open < $filename_1: $!";
1179
1180     my $params_1 = { file => $handle_1, matchpoint => 'userid', overwrite_passwords => 1, overwrite_cardnumber => 1, send_welcome => 1};
1181
1182     my $result = $patrons_import->import_patrons($params_1, $defaults);
1183     is($result->{already_in_db}, 0, 'New borrower imported as expected');
1184     is($result->{feedback}->[3]->{name}, 'welcome_sent', 'Email send reported');
1185     my $eldridge = Koha::Patrons->find({ userid => 'EldridgeS'});
1186     my $notices = Koha::Notice::Messages->search({ borrowernumber => $eldridge->borrowernumber });
1187     is($notices->count, 1, 'Notice was queued');
1188 };
1189
1190 subtest 'test update_dateexpiry when no dateexpiry in file' => sub {
1191
1192     # Normally, imports on ly update included columns, however, if we are asking Koha
1193     # to calculate expiration dates
1194
1195     plan tests => 3;
1196
1197     my $csv_headers = 'cardnumber,surname,branchcode,categorycode,dateenrolled';
1198     my $patron      = $builder->build_object( { class => 'Koha::Patrons' } );
1199     $patron->dateexpiry( dt_from_string() );
1200     $patron->dateenrolled( dt_from_string->add( months => '-24', end_of_month => 'limit' ) )->store;
1201     my $csv_values = join(
1202         ',', $patron->cardnumber, $patron->surname, $patron->branchcode, $patron->categorycode,
1203         $patron->dateenrolled
1204     );
1205     $patron->category->enrolmentperiod(42)->enrolmentperioddate(undef)->store();
1206
1207     my $filename_1 = make_csv( $temp_dir, $csv_headers, $csv_values );
1208     open( my $handle_1, "<", $filename_1 ) or die "cannot open < $filename_1: $!";
1209     my $params = { file => $handle_1, matchpoint => 'cardnumber', overwrite_cardnumber => 1, update_dateexpiry => 1 };
1210     my $result = $patrons_import->import_patrons( $params, {} );
1211
1212     $patron->discard_changes();
1213
1214     is(
1215         $patron->dateexpiry, dt_from_string->add( months => 18, end_of_month => 'limit' )->ymd,
1216         "Expiration date is correct with update_dateexpiry = true no dateexpiry in file and update_dateexpiryfromtoday false (i.e. use passed dateenrolled) "
1217     );
1218
1219     $filename_1 = make_csv( $temp_dir, $csv_headers, $csv_values );
1220     open( $handle_1, "<", $filename_1 ) or die "cannot open < $filename_1: $!";
1221     $params = {
1222         file => $handle_1, matchpoint => 'cardnumber', overwrite_cardnumber => 1, update_dateexpiry => 1,
1223         update_dateexpiry_from_today => 1
1224     };
1225     $result = $patrons_import->import_patrons( $params, {} );
1226     $patron->discard_changes();
1227     is(
1228         $patron->dateexpiry, dt_from_string->add( months => 42, end_of_month => 'limit' )->ymd,
1229         "Expiration date is correct with update_dateexpiry = true no dateexpiry in file and update_dateexpiryfromtoday true "
1230     );
1231
1232     $csv_headers = 'cardnumber,surname,branchcode,categorycode';
1233     $csv_values  = join( ',', $patron->cardnumber, $patron->surname, $patron->branchcode, $patron->categorycode );
1234     $filename_1  = make_csv( $temp_dir, $csv_headers, $csv_values );
1235     open( $handle_1, "<", $filename_1 ) or die "cannot open < $filename_1: $!";
1236     $patron->dateexpiry( dt_from_string() );
1237     $patron->dateenrolled( dt_from_string->add( months => '-24', end_of_month => 'limit' ) )->store;
1238     $params = { file => $handle_1, matchpoint => 'cardnumber', overwrite_cardnumber => 1, update_dateexpiry => 1 };
1239     $result = $patrons_import->import_patrons( $params, {} );
1240     $patron->discard_changes();
1241     is(
1242         $patron->dateexpiry, dt_from_string->add( months => 42, end_of_month => 'limit' )->ymd,
1243         "Expiration date is correct with update_dateexpiry = true no dateexpiry in file and update_dateexpiryfromtoday false but no dateenrolled in file (today is used) "
1244     );
1245
1246 };
1247
1248 subtest 'prevent regression in update of dateexpiry with no date related flags' => sub {
1249     # If the --update-expiration or --expiration-from-today flgas are not passed, the behaviour should be as foolows:
1250     # 1) dateexpiry column is blank - preserve existing patron expiry date
1251     # 2) dateexpiry column has a date value - update expiry date to match this new date
1252     plan tests => 2;
1253
1254     my $csv_headers = 'cardnumber,surname,branchcode,categorycode,dateenrolled,dateexpiry';
1255     my $patron      = $builder->build_object( { class => 'Koha::Patrons' } );
1256     $patron->dateexpiry( '2099-12-31' );
1257     $patron->dateenrolled( dt_from_string->add( months => '-24', end_of_month => 'limit' ) )->store;
1258     my $csv_values = join(
1259         ',', $patron->cardnumber, $patron->surname, $patron->branchcode, $patron->categorycode,
1260         $patron->dateenrolled, ''
1261     );
1262
1263     my $filename_1 = make_csv( $temp_dir, $csv_headers, $csv_values );
1264     open( my $handle_1, "<", $filename_1 ) or die "cannot open < $filename_1: $!";
1265     my $params = { file => $handle_1, matchpoint => 'cardnumber', overwrite_cardnumber => 1 };
1266     my $result = $patrons_import->import_patrons( $params, {} );
1267     $patron->discard_changes();
1268
1269     is(
1270         $patron->dateexpiry, '2099-12-31',
1271         'No expiry date provided in CSV so the existing patron expiry date is preserved'
1272     );
1273
1274     $csv_values = join(
1275         ',', $patron->cardnumber, $patron->surname, $patron->branchcode, $patron->categorycode,
1276         $patron->dateenrolled, '2098-01-01'
1277     );
1278
1279     $filename_1 = make_csv( $temp_dir, $csv_headers, $csv_values );
1280     open( $handle_1, "<", $filename_1 ) or die "cannot open < $filename_1: $!";
1281     $params = { file => $handle_1, matchpoint => 'cardnumber', overwrite_cardnumber => 1 };
1282     $result = $patrons_import->import_patrons( $params, {} );
1283     $patron->discard_changes();
1284
1285     is(
1286         $patron->dateexpiry, '2098-01-01',
1287         'Expiry date updated to match the date provided in the CSV file'
1288     );
1289 };
1290
1291
1292 # got is { code => $code, attribute => $attribute }
1293 # expected is { $code => \@attributes }
1294 sub compare_patron_attributes {
1295     my ( $got, $expected ) = @_;
1296
1297     $got = [ map { { code => $_->{code}, attribute => $_->{attribute} } } @$got ];
1298     $expected = [
1299         map {
1300             my $code = $_;
1301             map { { code => $code, attribute => $_ } } @{ $expected->{$code} }
1302           } keys %$expected
1303     ];
1304     for my $v ( $got, $expected ) {
1305         $v = [
1306             sort {
1307                 $a->{code} cmp $b->{code} || $a->{attribute} cmp $b->{attribute}
1308             } @$v
1309         ];
1310     }
1311     is_deeply($got, $expected);
1312 }
1313
1314 # ###### Test utility ###########
1315 sub make_csv {
1316     my ($temp_dir, @lines) = @_;
1317
1318     my ($fh, $filename) = tempfile( DIR => $temp_dir) or die $!;
1319     print $fh $_."\r\n" foreach @lines;
1320     close $fh or die $!;
1321
1322     return $filename;
1323 }
1324
1325 1;