Bug 17359: Use a unicode character in tests
[srvgit] / 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 => 157;
22 use Test::Warn;
23 use Encode qw( encode_utf8 );
24 use utf8;
25
26 # To be replaced by t::lib::Mock
27 use Test::MockModule;
28 use Koha::Database;
29
30 use File::Temp qw(tempfile tempdir);
31 my $temp_dir = tempdir('Koha_patrons_import_test_XXXX', CLEANUP => 1, TMPDIR => 1);
32
33 use t::lib::Mocks;
34 use t::lib::TestBuilder;
35 my $builder = t::lib::TestBuilder->new;
36
37 my $schema = Koha::Database->new->schema;
38 $schema->storage->txn_begin;
39
40 # ########## Tests start here #############################
41 # Given ... we can use the module
42 BEGIN { use_ok('Koha::Patrons::Import'); }
43
44 my $patrons_import = new_ok('Koha::Patrons::Import');
45
46 subtest 'test_methods' => sub {
47     plan tests => 1;
48
49     # Given ... we can reach the method(s)
50     my @methods = ('import_patrons',
51                    'set_attribute_types',
52                    'prepare_columns',
53                    'set_column_keys',
54                    'set_patron_attributes',
55                    'check_branch_code',
56                    'format_dates',
57                   );
58     can_ok('Koha::Patrons::Import', @methods);
59 };
60
61 subtest 'test_attributes' => sub {
62     plan tests => 1;
63
64     my @attributes = ('today_iso', 'text_csv');
65     can_ok('Koha::Patrons::Import', @attributes);
66 };
67
68 # Tests for Koha::Patrons::Import::import_patrons()
69 # Given ... nothing much. When ... Then ...
70 my $result;
71 warning_is { $result = $patrons_import->import_patrons(undef) }
72            { carped => 'No file handle passed in!' },
73            " Koha::Patrons::Import->import_patrons carps if no file handle is passed";
74 is($result, undef, 'Got the expected undef from import_patrons with nothing much');
75
76 # Given ... some params but no file handle.
77 my $params_0 = { some_stuff => 'random stuff', };
78
79 # When ... Then ...
80 my $result_0;
81 warning_is { $result_0 = $patrons_import->import_patrons($params_0) }
82            { carped => 'No file handle passed in!' },
83            " Koha::Patrons::Import->import_patrons carps if no file handle is passed";
84 is($result_0, undef, 'Got the expected undef from import_patrons with no file handle');
85
86 # Given ... a file handle to file with headers only.
87 t::lib::Mocks::mock_preference('ExtendedPatronAttributes', 0);
88 t::lib::Mocks::mock_preference('dateformat', 'us');
89
90
91 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';
92 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';
93 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';
94 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';
95
96 my $filename_1 = make_csv($temp_dir, $csv_headers, $csv_one_line);
97 open(my $handle_1, "<", $filename_1) or die "cannot open < $filename_1: $!";
98 my $params_1 = { file => $handle_1, };
99
100 # When ...
101 my $result_1 = $patrons_import->import_patrons($params_1);
102
103 # Then ...
104 is($result_1->{already_in_db}, 0, 'Got the expected 0 already_in_db from import_patrons with no matchpoint defined');
105 is(scalar @{$result_1->{errors}}, 0, 'Got the expected 0 size error array from import_patrons with no matchpoint defined');
106
107 is($result_1->{feedback}->[0]->{feedback}, 1, 'Got the expected 1 feedback from import_patrons with no matchpoint defined');
108 is($result_1->{feedback}->[0]->{name}, 'headerrow', 'Got the expected header row name from import_patrons with no matchpoint defined');
109 is($result_1->{feedback}->[0]->{value}, $res_header, 'Got the expected header row value from import_patrons with no matchpoint defined');
110
111 is($result_1->{feedback}->[1]->{feedback}, 1, 'Got the expected second feedback from import_patrons with no matchpoint defined');
112 is($result_1->{feedback}->[1]->{name}, 'lastimported', 'Got the expected last imported name from import_patrons with no matchpoint defined');
113 like($result_1->{feedback}->[1]->{value}, qr/^Nancy \/ \d+/, 'Got the expected second header row value from import_patrons with no matchpoint defined');
114
115 is($result_1->{imported}, 1, 'Got the expected 1 imported result from import_patrons with no matchpoint defined');
116 is($result_1->{invalid}, 0, 'Got the expected 0 invalid result from import_patrons with no matchpoint defined');
117 is($result_1->{overwritten}, 0, 'Got the expected 0 overwritten result from import_patrons with no matchpoint defined');
118
119 # Given ... a valid file handle, a bad matchpoint resulting in invalid card number
120 my $filename_2 = make_csv($temp_dir, $csv_headers, $csv_one_line);
121 open(my $handle_2, "<", $filename_2) or die "cannot open < $filename_2: $!";
122 my $params_2 = { file => $handle_2, matchpoint => 'SHOW_BCODE', };
123
124 # When ...
125 my $result_2 = $patrons_import->import_patrons($params_2);
126
127 # Then ...
128 is($result_2->{already_in_db}, 0, 'Got the expected 0 already_in_db from import_patrons with invalid card number');
129 is($result_2->{errors}->[0]->{borrowernumber}, undef, 'Got the expected undef borrower number from import patrons with invalid card number');
130 is($result_2->{errors}->[0]->{cardnumber}, 1000, 'Got the expected 1000 card number from import patrons with invalid card number');
131 is($result_2->{errors}->[0]->{invalid_cardnumber}, 1, 'Got the expected invalid card number from import patrons with invalid card number');
132
133 is($result_2->{feedback}->[0]->{feedback}, 1, 'Got the expected 1 feedback from import_patrons with invalid card number');
134 is($result_2->{feedback}->[0]->{name}, 'headerrow', 'Got the expected header row name from import_patrons with invalid card number');
135 is($result_2->{feedback}->[0]->{value}, $res_header, 'Got the expected header row value from import_patrons with invalid card number');
136
137 is($result_2->{imported}, 0, 'Got the expected 0 imported result from import_patrons with invalid card number');
138 is($result_2->{invalid}, 1, 'Got the expected 1 invalid result from import_patrons with invalid card number');
139 is($result_2->{overwritten}, 0, 'Got the expected 0 overwritten result from import_patrons with invalid card number');
140
141 # Given ... valid file handle, good matchpoint that matches should not overwrite when not set.
142 my $filename_3 = make_csv($temp_dir, $csv_headers, $csv_one_line);
143 open(my $handle_3, "<", $filename_3) or die "cannot open < $filename_3: $!";
144 my $params_3 = { file => $handle_3, matchpoint => 'cardnumber', };
145
146 # When ...
147 my $result_3 = $patrons_import->import_patrons($params_3);
148
149 # Then ...
150 is($result_3->{already_in_db}, 1, 'Got the expected 1 already_in_db from import_patrons with duplicate userid');
151 is($result_3->{errors}->[0]->{duplicate_userid}, undef, 'No duplicate userid error from import patrons with duplicate userid (it is our own)');
152 is($result_3->{errors}->[0]->{userid}, undef, 'No duplicate userid error from import patrons with duplicate userid (it is our own)');
153
154 is($result_3->{feedback}->[0]->{feedback}, 1, 'Got 1 expected feedback from import_patrons that matched but not overwritten');
155 is($result_3->{feedback}->[0]->{name}, 'headerrow', 'Got the expected header row name from import_patrons with duplicate userid');
156 is($result_3->{feedback}->[0]->{value}, $res_header, 'Got the expected header row value from import_patrons with duplicate userid');
157
158 is($result_3->{imported}, 0, 'Got the expected 0 imported result from import_patrons');
159 is($result_3->{invalid}, 0, 'Got the expected 0 invalid result from import_patrons');
160 is($result_3->{overwritten}, 0, 'Got the expected 0 overwritten result from import_patrons that matched');
161
162 # Given ... valid file handle, good matchpoint that matches should overwrite when set.
163 my $filename_3a = make_csv($temp_dir, $csv_headers, $csv_one_line);
164 open(my $handle_3a, "<", $filename_3a) or die "cannot open < $filename_3: $!";
165 my $params_3a = { file => $handle_3a, matchpoint => 'cardnumber', overwrite_cardnumber => 1};
166
167 # When ...
168 my $result_3a = $patrons_import->import_patrons($params_3a);
169
170 # Then ...
171 is($result_3a->{already_in_db}, 0, 'Got the expected 0 already_in_db from import_patrons when matched and overwrite set');
172 is($result_3a->{errors}->[0]->{duplicate_userid}, undef, 'No duplicate userid error from import patrons with duplicate userid (it is our own)');
173 is($result_3a->{errors}->[0]->{userid}, undef, 'No duplicate userid error from import patrons with duplicate userid (it is our own)');
174
175 is($result_3a->{feedback}->[0]->{feedback}, 1, 'Got 1 expected feedback from import_patrons that matched and overwritten');
176 is($result_3a->{feedback}->[0]->{name}, 'headerrow', 'Got the expected header row name from import_patrons with duplicate userid');
177 is($result_3a->{feedback}->[0]->{value}, $res_header, 'Got the expected header row value from import_patrons with duplicate userid');
178
179 is($result_3a->{imported}, 0, 'Got the expected 0 imported result from import_patrons');
180 is($result_3a->{invalid}, 0, 'Got the expected 0 invalid result from import_patrons');
181 is($result_3a->{overwritten}, 1, 'Got the expected 1 overwritten result from import_patrons that matched');
182
183 # Given ... valid file handle, good matchpoint that does not match and conflicting userid.
184 my $filename_3b = make_csv($temp_dir, $csv_headers, $csv_one_line_a);
185 open(my $handle_3b, "<", $filename_3b) or die "cannot open < $filename_3: $!";
186 my $params_3b = { file => $handle_3b, matchpoint => 'cardnumber', };
187
188 # When ...
189 my $result_3b = $patrons_import->import_patrons($params_3b);
190
191 # Then ...
192 is($result_3b->{already_in_db}, 0, 'Got the expected 0 already_in_db from import_patrons with duplicate userid');
193 is($result_3b->{errors}->[0]->{duplicate_userid}, 1, 'Got the expected duplicate userid error from import patrons with duplicate userid');
194 is($result_3b->{errors}->[0]->{userid}, 'jjenkins0', 'Got the expected userid error from import patrons with duplicate userid');
195
196 is($result_3b->{feedback}->[0]->{feedback}, 1, 'Got the expected 1 feedback from import_patrons with duplicate userid');
197 is($result_3b->{feedback}->[0]->{name}, 'headerrow', 'Got the expected header row name from import_patrons with duplicate userid');
198 is($result_3b->{feedback}->[0]->{value}, $res_header, 'Got the expected header row value from import_patrons with duplicate userid');
199
200 is($result_3b->{imported}, 0, 'Got the expected 0 imported result from import_patrons with duplicate userid');
201 is($result_3b->{invalid}, 1, 'Got the expected 1 invalid result from import_patrons with duplicate userid');
202 is($result_3b->{overwritten}, 0, 'Got the expected 0 overwritten result from import_patrons with duplicate userid');
203
204 # Given ... a new input and mocked C4::Context
205 t::lib::Mocks::mock_preference('ExtendedPatronAttributes', 1);
206 my $attribute = $builder->build({ source => "BorrowerAttributeType"});
207
208 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';
209 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';
210 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';
211 my $filename_4 = make_csv($temp_dir, $csv_headers_a, $new_input_line);
212 open(my $handle_4, "<", $filename_4) or die "cannot open < $filename_4: $!";
213 my $params_4 = { file => $handle_4, matchpoint => $attribute->{code}, };
214
215 # When ...
216 my $result_4 = $patrons_import->import_patrons($params_4);
217
218 # Then ...
219 is($result_4->{already_in_db}, 0, 'Got the expected 0 already_in_db from import_patrons with extended user');
220 is(scalar @{$result_4->{errors}}, 0, 'Got the expected 0 size error array from import_patrons with extended user');
221
222 is($result_4->{feedback}->[0]->{feedback}, 1, 'Got the expected 1 feedback from import_patrons with extended user');
223 is($result_4->{feedback}->[0]->{name}, 'headerrow', 'Got the expected header row name from import_patrons with extended user');
224 is($result_4->{feedback}->[0]->{value}, $res_header_a, 'Got the expected header row value from import_patrons with extended user');
225
226 is($result_4->{feedback}->[1]->{feedback}, 1, 'Got the expected second feedback from import_patrons with extended user');
227 is($result_4->{feedback}->[1]->{name}, 'attribute string', 'Got the expected attribute string from import_patrons with extended user');
228 is($result_4->{feedback}->[1]->{value}, $attribute->{code}.':1', 'Got the expected second feedback value from import_patrons with extended user');
229
230 is($result_4->{feedback}->[2]->{feedback}, 1, 'Got the expected third feedback from import_patrons with extended user');
231 is($result_4->{feedback}->[2]->{name}, 'lastimported', 'Got the expected last imported name from import_patrons with extended user');
232 like($result_4->{feedback}->[2]->{value}, qr/^Donna \/ \d+/, 'Got the expected third feedback value from import_patrons with extended user');
233
234 is($result_4->{imported}, 1, 'Got the expected 1 imported result from import_patrons with extended user');
235 is($result_4->{invalid}, 0, 'Got the expected 0 invalid result from import_patrons with extended user');
236 is($result_4->{overwritten}, 0, 'Got the expected 0 overwritten result from import_patrons with extended user');
237
238 seek $handle_4,0,0; #Reset to verify finding a matched patron works
239 my $result_4a = $patrons_import->import_patrons($params_4);
240 is($result_4a->{already_in_db}, 1, 'Got the expected 1 already_in_db from import_patrons with extended user matched');
241 is(scalar @{$result_4->{errors}}, 0, 'Got the expected 0 size error array from import_patrons with extended user matched');
242
243 is($result_4a->{feedback}->[0]->{feedback}, 1, 'Got the expected 1 feedback from import_patrons with extended user matched');
244 is($result_4a->{feedback}->[0]->{name}, 'headerrow', 'Got the expected header row name from import_patrons with extended user matched');
245 is($result_4a->{feedback}->[0]->{value}, $res_header_a, 'Got the expected header row value from import_patrons with extended user matched');
246
247 is($result_4a->{feedback}->[1]->{feedback}, 1, 'Got the expected second feedback from import_patrons with extended user matched');
248 is($result_4a->{feedback}->[1]->{name}, 'attribute string', 'Got the expected attribute string from import_patrons with extended user matched');
249 is($result_4a->{feedback}->[1]->{value}, $attribute->{code}.':1', 'Got the expected second feedback value from import_patrons with extended user matched');
250
251 is($result_4a->{feedback}->[2]->{already_in_db}, '1', 'Got the expected already_in_db from import_patrons with extended user matched');
252 like($result_4a->{feedback}->[2]->{value}, qr/^Donna \/ \d+/, 'Got the expected third feedback value from import_patrons with extended user matched');
253
254 is($result_4a->{imported}, 0, 'Got the expected 0 imported result from import_patrons with extended user matched');
255 is($result_4a->{invalid}, 0, 'Got the expected 0 invalid result from import_patrons with extended user matched');
256 is($result_4a->{overwritten}, 0, 'Got the expected 0 overwritten result from import_patrons with extended user matched');
257
258 t::lib::Mocks::mock_preference('ExtendedPatronAttributes', 0);
259
260 my $surname ='Chloé❤';
261 # Given ... 3 new inputs. One with no branch code, one with unexpected branch code.
262 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|;
263 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|;
264 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|;
265
266 my $filename_5 = make_csv($temp_dir, $csv_headers, encode_utf8($input_no_branch), encode_utf8($input_good_branch), encode_utf8($input_na_branch));
267 open(my $handle_5, "<", $filename_5) or die "cannot open < $filename_5: $!";
268 my $params_5 = { file => $handle_5, matchpoint => 'cardnumber', };
269
270 # When ...
271 my $result_5 = $patrons_import->import_patrons($params_5);
272
273 # Then ...
274 is($result_5->{already_in_db}, 0, 'Got the expected 0 already_in_db from import_patrons for branch tests');
275
276 is($result_5->{errors}->[0]->{missing_criticals}->[0]->{borrowernumber}, 'UNDEF', 'Got the expected undef borrower number error from import patrons for branch tests');
277 is($result_5->{errors}->[0]->{missing_criticals}->[0]->{key}, 'branchcode', 'Got the expected branch code key from import patrons for branch tests');
278 is($result_5->{errors}->[0]->{missing_criticals}->[0]->{line}, 2, 'Got the expected 2 line number error from import patrons for branch tests');
279 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');
280 is($result_5->{errors}->[0]->{missing_criticals}->[0]->{surname}, $surname, 'Got the expected surname error from import patrons for branch tests');
281
282 is($result_5->{errors}->[1]->{missing_criticals}->[0]->{borrowernumber}, 'UNDEF', 'Got the expected undef borrower number error from import patrons for branch tests');
283 is($result_5->{errors}->[1]->{missing_criticals}->[0]->{branch_map}, 1, 'Got the expected 1 branchmap error from import patrons for branch tests');
284 is($result_5->{errors}->[1]->{missing_criticals}->[0]->{key}, 'branchcode', 'Got the expected branch code key from import patrons for branch tests');
285 is($result_5->{errors}->[1]->{missing_criticals}->[0]->{line}, 4, 'Got the expected 4 line number error from import patrons for branch tests');
286 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');
287 is($result_5->{errors}->[1]->{missing_criticals}->[0]->{surname}, $surname, 'Got the expected surname error from import patrons for branch tests');
288 is($result_5->{errors}->[1]->{missing_criticals}->[0]->{value}, 'ZZZ', 'Got the expected ZZZ value error from import patrons for branch tests');
289
290 is($result_5->{feedback}->[0]->{feedback}, 1, 'Got the expected 1 feedback from import_patrons for branch tests');
291 is($result_5->{feedback}->[0]->{name}, 'headerrow', 'Got the expected header row name from import_patrons for branch tests');
292 is($result_5->{feedback}->[0]->{value}, $res_header, 'Got the expected header row value from import_patrons for branch tests');
293
294 is($result_5->{feedback}->[1]->{feedback}, 1, 'Got the expected 1 feedback from import_patrons for branch tests');
295 is($result_5->{feedback}->[1]->{name}, 'lastimported', 'Got the expected lastimported name from import_patrons for branch tests');
296 like($result_5->{feedback}->[1]->{value},  qr/^$surname \/ \d+/, 'Got the expected last imported value from import_patrons with for branch tests');
297
298 is($result_5->{imported}, 1, 'Got the expected 1 imported result from import patrons for branch tests');
299 is($result_5->{invalid}, 2, 'Got the expected 2 invalid result from import patrons for branch tests');
300 is($result_5->{overwritten}, 0, 'Got the expected 0 overwritten result from import patrons for branch tests');
301
302 # Given ... 3 new inputs. One with no category code, one with unexpected category code.
303 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';
304 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';
305 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';
306
307 my $filename_6 = make_csv($temp_dir, $csv_headers, $input_no_category, $input_good_category, $input_na_category);
308 open(my $handle_6, "<", $filename_6) or die "cannot open < $filename_6: $!";
309 my $params_6 = { file => $handle_6, matchpoint => 'cardnumber', };
310
311 # When ...
312 my $result_6 = $patrons_import->import_patrons($params_6);
313
314 # Then ...
315 is($result_6->{already_in_db}, 0, 'Got the expected 0 already_in_db from import_patrons for category tests');
316
317 is($result_6->{errors}->[0]->{missing_criticals}->[0]->{borrowernumber}, 'UNDEF', 'Got the expected undef borrower number error from import patrons for category tests');
318 is($result_6->{errors}->[0]->{missing_criticals}->[0]->{key}, 'categorycode', 'Got the expected category code key from import patrons for category tests');
319 is($result_6->{errors}->[0]->{missing_criticals}->[0]->{line}, 2, 'Got the expected 2 line number error from import patrons for category tests');
320 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');
321 is($result_6->{errors}->[0]->{missing_criticals}->[0]->{surname}, 'Christina', 'Got the expected surname error from import patrons for category tests');
322
323 is($result_6->{errors}->[1]->{missing_criticals}->[0]->{borrowernumber}, 'UNDEF', 'Got the expected undef borrower number error from import patrons for category tests');
324 is($result_6->{errors}->[1]->{missing_criticals}->[0]->{category_map}, 1, 'Got the expected 1 category_map error from import patrons for category tests');
325 is($result_6->{errors}->[1]->{missing_criticals}->[0]->{key}, 'categorycode', 'Got the expected category code key from import patrons for category tests');
326 is($result_6->{errors}->[1]->{missing_criticals}->[0]->{line}, 4, 'Got the expected 4 line number error from import patrons for category tests');
327 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');
328 is($result_6->{errors}->[1]->{missing_criticals}->[0]->{surname}, 'Emily', 'Got the expected surname error from import patrons for category tests');
329 is($result_6->{errors}->[1]->{missing_criticals}->[0]->{value}, 'ZZ', 'Got the expected ZZ value error from import patrons for category tests');
330
331 is($result_6->{feedback}->[0]->{feedback}, 1, 'Got the expected 1 feedback from import_patrons for category tests');
332 is($result_6->{feedback}->[0]->{name}, 'headerrow', 'Got the expected header row name from import_patrons for category tests');
333 is($result_6->{feedback}->[0]->{value}, $res_header, 'Got the expected header row value from import_patrons for category tests');
334
335 is($result_6->{feedback}->[1]->{feedback}, 1, 'Got the expected 1 feedback from import_patrons for category tests');
336 is($result_6->{feedback}->[1]->{name}, 'lastimported', 'Got the expected lastimported name from import_patrons for category tests');
337 like($result_6->{feedback}->[1]->{value},  qr/^Peter \/ \d+/, 'Got the expected last imported value from import_patrons with for category tests');
338
339 is($result_6->{imported}, 1, 'Got the expected 1 imported result from import patrons for category tests');
340 is($result_6->{invalid}, 2, 'Got the expected 2 invalid result from import patrons for category tests');
341 is($result_6->{overwritten}, 0, 'Got the expected 0 overwritten result from import patrons for category tests');
342
343 # Given ... 2 new inputs. One without dateofbirth, dateenrolled and dateexpiry values.
344 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';
345 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';
346
347 my $filename_7 = make_csv($temp_dir, $csv_headers, $input_complete, $input_no_date);
348 open(my $handle_7, "<", $filename_7) or die "cannot open < $filename_7: $!";
349 my $params_7 = { file => $handle_7, matchpoint => 'cardnumber', };
350
351 # When ...
352 my $result_7 = $patrons_import->import_patrons($params_7);
353
354 # Then ...
355 is($result_7->{already_in_db}, 0, 'Got the expected 0 already_in_db from import_patrons for dates tests');
356 is(scalar @{$result_7->{errors}}, 1, 'Got the expected 1 error array size from import_patrons for dates tests');
357 is(scalar @{$result_7->{errors}->[0]->{missing_criticals}}, 3, 'Got the expected 3 missing critical errors from import_patrons for dates tests');
358
359 is($result_7->{errors}->[0]->{missing_criticals}->[0]->{bad_date}, 1, 'Got the expected 1 bad_date error from import patrons for dates tests');
360 is($result_7->{errors}->[0]->{missing_criticals}->[0]->{borrowernumber}, 'UNDEF', 'Got the expected undef borrower number error from import patrons for dates tests');
361 is($result_7->{errors}->[0]->{missing_criticals}->[0]->{key}, 'dateofbirth', 'Got the expected dateofbirth key from import patrons for dates tests');
362 is($result_7->{errors}->[0]->{missing_criticals}->[0]->{line}, 3, 'Got the expected 2 line number error from import patrons for dates tests');
363 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');
364 is($result_7->{errors}->[0]->{missing_criticals}->[0]->{surname}, 'Ralph', 'Got the expected surname error from import patrons for dates tests');
365
366 is($result_7->{errors}->[0]->{missing_criticals}->[1]->{key}, 'dateenrolled', 'Got the expected dateenrolled key from import patrons for dates tests');
367 is($result_7->{errors}->[0]->{missing_criticals}->[2]->{key}, 'dateexpiry', 'Got the expected dateexpiry key from import patrons for dates tests');
368
369 is(scalar @{$result_7->{feedback}}, 2, 'Got the expected 2 feedback from import patrons for dates tests');
370 is($result_7->{feedback}->[0]->{feedback}, 1, 'Got the expected 1 feedback from import_patrons for dates tests');
371 is($result_7->{feedback}->[0]->{name}, 'headerrow', 'Got the expected header row name from import_patrons for dates tests');
372 is($result_7->{feedback}->[0]->{value}, $res_header, 'Got the expected header row value from import_patrons for dates tests');
373
374 is($result_7->{feedback}->[1]->{feedback}, 1, 'Got the expected 1 feedback from import_patrons for dates tests');
375 is($result_7->{feedback}->[1]->{name}, 'lastimported', 'Got the expected lastimported from import_patrons for dates tests');
376 like($result_7->{feedback}->[1]->{value}, qr/^Christina \/ \d+/, 'Got the expected lastimported value from import_patrons for dates tests');
377
378 is($result_7->{imported}, 1, 'Got the expected 1 imported result from import patrons for dates tests');
379 is($result_7->{invalid}, 1, 'Got the expected 1 invalid result from import patrons for dates tests');
380 is($result_7->{overwritten}, 0, 'Got the expected 0 overwritten result from import patrons for dates tests');
381
382 subtest 'test_import_without_cardnumber' => sub {
383     plan tests => 2;
384
385     #Remove possible existing user with a "" as cardnumber
386     my $blank_card = Koha::Patrons->find({ cardnumber => '' });
387     $blank_card->delete if $blank_card;
388
389     my $branchcode = $builder->build({ source => "Branch"})->{branchcode};
390     my $categorycode = $builder->build({ source => "Category"})->{categorycode};
391     my $csv_headers  = 'surname, branchcode, categorycode';
392     my $res_headers  = 'surname, branchcode, categorycode';
393     my $csv_nocard_1 = "Squarepants,$branchcode,$categorycode";
394     my $csv_nocard_2 = "Star,$branchcode,$categorycode";
395
396     my $filename_1 = make_csv($temp_dir, $csv_headers, $csv_nocard_1, $csv_nocard_2);
397     open(my $handle_1, "<", $filename_1) or die "cannot open < $filename_1: $!";
398     my $params_1 = { file => $handle_1, };
399
400     my $defaults = { cardnumber => "" }; #currently all the defaults come as "" if not filled
401
402     my $result = $patrons_import->import_patrons($params_1, $defaults);
403     like($result->{feedback}->[1]->{value}, qr/^Squarepants \/ \d+/, 'First borrower imported as expected');
404     like($result->{feedback}->[2]->{value}, qr/^Star \/ \d+/, 'Second borrower imported as expected');
405
406 };
407
408 subtest 'test_import_with_cardnumber_0' => sub {
409     plan tests => 2;
410
411     #Remove possible existing user with a "" as cardnumber
412     my $zero_card = Koha::Patrons->find({ cardnumber => 0 });
413     $zero_card->delete if $zero_card;
414
415     my $branchcode = $builder->build({ source => "Branch"})->{branchcode};
416     my $categorycode = $builder->build({ source => "Category"})->{categorycode};
417     my $csv_headers  = 'cardnumber,surname, branchcode, categorycode';
418     my $res_headers  = 'cardnumber,surname, branchcode, categorycode';
419     my $csv_nocard_1 = "0,Squarepants,$branchcode,$categorycode";
420
421     my $filename_1 = make_csv($temp_dir, $csv_headers, $csv_nocard_1);
422     open(my $handle_1, "<", $filename_1) or die "cannot open < $filename_1: $!";
423     my $params_1 = { file => $handle_1, };
424
425     my $defaults = { cardnumber => "" }; #currently all the defaults come as "" if not filled
426
427     my $result = $patrons_import->import_patrons($params_1, $defaults);
428     like($result->{feedback}->[1]->{value}, qr/^Squarepants \/ \d+/, 'First borrower imported as expected');
429     $zero_card = Koha::Patrons->find({ cardnumber => 0 });
430     is($zero_card->surname.$zero_card->branchcode.$zero_card->categorycode,'Squarepants'.$branchcode.$categorycode,"Patron with cardnumber 0 is the imported patron");
431
432 };
433
434
435 subtest 'test_prepare_columns' => sub {
436     plan tests => 16;
437
438     # Given ... no header row
439     my %csvkeycol_0;
440     my @errors_0;
441
442     # When ...
443     my @csvcolumns_0 = $patrons_import->prepare_columns({headerrow => undef, keycol => \%csvkeycol_0, errors => \@errors_0, });
444
445     # Then ...
446     is(scalar @csvcolumns_0, 0, 'Got the expected empty column array from prepare columns with no header row');
447
448     is(scalar @errors_0, 1, 'Got the expected 1 entry in error array from prepare columns with no header row');
449     is($errors_0[0]->{badheader}, 1, 'Got the expected 1 badheader from prepare columns with no header row');
450     is($errors_0[0]->{line}, 1, 'Got the expected 1 line from prepare columns with no header row');
451     is($errors_0[0]->{lineraw}, undef, 'Got the expected undef lineraw from prepare columns with no header row');
452
453     # Given ... a good header row with plenty of whitespaces
454     my $headerrow_1 = 'a,    b ,        c,  ,   d';
455     my %csvkeycol_1;
456     my @errors_1;
457
458     # When ...
459     my @csvcolumns_1 = $patrons_import->prepare_columns({headerrow => $headerrow_1, keycol => \%csvkeycol_1, errors => \@errors_1, });
460
461     # Then ...
462     is(scalar @csvcolumns_1, 5, 'Got the expected 5 column array from prepare columns');
463     is($csvcolumns_1[0], 'a', 'Got the expected a header from prepare columns');
464     is($csvcolumns_1[1], 'b', 'Got the expected b header from prepare columns');
465     is($csvcolumns_1[2], 'c', 'Got the expected c header from prepare columns');
466     is($csvcolumns_1[3], '', 'Got the expected empty header from prepare columns');
467     is($csvcolumns_1[4], 'd', 'Got the expected d header from prepare columns');
468
469     is($csvkeycol_1{a}, 0, 'Got the expected 0 value for key a from prepare columns hash');
470     is($csvkeycol_1{b}, 1, 'Got the expected 1 value for key b from prepare columns hash');
471     is($csvkeycol_1{c}, 2, 'Got the expected 2 value for key c from prepare columns hash');
472     is($csvkeycol_1{''}, 3, 'Got the expected 3 value for empty string key from prepare columns hash');
473     is($csvkeycol_1{d}, 4, 'Got the expected 4 value for key d from prepare columns hash');
474 };
475
476 subtest 'test_set_column_keys' => sub {
477     plan tests => 5;
478
479     # Given ... nothing at all
480     # When ... Then ...
481     my $attr_type_0 = $patrons_import->set_attribute_types(undef);
482     is($attr_type_0, undef, 'Got the expected undef attribute type from set attribute types with nothing');
483
484     # Given ... extended but not matchpoint
485     my $params_1 = { extended => 1, matchpoint => undef, };
486
487     # When ... Then ...
488     my $attr_type_1 = $patrons_import->set_attribute_types($params_1);
489     is($attr_type_1, undef, 'Got the expected undef attribute type from set attribute types with no matchpoint');
490
491     # Given ... extended and unexpected matchpoint
492     my $params_2 = { extended => 1, matchpoint => 'unexpected', };
493
494     # When ... Then ...
495     my $attr_type_2 = $patrons_import->set_attribute_types($params_2);
496     is($attr_type_2, undef, 'Got the expected undef attribute type from set attribute types with unexpected matchpoint');
497
498     # Given ...
499     my $code_3   = 'SHOW_BCODE';
500     my $params_3 = { extended => 1, matchpoint => $code_3, };
501
502     # When ...
503     my $attr_type_3 = $patrons_import->set_attribute_types($params_3);
504
505     # Then ...
506     isa_ok($attr_type_3, 'C4::Members::AttributeTypes');
507     is($attr_type_3->{code}, $code_3, 'Got the expected code attribute type from set attribute types');
508 };
509
510 subtest 'test_set_column_keys' => sub {
511     plan tests => 2;
512
513     my @columns = Koha::Patrons->columns;
514     # Given ... nothing at all
515     # When ... Then ...
516     my @columnkeys_0 = $patrons_import->set_column_keys(undef);
517     # -1 because we do not want the borrowernumber column
518     is(scalar @columnkeys_0, @columns - 1, 'Got the expected array size from set column keys with undef extended');
519
520     # Given ... extended.
521     my $extended = 1;
522
523     # When ... Then ...
524     my @columnkeys_1 = $patrons_import->set_column_keys($extended);
525     is(scalar @columnkeys_1, @columns - 1 + $extended, 'Got the expected array size from set column keys with extended');
526 };
527
528 subtest 'test_set_patron_attributes' => sub {
529     plan tests => 13;
530
531     # Given ... nothing at all
532     # When ... Then ...
533     my $result_0 = $patrons_import->set_patron_attributes(undef, undef, undef);
534     is($result_0, undef, 'Got the expected undef from set patron attributes with nothing');
535
536     # Given ... not extended.
537     my $extended_1 = 0;
538
539     # When ... Then ...
540     my $result_1 = $patrons_import->set_patron_attributes($extended_1, undef, undef);
541     is($result_1, undef, 'Got the expected undef from set patron attributes with not extended');
542
543     # Given ... NO patrons attributes
544     my $extended_2          = 1;
545     my $patron_attributes_2 = undef;
546     my @feedback_2;
547
548     # When ...
549     my $result_2 = $patrons_import->set_patron_attributes($extended_2, $patron_attributes_2, \@feedback_2);
550
551     # Then ...
552     is($result_2, undef, 'Got the expected undef from set patron attributes with no patrons attributes');
553     is(scalar @feedback_2, 0, 'Got the expected 0 size feedback array from set patron attributes with no patrons attributes');
554
555     # Given ... some patrons attributes
556     my $patron_attributes_3 = "homeroom:1150605,grade:01";
557     my @feedback_3;
558
559     # When ...
560     my $result_3 = $patrons_import->set_patron_attributes($extended_2, $patron_attributes_3, \@feedback_3);
561
562     # Then ...
563     ok($result_3, 'Got some data back from set patron attributes');
564     is($result_3->[0]->{code}, 'grade', 'Got the expected first code from set patron attributes');
565     is($result_3->[0]->{value}, '01', 'Got the expected first value from set patron attributes');
566
567     is($result_3->[1]->{code}, 'homeroom', 'Got the expected second code from set patron attributes');
568     is($result_3->[1]->{value}, 1150605, 'Got the expected second value from set patron attributes');
569
570     is(scalar @feedback_3, 1, 'Got the expected 1 array size from set patron attributes with extended user');
571     is($feedback_3[0]->{feedback}, 1, 'Got the expected second feedback from set patron attributes with extended user');
572     is($feedback_3[0]->{name}, 'attribute string', 'Got the expected attribute string from set patron attributes with extended user');
573     is($feedback_3[0]->{value}, 'homeroom:1150605,grade:01', 'Got the expected feedback value from set patron attributes with extended user');
574 };
575
576 subtest 'test_check_branch_code' => sub {
577     plan tests => 11;
578
579     # Given ... no branch code.
580     my $borrowerline      = 'some, line';
581     my $line_number       = 78;
582     my @missing_criticals = ();
583
584     # When ...
585     $patrons_import->check_branch_code(undef, $borrowerline, $line_number, \@missing_criticals);
586
587     # Then ...
588     is(scalar @missing_criticals, 1, 'Got the expected missing critical array size of 1 from check_branch_code with no branch code');
589
590     is($missing_criticals[0]->{key}, 'branchcode', 'Got the expected branchcode key from check_branch_code with no branch code');
591     is($missing_criticals[0]->{line}, $line_number, 'Got the expected line number from check_branch_code with no branch code');
592     is($missing_criticals[0]->{lineraw}, $borrowerline, 'Got the expected lineraw value from check_branch_code with no branch code');
593
594     # Given ... unknown branch code
595     my $branchcode_1        = 'unexpected';
596     my $borrowerline_1      = 'some, line,'.$branchcode_1;
597     my $line_number_1       = 79;
598     my @missing_criticals_1 = ();
599
600     # When ...
601     $patrons_import->check_branch_code($branchcode_1, $borrowerline_1, $line_number_1, \@missing_criticals_1);
602
603     # Then ...
604     is(scalar @missing_criticals_1, 1, 'Got the expected missing critical array size of 1 from check_branch_code with unexpected branch code');
605
606     is($missing_criticals_1[0]->{branch_map}, 1, 'Got the expected 1 branch_map from check_branch_code with unexpected branch code');
607     is($missing_criticals_1[0]->{key}, 'branchcode', 'Got the expected branchcode key from check_branch_code with unexpected branch code');
608     is($missing_criticals_1[0]->{line}, $line_number_1, 'Got the expected line number from check_branch_code with unexpected branch code');
609     is($missing_criticals_1[0]->{lineraw}, $borrowerline_1, 'Got the expected lineraw value from check_branch_code with unexpected branch code');
610     is($missing_criticals_1[0]->{value}, $branchcode_1, 'Got the expected value from check_branch_code with unexpected branch code');
611
612     # Given ... a known branch code. Relies on database sample data
613     my $branchcode_2        = 'FFL';
614     my $borrowerline_2      = 'some, line,'.$branchcode_2;
615     my $line_number_2       = 80;
616     my @missing_criticals_2 = ();
617
618     # When ...
619     $patrons_import->check_branch_code($branchcode_2, $borrowerline_2, $line_number_2, \@missing_criticals_2);
620
621     # Then ...
622     is(scalar @missing_criticals_2, 0, 'Got the expected missing critical array size of 0 from check_branch_code');
623 };
624
625 subtest 'test_check_borrower_category' => sub {
626     plan tests => 11;
627
628     # Given ... no category code.
629     my $borrowerline      = 'some, line';
630     my $line_number       = 781;
631     my @missing_criticals = ();
632
633     # When ...
634     $patrons_import->check_borrower_category(undef, $borrowerline, $line_number, \@missing_criticals);
635
636     # Then ...
637     is(scalar @missing_criticals, 1, 'Got the expected missing critical array size of 1 from check_branch_code with no category code');
638
639     is($missing_criticals[0]->{key}, 'categorycode', 'Got the expected categorycode key from check_branch_code with no category code');
640     is($missing_criticals[0]->{line}, $line_number, 'Got the expected line number from check_branch_code with no category code');
641     is($missing_criticals[0]->{lineraw}, $borrowerline, 'Got the expected lineraw value from check_branch_code with no category code');
642
643     # Given ... unknown category code
644     my $categorycode_1      = 'unexpected';
645     my $borrowerline_1      = 'some, line, line, '.$categorycode_1;
646     my $line_number_1       = 791;
647     my @missing_criticals_1 = ();
648
649     # When ...
650     $patrons_import->check_borrower_category($categorycode_1, $borrowerline_1, $line_number_1, \@missing_criticals_1);
651
652     # Then ...
653     is(scalar @missing_criticals_1, 1, 'Got the expected missing critical array size of 1 from check_branch_code with unexpected category code');
654
655     is($missing_criticals_1[0]->{category_map}, 1, 'Got the expected 1 category_map from check_branch_code with unexpected category code');
656     is($missing_criticals_1[0]->{key}, 'categorycode', 'Got the expected branchcode key from check_branch_code with unexpected category code');
657     is($missing_criticals_1[0]->{line}, $line_number_1, 'Got the expected line number from check_branch_code with unexpected category code');
658     is($missing_criticals_1[0]->{lineraw}, $borrowerline_1, 'Got the expected lineraw value from check_branch_code with unexpected category code');
659     is($missing_criticals_1[0]->{value}, $categorycode_1, 'Got the expected value from check_branch_code with unexpected category code');
660
661     # Given ... a known category code. Relies on database sample data.
662     my $categorycode_2      = 'T';
663     my $borrowerline_2      = 'some, line,'.$categorycode_2;
664     my $line_number_2       = 801;
665     my @missing_criticals_2 = ();
666
667     # When ...
668     $patrons_import->check_borrower_category($categorycode_2, $borrowerline_2, $line_number_2, \@missing_criticals_2);
669
670     # Then ...
671     is(scalar @missing_criticals_2, 0, 'Got the expected missing critical array size of 0 from check_branch_code');
672 };
673
674 subtest 'test_format_dates' => sub {
675     plan tests => 22;
676
677     # Given ... no borrower data.
678     my $borrowerline      = 'another line';
679     my $line_number       = 987;
680     my @missing_criticals = ();
681     my %borrower;
682     my $params = {borrower => \%borrower, lineraw => $borrowerline, line => $line_number, missing_criticals => \@missing_criticals, };
683
684     # When ...
685     $patrons_import->format_dates($params);
686
687     # Then ...
688     ok( not(%borrower), 'Got the expected no borrower from format_dates with no dates');
689     is(scalar @missing_criticals, 0, 'Got the expected missing critical array size of 0 from format_dates with no dates');
690
691     # Given ... some good dates
692     my @missing_criticals_1 = ();
693     my $dateofbirth_1  = '2016-05-03';
694     my $dateenrolled_1 = '2016-05-04';
695     my $dateexpiry_1   = '2016-05-06';
696     my $borrower_1     = { dateofbirth => $dateofbirth_1, dateenrolled => $dateenrolled_1, dateexpiry => $dateexpiry_1, };
697     my $params_1       = {borrower => $borrower_1, lineraw => $borrowerline, line => $line_number, missing_criticals => \@missing_criticals_1, };
698
699     # When ...
700     $patrons_import->format_dates($params_1);
701
702     # Then ...
703     is($borrower_1->{dateofbirth}, $dateofbirth_1, 'Got the expected date of birth from format_dates with good dates');
704     is($borrower_1->{dateenrolled}, $dateenrolled_1, 'Got the expected date of birth from format_dates with good dates');
705     is($borrower_1->{dateexpiry}, $dateexpiry_1, 'Got the expected date of birth from format_dates with good dates');
706     is(scalar @missing_criticals_1, 0, 'Got the expected missing critical array size of 0 from check_branch_code with good dates');
707
708     # Given ... some very bad dates
709     my @missing_criticals_2 = ();
710     my $dateofbirth_2  = '03-2016-05';
711     my $dateenrolled_2 = '04-2016-05';
712     my $dateexpiry_2   = '06-2016-05';
713     my $borrower_2     = { dateofbirth => $dateofbirth_2, dateenrolled => $dateenrolled_2, dateexpiry => $dateexpiry_2, };
714     my $params_2       = {borrower => $borrower_2, lineraw => $borrowerline, line => $line_number, missing_criticals => \@missing_criticals_2, };
715
716     # When ...
717     $patrons_import->format_dates($params_2);
718
719     # Then ...
720     is($borrower_2->{dateofbirth}, '', 'Got the expected empty date of birth from format_dates with bad dates');
721     is($borrower_2->{dateenrolled}, '', 'Got the expected emptydate of birth from format_dates with bad dates');
722     is($borrower_2->{dateexpiry}, '', 'Got the expected empty date of birth from format_dates with bad dates');
723
724     is(scalar @missing_criticals_2, 3, 'Got the expected missing critical array size of 3 from check_branch_code with bad dates');
725     is($missing_criticals_2[0]->{bad_date}, 1, 'Got the expected first bad date flag from check_branch_code with bad dates');
726     is($missing_criticals_2[0]->{key}, 'dateofbirth', 'Got the expected dateofbirth key from check_branch_code with bad dates');
727     is($missing_criticals_2[0]->{line}, $line_number, 'Got the expected first line from check_branch_code with bad dates');
728     is($missing_criticals_2[0]->{lineraw}, $borrowerline, 'Got the expected first lineraw from check_branch_code with bad dates');
729
730     is($missing_criticals_2[1]->{bad_date}, 1, 'Got the expected second bad date flag from check_branch_code with bad dates');
731     is($missing_criticals_2[1]->{key}, 'dateenrolled', 'Got the expected dateenrolled key from check_branch_code with bad dates');
732     is($missing_criticals_2[1]->{line}, $line_number, 'Got the expected second line from check_branch_code with bad dates');
733     is($missing_criticals_2[1]->{lineraw}, $borrowerline, 'Got the expected second lineraw from check_branch_code with bad dates');
734
735     is($missing_criticals_2[2]->{bad_date}, 1, 'Got the expected third bad date flag from check_branch_code with bad dates');
736     is($missing_criticals_2[2]->{key}, 'dateexpiry', 'Got the expected dateexpiry key from check_branch_code with bad dates');
737     is($missing_criticals_2[2]->{line}, $line_number, 'Got the expected third line from check_branch_code with bad dates');
738     is($missing_criticals_2[2]->{lineraw}, $borrowerline, 'Got the expected third lineraw from check_branch_code with bad dates');
739 };
740
741 # ###### Test utility ###########
742 sub make_csv {
743     my ($temp_dir, @lines) = @_;
744
745     my ($fh, $filename) = tempfile( DIR => $temp_dir) or die $!;
746     print $fh $_."\r\n" foreach @lines;
747     close $fh or die $!;
748
749     return $filename;
750 }
751
752 1;