5 use Test::More tests => 7;
10 my $contextmodule = new Test::MockModule('C4::Context');
11 $contextmodule->mock('_new_dbh', sub {
12 my $dbh = DBI->connect( 'DBI:Mock:', '', '' )
13 || die "Cannot create handle: $DBI::errstr\n";
17 use_ok('C4::Circulation');
19 my $dbh = C4::Context->dbh();
22 my $renewalperiod = 5;
23 my $lengthunit = 'days';
26 issuelength => $issuelength,
27 renewalperiod => $renewalperiod,
28 lengthunit => $lengthunit
42 my $mock_loan_length = [
43 ['issuelength', 'renewalperiod', 'lengthunit'],
44 [$issuelength, $renewalperiod, $lengthunit]
47 my $categorycode = 'B';
49 my $branchcode = 'FPL';
52 $dbh->{mock_add_resultset} = $mock_loan_length;
53 $loanlength = C4::Circulation::GetLoanLength($categorycode, $itemtype, $branchcode);
54 is_deeply($loanlength, $expected, 'first matches');
56 $dbh->{mock_add_resultset} = $mock_undef;
57 $loanlength = C4::Circulation::GetLoanLength($categorycode, $itemtype, $branchcode);
58 is_deeply($loanlength, $default, 'none matches');
62 #Set syspref ReturnBeforeExpiry = 1 and useDaysMode = 'Days'
63 $contextmodule->mock('preference', sub {
64 my ($self, $syspref) = @_;
66 when ("ReturnBeforeExpiry"){ return 1; }
67 when ("useDaysMode"){ return 'Days'; }
72 my $dateexpiry = '2013-01-01';
74 my $borrower = {categorycode => 'B', dateexpiry => $dateexpiry};
75 my $start_date = DateTime->new({year => 2013, month => 2, day => 9});
76 $dbh->{mock_add_resultset} = $mock_loan_length;
77 my $date = C4::Circulation::CalcDateDue( $start_date, $itemtype, $branchcode, $borrower );
78 is($date, $dateexpiry . 'T23:59:00', 'date expiry');
80 $dbh->{mock_add_resultset} = $mock_loan_length;
81 $date = C4::Circulation::CalcDateDue( $start_date, $itemtype, $branchcode, $borrower, 1 );
84 #Set syspref ReturnBeforeExpiry = 1 and useDaysMode != 'Days'
85 $contextmodule->mock('preference', sub {
86 my ($self, $syspref) = @_;
88 when ("ReturnBeforeExpiry"){ return 1; }
89 when ("useDaysMode"){ return 'noDays'; }
94 $borrower = {categorycode => 'B', dateexpiry => $dateexpiry};
95 $start_date = DateTime->new({year => 2013, month => 2, day => 9});
96 $dbh->{mock_add_resultset} = $mock_loan_length;
97 $date = C4::Circulation::CalcDateDue( $start_date, $itemtype, $branchcode, $borrower );
98 is($date, $dateexpiry . 'T23:59:00', 'date expiry');
100 $dbh->{mock_add_resultset} = $mock_loan_length;
101 $date = C4::Circulation::CalcDateDue( $start_date, $itemtype, $branchcode, $borrower, 1 );
104 #Set syspref ReturnBeforeExpiry = 0 and useDaysMode = 'Days'
105 $contextmodule->mock('preference', sub {
106 my ($self, $syspref) = @_;
108 when ("ReturnBeforeExpiry"){ return 0; }
109 when ("useDaysMode"){ return 'Days'; }
114 $borrower = {categorycode => 'B', dateexpiry => $dateexpiry};
115 $start_date = DateTime->new({year => 2013, month => 2, day => 9});
116 $dbh->{mock_add_resultset} = $mock_loan_length;
117 $date = C4::Circulation::CalcDateDue( $start_date, $itemtype, $branchcode, $borrower );
118 is($date, '2013-02-' . (9 + $issuelength) . 'T23:59:00', "date expiry ( 9 + $issuelength )");
120 $dbh->{mock_add_resultset} = $mock_loan_length;
121 $date = C4::Circulation::CalcDateDue( $start_date, $itemtype, $branchcode, $borrower, 1 );
122 is($date, '2013-02-' . (9 + $renewalperiod) . 'T23:59:00', "date expiry ( 9 + $renewalperiod )");