Bug 11580: Add one more test and clear the cache
[koha.git] / t / db_dependent / Circulation / CalcDateDue.t
blobc6b6b5290110e7b975c13b55e89a25367f355064
1 #!/usr/bin/perl
3 use Modern::Perl;
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;
13 use_ok('C4::Circulation');
15 my $schema = Koha::Database->new->schema;
16 $schema->storage->txn_begin;
17 my $builder = t::lib::TestBuilder->new;
19 my $categorycode = 'B';
20 my $itemtype = 'MX';
21 my $branchcode = 'FPL';
22 my $issuelength = 10;
23 my $renewalperiod = 5;
24 my $lengthunit = 'days';
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 });
35 #Set syspref ReturnBeforeExpiry = 1 and useDaysMode = 'Days'
36 t::lib::Mocks::mock_preference('ReturnBeforeExpiry', 1);
37 t::lib::Mocks::mock_preference('useDaysMode', 'Days');
39 my $cache = Koha::Caches->get_instance();
40 $cache->clear_from_cache('single_holidays');
42 my $dateexpiry = '2013-01-01';
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 );
51 #Set syspref ReturnBeforeExpiry = 1 and useDaysMode != 'Days'
52 t::lib::Mocks::mock_preference('ReturnBeforeExpiry', 1);
53 t::lib::Mocks::mock_preference('useDaysMode', 'noDays');
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');
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'
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'
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');
83 $date = C4::Circulation::CalcDateDue( $start_date, $itemtype, $branchcode, $borrower, 1 );
86 #Set syspref ReturnBeforeExpiry = 0 and useDaysMode = 'Days'
87 t::lib::Mocks::mock_preference('ReturnBeforeExpiry', 0);
88 t::lib::Mocks::mock_preference('useDaysMode', 'Days');
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 )");
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 )");
98 $cache->clear_from_cache('single_holidays');
99 $schema->storage->txn_rollback;