Bug 17583: Mock the pref before tests
[koha.git] / t / db_dependent / DecreaseLoanHighHolds.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
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18 use Modern::Perl;
19
20 use C4::Circulation;
21 use Koha::Database;
22 use Koha::Patron;
23 use Koha::Biblio;
24 use Koha::Item;
25 use Koha::Holds;
26 use Koha::Hold;
27 use t::lib::TestBuilder;
28 use t::lib::Mocks;
29
30 use Test::More tests => 14;
31
32 my $dbh    = C4::Context->dbh;
33 my $schema = Koha::Database->new()->schema();
34 my $builder = t::lib::TestBuilder->new;
35
36 # Start transaction
37 $dbh->{RaiseError} = 1;
38 $schema->storage->txn_begin();
39
40 $dbh->do('DELETE FROM issues');
41 $dbh->do('DELETE FROM issuingrules');
42 $dbh->do('DELETE FROM borrowers');
43 $dbh->do('DELETE FROM items');
44
45 my $library  = $builder->build( { source => 'Branch' } );
46 my $category = $builder->build( { source => 'Category' } );
47 my $itemtype = $builder->build( { source => 'Itemtype' } )->{itemtype};
48
49 # Set userenv
50 C4::Context->_new_userenv('xxx');
51 C4::Context->set_userenv( 0, 0, 0, 'firstname', 'surname', $library->{branchcode}, 'Midway Public Library', '', '', '' );
52 is( C4::Context->userenv->{branch}, $library->{branchcode}, 'userenv set' );
53
54 my @patrons;
55 for my $i ( 1 .. 20 ) {
56     my $patron = Koha::Patron->new(
57         { cardnumber => $i, firstname => 'Kyle', surname => 'Hall', categorycode => $category->{categorycode}, branchcode => $library->{branchcode} } )
58       ->store();
59     push( @patrons, $patron );
60 }
61
62 my $biblio = Koha::Biblio->new()->store();
63 my $biblioitem =
64   $schema->resultset('Biblioitem')->new( { biblionumber => $biblio->biblionumber } )->insert();
65
66 my @items;
67 for my $i ( 1 .. 10 ) {
68     my $item = Koha::Item->new(
69         {
70             biblionumber     => $biblio->id(),
71             biblioitemnumber => $biblioitem->id(),
72             barcode          => $i,
73             itype            => $itemtype
74         }
75     )->store();
76     push( @items, $item );
77 }
78
79 for my $i ( 0 .. 5 ) {
80     my $patron = $patrons[$i];
81     my $hold   = Koha::Hold->new(
82         {
83             borrowernumber => $patron->id,
84             biblionumber   => $biblio->id,
85             branchcode     => $library->{branchcode},
86         }
87     )->store();
88 }
89
90 $builder->build(
91     {
92         source => 'Issuingrule',
93         value => {
94             branchcode => '*',
95             categorycode => '*',
96             itemtype => '*',
97             issuelength => '14',
98             lengthunit => 'days',
99             reservesallowed => '99',
100         }
101     }
102 );
103
104 my $item   = pop(@items);
105 my $patron = pop(@patrons);
106
107 t::lib::Mocks::mock_preference( 'decreaseLoanHighHolds',               1 );
108 t::lib::Mocks::mock_preference( 'decreaseLoanHighHoldsDuration',       1 );
109 t::lib::Mocks::mock_preference( 'decreaseLoanHighHoldsValue',          1 );
110 t::lib::Mocks::mock_preference( 'decreaseLoanHighHoldsControl',        'static' );
111 t::lib::Mocks::mock_preference( 'decreaseLoanHighHoldsIgnoreStatuses', 'damaged,itemlost,notforloan,withdrawn' );
112
113 my $item_hr = { itemnumber => $item->id, biblionumber => $biblio->id, homebranch => $library->{branchcode}, holdingbranch => $library->{branchcode}, barcode => $item->barcode };
114 my $patron_hr = { borrowernumber => $patron->id, branchcode => $library->{branchcode} };
115
116 my $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
117 is( $data->{exceeded},        1,          "Static mode should exceed threshold" );
118 is( $data->{outstanding},     6,          "Should have 5 outstanding holds" );
119 is( $data->{duration},        1,          "Should have duration of 1" );
120 is( ref( $data->{due_date} ), 'DateTime', "due_date should be a DateTime object" );
121
122 t::lib::Mocks::mock_preference( 'decreaseLoanHighHoldsControl', 'dynamic' );
123 $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
124 is( $data->{exceeded}, 0, "Should not exceed threshold" );
125
126 for my $i ( 5 .. 10 ) {
127     my $patron = $patrons[$i];
128     my $hold   = Koha::Hold->new(
129         {
130             borrowernumber => $patron->id,
131             biblionumber   => $biblio->id,
132             branchcode     => $library->{branchcode},
133         }
134     )->store();
135 }
136
137 $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
138 is( $data->{exceeded}, 1, "Should exceed threshold of 1" );
139
140 t::lib::Mocks::mock_preference( 'decreaseLoanHighHoldsValue', 2 );
141 $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
142 is( $data->{exceeded}, 0, "Should not exceed threshold of 2" );
143
144 my $unholdable = pop(@items);
145 $unholdable->damaged(-1);
146 $unholdable->store();
147
148 $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
149 is( $data->{exceeded}, 1, "Should exceed threshold with one damaged item" );
150
151 $unholdable->damaged(0);
152 $unholdable->itemlost(-1);
153 $unholdable->store();
154
155 $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
156 is( $data->{exceeded}, 1, "Should exceed threshold with one lost item" );
157
158 $unholdable->itemlost(0);
159 $unholdable->notforloan(-1);
160 $unholdable->store();
161
162 $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
163 is( $data->{exceeded}, 1, "Should exceed threshold with one notforloan item" );
164
165 $unholdable->notforloan(0);
166 $unholdable->withdrawn(-1);
167 $unholdable->store();
168
169 $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
170 is( $data->{exceeded}, 1, "Should exceed threshold with one withdrawn item" );
171
172 t::lib::Mocks::mock_preference('CircControl', 'PatronLibrary');
173
174 my ( undef, $needsconfirmation ) = CanBookBeIssued( $patron_hr, $item->barcode );
175 ok( $needsconfirmation->{HIGHHOLDS}, "High holds checkout needs confirmation" );
176
177 ( undef, $needsconfirmation ) = CanBookBeIssued( $patron_hr, $item->barcode, undef, undef, undef, { override_high_holds => 1 } );
178 ok( !$needsconfirmation->{HIGHHOLDS}, "High holds checkout does not need confirmation" );
179
180 $schema->storage->txn_rollback();
181 1;