5 use t::lib::Mocks::Context;
6 use Test::More tests => 7;
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";
18 use_ok('C4::Circulation');
20 my $dbh = C4::Context->dbh();
23 my $renewalperiod = 5;
24 my $lengthunit = 'days';
27 issuelength => $issuelength,
28 renewalperiod => $renewalperiod,
29 lengthunit => $lengthunit
43 my $mock_loan_length = [
44 ['issuelength', 'renewalperiod', 'lengthunit'],
45 [$issuelength, $renewalperiod, $lengthunit]
48 my $categorycode = 'B';
50 my $branchcode = 'FPL';
53 $dbh->{mock_add_resultset} = $mock_loan_length;
54 $loanlength = C4::Circulation::GetLoanLength($categorycode, $itemtype, $branchcode);
55 is_deeply($loanlength, $expected, 'first matches');
57 $dbh->{mock_add_resultset} = $mock_undef;
58 $loanlength = C4::Circulation::GetLoanLength($categorycode, $itemtype, $branchcode);
59 is_deeply($loanlength, $default, 'none matches');
63 #Set syspref ReturnBeforeExpiry = 1 and useDaysMode = 'Days'
64 $contextmodule->mock('preference', sub {
65 my ($self, $syspref) = @_;
67 when ("ReturnBeforeExpiry"){ return 1; }
68 when ("useDaysMode"){ return 'Days'; }
73 my $dateexpiry = '2013-01-01';
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');
81 $dbh->{mock_add_resultset} = $mock_loan_length;
82 $date = C4::Circulation::CalcDateDue( $start_date, $itemtype, $branchcode, $borrower, 1 );
85 #Set syspref ReturnBeforeExpiry = 1 and useDaysMode != 'Days'
86 $contextmodule->mock('preference', sub {
87 my ($self, $syspref) = @_;
89 when ("ReturnBeforeExpiry"){ return 1; }
90 when ("useDaysMode"){ return 'noDays'; }
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');
101 $dbh->{mock_add_resultset} = $mock_loan_length;
102 $date = C4::Circulation::CalcDateDue( $start_date, $itemtype, $branchcode, $borrower, 1 );
105 #Set syspref ReturnBeforeExpiry = 0 and useDaysMode = 'Days'
106 $contextmodule->mock('preference', sub {
107 my ($self, $syspref) = @_;
109 when ("ReturnBeforeExpiry"){ return 0; }
110 when ("useDaysMode"){ return 'Days'; }
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 )");
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 )");