|
Lines 1-6
Link Here
|
| 1 |
#!/usr/bin/perl |
1 |
#!/usr/bin/perl |
| 2 |
|
2 |
|
| 3 |
# Copyright 2015 Koha Development team |
3 |
# Copyright 2019 Koha Development team |
| 4 |
# |
4 |
# |
| 5 |
# This file is part of Koha |
5 |
# This file is part of Koha |
| 6 |
# |
6 |
# |
|
Lines 19-25
Link Here
|
| 19 |
|
19 |
|
| 20 |
use Modern::Perl; |
20 |
use Modern::Perl; |
| 21 |
|
21 |
|
| 22 |
use Test::More tests => 23; |
22 |
use Test::More tests => 24; |
| 23 |
use Test::Exception; |
23 |
use Test::Exception; |
| 24 |
use Test::MockModule; |
24 |
use Test::MockModule; |
| 25 |
use Test::Warn; |
25 |
use Test::Warn; |
|
Lines 31-36
use Koha::Patron::Category;
Link Here
|
| 31 |
use Koha::Patron::Categories; |
31 |
use Koha::Patron::Categories; |
| 32 |
use Koha::Patrons; |
32 |
use Koha::Patrons; |
| 33 |
use Koha::Database; |
33 |
use Koha::Database; |
|
|
34 |
use Koha::DateUtils qw( dt_from_string ); |
| 34 |
|
35 |
|
| 35 |
use t::lib::TestBuilder; |
36 |
use t::lib::TestBuilder; |
| 36 |
use t::lib::Mocks; |
37 |
use t::lib::Mocks; |
|
Lines 1035-1040
subtest "attributes_from_api() tests" => sub {
Link Here
|
| 1035 |
$city->attributes_from_api($api_attributes) |
1036 |
$city->attributes_from_api($api_attributes) |
| 1036 |
); |
1037 |
); |
| 1037 |
|
1038 |
|
|
|
1039 |
$schema->storage->txn_rollback; |
| 1040 |
|
| 1041 |
}; |
| 1042 |
|
| 1043 |
subtest "filter_by_last_update" => sub { |
| 1044 |
|
| 1045 |
$schema->storage->txn_begin; |
| 1046 |
|
| 1047 |
my $now = dt_from_string->truncate( to => 'day' ); |
| 1048 |
my @borrowernumbers; |
| 1049 |
# Building 6 patrons that have been created today, yesterday, ... 1 per day |
| 1050 |
for my $i ( 0 .. 5 ) { |
| 1051 |
push @borrowernumbers, |
| 1052 |
$builder->build_object( |
| 1053 |
{ |
| 1054 |
class => 'Koha::Patrons', |
| 1055 |
value => { updated_on => $now->clone->subtract( days => $i ) } |
| 1056 |
} |
| 1057 |
)->borrowernumber; |
| 1058 |
} |
| 1059 |
|
| 1060 |
my $patrons = Koha::Patrons->search( |
| 1061 |
{ borrowernumber => { -in => \@borrowernumbers } } ); |
| 1062 |
|
| 1063 |
try { |
| 1064 |
$patrons->filter_by_last_update( { timestamp_column_name => 'updated_on' } ) |
| 1065 |
->count; |
| 1066 |
} |
| 1067 |
catch { |
| 1068 |
ok( |
| 1069 |
$_->isa('Koha::Exceptions::MissingParameter'), |
| 1070 |
'Should raise an exception if no parameter given' |
| 1071 |
); |
| 1072 |
}; |
| 1073 |
|
| 1074 |
my $count = $patrons->filter_by_last_update( |
| 1075 |
{ timestamp_column_name => 'updated_on', days => 2 } )->count; |
| 1076 |
is( $count, 3, '3 patrons have been updated before the last 2 days (exclusive)' ); |
| 1077 |
|
| 1078 |
$count = $patrons->filter_by_last_update( |
| 1079 |
{ timestamp_column_name => 'updated_on', days => 1 } )->count; |
| 1080 |
is( $count, 4, '4 patrons have been updated before yesterday (exclusive)' ); |
| 1081 |
|
| 1082 |
$count = $patrons->filter_by_last_update( |
| 1083 |
{ timestamp_column_name => 'updated_on', days => 0 } )->count; |
| 1084 |
is( $count, 5, '5 patrons have been updated before today (exclusive)' ); |
| 1085 |
|
| 1086 |
$count = $patrons->filter_by_last_update( |
| 1087 |
{ timestamp_column_name => 'updated_on', from => $now } )->count; |
| 1088 |
is( $count, 1, '1 patron has been updated "from today" (inclusive)' ); |
| 1089 |
|
| 1090 |
$count = $patrons->filter_by_last_update( |
| 1091 |
{ timestamp_column_name => 'updated_on', to => $now } )->count; |
| 1092 |
is( $count, 6, '6 patrons have been updated "to today" (inclusive)' ); |
| 1093 |
|
| 1094 |
$count = $patrons->filter_by_last_update( |
| 1095 |
{ |
| 1096 |
timestamp_column_name => 'updated_on', |
| 1097 |
from => $now->clone->subtract( days => 4 ), |
| 1098 |
to => $now->clone->subtract( days => 2 ) |
| 1099 |
} |
| 1100 |
)->count; |
| 1101 |
is( $count, 3, '3 patrons have been updated between D-4 and D-2' ); |
| 1102 |
|
| 1103 |
t::lib::Mocks::mock_preference( 'dateformat', 'metric' ); |
| 1104 |
try { |
| 1105 |
$count = $patrons->filter_by_last_update( |
| 1106 |
{ timestamp_column_name => 'updated_on', from => '1970-12-31' } ) |
| 1107 |
->count; |
| 1108 |
} |
| 1109 |
catch { |
| 1110 |
ok( |
| 1111 |
$_->isa( |
| 1112 |
'No exception raised, from and to parameters can take an iso formatted date' |
| 1113 |
) |
| 1114 |
); |
| 1115 |
}; |
| 1116 |
try { |
| 1117 |
$count = $patrons->filter_by_last_update( |
| 1118 |
{ timestamp_column_name => 'updated_on', from => '31/12/1970' } ) |
| 1119 |
->count; |
| 1120 |
} |
| 1121 |
catch { |
| 1122 |
ok( |
| 1123 |
$_->isa( |
| 1124 |
'No exception raised, from and to parameters can take an metric formatted date (depending on dateformat syspref)' |
| 1125 |
) |
| 1126 |
); |
| 1127 |
}; |
| 1128 |
|
| 1038 |
$schema->storage->txn_rollback; |
1129 |
$schema->storage->txn_rollback; |
| 1039 |
}; |
1130 |
}; |
| 1040 |
|
1131 |
|
| 1041 |
- |
|
|