Bug 34617: Unit tests

Signed-off-by: David Nind <david@davidnind.com>

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
This commit is contained in:
Nick Clemens 2023-08-25 14:51:23 +00:00 committed by Tomas Cohen Arazi
parent 086810633b
commit b38780f6cb
Signed by: tomascohen
GPG key ID: 0A272EA1B2F3C15F

View file

@ -18,7 +18,7 @@
# along with Koha; if not, see <http://www.gnu.org/licenses>.
use Modern::Perl;
use Test::More tests => 176;
use Test::More tests => 177;
use Test::Warn;
use Test::Exception;
use Encode qw( encode_utf8 );
@ -186,15 +186,15 @@ is($result_3a->{imported}, 0, 'Got the expected 0 imported result from import_pa
is($result_3a->{invalid}, 0, 'Got the expected 0 invalid result from import_patrons');
is($result_3a->{overwritten}, 1, 'Got the expected 1 overwritten result from import_patrons that matched');
my $patron_3 = Koha::Patrons->find({ cardnumber => '1000' });
is( $patron_3->dateexpiry, '2015-07-01', "Expiration date is correct with update_dateexpiry = false" );
# Given ... valid file handle, good matchpoint that matches should overwrite when set, surname is protected from
# overwrite but firstname is not
my $filename_3c = make_csv($temp_dir, $csv_headers, $csv_one_line_b);
open(my $handle_3c, "<", $filename_3c) or die "cannot open < $filename_3: $!";
my $params_3c = { file => $handle_3c, matchpoint => 'cardnumber', overwrite_cardnumber => 1, preserve_fields => [ 'firstname' ], update_dateexpiry => 1, update_dateexpiry_from_today => 1 };
my $patron_3 = Koha::Patrons->find({ cardnumber => '1000' });
is( $patron_3->dateexpiry, '2015-07-01', "Expiration date is correct with update_dateexpiry = false" );
# When ...
my $result_3c;
warning_is { $result_3c = $patrons_import->import_patrons($params_3c) }
@ -1175,6 +1175,64 @@ subtest 'welcome_email' => sub {
is($notices->count, 1, 'Notice was queued');
};
subtest 'test update_dateexpiry when no dateexpiry in file' => sub {
# Normally, imports on ly update included columns, however, if we are asking Koha
# to calculate expiration dates
plan tests => 3;
my $csv_headers = 'cardnumber,surname,branchcode,categorycode,dateenrolled';
my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
$patron->dateexpiry( dt_from_string() );
$patron->dateenrolled( dt_from_string->add( months => '-24', end_of_month => 'limit' ) )->store;
my $csv_values = join(
',', $patron->cardnumber, $patron->surname, $patron->branchcode, $patron->categorycode,
$patron->dateenrolled
);
$patron->category->enrolmentperiod(42)->enrolmentperioddate(undef)->store();
my $filename_1 = make_csv( $temp_dir, $csv_headers, $csv_values );
open( my $handle_1, "<", $filename_1 ) or die "cannot open < $filename_1: $!";
my $params = { file => $handle_1, matchpoint => 'cardnumber', overwrite_cardnumber => 1, update_dateexpiry => 1 };
my $result = $patrons_import->import_patrons( $params, {} );
$patron->discard_changes();
is(
$patron->dateexpiry, dt_from_string->add( months => 18, end_of_month => 'limit' )->ymd,
"Expiration date is correct with update_dateexpiry = true no dateexpiry in file and update_datexpiryfromtoday false (i.e. use passed dateenrolled) "
);
$filename_1 = make_csv( $temp_dir, $csv_headers, $csv_values );
open( $handle_1, "<", $filename_1 ) or die "cannot open < $filename_1: $!";
$params = {
file => $handle_1, matchpoint => 'cardnumber', overwrite_cardnumber => 1, update_dateexpiry => 1,
update_dateexpiry_from_today => 1
};
$result = $patrons_import->import_patrons( $params, {} );
$patron->discard_changes();
is(
$patron->dateexpiry, dt_from_string->add( months => 42, end_of_month => 'limit' )->ymd,
"Expiration date is correct with update_dateexpiry = true no dateexpiry in file and update_datexpiryfromtoday true "
);
$csv_headers = 'cardnumber,surname,branchcode,categorycode';
$csv_values = join( ',', $patron->cardnumber, $patron->surname, $patron->branchcode, $patron->categorycode );
$filename_1 = make_csv( $temp_dir, $csv_headers, $csv_values );
open( $handle_1, "<", $filename_1 ) or die "cannot open < $filename_1: $!";
$patron->dateexpiry( dt_from_string() );
$patron->dateenrolled( dt_from_string->add( months => '-24', end_of_month => 'limit' ) )->store;
$params = { file => $handle_1, matchpoint => 'cardnumber', overwrite_cardnumber => 1, update_dateexpiry => 1 };
$result = $patrons_import->import_patrons( $params, {} );
$patron->discard_changes();
is(
$patron->dateexpiry, dt_from_string->add( months => 42, end_of_month => 'limit' )->ymd,
"Expiration date is correct with update_dateexpiry = true no dateexpiry in file and update_datexpiryfromtoday false but no dateenrolled in file (today is used) "
);
};
# got is { code => $code, attribute => $attribute }
# expected is { $code => \@attributes }
sub compare_patron_attributes {