Bug 21133: Fix use statements order
[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 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
18 my $schema = Koha::Database->schema;
19 $schema->storage->txn_begin;
20 my $builder = t::lib::TestBuilder->new;
21 my $dbh = C4::Context->dbh;
22 $dbh->{RaiseError} = 1;
23
24 my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
25 my $itemtype   = $builder->build({ source => 'Itemtype' })->{itemtype};
26 my $patron_category = $builder->build({ source => 'Category' });
27
28 local $SIG{__WARN__} = sub { warn $_[0] unless $_[0] =~ /redefined/ };
29 my $userenv->{branch} = $branchcode;
30 *C4::Context::userenv = \&Mock_userenv;
31
32 # Test without maxsuspensiondays set
33 Koha::IssuingRules->search->delete;
34 $builder->build(
35     {
36         source => 'Issuingrule',
37         value  => {
38             categorycode => '*',
39             itemtype     => '*',
40             branchcode   => '*',
41             firstremind  => 0,
42             finedays     => 2,
43             lengthunit   => 'days',
44             suspension_chargeperiod => 1,
45         }
46     }
47 );
48
49 my $borrowernumber = Koha::Patron->new({
50     firstname =>  'my firstname',
51     surname => 'my surname',
52     categorycode => $patron_category->{categorycode},
53     branchcode => $branchcode,
54 })->store->borrowernumber;
55 my $borrower = Koha::Patrons->find( $borrowernumber )->unblessed;
56
57 my $record = MARC::Record->new();
58 $record->append_fields(
59     MARC::Field->new('100', ' ', ' ', a => 'My author'),
60     MARC::Field->new('245', ' ', ' ', a => 'My title'),
61 );
62
63 my $barcode = 'bc_maxsuspensiondays';
64 my ($biblionumber, $biblioitemnumber) = AddBiblio($record, '');
65 my (undef, undef, $itemnumber) = AddItem({
66         homebranch => $branchcode,
67         holdingbranch => $branchcode,
68         barcode => $barcode,
69         itype => $itemtype
70     } , $biblionumber);
71
72 # clear any holidays to avoid throwing off the suspension day
73 # calculations
74 $dbh->do('DELETE FROM special_holidays');
75 $dbh->do('DELETE FROM repeatable_holidays');
76
77 my $daysago20 = dt_from_string->add_duration(DateTime::Duration->new(days => -20));
78 my $daysafter40 = dt_from_string->add_duration(DateTime::Duration->new(days => 40));
79
80 AddIssue( $borrower, $barcode, $daysago20 );
81 AddReturn( $barcode, $branchcode );
82 my $debarments = GetDebarments({borrowernumber => $borrower->{borrowernumber}});
83 is(
84     $debarments->[0]->{expiration},
85     output_pref({ dt => $daysafter40, dateformat => 'iso', dateonly => 1 }),
86     'calculate suspension with no maximum set'
87 );
88 DelDebarment( $debarments->[0]->{borrower_debarment_id} );
89
90 # Test with maxsuspensiondays = 10 days
91 my $issuing_rule = Koha::IssuingRules->search->next;
92 $issuing_rule->maxsuspensiondays( 10 )->store;
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 $schema->storage->txn_rollback;
106
107 # C4::Context->userenv
108 sub Mock_userenv {
109     return $userenv;
110 }