Bug 15548: Move new patron related code to Patron*
[koha.git] / t / db_dependent / Circulation / IssuingRules / maxsuspensiondays.t
1 use Modern::Perl;
2 use Test::More tests => 2;
3
4 use MARC::Record;
5 use MARC::Field;
6 use Test::MockModule;
7 use C4::Context;
8
9 use C4::Biblio qw( AddBiblio );
10 use C4::Circulation qw( AddIssue AddReturn );
11 use C4::Items qw( AddItem );
12 use C4::Members qw( AddMember GetMember );
13 use Koha::Database;
14 use Koha::DateUtils;
15 use Koha::Patron::Debarments qw( GetDebarments DelDebarment );
16
17 use t::lib::TestBuilder;
18
19 my $schema = Koha::Database->schema;
20 $schema->storage->txn_begin;
21 my $builder = t::lib::TestBuilder->new;
22 my $dbh = C4::Context->dbh;
23 $dbh->{RaiseError} = 1;
24
25 my $library = $builder->build({
26     source => 'Branch',
27 });
28
29 my $branchcode = $library->{branchcode};
30 local $SIG{__WARN__} = sub { warn $_[0] unless $_[0] =~ /redefined/ };
31 my $userenv->{branch} = $branchcode;
32 *C4::Context::userenv = \&Mock_userenv;
33
34 my $circulation_module = Test::MockModule->new('C4::Circulation');
35
36 # Test without maxsuspensiondays set
37 $circulation_module->mock('GetIssuingRule', sub {
38         return {
39             firstremind => 0,
40             finedays => 2,
41             lengthunit => 'days',
42         }
43 });
44
45 my $borrowernumber = AddMember(
46     firstname =>  'my firstname',
47     surname => 'my surname',
48     categorycode => 'S',
49     branchcode => $branchcode,
50 );
51 my $borrower = GetMember( borrowernumber => $borrowernumber );
52
53 my $record = MARC::Record->new();
54 $record->append_fields(
55     MARC::Field->new('100', ' ', ' ', a => 'My author'),
56     MARC::Field->new('245', ' ', ' ', a => 'My title'),
57 );
58
59 my $barcode = 'bc_maxsuspensiondays';
60 my ($biblionumber, $biblioitemnumber) = AddBiblio($record, '');
61 my (undef, undef, $itemnumber) = AddItem({
62         homebranch => $branchcode,
63         holdingbranch => $branchcode,
64         barcode => $barcode,
65     } , $biblionumber);
66
67 # clear any holidays to avoid throwing off the suspension day
68 # calculations
69 $dbh->do('DELETE FROM special_holidays');
70 $dbh->do('DELETE FROM repeatable_holidays');
71
72 my $daysago20 = dt_from_string->add_duration(DateTime::Duration->new(days => -20));
73 my $daysafter40 = dt_from_string->add_duration(DateTime::Duration->new(days => 40));
74
75 AddIssue( $borrower, $barcode, $daysago20 );
76 AddReturn( $barcode, $branchcode );
77 my $debarments = GetDebarments({borrowernumber => $borrower->{borrowernumber}});
78 is(
79     $debarments->[0]->{expiration},
80     output_pref({ dt => $daysafter40, dateformat => 'iso', dateonly => 1 }),
81     'calculate suspension with no maximum set'
82 );
83 DelDebarment( $debarments->[0]->{borrower_debarment_id} );
84
85 # Test with maxsuspensiondays = 10 days
86 $circulation_module->mock('GetIssuingRule', sub {
87         return {
88             firstremind => 0,
89             finedays => 2,
90             maxsuspensiondays => 10,
91             lengthunit => 'days',
92         }
93 });
94 my $daysafter10 = dt_from_string->add_duration(DateTime::Duration->new(days => 10));
95 AddIssue( $borrower, $barcode, $daysago20 );
96 AddReturn( $barcode, $branchcode );
97 $debarments = GetDebarments({borrowernumber => $borrower->{borrowernumber}});
98 is(
99     $debarments->[0]->{expiration},
100     output_pref({ dt => $daysafter10, dateformat => 'iso', dateonly => 1 }),
101     'calculate suspension with a maximum set'
102 );
103 DelDebarment( $debarments->[0]->{borrower_debarment_id} );
104
105
106 # C4::Context->userenv
107 sub Mock_userenv {
108     return $userenv;
109 }