Bug 8300: Add mechanized unit test for batch import
[koha.git] / t / db_dependent / Circulation_issuingrules.t
1 #!/usr/bin/perl
2
3 use Modern::Perl;
4
5 use t::lib::Mocks::Context;
6 use Test::More tests => 7;
7 use Test::MockModule;
8 use DBI;
9 use DateTime;
10
11 my $contextmodule = new Test::MockModule('C4::Context');
12 $contextmodule->mock('_new_dbh', sub {
13     my $dbh = DBI->connect( 'DBI:Mock:', '', '' )
14     || die "Cannot create handle: $DBI::errstr\n";
15     return $dbh
16 });
17
18 use_ok('C4::Circulation');
19
20 my $dbh = C4::Context->dbh();
21
22 my $issuelength = 10;
23 my $renewalperiod = 5;
24 my $lengthunit = 'days';
25
26 my $expected = {
27     issuelength => $issuelength,
28     renewalperiod => $renewalperiod,
29     lengthunit => $lengthunit
30 };
31
32 my $default = {
33     issuelength => 21,
34     renewalperiod => 21,
35     lengthunit => 'days'
36 };
37
38 my $loanlength;
39 my $mock_undef = [
40     []
41 ];
42
43 my $mock_loan_length = [
44     ['issuelength', 'renewalperiod', 'lengthunit'],
45     [$issuelength, $renewalperiod, $lengthunit]
46 ];
47
48 my $categorycode = 'B';
49 my $itemtype = 'MX';
50 my $branchcode = 'FPL';
51
52 #=== GetLoanLength
53 $dbh->{mock_add_resultset} = $mock_loan_length;
54 $loanlength = C4::Circulation::GetLoanLength($categorycode, $itemtype, $branchcode);
55 is_deeply($loanlength, $expected, 'first matches');
56
57 $dbh->{mock_add_resultset} = $mock_undef;
58 $loanlength = C4::Circulation::GetLoanLength($categorycode, $itemtype, $branchcode);
59 is_deeply($loanlength, $default, 'none matches');
60
61 #=== CalcDateDue
62
63 #Set syspref ReturnBeforeExpiry = 1 and useDaysMode = 'Days'
64 $contextmodule->mock('preference', sub {
65     my ($self, $syspref) = @_;
66     given ( $syspref ) {
67         when ("ReturnBeforeExpiry"){ return 1; }
68         when ("useDaysMode"){ return 'Days'; }
69         default{ return; }
70     }
71 });
72
73 my $dateexpiry = '2013-01-01';
74
75 my $borrower = {categorycode => 'B', dateexpiry => $dateexpiry};
76 my $start_date = DateTime->new({year => 2013, month => 2, day => 9});
77 $dbh->{mock_add_resultset} = $mock_loan_length;
78 my $date = C4::Circulation::CalcDateDue( $start_date, $itemtype, $branchcode, $borrower );
79 is($date, $dateexpiry . 'T23:59:00', 'date expiry');
80
81 $dbh->{mock_add_resultset} = $mock_loan_length;
82 $date = C4::Circulation::CalcDateDue( $start_date, $itemtype, $branchcode, $borrower, 1 );
83
84
85 #Set syspref ReturnBeforeExpiry = 1 and useDaysMode != 'Days'
86 $contextmodule->mock('preference', sub {
87     my ($self, $syspref) = @_;
88     given ( $syspref ) {
89         when ("ReturnBeforeExpiry"){ return 1; }
90         when ("useDaysMode"){ return 'noDays'; }
91         default{ return; }
92     }
93 });
94
95 $borrower = {categorycode => 'B', dateexpiry => $dateexpiry};
96 $start_date = DateTime->new({year => 2013, month => 2, day => 9});
97 $dbh->{mock_add_resultset} = $mock_loan_length;
98 $date = C4::Circulation::CalcDateDue( $start_date, $itemtype, $branchcode, $borrower );
99 is($date, $dateexpiry . 'T23:59:00', 'date expiry');
100
101 $dbh->{mock_add_resultset} = $mock_loan_length;
102 $date = C4::Circulation::CalcDateDue( $start_date, $itemtype, $branchcode, $borrower, 1 );
103
104
105 #Set syspref ReturnBeforeExpiry = 0 and useDaysMode = 'Days'
106 $contextmodule->mock('preference', sub {
107     my ($self, $syspref) = @_;
108     given ( $syspref ) {
109         when ("ReturnBeforeExpiry"){ return 0; }
110         when ("useDaysMode"){ return 'Days'; }
111         default{ return; }
112     }
113 });
114
115 $borrower = {categorycode => 'B', dateexpiry => $dateexpiry};
116 $start_date = DateTime->new({year => 2013, month => 2, day => 9});
117 $dbh->{mock_add_resultset} = $mock_loan_length;
118 $date = C4::Circulation::CalcDateDue( $start_date, $itemtype, $branchcode, $borrower );
119 is($date, '2013-02-' . (9 + $issuelength) . 'T23:59:00', "date expiry ( 9 + $issuelength )");
120
121 $dbh->{mock_add_resultset} = $mock_loan_length;
122 $date = C4::Circulation::CalcDateDue( $start_date, $itemtype, $branchcode, $borrower, 1 );
123 is($date, '2013-02-' . (9 + $renewalperiod) . 'T23:59:00', "date expiry ( 9 + $renewalperiod )");