|
Line 0
Link Here
|
| 0 |
- |
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; |
| 22 |
use Test::MockModule; |
| 23 |
|
| 24 |
use Koha::Database; |
| 25 |
|
| 26 |
use File::Temp qw(tempfile tempdir); |
| 27 |
my $temp_dir = tempdir('Koha_patrons_import_test_XXXX', CLEANUP => 1, TMPDIR => 1); |
| 28 |
|
| 29 |
use t::lib::TestBuilder; |
| 30 |
my $builder = t::lib::TestBuilder->new; |
| 31 |
|
| 32 |
my $schema = Koha::Database->new->schema; |
| 33 |
$schema->storage->txn_begin; |
| 34 |
|
| 35 |
# ########## Tests start here ############################# |
| 36 |
# Given ... we can use the module |
| 37 |
BEGIN { use_ok('Koha::Patrons::Import'); } |
| 38 |
|
| 39 |
# Given ... we can reach the method(s) |
| 40 |
my @methods = ('import_patrons'); |
| 41 |
can_ok('Koha::Patrons::Import', @methods); |
| 42 |
|
| 43 |
# Tests for Koha::Patrons::Import::import_patrons() |
| 44 |
# Given ... nothing much. When ... Then ... |
| 45 |
my $result = Koha::Patrons::Import::import_patrons(undef); |
| 46 |
is($result, undef, 'Got the expected undef from import_patrons with nothing much'); |
| 47 |
|
| 48 |
# Given ... some params but no file handle. |
| 49 |
my $params_0 = { some_stuff => 'random stuff', }; |
| 50 |
|
| 51 |
# When ... Then ... |
| 52 |
my $result_0 = Koha::Patrons::Import::import_patrons($params_0); |
| 53 |
is($result_0, undef, 'Got the expected undef from import_patrons with no file handle'); |
| 54 |
|
| 55 |
# Given ... a file handle to file with headers only. |
| 56 |
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'; |
| 57 |
my $csv_one_line = '1000,Nancy,Jenkins,Dr,,NJ,78,Circle,Bunting,El Paso,Henderson,Texas,79984,United States,ajenkins0@sourceforge.net,7-(388)559-6763,3-(373)151-4471,8-(509)286-4001,16/10/1965,CPL,PT,28/12/2014,01/07/2015,jjenkins0,DPQILy'; |
| 58 |
|
| 59 |
my $filename_1 = make_csv($temp_dir, $csv_headers, $csv_one_line); |
| 60 |
open(my $handle_1, "<", $filename_1) or die "cannot open < $filename_1: $!"; |
| 61 |
my $params_1 = { file => $handle_1, }; |
| 62 |
|
| 63 |
# When ... |
| 64 |
my $result_1 = Koha::Patrons::Import::import_patrons($params_1); |
| 65 |
|
| 66 |
# Then ... |
| 67 |
is($result_1->{imported}, 1, 'Got the expected 1 imported result from import_patrons with no matchpoint defined'); |
| 68 |
is($result_1->{invalid}, 0, 'Got the expected 0 invalid result from import_patrons with no matchpoint defined'); |
| 69 |
|
| 70 |
# Given ... a valid file handle, a bad matchpoint resulting in invalid card number |
| 71 |
my $filename_2 = make_csv($temp_dir, $csv_headers, $csv_one_line); |
| 72 |
open(my $handle_2, "<", $filename_2) or die "cannot open < $filename_2: $!"; |
| 73 |
my $params_2 = { file => $handle_2, matchpoint => 'SHOW_BCODE', }; |
| 74 |
|
| 75 |
# When ... |
| 76 |
my $result_2 = Koha::Patrons::Import::import_patrons($params_2); |
| 77 |
|
| 78 |
# Then ... |
| 79 |
is($result_2->{imported}, 0, 'Got the expected 0 imported result from import_patrons with no matchpoint defined'); |
| 80 |
is($result_2->{invalid}, 1, 'Got the expected 1 invalid result from import_patrons with no matchpoint defined'); |
| 81 |
is($result_2->{errors}->[0]->{invalid_cardnumber}, 1, 'Got the expected invalid card number from import patrons with invalid card number'); |
| 82 |
|
| 83 |
# Given ... valid file handle, good matchpoint but same input as prior test. |
| 84 |
my $filename_3 = make_csv($temp_dir, $csv_headers, $csv_one_line); |
| 85 |
open(my $handle_3, "<", $filename_3) or die "cannot open < $filename_3: $!"; |
| 86 |
my $params_3 = { file => $handle_3, matchpoint => 'cardnumber', }; |
| 87 |
|
| 88 |
# When ... |
| 89 |
my $result_3 = Koha::Patrons::Import::import_patrons($params_3); |
| 90 |
|
| 91 |
# Then ... |
| 92 |
is($result_3->{imported}, 0, 'Got the expected 0 imported result from import_patrons with duplicate userid'); |
| 93 |
is($result_3->{invalid}, 1, 'Got the expected 1 invalid result from import_patrons with duplicate userid'); |
| 94 |
is($result_3->{errors}->[0]->{duplicate_userid}, 1, 'Got the expected duplicate userid error from import patrons with duplicate userid'); |
| 95 |
|
| 96 |
# Given ... a new input and mocked C4::Context |
| 97 |
my $context = new Test::MockModule('C4::Context'); |
| 98 |
$context->mock('preference', sub { my ($mod, $meth) = @_; if ( $meth eq 'ExtendedPatronAttributes' ) { return 1; } }); |
| 99 |
|
| 100 |
my $new_input_line = '1001,Donna,Sullivan,Mrs,Henry,DS,59,Court,Burrows,Reading,Salt Lake City,Pennsylvania,19605,United States,hsullivan1@purevolume.com,3-(864)009-3006,7-(291)885-8423,1-(879)095-5038,19/09/1970,LPL,PT,04/03/2015,01/07/2015,hsullivan1,8j6P6Dmap'; |
| 101 |
my $filename_4 = make_csv($temp_dir, $csv_headers, $new_input_line); |
| 102 |
open(my $handle_4, "<", $filename_4) or die "cannot open < $filename_4: $!"; |
| 103 |
my $params_4 = { file => $handle_4, matchpoint => 'cardnumber', }; |
| 104 |
|
| 105 |
# When ... Then ... |
| 106 |
my $result_4 = Koha::Patrons::Import::import_patrons($params_4); |
| 107 |
is($result_4->{imported}, 1, 'Got the expected 1 imported result from import_patrons with extended user'); |
| 108 |
|
| 109 |
# ###### Test utility ########### |
| 110 |
sub make_csv { |
| 111 |
my ($temp_dir, @lines) = @_; |
| 112 |
|
| 113 |
my ($fh, $filename) = tempfile( DIR => $temp_dir) or die $!; |
| 114 |
print $fh $_."\r\n" foreach @lines; |
| 115 |
close $fh or die $!; |
| 116 |
|
| 117 |
return $filename; |
| 118 |
} |
| 119 |
|
| 120 |
done_testing(); |
| 121 |
|
| 122 |
1; |