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