Bug 18936: Fix some more tests
[koha.git] / t / db_dependent / Circulation / IssuingRules / maxsuspensiondays.t
1 use Modern::Perl;
2 use Test::More tests => 4;
3
4 use MARC::Record;
5 use MARC::Field;
6 use C4::Context;
7
8 use C4::Circulation qw( AddIssue AddReturn );
9 use C4::Items qw( AddItem );
10 use C4::Biblio qw( AddBiblio );
11 use Koha::Database;
12 use Koha::DateUtils;
13 use Koha::Patron::Debarments qw( GetDebarments DelDebarment );
14 use Koha::Patrons;
15
16 use t::lib::TestBuilder;
17 use t::lib::Mocks;
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 $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
26 my $itemtype   = $builder->build({ source => 'Itemtype' })->{itemtype};
27 my $patron_category = $builder->build({ source => 'Category' });
28
29 t::lib::Mocks::mock_userenv({ branchcode => $branchcode });
30
31 # Test without maxsuspensiondays set
32 Koha::CirculationRules->search->delete;
33 Koha::CirculationRules->set_rules(
34     {
35         categorycode => undef,
36         itemtype     => undef,
37         branchcode   => undef,
38         rules        => {
39             firstremind => 0,
40             finedays    => 2,
41             lengthunit  => 'days',
42             suspension_chargeperiod => 1,
43         }
44     }
45 );
46
47 my $borrowernumber = Koha::Patron->new({
48     firstname =>  'my firstname',
49     surname => 'my surname',
50     categorycode => $patron_category->{categorycode},
51     branchcode => $branchcode,
52 })->store->borrowernumber;
53 my $borrower = Koha::Patrons->find( $borrowernumber )->unblessed;
54
55 my $record = MARC::Record->new();
56 $record->append_fields(
57     MARC::Field->new('100', ' ', ' ', a => 'My author'),
58     MARC::Field->new('245', ' ', ' ', a => 'My title'),
59 );
60
61 my $barcode = 'bc_maxsuspensiondays';
62 my ($biblionumber, $biblioitemnumber) = AddBiblio($record, '');
63 my (undef, undef, $itemnumber) = AddItem({
64         homebranch => $branchcode,
65         holdingbranch => $branchcode,
66         barcode => $barcode,
67         itype => $itemtype
68     } , $biblionumber);
69
70 # clear any holidays to avoid throwing off the suspension day
71 # calculations
72 $dbh->do('DELETE FROM special_holidays');
73 $dbh->do('DELETE FROM repeatable_holidays');
74
75 my $daysago20 = dt_from_string->add_duration(DateTime::Duration->new(days => -20));
76 my $daysafter40 = dt_from_string->add_duration(DateTime::Duration->new(days => 40));
77
78 AddIssue( $borrower, $barcode, $daysago20 );
79 AddReturn( $barcode, $branchcode );
80 my $debarments = GetDebarments({borrowernumber => $borrower->{borrowernumber}});
81 is(
82     $debarments->[0]->{expiration},
83     output_pref({ dt => $daysafter40, dateformat => 'iso', dateonly => 1 }),
84     'calculate suspension with no maximum set'
85 );
86 DelDebarment( $debarments->[0]->{borrower_debarment_id} );
87
88 # Test with maxsuspensiondays = 10 days
89 Koha::CirculationRules->set_rules(
90     {
91         categorycode => undef,
92         itemtype     => undef,
93         branchcode   => undef,
94         rules        => {
95             maxsuspensiondays => 10,
96         }
97     }
98 );
99
100 my $daysafter10 = dt_from_string->add_duration(DateTime::Duration->new(days => 10));
101 AddIssue( $borrower, $barcode, $daysago20 );
102 AddReturn( $barcode, $branchcode );
103 $debarments = GetDebarments({borrowernumber => $borrower->{borrowernumber}});
104 is(
105     $debarments->[0]->{expiration},
106     output_pref({ dt => $daysafter10, dateformat => 'iso', dateonly => 1 }),
107     'calculate suspension with a maximum set'
108 );
109 DelDebarment( $debarments->[0]->{borrower_debarment_id} );
110
111 subtest "suspension_chargeperiod" => sub {
112     Koha::CirculationRules->set_rules(
113         {
114             categorycode => undef,
115             itemtype     => undef,
116             branchcode   => undef,
117             rules        => {
118                 firstremind  => 0,
119                 finedays     => 7,
120                 lengthunit   => 'days',
121                 suspension_chargeperiod => 15,
122                 maxsuspensiondays => 333,
123             }
124         }
125     );
126     my $patron = $builder->build_object({ class => 'Koha::Patrons' });
127     my $item = $builder->build_sample_item;
128
129     my $last_year = dt_from_string->clone->subtract( years => 1 );
130     my $today = dt_from_string;
131     my $new_debar_dt = C4::Circulation::_calculate_new_debar_dt( $patron->unblessed, $item->unblessed, $last_year, $today );
132     is( $new_debar_dt->truncate( to => 'day' ),
133         $today->clone->add( days => 365 / 15 * 7 )->truncate( to => 'day' ) );
134
135 };
136
137 subtest "maxsuspensiondays" => sub {
138     Koha::CirculationRules->set_rules(
139         {
140             categorycode => undef,
141             itemtype     => undef,
142             branchcode   => undef,
143             rules        => {
144                 firstremind  => 0,
145                 finedays     => 15,
146                 lengthunit   => 'days',
147                 suspension_chargeperiod => 7,
148                 maxsuspensiondays => 333,
149             }
150         }
151     );
152     my $patron = $builder->build_object({ class => 'Koha::Patrons' });
153     my $item = $builder->build_sample_item;
154
155     my $last_year = dt_from_string->clone->subtract( years => 1 );
156     my $today = dt_from_string;
157     my $new_debar_dt = C4::Circulation::_calculate_new_debar_dt( $patron->unblessed, $item->unblessed, $last_year, $today );
158     is( $new_debar_dt->truncate( to => 'day' ),
159         $today->clone->add( days => 333 )->truncate( to => 'day' ) );
160 };
161
162 $schema->storage->txn_rollback;