Bug 23404: add UT to show bug
[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::Circulation;
22 use C4::Biblio;
23 use C4::Items;
24 use C4::Members;
25 use C4::Context;
26
27 use Koha::DateUtils qw( dt_from_string );
28 use Koha::Database;
29 use Koha::Checkouts;
30 use Koha::CirculationRules;
31
32 use t::lib::TestBuilder;
33 use t::lib::Mocks;
34
35 my $schema = Koha::Database->new->schema;
36 $schema->storage->txn_begin;
37
38 our $dbh = C4::Context->dbh;
39
40 $dbh->do(q|DELETE FROM issues|);
41 $dbh->do(q|DELETE FROM issuingrules|);
42
43 my $builder = t::lib::TestBuilder->new();
44
45 my $branch = $builder->build({
46     source => 'Branch',
47 });
48
49 my $patron_category = $builder->build({ source => 'Category', value => { categorycode => 'NOT_X', category_type => 'P', enrolmentfee => 0 } });
50 my $patron = $builder->build_object({
51     class => 'Koha::Patrons',
52     value => {
53         branchcode => $branch->{branchcode},
54         debarred => undef,
55         categorycode => $patron_category->{categorycode},
56     },
57 });
58 my $patron_unblessed = $patron->unblessed;
59
60 my $biblio = $builder->build({
61     source => 'Biblio',
62     value => {
63         branchcode => $branch->{branchcode},
64     },
65 });
66 $builder->build(
67     {
68         source => 'Biblioitem',
69         value  => { biblionumber => $biblio->{biblionumber} }
70     }
71 );
72 my $item = $builder->build({
73     source => 'Item',
74     value => {
75         biblionumber => $biblio->{biblionumber},
76         homebranch => $branch->{branchcode},
77         holdingbranch => $branch->{branchcode},
78         notforloan => 0,
79         withdrawn => 0,
80         lost => 0,
81     },
82 });
83
84 my $issuingrule = $builder->build({
85     source => 'Issuingrule',
86     value => {
87         branchcode         => $branch->{branchcode},
88         categorycode       => '*',
89         itemtype           => '*',
90         lengthunit         => 'days',
91         issuelength        => 5,
92         hardduedate        => undef,
93         hardduedatecompare => 0,
94     },
95 });
96 Koha::CirculationRules->search()->delete();
97 Koha::CirculationRules->set_rules(
98     {
99         branchcode   => $branch->{branchcode},
100         categorycode => '*',
101         itemtype     => '*',
102         rules        => {
103             maxissueqty       => 2,
104             maxonsiteissueqty => 1,
105         }
106     }
107 );
108
109 t::lib::Mocks::mock_userenv({ patron => $patron });
110
111 t::lib::Mocks::mock_preference('AllowTooManyOverride', 0);
112
113 # Add onsite checkout
114 C4::Circulation::AddIssue( $patron_unblessed, $item->{barcode}, dt_from_string, undef, dt_from_string, undef, { onsite_checkout => 1 } );
115
116 my ( $impossible, $messages );
117 t::lib::Mocks::mock_preference('SwitchOnSiteCheckouts', 0);
118 ( $impossible, undef, undef, $messages ) = C4::Circulation::CanBookBeIssued( $patron, $item->{barcode} );
119 is( $impossible->{NO_RENEWAL_FOR_ONSITE_CHECKOUTS}, 1, 'Do not renew on-site checkouts' );
120
121 t::lib::Mocks::mock_preference('SwitchOnSiteCheckouts', 1);
122 ( $impossible, undef, undef, $messages ) = C4::Circulation::CanBookBeIssued( $patron, $item->{barcode} );
123 is( $messages->{ONSITE_CHECKOUT_WILL_BE_SWITCHED}, 1, 'If SwitchOnSiteCheckouts, switch the on-site checkout' );
124 is( exists $impossible->{TOO_MANY}, '', 'If SwitchOnSiteCheckouts, switch the on-site checkout' );
125 C4::Circulation::AddIssue( $patron_unblessed, $item->{barcode}, undef, undef, undef, undef, { switch_onsite_checkout => 1 } );
126 my $issue = Koha::Checkouts->find( { itemnumber => $item->{itemnumber} } );
127 is( $issue->onsite_checkout, 0, 'The issue should have been switched to a regular checkout' );
128 my $five_days_after = dt_from_string->add( days => 5 )->set( hour => 23, minute => 59, second => 0 );
129 is( dt_from_string($issue->date_due, 'sql'), $five_days_after, 'The date_due should have been set depending on the circ rules when the on-site checkout has been switched' );
130
131 # Specific case
132 t::lib::Mocks::mock_preference('ConsiderOnSiteCheckoutsAsNormalCheckouts', 1);
133 my $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
145 C4::Circulation::AddIssue( $patron_unblessed, $another_item->{barcode}, dt_from_string, undef, dt_from_string, undef, { onsite_checkout => 1 } );
146 ( $impossible, undef, undef, $messages ) = C4::Circulation::CanBookBeIssued( $patron, $another_item->{barcode} );
147 is( $messages->{ONSITE_CHECKOUT_WILL_BE_SWITCHED}, 1, 'Specific case 1 - Switch is allowed' );
148 is( exists $impossible->{TOO_MANY}, '', 'Specific case 1 - Switch is allowed' );
149
150 my $yet_another_item = $builder->build({
151     source => 'Item',
152     value => {
153         biblionumber => $biblio->{biblionumber},
154         homebranch => $branch->{branchcode},
155         holdingbranch => $branch->{branchcode},
156         notforloan => 0,
157         withdrawn => 0,
158         lost => 0,
159     },
160 });
161 ( $impossible, undef, undef, undef ) = C4::Circulation::CanBookBeIssued( $patron, $yet_another_item->{barcode} );
162 is( $impossible->{TOO_MANY}, 'TOO_MANY_CHECKOUTS', 'Not a specific case, $delta should not be incremented' );
163
164 Koha::CirculationRules->search()->delete();
165 Koha::CirculationRules->set_rules(
166     {
167         branchcode   => $branch->{branchcode},
168         categorycode => '*',
169         itemtype     => '*',
170         rules        => {
171             maxissueqty       => 2,
172             maxonsiteissueqty => 1,
173         }
174     }
175 );
176 ( $impossible, undef, undef, $messages ) = C4::Circulation::CanBookBeIssued( $patron, $another_item->{barcode} );
177 is( $messages->{ONSITE_CHECKOUT_WILL_BE_SWITCHED}, 1, 'Specific case 2 - Switch is allowed' );
178 is( exists $impossible->{TOO_MANY}, '', 'Specific case 2 - Switch is allowed' );
179
180 $schema->storage->txn_rollback;
181