Bug 12230: Set a maximum suspension days as a new issuing rule
[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::DateUtils;
14 use Koha::Borrower::Debarments qw( GetDebarments DelDebarment );
15 my $dbh = C4::Context->dbh;
16 $dbh->{RaiseError} = 1;
17 $dbh->{AutoCommit} = 0;
18
19 my $branchcode = 'CPL';
20 local $SIG{__WARN__} = sub { warn $_[0] unless $_[0] =~ /redefined/ };
21 my $userenv->{branch} = $branchcode;
22 *C4::Context::userenv = \&Mock_userenv;
23
24 my $circulation_module = Test::MockModule->new('C4::Circulation');
25
26 # Test without maxsuspensiondays set
27 $circulation_module->mock('GetIssuingRule', sub {
28         return {
29             firstremind => 0,
30             finedays => 2,
31             lengthunit => 'days',
32         }
33 });
34
35 my $borrowernumber = AddMember(
36     firstname =>  'my firstname',
37     surname => 'my surname',
38     categorycode => 'S',
39     branchcode => $branchcode,
40 );
41 my $borrower = GetMember( borrowernumber => $borrowernumber );
42
43 my $record = MARC::Record->new();
44 $record->append_fields(
45     MARC::Field->new('100', ' ', ' ', a => 'My author'),
46     MARC::Field->new('245', ' ', ' ', a => 'My title'),
47 );
48
49 my $barcode = 'bc_maxsuspensiondays';
50 my ($biblionumber, $biblioitemnumber) = AddBiblio($record, '');
51 my (undef, undef, $itemnumber) = AddItem({
52         homebranch => $branchcode,
53         holdingbranch => $branchcode,
54         barcode => $barcode,
55     } , $biblionumber);
56
57
58 my $daysago20 = dt_from_string->add_duration(DateTime::Duration->new(days => -20));
59 my $daysafter40 = dt_from_string->add_duration(DateTime::Duration->new(days => 40));
60
61 AddIssue( $borrower, $barcode, $daysago20 );
62 AddReturn( $barcode, $branchcode );
63 my $debarments = GetDebarments({borrowernumber => $borrower->{borrowernumber}});
64 is( $debarments->[0]->{expiration}, output_pref({ dt => $daysafter40, dateformat => 'iso', dateonly => 1 }));
65 DelDebarment( $debarments->[0]->{borrower_debarment_id} );
66
67 # Test with maxsuspensiondays = 10 days
68 $circulation_module->mock('GetIssuingRule', sub {
69         return {
70             firstremind => 0,
71             finedays => 2,
72             maxsuspensiondays => 10,
73             lengthunit => 'days',
74         }
75 });
76 my $daysafter10 = dt_from_string->add_duration(DateTime::Duration->new(days => 10));
77 AddIssue( $borrower, $barcode, $daysago20 );
78 AddReturn( $barcode, $branchcode );
79 $debarments = GetDebarments({borrowernumber => $borrower->{borrowernumber}});
80 is( $debarments->[0]->{expiration}, output_pref({ dt => $daysafter10, dateformat => 'iso', dateonly => 1 }));
81 DelDebarment( $debarments->[0]->{borrower_debarment_id} );
82
83
84 # C4::Context->userenv
85 sub Mock_userenv {
86     return $userenv;
87 }