Bug 18321: Add tests
[koha.git] / t / db_dependent / Circulation / SwitchOnSiteCheckouts.t
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 3 of the License, or (at your option) any later
8 # version.
9 #
10 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License along
15 # with Koha; if not, see <http://www.gnu.org/licenses>.
16
17 use Modern::Perl;
18 use Test::More tests => 10;
19 use C4::Context;
20
21 use C4::Biblio;
22 use C4::Members;
23 use C4::Circulation;
24 use C4::Items;
25 use C4::Context;
26
27 use Koha::DateUtils qw( dt_from_string );
28 use Koha::Database;
29
30 use t::lib::TestBuilder;
31 use t::lib::Mocks;
32
33 my $schema = Koha::Database->new->schema;
34 $schema->storage->txn_begin;
35
36 our $dbh = C4::Context->dbh;
37
38 $dbh->do(q|DELETE FROM branch_item_rules|);
39 $dbh->do(q|DELETE FROM issues|);
40 $dbh->do(q|DELETE FROM branch_borrower_circ_rules|);
41 $dbh->do(q|DELETE FROM default_branch_circ_rules|);
42 $dbh->do(q|DELETE FROM default_circ_rules|);
43 $dbh->do(q|DELETE FROM default_branch_item_rules|);
44 $dbh->do(q|DELETE FROM issuingrules|);
45
46 my $builder = t::lib::TestBuilder->new();
47
48 my $branch = $builder->build({
49     source => 'Branch',
50 });
51
52 my $patron = $builder->build({
53     source => 'Borrower',
54     value => {
55         branchcode => $branch->{branchcode},
56         debarred => undef,
57     },
58 });
59
60 my $biblio = $builder->build({
61     source => 'Biblio',
62     value => {
63         branchcode => $branch->{branchcode},
64     },
65 });
66 my $item = $builder->build({
67     source => 'Item',
68     value => {
69         biblionumber => $biblio->{biblionumber},
70         homebranch => $branch->{branchcode},
71         holdingbranch => $branch->{branchcode},
72         notforloan => 0,
73         withdrawn => 0,
74         lost => 0,
75     },
76 });
77
78 my $issuingrule = $builder->build({
79     source => 'Issuingrule',
80     value => {
81         branchcode         => $branch->{branchcode},
82         categorycode       => '*',
83         itemtype           => '*',
84         maxissueqty        => 2,
85         maxonsiteissueqty  => 1,
86         lengthunit         => 'days',
87         issuelength        => 5,
88     },
89 });
90
91 C4::Context->_new_userenv ('DUMMY_SESSION_ID');
92 C4::Context->set_userenv($patron->{borrowernumber}, $patron->{userid}, 'usercnum', 'First name', 'Surname', $branch->{branchcode}, 'My Library', 0);
93
94 t::lib::Mocks::mock_preference('AllowTooManyOverride', 0);
95
96 # Add onsite checkout
97 C4::Circulation::AddIssue( $patron, $item->{barcode}, dt_from_string, undef, dt_from_string, undef, { onsite_checkout => 1 } );
98
99 my ( $impossible, $messages );
100 t::lib::Mocks::mock_preference('SwitchOnSiteCheckouts', 0);
101 ( $impossible, undef, undef, $messages ) = C4::Circulation::CanBookBeIssued( $patron, $item->{barcode} );
102 is( $impossible->{NO_RENEWAL_FOR_ONSITE_CHECKOUTS}, 1, 'Do not renew on-site checkouts' );
103
104 t::lib::Mocks::mock_preference('SwitchOnSiteCheckouts', 1);
105 ( $impossible, undef, undef, $messages ) = C4::Circulation::CanBookBeIssued( $patron, $item->{barcode} );
106 is( $messages->{ONSITE_CHECKOUT_WILL_BE_SWITCHED}, 1, 'If SwitchOnSiteCheckouts, switch the on-site checkout' );
107 is( exists $impossible->{TOO_MANY}, '', 'If SwitchOnSiteCheckouts, switch the on-site checkout' );
108 C4::Circulation::AddIssue( $patron, $item->{barcode}, undef, undef, undef, undef, { switch_onsite_checkout => 1 } );
109 my $issue = C4::Circulation::GetItemIssue( $item->{itemnumber} );
110 is( $issue->{onsite_checkout}, 0, 'The issue should have been switched to a regular checkout' );
111 my $five_days_after = dt_from_string->add( days => 5 )->set( hour => 23, minute => 59, second => 0 );
112 is( $issue->{date_due}, $five_days_after, 'The date_due should have been set depending on the circ rules when the on-site checkout has been switched' );
113
114 # Specific case
115 t::lib::Mocks::mock_preference('ConsiderOnSiteCheckoutsAsNormalCheckouts', 1);
116 my $another_item = $builder->build({
117     source => 'Item',
118     value => {
119         biblionumber => $biblio->{biblionumber},
120         homebranch => $branch->{branchcode},
121         holdingbranch => $branch->{branchcode},
122         notforloan => 0,
123         withdrawn => 0,
124         lost => 0,
125     },
126 });
127
128 C4::Circulation::AddIssue( $patron, $another_item->{barcode}, dt_from_string, undef, dt_from_string, undef, { onsite_checkout => 1 } );
129 ( $impossible, undef, undef, $messages ) = C4::Circulation::CanBookBeIssued( $patron, $another_item->{barcode} );
130 is( $messages->{ONSITE_CHECKOUT_WILL_BE_SWITCHED}, 1, 'Specific case 1 - Switch is allowed' );
131 is( exists $impossible->{TOO_MANY}, '', 'Specific case 1 - Switch is allowed' );
132
133 my $yet_another_item = $builder->build({
134     source => 'Item',
135     value => {
136         biblionumber => $biblio->{biblionumber},
137         homebranch => $branch->{branchcode},
138         holdingbranch => $branch->{branchcode},
139         notforloan => 0,
140         withdrawn => 0,
141         lost => 0,
142     },
143 });
144 ( $impossible, undef, undef, undef ) = C4::Circulation::CanBookBeIssued( $patron, $yet_another_item->{barcode} );
145 is( $impossible->{TOO_MANY}, 'TOO_MANY_CHECKOUTS', 'Not a specific case, $delta should not be incremented' );
146
147 $dbh->do(q|DELETE FROM issuingrules|);
148 my $borrower_circ_rule = $builder->build({
149     source => 'DefaultCircRule',
150     value => {
151         branchcode         => $branch->{branchcode},
152         categorycode       => '*',
153         maxissueqty        => 2,
154         maxonsiteissueqty  => 1,
155     },
156 });
157 ( $impossible, undef, undef, $messages ) = C4::Circulation::CanBookBeIssued( $patron, $another_item->{barcode} );
158 is( $messages->{ONSITE_CHECKOUT_WILL_BE_SWITCHED}, 1, 'Specific case 2 - Switch is allowed' );
159 is( exists $impossible->{TOO_MANY}, '', 'Specific case 2 - Switch is allowed' );
160
161 $schema->storage->txn_rollback;
162
163 1;