Bug 18936: (follow-up) Add foreign key and scope enhancement to circ rules
[srvgit] / t / db_dependent / Circulation / GetHardDueDate.t
1 #!/usr/bin/perl
2
3 use Modern::Perl;
4 use C4::Context;
5 use DateTime;
6 use Koha::Database;
7 use Koha::DateUtils;
8 use Koha::CirculationRules;
9 use Koha::Library;
10
11 use t::lib::TestBuilder;
12
13 use Test::More tests => 10;
14
15 BEGIN {
16     use_ok('C4::Circulation');
17 }
18 can_ok(
19     'C4::Circulation',
20     qw(
21       GetHardDueDate
22       GetLoanLength
23       )
24 );
25
26 my $schema = Koha::Database->new->schema;
27 $schema->storage->txn_begin;
28 my $dbh = C4::Context->dbh;
29
30 $dbh->do(q|DELETE FROM issues|);
31 $dbh->do(q|DELETE FROM items|);
32 $dbh->do(q|DELETE FROM borrowers|);
33 $dbh->do(q|DELETE FROM edifact_ean|);
34 $dbh->do(q|DELETE FROM branches|);
35 $dbh->do(q|DELETE FROM categories|);
36 $dbh->do(q|DELETE FROM circulation_rules|);
37
38 #Add sample datas
39
40 #Add branch and category
41 my $samplebranch1 = {
42     branchcode     => 'SAB1',
43     branchname     => 'Sample Branch',
44     branchaddress1 => 'sample adr1',
45     branchaddress2 => 'sample adr2',
46     branchaddress3 => 'sample adr3',
47     branchzip      => 'sample zip',
48     branchcity     => 'sample city',
49     branchstate    => 'sample state',
50     branchcountry  => 'sample country',
51     branchphone    => 'sample phone',
52     branchfax      => 'sample fax',
53     branchemail    => 'sample email',
54     branchurl      => 'sample url',
55     branchip       => 'sample ip',
56     branchprinter  => undef,
57     opac_info      => 'sample opac',
58 };
59 my $samplebranch2 = {
60     branchcode     => 'SAB2',
61     branchname     => 'Sample Branch2',
62     branchaddress1 => 'sample adr1_2',
63     branchaddress2 => 'sample adr2_2',
64     branchaddress3 => 'sample adr3_2',
65     branchzip      => 'sample zip2',
66     branchcity     => 'sample city2',
67     branchstate    => 'sample state2',
68     branchcountry  => 'sample country2',
69     branchphone    => 'sample phone2',
70     branchfax      => 'sample fax2',
71     branchemail    => 'sample email2',
72     branchurl      => 'sample url2',
73     branchip       => 'sample ip2',
74     branchprinter  => undef,
75     opac_info      => 'sample opac2',
76 };
77 Koha::Library->new($samplebranch1)->store;
78 Koha::Library->new($samplebranch2)->store;
79
80 my $samplecat = {
81     categorycode          => 'CAT1',
82     description           => 'Description1',
83     enrolmentperiod       => undef,
84     enrolmentperioddate   => undef,
85     dateofbirthrequired   => undef,
86     finetype              => undef,
87     bulk                  => undef,
88     enrolmentfee          => undef,
89     overduenoticerequired => undef,
90     issuelimit            => undef,
91     reservefee            => undef,
92     hidelostitems         => 0,
93     category_type         => 'A',
94 };
95 my $query =
96 "INSERT INTO categories (categorycode,description,enrolmentperiod,enrolmentperioddate,dateofbirthrequired ,finetype,bulk,enrolmentfee,overduenoticerequired,issuelimit ,reservefee ,hidelostitems ,category_type) VALUES( ?,?,?,?,?,?,?,?,?,?,?,?,?)";
97 $dbh->do(
98     $query, {},
99     $samplecat->{categorycode},          $samplecat->{description},
100     $samplecat->{enrolmentperiod},       $samplecat->{enrolmentperioddate},
101     $samplecat->{dateofbirthrequired},   $samplecat->{finetype},
102     $samplecat->{bulk},                  $samplecat->{enrolmentfee},
103     $samplecat->{overduenoticerequired}, $samplecat->{issuelimit},
104     $samplecat->{reservefee},            $samplecat->{hidelostitems},
105     $samplecat->{category_type}
106 );
107
108 my $builder = t::lib::TestBuilder->new;
109 my $sampleitemtype1 = $builder->build({ source => 'Itemtype' })->{itemtype};
110 my $sampleitemtype2 = $builder->build({ source => 'Itemtype' })->{itemtype};
111
112 #Begin Tests
113
114 my $default = {
115     issuelength => 0,
116     renewalperiod => 0,
117     lengthunit => 'days'
118 };
119
120 #Test get_effective_rules
121 my $sampleissuingrule1 = {
122     branchcode   => $samplebranch1->{branchcode},
123     categorycode => $samplecat->{categorycode},
124     itemtype     => $sampleitemtype1,
125     rules        => {
126         finedays                         => 0,
127         lengthunit                       => 'days',
128         renewalperiod                    => 5,
129         norenewalbefore                  => 6,
130         auto_renew                       => 0,
131         issuelength                      => 5,
132         chargeperiod                     => 0,
133         chargeperiod_charge_at           => 0,
134         rentaldiscount                   => 2,
135         reservesallowed                  => 0,
136         hardduedate                      => '2013-01-01',
137         fine                             => 0,
138         hardduedatecompare               => 5,
139         overduefinescap                  => 0,
140         renewalsallowed                  => 0,
141         firstremind                      => 0,
142         maxsuspensiondays                => 0,
143         onshelfholds                     => 0,
144         opacitemholds                    => 'N',
145         cap_fine_to_replacement_price    => 0,
146         holds_per_record                 => 1,
147         article_requests                 => 'yes',
148         no_auto_renewal_after            => undef,
149         no_auto_renewal_after_hard_limit => undef,
150         suspension_chargeperiod          => 1,
151         holds_per_day                    => undef,
152     }
153 };
154 my $sampleissuingrule2 = {
155     branchcode   => $samplebranch2->{branchcode},
156     categorycode => $samplecat->{categorycode},
157     itemtype     => $sampleitemtype1,
158     rules        => {
159         renewalsallowed               => 0,
160         renewalperiod                 => 2,
161         norenewalbefore               => 7,
162         auto_renew                    => 0,
163         reservesallowed               => 0,
164         issuelength                   => 2,
165         lengthunit                    => 'days',
166         hardduedate                   => 2,
167         hardduedatecompare            => undef,
168         fine                          => undef,
169         finedays                      => undef,
170         firstremind                   => undef,
171         chargeperiod                  => undef,
172         chargeperiod_charge_at        => 0,
173         rentaldiscount                => 2.00,
174         overduefinescap               => undef,
175         maxsuspensiondays             => 0,
176         onshelfholds                  => 1,
177         opacitemholds                 => 'Y',
178         cap_fine_to_replacement_price => 0,
179         holds_per_record              => 1,
180         article_requests              => 'yes',
181     }
182 };
183 my $sampleissuingrule3 = {
184     branchcode   => $samplebranch1->{branchcode},
185     categorycode => $samplecat->{categorycode},
186     itemtype     => $sampleitemtype2,
187     rules        => {
188         renewalsallowed               => 0,
189         renewalperiod                 => 3,
190         norenewalbefore               => 8,
191         auto_renew                    => 0,
192         reservesallowed               => 0,
193         issuelength                   => 3,
194         lengthunit                    => 'days',
195         hardduedate                   => 3,
196         hardduedatecompare            => undef,
197         fine                          => undef,
198         finedays                      => undef,
199         firstremind                   => undef,
200         chargeperiod                  => undef,
201         chargeperiod_charge_at        => 0,
202         rentaldiscount                => 3.00,
203         overduefinescap               => undef,
204         maxsuspensiondays             => 0,
205         onshelfholds                  => 1,
206         opacitemholds                 => 'F',
207         cap_fine_to_replacement_price => 0,
208         holds_per_record              => 1,
209         article_requests              => 'yes',
210     }
211 };
212
213 Koha::CirculationRules->set_rules( $sampleissuingrule1 );
214 Koha::CirculationRules->set_rules( $sampleissuingrule2 );
215 Koha::CirculationRules->set_rules( $sampleissuingrule3 );
216
217 #Test GetLoanLength
218 is_deeply(
219     C4::Circulation::GetLoanLength(
220         $samplecat->{categorycode},
221         $sampleitemtype1, $samplebranch1->{branchcode}
222     ),
223     { issuelength => 5, lengthunit => 'days', renewalperiod => 5 },
224     "GetLoanLength"
225 );
226
227 is_deeply(
228     C4::Circulation::GetLoanLength(),
229     $default,
230     "Without parameters, GetLoanLength returns hardcoded values"
231 );
232 is_deeply(
233     C4::Circulation::GetLoanLength( -1, -1 ),
234     $default,
235     "With wrong parameters, GetLoanLength returns hardcoded values"
236 );
237 is_deeply(
238     C4::Circulation::GetLoanLength( $samplecat->{categorycode} ),
239     $default,
240     "With only one parameter, GetLoanLength returns hardcoded values"
241 );    #NOTE : is that really what is expected?
242 is_deeply(
243     C4::Circulation::GetLoanLength( $samplecat->{categorycode}, $sampleitemtype1 ),
244     $default,
245     "With only two parameters, GetLoanLength returns hardcoded values"
246 );    #NOTE : is that really what is expected?
247 is_deeply(
248     C4::Circulation::GetLoanLength( $samplecat->{categorycode}, $sampleitemtype1, $samplebranch1->{branchcode} ),
249     {
250         issuelength   => 5,
251         renewalperiod => 5,
252         lengthunit    => 'days',
253     },
254     "With the correct number of parameters, GetLoanLength returns the expected values"
255 );
256
257 #Test GetHardDueDate
258 my @hardduedate = C4::Circulation::GetHardDueDate( $samplecat->{categorycode},
259     $sampleitemtype1, $samplebranch1->{branchcode} );
260 is_deeply(
261     \@hardduedate,
262     [
263         dt_from_string( $sampleissuingrule1->{rules}->{hardduedate}, 'iso' ),
264         $sampleissuingrule1->{rules}->{hardduedatecompare}
265     ],
266     "GetHardDueDate returns the duedate and the duedatecompare"
267 );