Line 0
Link Here
|
|
|
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 => 124; |
22 |
use Test::Warn; |
23 |
|
24 |
# To be replaced by t::lib::Mock |
25 |
use Test::MockModule; |
26 |
use Koha::Database; |
27 |
|
28 |
use File::Temp qw(tempfile tempdir); |
29 |
my $temp_dir = tempdir('Koha_patrons_import_test_XXXX', CLEANUP => 1, TMPDIR => 1); |
30 |
|
31 |
use t::lib::TestBuilder; |
32 |
my $builder = t::lib::TestBuilder->new; |
33 |
|
34 |
my $schema = Koha::Database->new->schema; |
35 |
$schema->storage->txn_begin; |
36 |
|
37 |
# ########## Tests start here ############################# |
38 |
# Given ... we can use the module |
39 |
BEGIN { use_ok('Koha::Patrons::Import'); } |
40 |
|
41 |
my $patrons_import = new_ok('Koha::Patrons::Import'); |
42 |
|
43 |
subtest 'test_methods' => sub { |
44 |
plan tests => 1; |
45 |
|
46 |
# Given ... we can reach the method(s) |
47 |
my @methods = ('import_patrons', |
48 |
'set_attribute_types', |
49 |
'prepare_columns', |
50 |
'set_column_keys', |
51 |
'set_patron_attributes', |
52 |
'check_branch_code', |
53 |
'format_dates', |
54 |
); |
55 |
can_ok('Koha::Patrons::Import', @methods); |
56 |
}; |
57 |
|
58 |
subtest 'test_attributes' => sub { |
59 |
plan tests => 1; |
60 |
|
61 |
my @attributes = ('today_iso', 'text_csv'); |
62 |
can_ok('Koha::Patrons::Import', @attributes); |
63 |
}; |
64 |
|
65 |
# Tests for Koha::Patrons::Import::import_patrons() |
66 |
# Given ... nothing much. When ... Then ... |
67 |
my $result; |
68 |
warning_is { $result = $patrons_import->import_patrons(undef) } |
69 |
{ carped => 'No file handle passed in!' }, |
70 |
" Koha::Patrons::Import->import_patrons carps if no file handle is passed"; |
71 |
is($result, undef, 'Got the expected undef from import_patrons with nothing much'); |
72 |
|
73 |
# Given ... some params but no file handle. |
74 |
my $params_0 = { some_stuff => 'random stuff', }; |
75 |
|
76 |
# When ... Then ... |
77 |
my $result_0; |
78 |
warning_is { $result_0 = $patrons_import->import_patrons($params_0) } |
79 |
{ carped => 'No file handle passed in!' }, |
80 |
" Koha::Patrons::Import->import_patrons carps if no file handle is passed"; |
81 |
is($result_0, undef, 'Got the expected undef from import_patrons with no file handle'); |
82 |
|
83 |
# Given ... a file handle to file with headers only. |
84 |
my $ExtendedPatronAttributes = 0; |
85 |
my $context = Test::MockModule->new('C4::Context'); # Necessary mocking for consistent test results. |
86 |
$context->mock('preference', sub { my ($mod, $meth) = @_; |
87 |
if ( $meth eq 'ExtendedPatronAttributes' ) { return $ExtendedPatronAttributes; } |
88 |
if ( $meth eq 'dateformat' ) { return 'us'; } |
89 |
}); |
90 |
|
91 |
|
92 |
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'; |
93 |
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'; |
94 |
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'; |
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 but same input as prior test. |
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}, 0, 'Got the expected 0 already_in_db from import_patrons with duplicate userid'); |
151 |
is($result_3->{errors}->[0]->{duplicate_userid}, 1, 'Got the expected duplicate userid error from import patrons with duplicate userid'); |
152 |
is($result_3->{errors}->[0]->{userid}, 'jjenkins0', 'Got the expected userid error from import patrons with duplicate userid'); |
153 |
|
154 |
is($result_3->{feedback}->[0]->{feedback}, 1, 'Got the expected 1 feedback from import_patrons with duplicate userid'); |
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 with duplicate userid'); |
159 |
is($result_3->{invalid}, 1, 'Got the expected 1 invalid result from import_patrons with duplicate userid'); |
160 |
is($result_3->{overwritten}, 0, 'Got the expected 0 overwritten result from import_patrons with duplicate userid'); |
161 |
|
162 |
# Given ... a new input and mocked C4::Context |
163 |
$ExtendedPatronAttributes = 1; # Updates mocked C4::Preferences result. |
164 |
|
165 |
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'; |
166 |
my $filename_4 = make_csv($temp_dir, $csv_headers, $new_input_line); |
167 |
open(my $handle_4, "<", $filename_4) or die "cannot open < $filename_4: $!"; |
168 |
my $params_4 = { file => $handle_4, matchpoint => 'SHOW_BCODE', }; |
169 |
|
170 |
# When ... |
171 |
my $result_4 = $patrons_import->import_patrons($params_4); |
172 |
|
173 |
# Then ... |
174 |
is($result_4->{already_in_db}, 0, 'Got the expected 0 already_in_db from import_patrons with extended user'); |
175 |
is(scalar @{$result_4->{errors}}, 0, 'Got the expected 0 size error array from import_patrons with extended user'); |
176 |
|
177 |
is($result_4->{feedback}->[0]->{feedback}, 1, 'Got the expected 1 feedback from import_patrons with extended user'); |
178 |
is($result_4->{feedback}->[0]->{name}, 'headerrow', 'Got the expected header row name from import_patrons with extended user'); |
179 |
is($result_4->{feedback}->[0]->{value}, $res_header, 'Got the expected header row value from import_patrons with extended user'); |
180 |
|
181 |
is($result_4->{feedback}->[1]->{feedback}, 1, 'Got the expected second feedback from import_patrons with extended user'); |
182 |
is($result_4->{feedback}->[1]->{name}, 'attribute string', 'Got the expected attribute string from import_patrons with extended user'); |
183 |
is($result_4->{feedback}->[1]->{value}, '', 'Got the expected second feedback value from import_patrons with extended user'); |
184 |
|
185 |
is($result_4->{feedback}->[2]->{feedback}, 1, 'Got the expected third feedback from import_patrons with extended user'); |
186 |
is($result_4->{feedback}->[2]->{name}, 'lastimported', 'Got the expected last imported name from import_patrons with extended user'); |
187 |
like($result_4->{feedback}->[2]->{value}, qr/^Donna \/ \d+/, 'Got the expected third feedback value from import_patrons with extended user'); |
188 |
|
189 |
is($result_4->{imported}, 1, 'Got the expected 1 imported result from import_patrons with extended user'); |
190 |
is($result_4->{invalid}, 0, 'Got the expected 0 invalid result from import_patrons with extended user'); |
191 |
is($result_4->{overwritten}, 0, 'Got the expected 0 overwritten result from import_patrons with extended user'); |
192 |
|
193 |
$context->unmock('preference'); |
194 |
|
195 |
# Given ... 3 new inputs. One with no branch code, one with unexpected branch code. |
196 |
my $input_no_branch = '1002,Johnny,Reynolds,Mr,Patricia,JR,12,Hill,Kennedy,Saint Louis,Colorado Springs,Missouri,63131,United States,preynolds2@washington.edu,7-(925)314-9514,0-(315)973-8956,4-(510)556-2323,09/18/1967,,PT,05/07/2015,07/01/2015,preynolds2,K3HiDzl'; |
197 |
my $input_good_branch = '1003,Linda,Richardson,Mr,Kimberly,LR,90,Place,Bayside,Atlanta,Erie,Georgia,31190,United States,krichardson3@pcworld.com,8-(035)185-0387,4-(796)518-3676,3-(644)960-3789,04/13/1954,RPL,PT,06/06/2015,07/01/2015,krichardson3,P3EO0MVRPXbM'; |
198 |
my $input_na_branch = '1005,Ruth,Greene,Mr,Michael,RG,3,Avenue,Grim,Peoria,Jacksonville,Illinois,61614,United States,mgreene5@seesaa.net,3-(941)565-5752,1-(483)885-8138,4-(979)577-6908,02/09/1957,ZZZ,ST,04/02/2015,07/01/2015,mgreene5,or4ORT6JH'; |
199 |
|
200 |
my $filename_5 = make_csv($temp_dir, $csv_headers, $input_no_branch, $input_good_branch, $input_na_branch); |
201 |
open(my $handle_5, "<", $filename_5) or die "cannot open < $filename_5: $!"; |
202 |
my $params_5 = { file => $handle_5, matchpoint => 'cardnumber', }; |
203 |
|
204 |
# When ... |
205 |
my $result_5 = $patrons_import->import_patrons($params_5); |
206 |
|
207 |
# Then ... |
208 |
is($result_5->{already_in_db}, 0, 'Got the expected 0 already_in_db from import_patrons for branch tests'); |
209 |
|
210 |
is($result_5->{errors}->[0]->{missing_criticals}->[0]->{borrowernumber}, 'UNDEF', 'Got the expected undef borrower number error from import patrons for branch tests'); |
211 |
is($result_5->{errors}->[0]->{missing_criticals}->[0]->{key}, 'branchcode', 'Got the expected branch code key from import patrons for branch tests'); |
212 |
is($result_5->{errors}->[0]->{missing_criticals}->[0]->{line}, 2, 'Got the expected 2 line number error from import patrons for branch tests'); |
213 |
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'); |
214 |
is($result_5->{errors}->[0]->{missing_criticals}->[0]->{surname}, 'Johnny', 'Got the expected surname error from import patrons for branch tests'); |
215 |
|
216 |
is($result_5->{errors}->[1]->{missing_criticals}->[0]->{borrowernumber}, 'UNDEF', 'Got the expected undef borrower number error from import patrons for branch tests'); |
217 |
is($result_5->{errors}->[1]->{missing_criticals}->[0]->{branch_map}, 1, 'Got the expected 1 branchmap error from import patrons for branch tests'); |
218 |
is($result_5->{errors}->[1]->{missing_criticals}->[0]->{key}, 'branchcode', 'Got the expected branch code key from import patrons for branch tests'); |
219 |
is($result_5->{errors}->[1]->{missing_criticals}->[0]->{line}, 4, 'Got the expected 4 line number error from import patrons for branch tests'); |
220 |
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'); |
221 |
is($result_5->{errors}->[1]->{missing_criticals}->[0]->{surname}, 'Ruth', 'Got the expected surname error from import patrons for branch tests'); |
222 |
is($result_5->{errors}->[1]->{missing_criticals}->[0]->{value}, 'ZZZ', 'Got the expected ZZZ value error from import patrons for branch tests'); |
223 |
|
224 |
is($result_5->{feedback}->[0]->{feedback}, 1, 'Got the expected 1 feedback from import_patrons for branch tests'); |
225 |
is($result_5->{feedback}->[0]->{name}, 'headerrow', 'Got the expected header row name from import_patrons for branch tests'); |
226 |
is($result_5->{feedback}->[0]->{value}, $res_header, 'Got the expected header row value from import_patrons for branch tests'); |
227 |
|
228 |
is($result_5->{feedback}->[1]->{feedback}, 1, 'Got the expected 1 feedback from import_patrons for branch tests'); |
229 |
is($result_5->{feedback}->[1]->{name}, 'lastimported', 'Got the expected lastimported name from import_patrons for branch tests'); |
230 |
like($result_5->{feedback}->[1]->{value}, qr/^Linda \/ \d+/, 'Got the expected last imported value from import_patrons with for branch tests'); |
231 |
|
232 |
is($result_5->{imported}, 1, 'Got the expected 1 imported result from import patrons for branch tests'); |
233 |
is($result_5->{invalid}, 2, 'Got the expected 2 invalid result from import patrons for branch tests'); |
234 |
is($result_5->{overwritten}, 0, 'Got the expected 0 overwritten result from import patrons for branch tests'); |
235 |
|
236 |
# Given ... 3 new inputs. One with no category code, one with unexpected category code. |
237 |
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'; |
238 |
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'; |
239 |
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'; |
240 |
|
241 |
my $filename_6 = make_csv($temp_dir, $csv_headers, $input_no_category, $input_good_category, $input_na_category); |
242 |
open(my $handle_6, "<", $filename_6) or die "cannot open < $filename_6: $!"; |
243 |
my $params_6 = { file => $handle_6, matchpoint => 'cardnumber', }; |
244 |
|
245 |
# When ... |
246 |
my $result_6 = $patrons_import->import_patrons($params_6); |
247 |
|
248 |
# Then ... |
249 |
is($result_6->{already_in_db}, 0, 'Got the expected 0 already_in_db from import_patrons for category tests'); |
250 |
|
251 |
is($result_6->{errors}->[0]->{missing_criticals}->[0]->{borrowernumber}, 'UNDEF', 'Got the expected undef borrower number error from import patrons for category tests'); |
252 |
is($result_6->{errors}->[0]->{missing_criticals}->[0]->{key}, 'categorycode', 'Got the expected category code key from import patrons for category tests'); |
253 |
is($result_6->{errors}->[0]->{missing_criticals}->[0]->{line}, 2, 'Got the expected 2 line number error from import patrons for category tests'); |
254 |
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'); |
255 |
is($result_6->{errors}->[0]->{missing_criticals}->[0]->{surname}, 'Christina', 'Got the expected surname error from import patrons for category tests'); |
256 |
|
257 |
is($result_6->{errors}->[1]->{missing_criticals}->[0]->{borrowernumber}, 'UNDEF', 'Got the expected undef borrower number error from import patrons for category tests'); |
258 |
is($result_6->{errors}->[1]->{missing_criticals}->[0]->{category_map}, 1, 'Got the expected 1 category_map error from import patrons for category tests'); |
259 |
is($result_6->{errors}->[1]->{missing_criticals}->[0]->{key}, 'categorycode', 'Got the expected category code key from import patrons for category tests'); |
260 |
is($result_6->{errors}->[1]->{missing_criticals}->[0]->{line}, 4, 'Got the expected 4 line number error from import patrons for category tests'); |
261 |
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'); |
262 |
is($result_6->{errors}->[1]->{missing_criticals}->[0]->{surname}, 'Emily', 'Got the expected surname error from import patrons for category tests'); |
263 |
is($result_6->{errors}->[1]->{missing_criticals}->[0]->{value}, 'ZZ', 'Got the expected ZZ value error from import patrons for category tests'); |
264 |
|
265 |
is($result_6->{feedback}->[0]->{feedback}, 1, 'Got the expected 1 feedback from import_patrons for category tests'); |
266 |
is($result_6->{feedback}->[0]->{name}, 'headerrow', 'Got the expected header row name from import_patrons for category tests'); |
267 |
is($result_6->{feedback}->[0]->{value}, $res_header, 'Got the expected header row value from import_patrons for category tests'); |
268 |
|
269 |
is($result_6->{feedback}->[1]->{feedback}, 1, 'Got the expected 1 feedback from import_patrons for category tests'); |
270 |
is($result_6->{feedback}->[1]->{name}, 'lastimported', 'Got the expected lastimported name from import_patrons for category tests'); |
271 |
like($result_6->{feedback}->[1]->{value}, qr/^Peter \/ \d+/, 'Got the expected last imported value from import_patrons with for category tests'); |
272 |
|
273 |
is($result_6->{imported}, 1, 'Got the expected 1 imported result from import patrons for category tests'); |
274 |
is($result_6->{invalid}, 2, 'Got the expected 2 invalid result from import patrons for category tests'); |
275 |
is($result_6->{overwritten}, 0, 'Got the expected 0 overwritten result from import patrons for category tests'); |
276 |
|
277 |
# Given ... 2 new inputs. One without dateofbirth, dateenrolled and dateexpiry values. |
278 |
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'; |
279 |
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'; |
280 |
|
281 |
my $filename_7 = make_csv($temp_dir, $csv_headers, $input_complete, $input_no_date); |
282 |
open(my $handle_7, "<", $filename_7) or die "cannot open < $filename_7: $!"; |
283 |
my $params_7 = { file => $handle_7, matchpoint => 'cardnumber', }; |
284 |
|
285 |
# When ... |
286 |
my $result_7 = $patrons_import->import_patrons($params_7); |
287 |
|
288 |
# Then ... |
289 |
is($result_7->{already_in_db}, 0, 'Got the expected 0 already_in_db from import_patrons for dates tests'); |
290 |
is(scalar @{$result_7->{errors}}, 1, 'Got the expected 1 error array size from import_patrons for dates tests'); |
291 |
is(scalar @{$result_7->{errors}->[0]->{missing_criticals}}, 3, 'Got the expected 3 missing critical errors from import_patrons for dates tests'); |
292 |
|
293 |
is($result_7->{errors}->[0]->{missing_criticals}->[0]->{bad_date}, 1, 'Got the expected 1 bad_date error from import patrons for dates tests'); |
294 |
is($result_7->{errors}->[0]->{missing_criticals}->[0]->{borrowernumber}, 'UNDEF', 'Got the expected undef borrower number error from import patrons for dates tests'); |
295 |
is($result_7->{errors}->[0]->{missing_criticals}->[0]->{key}, 'dateofbirth', 'Got the expected dateofbirth key from import patrons for dates tests'); |
296 |
is($result_7->{errors}->[0]->{missing_criticals}->[0]->{line}, 3, 'Got the expected 2 line number error from import patrons for dates tests'); |
297 |
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'); |
298 |
is($result_7->{errors}->[0]->{missing_criticals}->[0]->{surname}, 'Ralph', 'Got the expected surname error from import patrons for dates tests'); |
299 |
|
300 |
is($result_7->{errors}->[0]->{missing_criticals}->[1]->{key}, 'dateenrolled', 'Got the expected dateenrolled key from import patrons for dates tests'); |
301 |
is($result_7->{errors}->[0]->{missing_criticals}->[2]->{key}, 'dateexpiry', 'Got the expected dateexpiry key from import patrons for dates tests'); |
302 |
|
303 |
is(scalar @{$result_7->{feedback}}, 2, 'Got the expected 2 feedback from import patrons for dates tests'); |
304 |
is($result_7->{feedback}->[0]->{feedback}, 1, 'Got the expected 1 feedback from import_patrons for dates tests'); |
305 |
is($result_7->{feedback}->[0]->{name}, 'headerrow', 'Got the expected header row name from import_patrons for dates tests'); |
306 |
is($result_7->{feedback}->[0]->{value}, $res_header, 'Got the expected header row value from import_patrons for dates tests'); |
307 |
|
308 |
is($result_7->{feedback}->[1]->{feedback}, 1, 'Got the expected 1 feedback from import_patrons for dates tests'); |
309 |
is($result_7->{feedback}->[1]->{name}, 'lastimported', 'Got the expected lastimported from import_patrons for dates tests'); |
310 |
like($result_7->{feedback}->[1]->{value}, qr/^Christina \/ \d+/, 'Got the expected lastimported value from import_patrons for dates tests'); |
311 |
|
312 |
is($result_7->{imported}, 1, 'Got the expected 1 imported result from import patrons for dates tests'); |
313 |
is($result_7->{invalid}, 1, 'Got the expected 1 invalid result from import patrons for dates tests'); |
314 |
is($result_7->{overwritten}, 0, 'Got the expected 0 overwritten result from import patrons for dates tests'); |
315 |
|
316 |
subtest 'test_prepare_columns' => sub { |
317 |
plan tests => 16; |
318 |
|
319 |
# Given ... no header row |
320 |
my $headerrow_0; |
321 |
my %csvkeycol_0; |
322 |
my @errors_0; |
323 |
|
324 |
# When ... |
325 |
my @csvcolumns_0 = $patrons_import->prepare_columns({headerrow => undef, keycol => \%csvkeycol_0, errors => \@errors_0, }); |
326 |
|
327 |
# Then ... |
328 |
is(scalar @csvcolumns_0, 0, 'Got the expected empty column array from prepare columns with no header row'); |
329 |
|
330 |
is(scalar @errors_0, 1, 'Got the expected 1 entry in error array from prepare columns with no header row'); |
331 |
is($errors_0[0]->{badheader}, 1, 'Got the expected 1 badheader from prepare columns with no header row'); |
332 |
is($errors_0[0]->{line}, 1, 'Got the expected 1 line from prepare columns with no header row'); |
333 |
is($errors_0[0]->{lineraw}, undef, 'Got the expected undef lineraw from prepare columns with no header row'); |
334 |
|
335 |
# Given ... a good header row with plenty of whitespaces |
336 |
my $headerrow_1 = 'a, b , c, , d'; |
337 |
my %csvkeycol_1; |
338 |
my @errors_1; |
339 |
|
340 |
# When ... |
341 |
my @csvcolumns_1 = $patrons_import->prepare_columns({headerrow => $headerrow_1, keycol => \%csvkeycol_1, errors => \@errors_1, }); |
342 |
|
343 |
# Then ... |
344 |
is(scalar @csvcolumns_1, 5, 'Got the expected 5 column array from prepare columns'); |
345 |
is($csvcolumns_1[0], 'a', 'Got the expected a header from prepare columns'); |
346 |
is($csvcolumns_1[1], 'b', 'Got the expected b header from prepare columns'); |
347 |
is($csvcolumns_1[2], 'c', 'Got the expected c header from prepare columns'); |
348 |
is($csvcolumns_1[3], '', 'Got the expected empty header from prepare columns'); |
349 |
is($csvcolumns_1[4], 'd', 'Got the expected d header from prepare columns'); |
350 |
|
351 |
is($csvkeycol_1{a}, 0, 'Got the expected 0 value for key a from prepare columns hash'); |
352 |
is($csvkeycol_1{b}, 1, 'Got the expected 1 value for key b from prepare columns hash'); |
353 |
is($csvkeycol_1{c}, 2, 'Got the expected 2 value for key c from prepare columns hash'); |
354 |
is($csvkeycol_1{''}, 3, 'Got the expected 3 value for empty string key from prepare columns hash'); |
355 |
is($csvkeycol_1{d}, 4, 'Got the expected 4 value for key d from prepare columns hash'); |
356 |
}; |
357 |
|
358 |
subtest 'test_set_column_keys' => sub { |
359 |
plan tests => 5; |
360 |
|
361 |
# Given ... nothing at all |
362 |
# When ... Then ... |
363 |
my $attr_type_0 = $patrons_import->set_attribute_types(undef); |
364 |
is($attr_type_0, undef, 'Got the expected undef attribute type from set attribute types with nothing'); |
365 |
|
366 |
# Given ... extended but not matchpoint |
367 |
my $params_1 = { extended => 1, matchpoint => undef, }; |
368 |
|
369 |
# When ... Then ... |
370 |
my $attr_type_1 = $patrons_import->set_attribute_types($params_1); |
371 |
is($attr_type_1, undef, 'Got the expected undef attribute type from set attribute types with no matchpoint'); |
372 |
|
373 |
# Given ... extended and unexpected matchpoint |
374 |
my $params_2 = { extended => 1, matchpoint => 'unexpected', }; |
375 |
|
376 |
# When ... Then ... |
377 |
my $attr_type_2 = $patrons_import->set_attribute_types($params_2); |
378 |
is($attr_type_2, undef, 'Got the expected undef attribute type from set attribute types with unexpected matchpoint'); |
379 |
|
380 |
# Given ... |
381 |
my $code_3 = 'SHOW_BCODE'; |
382 |
my $params_3 = { extended => 1, matchpoint => $code_3, }; |
383 |
|
384 |
# When ... |
385 |
my $attr_type_3 = $patrons_import->set_attribute_types($params_3); |
386 |
|
387 |
# Then ... |
388 |
isa_ok($attr_type_3, 'C4::Members::AttributeTypes'); |
389 |
is($attr_type_3->{code}, $code_3, 'Got the expected code attribute type from set attribute types'); |
390 |
}; |
391 |
|
392 |
subtest 'test_set_column_keys' => sub { |
393 |
plan tests => 2; |
394 |
|
395 |
# Given ... nothing at all |
396 |
# When ... Then ... |
397 |
my @columnkeys_0 = $patrons_import->set_column_keys(undef); |
398 |
is(scalar @columnkeys_0, 66, 'Got the expected array size from set column keys with undef extended'); |
399 |
|
400 |
# Given ... extended. |
401 |
my $extended = 1; |
402 |
|
403 |
# When ... Then ... |
404 |
my @columnkeys_1 = $patrons_import->set_column_keys($extended); |
405 |
is(scalar @columnkeys_1, 67, 'Got the expected array size from set column keys with extended'); |
406 |
}; |
407 |
|
408 |
subtest 'test_set_patron_attributes' => sub { |
409 |
plan tests => 13; |
410 |
|
411 |
# Given ... nothing at all |
412 |
# When ... Then ... |
413 |
my $result_0 = $patrons_import->set_patron_attributes(undef, undef, undef); |
414 |
is($result_0, undef, 'Got the expected undef from set patron attributes with nothing'); |
415 |
|
416 |
# Given ... not extended. |
417 |
my $extended_1 = 0; |
418 |
|
419 |
# When ... Then ... |
420 |
my $result_1 = $patrons_import->set_patron_attributes($extended_1, undef, undef); |
421 |
is($result_1, undef, 'Got the expected undef from set patron attributes with not extended'); |
422 |
|
423 |
# Given ... NO patrons attributes |
424 |
my $extended_2 = 1; |
425 |
my $patron_attributes_2 = undef; |
426 |
my @feedback_2; |
427 |
|
428 |
# When ... |
429 |
my $result_2 = $patrons_import->set_patron_attributes($extended_2, $patron_attributes_2, \@feedback_2); |
430 |
|
431 |
# Then ... |
432 |
is($result_2, undef, 'Got the expected undef from set patron attributes with no patrons attributes'); |
433 |
is(scalar @feedback_2, 0, 'Got the expected 0 size feedback array from set patron attributes with no patrons attributes'); |
434 |
|
435 |
# Given ... some patrons attributes |
436 |
my $patron_attributes_3 = "homeroom:1150605,grade:01"; |
437 |
my @feedback_3; |
438 |
|
439 |
# When ... |
440 |
my $result_3 = $patrons_import->set_patron_attributes($extended_2, $patron_attributes_3, \@feedback_3); |
441 |
|
442 |
# Then ... |
443 |
ok($result_3, 'Got some data back from set patron attributes'); |
444 |
is($result_3->[0]->{code}, 'grade', 'Got the expected first code from set patron attributes'); |
445 |
is($result_3->[0]->{value}, '01', 'Got the expected first value from set patron attributes'); |
446 |
|
447 |
is($result_3->[1]->{code}, 'homeroom', 'Got the expected second code from set patron attributes'); |
448 |
is($result_3->[1]->{value}, 1150605, 'Got the expected second value from set patron attributes'); |
449 |
|
450 |
is(scalar @feedback_3, 1, 'Got the expected 1 array size from set patron attributes with extended user'); |
451 |
is($feedback_3[0]->{feedback}, 1, 'Got the expected second feedback from set patron attributes with extended user'); |
452 |
is($feedback_3[0]->{name}, 'attribute string', 'Got the expected attribute string from set patron attributes with extended user'); |
453 |
is($feedback_3[0]->{value}, 'homeroom:1150605,grade:01', 'Got the expected feedback value from set patron attributes with extended user'); |
454 |
}; |
455 |
|
456 |
subtest 'test_check_branch_code' => sub { |
457 |
plan tests => 11; |
458 |
|
459 |
# Given ... no branch code. |
460 |
my $borrowerline = 'some, line'; |
461 |
my $line_number = 78; |
462 |
my @missing_criticals = (); |
463 |
|
464 |
# When ... |
465 |
$patrons_import->check_branch_code(undef, $borrowerline, $line_number, \@missing_criticals); |
466 |
|
467 |
# Then ... |
468 |
is(scalar @missing_criticals, 1, 'Got the expected missing critical array size of 1 from check_branch_code with no branch code'); |
469 |
|
470 |
is($missing_criticals[0]->{key}, 'branchcode', 'Got the expected branchcode key from check_branch_code with no branch code'); |
471 |
is($missing_criticals[0]->{line}, $line_number, 'Got the expected line number from check_branch_code with no branch code'); |
472 |
is($missing_criticals[0]->{lineraw}, $borrowerline, 'Got the expected lineraw value from check_branch_code with no branch code'); |
473 |
|
474 |
# Given ... unknown branch code |
475 |
my $branchcode_1 = 'unexpected'; |
476 |
my $borrowerline_1 = 'some, line,'.$branchcode_1; |
477 |
my $line_number_1 = 79; |
478 |
my @missing_criticals_1 = (); |
479 |
|
480 |
# When ... |
481 |
$patrons_import->check_branch_code($branchcode_1, $borrowerline_1, $line_number_1, \@missing_criticals_1); |
482 |
|
483 |
# Then ... |
484 |
is(scalar @missing_criticals_1, 1, 'Got the expected missing critical array size of 1 from check_branch_code with unexpected branch code'); |
485 |
|
486 |
is($missing_criticals_1[0]->{branch_map}, 1, 'Got the expected 1 branch_map from check_branch_code with unexpected branch code'); |
487 |
is($missing_criticals_1[0]->{key}, 'branchcode', 'Got the expected branchcode key from check_branch_code with unexpected branch code'); |
488 |
is($missing_criticals_1[0]->{line}, $line_number_1, 'Got the expected line number from check_branch_code with unexpected branch code'); |
489 |
is($missing_criticals_1[0]->{lineraw}, $borrowerline_1, 'Got the expected lineraw value from check_branch_code with unexpected branch code'); |
490 |
is($missing_criticals_1[0]->{value}, $branchcode_1, 'Got the expected value from check_branch_code with unexpected branch code'); |
491 |
|
492 |
# Given ... a known branch code. Relies on database sample data |
493 |
my $branchcode_2 = 'FFL'; |
494 |
my $borrowerline_2 = 'some, line,'.$branchcode_2; |
495 |
my $line_number_2 = 80; |
496 |
my @missing_criticals_2 = (); |
497 |
|
498 |
# When ... |
499 |
$patrons_import->check_branch_code($branchcode_2, $borrowerline_2, $line_number_2, \@missing_criticals_2); |
500 |
|
501 |
# Then ... |
502 |
is(scalar @missing_criticals_2, 0, 'Got the expected missing critical array size of 0 from check_branch_code'); |
503 |
}; |
504 |
|
505 |
subtest 'test_check_borrower_category' => sub { |
506 |
plan tests => 11; |
507 |
|
508 |
# Given ... no category code. |
509 |
my $borrowerline = 'some, line'; |
510 |
my $line_number = 781; |
511 |
my @missing_criticals = (); |
512 |
|
513 |
# When ... |
514 |
$patrons_import->check_borrower_category(undef, $borrowerline, $line_number, \@missing_criticals); |
515 |
|
516 |
# Then ... |
517 |
is(scalar @missing_criticals, 1, 'Got the expected missing critical array size of 1 from check_branch_code with no category code'); |
518 |
|
519 |
is($missing_criticals[0]->{key}, 'categorycode', 'Got the expected categorycode key from check_branch_code with no category code'); |
520 |
is($missing_criticals[0]->{line}, $line_number, 'Got the expected line number from check_branch_code with no category code'); |
521 |
is($missing_criticals[0]->{lineraw}, $borrowerline, 'Got the expected lineraw value from check_branch_code with no category code'); |
522 |
|
523 |
# Given ... unknown category code |
524 |
my $categorycode_1 = 'unexpected'; |
525 |
my $borrowerline_1 = 'some, line, line, '.$categorycode_1; |
526 |
my $line_number_1 = 791; |
527 |
my @missing_criticals_1 = (); |
528 |
|
529 |
# When ... |
530 |
$patrons_import->check_borrower_category($categorycode_1, $borrowerline_1, $line_number_1, \@missing_criticals_1); |
531 |
|
532 |
# Then ... |
533 |
is(scalar @missing_criticals_1, 1, 'Got the expected missing critical array size of 1 from check_branch_code with unexpected category code'); |
534 |
|
535 |
is($missing_criticals_1[0]->{category_map}, 1, 'Got the expected 1 category_map from check_branch_code with unexpected category code'); |
536 |
is($missing_criticals_1[0]->{key}, 'categorycode', 'Got the expected branchcode key from check_branch_code with unexpected category code'); |
537 |
is($missing_criticals_1[0]->{line}, $line_number_1, 'Got the expected line number from check_branch_code with unexpected category code'); |
538 |
is($missing_criticals_1[0]->{lineraw}, $borrowerline_1, 'Got the expected lineraw value from check_branch_code with unexpected category code'); |
539 |
is($missing_criticals_1[0]->{value}, $categorycode_1, 'Got the expected value from check_branch_code with unexpected category code'); |
540 |
|
541 |
# Given ... a known category code. Relies on database sample data. |
542 |
my $categorycode_2 = 'T'; |
543 |
my $borrowerline_2 = 'some, line,'.$categorycode_2; |
544 |
my $line_number_2 = 801; |
545 |
my @missing_criticals_2 = (); |
546 |
|
547 |
# When ... |
548 |
$patrons_import->check_borrower_category($categorycode_2, $borrowerline_2, $line_number_2, \@missing_criticals_2); |
549 |
|
550 |
# Then ... |
551 |
is(scalar @missing_criticals_2, 0, 'Got the expected missing critical array size of 0 from check_branch_code'); |
552 |
}; |
553 |
|
554 |
subtest 'test_format_dates' => sub { |
555 |
plan tests => 22; |
556 |
|
557 |
# Given ... no borrower data. |
558 |
my $borrowerline = 'another line'; |
559 |
my $line_number = 987; |
560 |
my @missing_criticals = (); |
561 |
my %borrower; |
562 |
my $params = {borrower => \%borrower, lineraw => $borrowerline, line => $line_number, missing_criticals => \@missing_criticals, }; |
563 |
|
564 |
# When ... |
565 |
$patrons_import->format_dates($params); |
566 |
|
567 |
# Then ... |
568 |
ok( not(%borrower), 'Got the expected no borrower from format_dates with no dates'); |
569 |
is(scalar @missing_criticals, 0, 'Got the expected missing critical array size of 0 from format_dates with no dates'); |
570 |
|
571 |
# Given ... some good dates |
572 |
my @missing_criticals_1 = (); |
573 |
my $dateofbirth_1 = '2016-05-03'; |
574 |
my $dateenrolled_1 = '2016-05-04'; |
575 |
my $dateexpiry_1 = '2016-05-06'; |
576 |
my $borrower_1 = { dateofbirth => $dateofbirth_1, dateenrolled => $dateenrolled_1, dateexpiry => $dateexpiry_1, }; |
577 |
my $params_1 = {borrower => $borrower_1, lineraw => $borrowerline, line => $line_number, missing_criticals => \@missing_criticals_1, }; |
578 |
|
579 |
# When ... |
580 |
$patrons_import->format_dates($params_1); |
581 |
|
582 |
# Then ... |
583 |
is($borrower_1->{dateofbirth}, $dateofbirth_1, 'Got the expected date of birth from format_dates with good dates'); |
584 |
is($borrower_1->{dateenrolled}, $dateenrolled_1, 'Got the expected date of birth from format_dates with good dates'); |
585 |
is($borrower_1->{dateexpiry}, $dateexpiry_1, 'Got the expected date of birth from format_dates with good dates'); |
586 |
is(scalar @missing_criticals_1, 0, 'Got the expected missing critical array size of 0 from check_branch_code with good dates'); |
587 |
|
588 |
# Given ... some very bad dates |
589 |
my @missing_criticals_2 = (); |
590 |
my $dateofbirth_2 = '03-2016-05'; |
591 |
my $dateenrolled_2 = '04-2016-05'; |
592 |
my $dateexpiry_2 = '06-2016-05'; |
593 |
my $borrower_2 = { dateofbirth => $dateofbirth_2, dateenrolled => $dateenrolled_2, dateexpiry => $dateexpiry_2, }; |
594 |
my $params_2 = {borrower => $borrower_2, lineraw => $borrowerline, line => $line_number, missing_criticals => \@missing_criticals_2, }; |
595 |
|
596 |
# When ... |
597 |
$patrons_import->format_dates($params_2); |
598 |
|
599 |
# Then ... |
600 |
is($borrower_2->{dateofbirth}, '', 'Got the expected empty date of birth from format_dates with bad dates'); |
601 |
is($borrower_2->{dateenrolled}, '', 'Got the expected emptydate of birth from format_dates with bad dates'); |
602 |
is($borrower_2->{dateexpiry}, '', 'Got the expected empty date of birth from format_dates with bad dates'); |
603 |
|
604 |
is(scalar @missing_criticals_2, 3, 'Got the expected missing critical array size of 3 from check_branch_code with bad dates'); |
605 |
is($missing_criticals_2[0]->{bad_date}, 1, 'Got the expected first bad date flag from check_branch_code with bad dates'); |
606 |
is($missing_criticals_2[0]->{key}, 'dateofbirth', 'Got the expected dateofbirth key from check_branch_code with bad dates'); |
607 |
is($missing_criticals_2[0]->{line}, $line_number, 'Got the expected first line from check_branch_code with bad dates'); |
608 |
is($missing_criticals_2[0]->{lineraw}, $borrowerline, 'Got the expected first lineraw from check_branch_code with bad dates'); |
609 |
|
610 |
is($missing_criticals_2[1]->{bad_date}, 1, 'Got the expected second bad date flag from check_branch_code with bad dates'); |
611 |
is($missing_criticals_2[1]->{key}, 'dateenrolled', 'Got the expected dateenrolled key from check_branch_code with bad dates'); |
612 |
is($missing_criticals_2[1]->{line}, $line_number, 'Got the expected second line from check_branch_code with bad dates'); |
613 |
is($missing_criticals_2[1]->{lineraw}, $borrowerline, 'Got the expected second lineraw from check_branch_code with bad dates'); |
614 |
|
615 |
is($missing_criticals_2[2]->{bad_date}, 1, 'Got the expected third bad date flag from check_branch_code with bad dates'); |
616 |
is($missing_criticals_2[2]->{key}, 'dateexpiry', 'Got the expected dateexpiry key from check_branch_code with bad dates'); |
617 |
is($missing_criticals_2[2]->{line}, $line_number, 'Got the expected third line from check_branch_code with bad dates'); |
618 |
is($missing_criticals_2[2]->{lineraw}, $borrowerline, 'Got the expected third lineraw from check_branch_code with bad dates'); |
619 |
}; |
620 |
|
621 |
# ###### Test utility ########### |
622 |
sub make_csv { |
623 |
my ($temp_dir, @lines) = @_; |
624 |
|
625 |
my ($fh, $filename) = tempfile( DIR => $temp_dir) or die $!; |
626 |
print $fh $_."\r\n" foreach @lines; |
627 |
close $fh or die $!; |
628 |
|
629 |
return $filename; |
630 |
} |
631 |
|
632 |
1; |