Bug 18651: Fix tests if no circ rule exist
[koha.git] / t / db_dependent / Circulation / CalcDateDue.t
1 #!/usr/bin/perl
2
3 use Modern::Perl;
4
5 use Test::More tests => 5;
6 use Test::MockModule;
7 use DBI;
8 use DateTime;
9 use t::lib::Mocks;
10
11 BEGIN {
12     t::lib::Mocks::mock_dbh;
13 }
14
15 use_ok('C4::Circulation');
16
17 my $dbh = C4::Context->dbh();
18
19 my $issuelength = 10;
20 my $renewalperiod = 5;
21 my $lengthunit = 'days';
22
23 my $mock_undef = [
24     []
25 ];
26
27 my $mock_loan_length = [
28     ['issuelength', 'renewalperiod', 'lengthunit'],
29     [$issuelength, $renewalperiod, $lengthunit]
30 ];
31
32 my $categorycode = 'B';
33 my $itemtype = 'MX';
34 my $branchcode = 'FPL';
35
36 #Set syspref ReturnBeforeExpiry = 1 and useDaysMode = 'Days'
37 t::lib::Mocks::mock_preference('ReturnBeforeExpiry', 1);
38 t::lib::Mocks::mock_preference('useDaysMode', 'Days');
39
40 my $dateexpiry = '2013-01-01';
41
42 my $borrower = {categorycode => 'B', dateexpiry => $dateexpiry};
43 my $start_date = DateTime->new({year => 2013, month => 2, day => 9});
44 $dbh->{mock_add_resultset} = $mock_loan_length;
45 my $date = C4::Circulation::CalcDateDue( $start_date, $itemtype, $branchcode, $borrower );
46 is($date, $dateexpiry . 'T23:59:00', 'date expiry');
47 $dbh->{mock_add_resultset} = $mock_loan_length;
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 $dbh->{mock_add_resultset} = $mock_loan_length;
58 $date = C4::Circulation::CalcDateDue( $start_date, $itemtype, $branchcode, $borrower );
59 is($date, $dateexpiry . 'T23:59:00', 'date expiry');
60
61 $dbh->{mock_add_resultset} = $mock_loan_length;
62 $date = C4::Circulation::CalcDateDue( $start_date, $itemtype, $branchcode, $borrower, 1 );
63
64
65 #Set syspref ReturnBeforeExpiry = 0 and useDaysMode = 'Days'
66 t::lib::Mocks::mock_preference('ReturnBeforeExpiry', 0);
67 t::lib::Mocks::mock_preference('useDaysMode', 'Days');
68
69 $borrower = {categorycode => 'B', dateexpiry => $dateexpiry};
70 $start_date = DateTime->new({year => 2013, month => 2, day => 9});
71 $dbh->{mock_add_resultset} = $mock_loan_length;
72 $date = C4::Circulation::CalcDateDue( $start_date, $itemtype, $branchcode, $borrower );
73 is($date, '2013-02-' . (9 + $issuelength) . 'T23:59:00', "date expiry ( 9 + $issuelength )");
74
75 $dbh->{mock_add_resultset} = $mock_loan_length;
76 $date = C4::Circulation::CalcDateDue( $start_date, $itemtype, $branchcode, $borrower, 1 );
77 is($date, '2013-02-' . (9 + $renewalperiod) . 'T23:59:00', "date expiry ( 9 + $renewalperiod )");