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) = @_;
65 if ( $syspref eq "ReturnBeforeExpiry") {
68 elsif ( $syspref eq "useDaysMode") {
76 my $dateexpiry = '2013-01-01';
78 my $borrower = {categorycode => 'B', dateexpiry => $dateexpiry};
79 my $start_date = DateTime->new({year => 2013, month => 2, day => 9});
80 $dbh->{mock_add_resultset} = $mock_loan_length;
81 my $date = C4::Circulation::CalcDateDue( $start_date, $itemtype, $branchcode, $borrower );
82 is($date, $dateexpiry . 'T23:59:00', 'date expiry');
83 $dbh->{mock_add_resultset} = $mock_loan_length;
84 $date = C4::Circulation::CalcDateDue( $start_date, $itemtype, $branchcode, $borrower, 1 );
87 #Set syspref ReturnBeforeExpiry = 1 and useDaysMode != 'Days'
88 $contextmodule->mock('preference', sub {
89 my ($self, $syspref) = @_;
90 if ( $syspref eq "ReturnBeforeExpiry") {
93 elsif ($syspref eq "useDaysMode") {
101 $borrower = {categorycode => 'B', dateexpiry => $dateexpiry};
102 $start_date = DateTime->new({year => 2013, month => 2, day => 9});
103 $dbh->{mock_add_resultset} = $mock_loan_length;
104 $date = C4::Circulation::CalcDateDue( $start_date, $itemtype, $branchcode, $borrower );
105 is($date, $dateexpiry . 'T23:59:00', 'date expiry');
107 $dbh->{mock_add_resultset} = $mock_loan_length;
108 $date = C4::Circulation::CalcDateDue( $start_date, $itemtype, $branchcode, $borrower, 1 );
111 #Set syspref ReturnBeforeExpiry = 0 and useDaysMode = 'Days'
112 $contextmodule->mock('preference', sub {
113 my ($self, $syspref) = @_;
114 if ( $syspref eq "ReturnBeforeExpiry") {
117 elsif ( $syspref eq "useDaysMode") {
125 $borrower = {categorycode => 'B', dateexpiry => $dateexpiry};
126 $start_date = DateTime->new({year => 2013, month => 2, day => 9});
127 $dbh->{mock_add_resultset} = $mock_loan_length;
128 $date = C4::Circulation::CalcDateDue( $start_date, $itemtype, $branchcode, $borrower );
129 is($date, '2013-02-' . (9 + $issuelength) . 'T23:59:00', "date expiry ( 9 + $issuelength )");
131 $dbh->{mock_add_resultset} = $mock_loan_length;
132 $date = C4::Circulation::CalcDateDue( $start_date, $itemtype, $branchcode, $borrower, 1 );
133 is($date, '2013-02-' . (9 + $renewalperiod) . 'T23:59:00', "date expiry ( 9 + $renewalperiod )");