Bug 11580: Add one more test and clear the cache
[koha.git] / t / db_dependent / Circulation / CalcDateDue.t
1 #!/usr/bin/perl
2
3 use Modern::Perl;
4
5 use Test::More tests => 7;
6 use Test::MockModule;
7 use DBI;
8 use DateTime;
9 use t::lib::Mocks;
10 use t::lib::TestBuilder;
11 use C4::Calendar;
12
13 use_ok('C4::Circulation');
14
15 my $schema = Koha::Database->new->schema;
16 $schema->storage->txn_begin;
17 my $builder = t::lib::TestBuilder->new;
18
19 my $categorycode = 'B';
20 my $itemtype = 'MX';
21 my $branchcode = 'FPL';
22 my $issuelength = 10;
23 my $renewalperiod = 5;
24 my $lengthunit = 'days';
25
26 Koha::Database->schema->resultset('Issuingrule')->create({
27   categorycode => $categorycode,
28   itemtype => $itemtype,
29   branchcode => $branchcode,
30   issuelength => $issuelength,
31   renewalperiod => $renewalperiod,
32   lengthunit => $lengthunit,
33 });
34
35 #Set syspref ReturnBeforeExpiry = 1 and useDaysMode = 'Days'
36 t::lib::Mocks::mock_preference('ReturnBeforeExpiry', 1);
37 t::lib::Mocks::mock_preference('useDaysMode', 'Days');
38
39 my $cache           = Koha::Caches->get_instance();
40 $cache->clear_from_cache('single_holidays');
41
42 my $dateexpiry = '2013-01-01';
43
44 my $borrower = {categorycode => 'B', dateexpiry => $dateexpiry};
45 my $start_date = DateTime->new({year => 2013, month => 2, day => 9});
46 my $date = C4::Circulation::CalcDateDue( $start_date, $itemtype, $branchcode, $borrower );
47 is($date, $dateexpiry . 'T23:59:00', 'date expiry');
48 $date = C4::Circulation::CalcDateDue( $start_date, $itemtype, $branchcode, $borrower, 1 );
49
50
51 #Set syspref ReturnBeforeExpiry = 1 and useDaysMode != 'Days'
52 t::lib::Mocks::mock_preference('ReturnBeforeExpiry', 1);
53 t::lib::Mocks::mock_preference('useDaysMode', 'noDays');
54
55 $borrower = {categorycode => 'B', dateexpiry => $dateexpiry};
56 $start_date = DateTime->new({year => 2013, month => 2, day => 9});
57 $date = C4::Circulation::CalcDateDue( $start_date, $itemtype, $branchcode, $borrower );
58 is($date, $dateexpiry . 'T23:59:00', 'date expiry with useDaysMode to noDays');
59
60 # Let's add a special holiday on 2013-01-01. With ReturnBeforeExpiry and
61 # useDaysMode different from 'Days', return should forward the dateexpiry.
62 my $calendar = C4::Calendar->new(branchcode => $branchcode);
63 $calendar->insert_single_holiday(
64     day             => 1,
65     month           => 1,
66     year            => 2013,
67     title           =>'holidayTest',
68     description     => 'holidayDesc'
69 );
70 $date = C4::Circulation::CalcDateDue( $start_date, $itemtype, $branchcode, $borrower );
71 is($date, '2012-12-31T23:59:00', 'date expiry should be 2013-01-01 -1 day');
72 $calendar->insert_single_holiday(
73     day             => 31,
74     month           => 12,
75     year            => 2012,
76     title           =>'holidayTest',
77     description     => 'holidayDesc'
78 );
79 $date = C4::Circulation::CalcDateDue( $start_date, $itemtype, $branchcode, $borrower );
80 is($date, '2012-12-30T23:59:00', 'date expiry should be 2013-01-01 -2 day');
81
82
83 $date = C4::Circulation::CalcDateDue( $start_date, $itemtype, $branchcode, $borrower, 1 );
84
85
86 #Set syspref ReturnBeforeExpiry = 0 and useDaysMode = 'Days'
87 t::lib::Mocks::mock_preference('ReturnBeforeExpiry', 0);
88 t::lib::Mocks::mock_preference('useDaysMode', 'Days');
89
90 $borrower = {categorycode => 'B', dateexpiry => $dateexpiry};
91 $start_date = DateTime->new({year => 2013, month => 2, day => 9});
92 $date = C4::Circulation::CalcDateDue( $start_date, $itemtype, $branchcode, $borrower );
93 is($date, '2013-02-' . (9 + $issuelength) . 'T23:59:00', "date expiry ( 9 + $issuelength )");
94
95 $date = C4::Circulation::CalcDateDue( $start_date, $itemtype, $branchcode, $borrower, 1 );
96 is($date, '2013-02-' . (9 + $renewalperiod) . 'T23:59:00', "date expiry ( 9 + $renewalperiod )");
97
98 $cache->clear_from_cache('single_holidays');
99 $schema->storage->txn_rollback;