Bug 20443: Fix Patrons/Import.t
[srvgit] / t / db_dependent / Koha / Patrons / Import.t
index 5d33bd3..784d929 100644 (file)
 # along with Koha; if not, see <http://www.gnu.org/licenses>.
 
 use Modern::Perl;
-use Test::More tests => 155;
+use Test::More tests => 158;
 use Test::Warn;
+use Encode qw( encode_utf8 );
+use utf8;
 
 # To be replaced by t::lib::Mock
 use Test::MockModule;
@@ -49,7 +51,7 @@ subtest 'test_methods' => sub {
                    'set_attribute_types',
                    'prepare_columns',
                    'set_column_keys',
-                   'set_patron_attributes',
+                   'generate_patron_attributes',
                    'check_branch_code',
                    'format_dates',
                   );
@@ -163,7 +165,10 @@ open(my $handle_3a, "<", $filename_3a) or die "cannot open < $filename_3: $!";
 my $params_3a = { file => $handle_3a, matchpoint => 'cardnumber', overwrite_cardnumber => 1};
 
 # When ...
-my $result_3a = $patrons_import->import_patrons($params_3a);
+my $result_3a;
+warning_is { $result_3a = $patrons_import->import_patrons($params_3a) }
+           undef,
+           "No warning raised by import_patrons";
 
 # Then ...
 is($result_3a->{already_in_db}, 0, 'Got the expected 0 already_in_db from import_patrons when matched and overwrite set');
@@ -255,12 +260,13 @@ is($result_4a->{overwritten}, 0, 'Got the expected 0 overwritten result from imp
 
 t::lib::Mocks::mock_preference('ExtendedPatronAttributes', 0);
 
+my $surname ='Chloé❤';
 # Given ... 3 new inputs. One with no branch code, one with unexpected branch code.
-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';
-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';
-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';
+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|;
+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|;
+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|;
 
-my $filename_5 = make_csv($temp_dir, $csv_headers, $input_no_branch, $input_good_branch, $input_na_branch);
+my $filename_5 = make_csv($temp_dir, $csv_headers, encode_utf8($input_no_branch), encode_utf8($input_good_branch), encode_utf8($input_na_branch));
 open(my $handle_5, "<", $filename_5) or die "cannot open < $filename_5: $!";
 my $params_5 = { file => $handle_5, matchpoint => 'cardnumber', };
 
@@ -274,14 +280,14 @@ is($result_5->{errors}->[0]->{missing_criticals}->[0]->{borrowernumber}, 'UNDEF'
 is($result_5->{errors}->[0]->{missing_criticals}->[0]->{key}, 'branchcode', 'Got the expected branch code key from import patrons for branch tests');
 is($result_5->{errors}->[0]->{missing_criticals}->[0]->{line}, 2, 'Got the expected 2 line number error from import patrons for branch tests');
 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');
-is($result_5->{errors}->[0]->{missing_criticals}->[0]->{surname}, 'Johnny', 'Got the expected surname error from import patrons for branch tests');
+is($result_5->{errors}->[0]->{missing_criticals}->[0]->{surname}, $surname, 'Got the expected surname error from import patrons for branch tests');
 
 is($result_5->{errors}->[1]->{missing_criticals}->[0]->{borrowernumber}, 'UNDEF', 'Got the expected undef borrower number error from import patrons for branch tests');
 is($result_5->{errors}->[1]->{missing_criticals}->[0]->{branch_map}, 1, 'Got the expected 1 branchmap error from import patrons for branch tests');
 is($result_5->{errors}->[1]->{missing_criticals}->[0]->{key}, 'branchcode', 'Got the expected branch code key from import patrons for branch tests');
 is($result_5->{errors}->[1]->{missing_criticals}->[0]->{line}, 4, 'Got the expected 4 line number error from import patrons for branch tests');
 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');
-is($result_5->{errors}->[1]->{missing_criticals}->[0]->{surname}, 'Ruth', 'Got the expected surname error from import patrons for branch tests');
+is($result_5->{errors}->[1]->{missing_criticals}->[0]->{surname}, $surname, 'Got the expected surname error from import patrons for branch tests');
 is($result_5->{errors}->[1]->{missing_criticals}->[0]->{value}, 'ZZZ', 'Got the expected ZZZ value error from import patrons for branch tests');
 
 is($result_5->{feedback}->[0]->{feedback}, 1, 'Got the expected 1 feedback from import_patrons for branch tests');
@@ -290,7 +296,7 @@ is($result_5->{feedback}->[0]->{value}, $res_header, 'Got the expected header ro
 
 is($result_5->{feedback}->[1]->{feedback}, 1, 'Got the expected 1 feedback from import_patrons for branch tests');
 is($result_5->{feedback}->[1]->{name}, 'lastimported', 'Got the expected lastimported name from import_patrons for branch tests');
-like($result_5->{feedback}->[1]->{value},  qr/^Linda \/ \d+/, 'Got the expected last imported value from import_patrons with for branch tests');
+like($result_5->{feedback}->[1]->{value},  qr/^$surname \/ \d+/, 'Got the expected last imported value from import_patrons with for branch tests');
 
 is($result_5->{imported}, 1, 'Got the expected 1 imported result from import patrons for branch tests');
 is($result_5->{invalid}, 2, 'Got the expected 2 invalid result from import patrons for branch tests');
@@ -376,6 +382,59 @@ is($result_7->{imported}, 1, 'Got the expected 1 imported result from import pat
 is($result_7->{invalid}, 1, 'Got the expected 1 invalid result from import patrons for dates tests');
 is($result_7->{overwritten}, 0, 'Got the expected 0 overwritten result from import patrons for dates tests');
 
+subtest 'test_import_without_cardnumber' => sub {
+    plan tests => 2;
+
+    #Remove possible existing user with a "" as cardnumber
+    my $blank_card = Koha::Patrons->find({ cardnumber => '' });
+    $blank_card->delete if $blank_card;
+
+    my $branchcode = $builder->build({ source => "Branch"})->{branchcode};
+    my $categorycode = $builder->build({ source => "Category"})->{categorycode};
+    my $csv_headers  = 'surname, branchcode, categorycode';
+    my $res_headers  = 'surname, branchcode, categorycode';
+    my $csv_nocard_1 = "Squarepants,$branchcode,$categorycode";
+    my $csv_nocard_2 = "Star,$branchcode,$categorycode";
+
+    my $filename_1 = make_csv($temp_dir, $csv_headers, $csv_nocard_1, $csv_nocard_2);
+    open(my $handle_1, "<", $filename_1) or die "cannot open < $filename_1: $!";
+    my $params_1 = { file => $handle_1, };
+
+    my $defaults = { cardnumber => "" }; #currently all the defaults come as "" if not filled
+
+    my $result = $patrons_import->import_patrons($params_1, $defaults);
+    like($result->{feedback}->[1]->{value}, qr/^Squarepants \/ \d+/, 'First borrower imported as expected');
+    like($result->{feedback}->[2]->{value}, qr/^Star \/ \d+/, 'Second borrower imported as expected');
+
+};
+
+subtest 'test_import_with_cardnumber_0' => sub {
+    plan tests => 2;
+
+    #Remove possible existing user with a "" as cardnumber
+    my $zero_card = Koha::Patrons->find({ cardnumber => 0 });
+    $zero_card->delete if $zero_card;
+
+    my $branchcode = $builder->build({ source => "Branch"})->{branchcode};
+    my $categorycode = $builder->build({ source => "Category"})->{categorycode};
+    my $csv_headers  = 'cardnumber,surname, branchcode, categorycode';
+    my $res_headers  = 'cardnumber,surname, branchcode, categorycode';
+    my $csv_nocard_1 = "0,Squarepants,$branchcode,$categorycode";
+
+    my $filename_1 = make_csv($temp_dir, $csv_headers, $csv_nocard_1);
+    open(my $handle_1, "<", $filename_1) or die "cannot open < $filename_1: $!";
+    my $params_1 = { file => $handle_1, };
+
+    my $defaults = { cardnumber => "" }; #currently all the defaults come as "" if not filled
+
+    my $result = $patrons_import->import_patrons($params_1, $defaults);
+    like($result->{feedback}->[1]->{value}, qr/^Squarepants \/ \d+/, 'First borrower imported as expected');
+    $zero_card = Koha::Patrons->find({ cardnumber => 0 });
+    is($zero_card->surname.$zero_card->branchcode.$zero_card->categorycode,'Squarepants'.$branchcode.$categorycode,"Patron with cardnumber 0 is the imported patron");
+
+};
+
+
 subtest 'test_prepare_columns' => sub {
     plan tests => 16;
 
@@ -447,8 +506,8 @@ subtest 'test_set_column_keys' => sub {
     my $attr_type_3 = $patrons_import->set_attribute_types($params_3);
 
     # Then ...
-    isa_ok($attr_type_3, 'C4::Members::AttributeTypes');
-    is($attr_type_3->{code}, $code_3, 'Got the expected code attribute type from set attribute types');
+    isa_ok($attr_type_3, 'Koha::Patron::Attribute::Type');
+    is($attr_type_3->code, $code_3, 'Got the expected code attribute type from set attribute types');
 };
 
 subtest 'test_set_column_keys' => sub {
@@ -469,19 +528,19 @@ subtest 'test_set_column_keys' => sub {
     is(scalar @columnkeys_1, @columns - 1 + $extended, 'Got the expected array size from set column keys with extended');
 };
 
-subtest 'test_set_patron_attributes' => sub {
+subtest 'test_generate_patron_attributes' => sub {
     plan tests => 13;
 
     # Given ... nothing at all
     # When ... Then ...
-    my $result_0 = $patrons_import->set_patron_attributes(undef, undef, undef);
+    my $result_0 = $patrons_import->generate_patron_attributes(undef, undef, undef);
     is($result_0, undef, 'Got the expected undef from set patron attributes with nothing');
 
     # Given ... not extended.
     my $extended_1 = 0;
 
     # When ... Then ...
-    my $result_1 = $patrons_import->set_patron_attributes($extended_1, undef, undef);
+    my $result_1 = $patrons_import->generate_patron_attributes($extended_1, undef, undef);
     is($result_1, undef, 'Got the expected undef from set patron attributes with not extended');
 
     # Given ... NO patrons attributes
@@ -490,7 +549,7 @@ subtest 'test_set_patron_attributes' => sub {
     my @feedback_2;
 
     # When ...
-    my $result_2 = $patrons_import->set_patron_attributes($extended_2, $patron_attributes_2, \@feedback_2);
+    my $result_2 = $patrons_import->generate_patron_attributes($extended_2, $patron_attributes_2, \@feedback_2);
 
     # Then ...
     is($result_2, undef, 'Got the expected undef from set patron attributes with no patrons attributes');
@@ -501,15 +560,15 @@ subtest 'test_set_patron_attributes' => sub {
     my @feedback_3;
 
     # When ...
-    my $result_3 = $patrons_import->set_patron_attributes($extended_2, $patron_attributes_3, \@feedback_3);
+    my $result_3 = $patrons_import->generate_patron_attributes($extended_2, $patron_attributes_3, \@feedback_3);
 
     # Then ...
     ok($result_3, 'Got some data back from set patron attributes');
     is($result_3->[0]->{code}, 'grade', 'Got the expected first code from set patron attributes');
-    is($result_3->[0]->{value}, '01', 'Got the expected first value from set patron attributes');
+    is($result_3->[0]->{attribute}, '01', 'Got the expected first value from set patron attributes');
 
     is($result_3->[1]->{code}, 'homeroom', 'Got the expected second code from set patron attributes');
-    is($result_3->[1]->{value}, 1150605, 'Got the expected second value from set patron attributes');
+    is($result_3->[1]->{attribute}, 1150605, 'Got the expected second value from set patron attributes');
 
     is(scalar @feedback_3, 1, 'Got the expected 1 array size from set patron attributes with extended user');
     is($feedback_3[0]->{feedback}, 1, 'Got the expected second feedback from set patron attributes with extended user');