Bug 18226 - Remove "use Test::DBIx::Class" instantiations' dangerous code duplication...
[koha.git] / t / db_dependent / Circulation / CalcFine.t
1 #!/usr/bin/perl
2
3 use Modern::Perl;
4
5 use Test::More tests => 2;
6
7 use C4::Context;
8 use C4::Overdues;
9
10 use Koha::DateUtils qw( dt_from_string );
11
12 use t::lib::TestBuilder;
13 use t::lib::Mocks;
14
15 our $dbh = C4::Context->dbh;
16 $dbh->{AutoCommit} = 0;
17 $dbh->{RaiseError} = 1;
18
19 $dbh->do(q|DELETE FROM issues|);
20
21 my $builder = t::lib::TestBuilder->new();
22
23 my $branch = $builder->build(
24     {
25         source => 'Branch',
26     }
27 );
28
29 my $category = $builder->build(
30     {
31         source => 'Category',
32     }
33 );
34
35 my $patron = $builder->build(
36     {
37         source => 'Borrower',
38         value  => {
39             categorycode => $category->{categorycode},
40             branchcode   => $branch->{branchcode},
41         },
42     }
43 );
44
45 my $biblio = $builder->build(
46     {
47         source => 'Biblio',
48         value  => {
49             branchcode => $branch->{branchcode},
50         },
51     }
52 );
53
54 my $item = $builder->build(
55     {
56         source => 'Item',
57         value  => {
58             biblionumber     => $biblio->{biblionumber},
59             homebranch       => $branch->{branchcode},
60             holdingbranch    => $branch->{branchcode},
61             replacementprice => '5.00',
62         },
63     }
64 );
65
66 subtest 'Test basic functionality' => sub {
67     plan tests => 1;
68
69     my $rule = $builder->schema->resultset('Issuingrule')->find({
70         branchcode                    => '*',
71         categorycode                  => '*',
72         itemtype                      => '*',
73     });
74     $rule->delete if $rule;
75     my $issuingrule = $builder->build(
76         {
77             source => 'Issuingrule',
78             value  => {
79                 branchcode                    => '*',
80                 categorycode                  => '*',
81                 itemtype                      => '*',
82                 fine                          => '1.00',
83                 lengthunit                    => 'days',
84                 finedays                      => 0,
85                 firstremind                   => 0,
86                 chargeperiod                  => 1,
87                 overduefinescap               => undef,
88                 cap_fine_to_replacement_price => 0,
89             },
90         }
91     );
92
93     my $start_dt = DateTime->new(
94         year       => 2000,
95         month      => 1,
96         day        => 1,
97     );
98
99     my $end_dt = DateTime->new(
100         year       => 2000,
101         month      => 1,
102         day        => 30,
103     );
104
105     my ($amount) = CalcFine( $item, $patron->{categorycode}, $branch->{branchcode}, $start_dt, $end_dt );
106
107     is( $amount, 29, 'Amount is calculated correctly' );
108
109     teardown();
110 };
111
112 subtest 'Test cap_fine_to_replacement_price' => sub {
113     plan tests => 1;
114     my $issuingrule = $builder->build(
115         {
116             source => 'Issuingrule',
117             value  => {
118                 branchcode                    => '*',
119                 categorycode                  => '*',
120                 itemtype                      => '*',
121                 fine                          => '1.00',
122                 lengthunit                    => 'days',
123                 finedays                      => 0,
124                 firstremind                   => 0,
125                 chargeperiod                  => 1,
126                 overduefinescap               => undef,
127                 cap_fine_to_replacement_price => 1,
128             },
129         }
130     );
131
132     my $start_dt = DateTime->new(
133         year       => 2000,
134         month      => 1,
135         day        => 1,
136     );
137
138     my $end_dt = DateTime->new(
139         year       => 2000,
140         month      => 1,
141         day        => 30,
142     );
143
144     my ($amount) = CalcFine( $item, $patron->{categorycode}, $branch->{branchcode}, $start_dt, $end_dt );
145
146     is( $amount, '5.00', 'Amount is calculated correctly' );
147
148     teardown();
149 };
150
151 sub teardown {
152     $dbh->do(q|DELETE FROM issuingrules|);
153 }