Bug 20144: [sql_modes] Fix date format in 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 use DateTime;
20
21 use C4::Circulation;
22 use Koha::Database;
23 use Koha::Patrons;
24 use Koha::Biblio;
25 use Koha::Item;
26 use Koha::Holds;
27 use Koha::Hold;
28 use t::lib::TestBuilder;
29 use t::lib::Mocks;
30
31 use Test::More tests => 17;
32
33 my $dbh    = C4::Context->dbh;
34 my $schema = Koha::Database->new()->schema();
35 my $builder = t::lib::TestBuilder->new;
36
37 # Start transaction
38 $dbh->{RaiseError} = 1;
39 $schema->storage->txn_begin();
40
41 $dbh->do('DELETE FROM issues');
42 $dbh->do('DELETE FROM issuingrules');
43 $dbh->do('DELETE FROM borrowers');
44 $dbh->do('DELETE FROM items');
45
46 my $now_value       = DateTime->now();
47 my $mocked_datetime = Test::MockModule->new('DateTime');
48 $mocked_datetime->mock( 'now', sub { return $now_value; } );
49
50 my $library  = $builder->build( { source => 'Branch' } );
51 my $category = $builder->build( { source => 'Category' } );
52 my $itemtype = $builder->build( { source => 'Itemtype' } )->{itemtype};
53
54 # Set userenv
55 C4::Context->_new_userenv('xxx');
56 C4::Context->set_userenv( 0, 0, 0, 'firstname', 'surname', $library->{branchcode}, 'Midway Public Library', '', '', '' );
57 is( C4::Context->userenv->{branch}, $library->{branchcode}, 'userenv set' );
58
59 my $patron_category = $builder->build({ source => 'Category', value => { category_type => 'P', enrolmentfee => 0 } });
60 my @patrons;
61 for my $i ( 1 .. 20 ) {
62     my $patron = Koha::Patron->new(
63         { cardnumber => $i, firstname => 'Kyle', surname => 'Hall', categorycode => $category->{categorycode}, branchcode => $library->{branchcode}, categorycode => $patron_category->{categorycode}, } )
64       ->store();
65     push( @patrons, $patron );
66 }
67
68 my $biblio = Koha::Biblio->new()->store();
69 my $biblioitem =
70   $schema->resultset('Biblioitem')->new( { biblionumber => $biblio->biblionumber } )->insert();
71
72 my @items;
73 for my $i ( 1 .. 10 ) {
74     my $item = Koha::Item->new(
75         {
76             biblionumber     => $biblio->id(),
77             biblioitemnumber => $biblioitem->id(),
78             barcode          => $i,
79             itype            => $itemtype
80         }
81     )->store();
82     push( @items, $item );
83 }
84
85 for my $i ( 0 .. 5 ) {
86     my $patron = $patrons[$i];
87     my $hold   = Koha::Hold->new(
88         {
89             borrowernumber => $patron->id,
90             biblionumber   => $biblio->id,
91             branchcode     => $library->{branchcode},
92         }
93     )->store();
94 }
95
96 $builder->build(
97     {
98         source => 'Issuingrule',
99         value => {
100             branchcode => '*',
101             categorycode => '*',
102             itemtype => '*',
103             issuelength => '14',
104             lengthunit => 'days',
105             reservesallowed => '99',
106         }
107     }
108 );
109
110 my $item   = pop(@items);
111 my $patron = pop(@patrons);
112
113 my $orig_due = C4::Circulation::CalcDateDue(
114     DateTime->now(time_zone => C4::Context->tz()),
115     $item->effective_itemtype,
116     $patron->branchcode,
117     $patron->unblessed
118 );
119
120 t::lib::Mocks::mock_preference( 'decreaseLoanHighHolds',               1 );
121 t::lib::Mocks::mock_preference( 'decreaseLoanHighHoldsDuration',       1 );
122 t::lib::Mocks::mock_preference( 'decreaseLoanHighHoldsValue',          1 );
123 t::lib::Mocks::mock_preference( 'decreaseLoanHighHoldsControl',        'static' );
124 t::lib::Mocks::mock_preference( 'decreaseLoanHighHoldsIgnoreStatuses', 'damaged,itemlost,notforloan,withdrawn' );
125
126 my $item_hr = { itemnumber => $item->id, biblionumber => $biblio->id, homebranch => $library->{branchcode}, holdingbranch => $library->{branchcode}, barcode => $item->barcode };
127 my $patron_hr = { borrowernumber => $patron->id, branchcode => $library->{branchcode} };
128
129 my $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
130 is( $data->{exceeded},        1,          "Static mode should exceed threshold" );
131 is( $data->{outstanding},     6,          "Should have 5 outstanding holds" );
132 is( $data->{duration},        1,          "Should have duration of 1" );
133 is( ref( $data->{due_date} ), 'DateTime', "due_date should be a DateTime object" );
134
135 my $duedate = $data->{due_date};
136 is($duedate->hour, $orig_due->hour, 'New due hour is equal to original due hour.');
137 is($duedate->min, $orig_due->min, 'New due minute is equal to original due minute.');
138 is($duedate->sec, 0, 'New due date second is zero.');
139
140 t::lib::Mocks::mock_preference( 'decreaseLoanHighHoldsControl', 'dynamic' );
141 $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
142 is( $data->{exceeded}, 0, "Should not exceed threshold" );
143
144 for my $i ( 5 .. 10 ) {
145     my $patron = $patrons[$i];
146     my $hold   = Koha::Hold->new(
147         {
148             borrowernumber => $patron->id,
149             biblionumber   => $biblio->id,
150             branchcode     => $library->{branchcode},
151         }
152     )->store();
153 }
154
155 $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
156 is( $data->{exceeded}, 1, "Should exceed threshold of 1" );
157
158 t::lib::Mocks::mock_preference( 'decreaseLoanHighHoldsValue', 2 );
159 $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
160 is( $data->{exceeded}, 0, "Should not exceed threshold of 2" );
161
162 my $unholdable = pop(@items);
163 $unholdable->damaged(-1);
164 $unholdable->store();
165
166 $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
167 is( $data->{exceeded}, 1, "Should exceed threshold with one damaged item" );
168
169 $unholdable->damaged(0);
170 $unholdable->itemlost(-1);
171 $unholdable->store();
172
173 $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
174 is( $data->{exceeded}, 1, "Should exceed threshold with one lost item" );
175
176 $unholdable->itemlost(0);
177 $unholdable->notforloan(-1);
178 $unholdable->store();
179
180 $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
181 is( $data->{exceeded}, 1, "Should exceed threshold with one notforloan item" );
182
183 $unholdable->notforloan(0);
184 $unholdable->withdrawn(-1);
185 $unholdable->store();
186
187 $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
188 is( $data->{exceeded}, 1, "Should exceed threshold with one withdrawn item" );
189
190 t::lib::Mocks::mock_preference('CircControl', 'PatronLibrary');
191
192 my $patron_object = Koha::Patrons->find( $patron_hr->{borrowernumber} );
193 my ( undef, $needsconfirmation ) = CanBookBeIssued( $patron_object, $item->barcode );
194 ok( $needsconfirmation->{HIGHHOLDS}, "High holds checkout needs confirmation" );
195
196 ( undef, $needsconfirmation ) = CanBookBeIssued( $patron_object, $item->barcode, undef, undef, undef, { override_high_holds => 1 } );
197 ok( !$needsconfirmation->{HIGHHOLDS}, "High holds checkout does not need confirmation" );
198
199 $schema->storage->txn_rollback();