Bug 24657: Fix t/db_dependent/Koha/Item.t and t/db_dependent/Holds.t
[koha.git] / t / db_dependent / Koha / Item.t
1 #!/usr/bin/perl
2
3 # Copyright 2019 Koha Development team
4 #
5 # This file is part of Koha
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use Modern::Perl;
21
22 use Test::More tests => 4;
23
24 use C4::Biblio;
25
26 use Koha::Items;
27 use Koha::Database;
28
29 use t::lib::TestBuilder;
30 use t::lib::Mocks;
31
32 my $schema  = Koha::Database->new->schema;
33 my $builder = t::lib::TestBuilder->new;
34
35 subtest 'hidden_in_opac() tests' => sub {
36
37     plan tests => 4;
38
39     $schema->storage->txn_begin;
40
41     my $item  = $builder->build_sample_item({ itemlost => 2 });
42     my $rules = {};
43
44     # disable hidelostitems as it interteres with OpachiddenItems for the calculation
45     t::lib::Mocks::mock_preference( 'hidelostitems', 0 );
46
47     ok( !$item->hidden_in_opac, 'No rules passed, shouldn\'t hide' );
48     ok( !$item->hidden_in_opac({ rules => $rules }), 'Empty rules passed, shouldn\'t hide' );
49
50     # enable hidelostitems to verify correct behaviour
51     t::lib::Mocks::mock_preference( 'hidelostitems', 1 );
52     ok( $item->hidden_in_opac, 'Even with no rules, item should hide because of hidelostitems syspref' );
53
54     # disable hidelostitems
55     t::lib::Mocks::mock_preference( 'hidelostitems', 0 );
56     my $withdrawn = $item->withdrawn + 1; # make sure this attribute doesn't match
57
58     $rules = { withdrawn => [$withdrawn], itype => [ $item->itype ] };
59
60     ok( $item->hidden_in_opac({ rules => $rules }), 'Rule matching itype passed, should hide' );
61
62
63
64     $schema->storage->txn_rollback;
65 };
66
67 subtest 'has_pending_hold() tests' => sub {
68
69     plan tests => 2;
70
71     $schema->storage->txn_begin;
72
73     my $dbh = C4::Context->dbh;
74     my $item  = $builder->build_sample_item({ itemlost => 0 });
75     my $itemnumber = $item->itemnumber;
76
77     $dbh->do("INSERT INTO tmp_holdsqueue (surname,borrowernumber,itemnumber) VALUES ('Clamp',42,$itemnumber)");
78     ok( $item->has_pending_hold, "Yes, we have a pending hold");
79     $dbh->do("DELETE FROM tmp_holdsqueue WHERE itemnumber=$itemnumber");
80     ok( !$item->has_pending_hold, "We don't have a pending hold if nothing in the tmp_holdsqueue");
81
82     $schema->storage->txn_rollback;
83 };
84
85 subtest "as_marc_field() tests" => sub {
86
87     my $mss = C4::Biblio::GetMarcSubfieldStructure( '', { unsafe => 1 } );
88
89     my @schema_columns = $schema->resultset('Item')->result_source->columns;
90     my @mapped_columns = grep { exists $mss->{'items.'.$_} } @schema_columns;
91
92     plan tests => 2 * (scalar @mapped_columns + 1) + 1;
93
94     $schema->storage->txn_begin;
95
96     my $item = $builder->build_sample_item;
97
98     # Tests with the mss parameter
99     my $marc_field = $item->as_marc_field({ mss => $mss });
100
101     is(
102         $marc_field->tag,
103         $mss->{'items.itemnumber'}[0]->{tagfield},
104         'Generated field set the right tag number'
105     );
106
107     foreach my $column ( @mapped_columns ) {
108         my $tagsubfield = $mss->{ 'items.' . $column }[0]->{tagsubfield};
109         is( $marc_field->subfield($tagsubfield),
110             $item->$column, "Value is mapped correctly for column $column" );
111     }
112
113     # Tests without the mss parameter
114     $marc_field = $item->as_marc_field();
115
116     is(
117         $marc_field->tag,
118         $mss->{'items.itemnumber'}[0]->{tagfield},
119         'Generated field set the right tag number'
120     );
121
122     foreach my $column (@mapped_columns) {
123         my $tagsubfield = $mss->{ 'items.' . $column }[0]->{tagsubfield};
124         is( $marc_field->subfield($tagsubfield),
125             $item->$column, "Value is mapped correctly for column $column" );
126     }
127
128     my $unmapped_subfield = Koha::MarcSubfieldStructure->new(
129         {
130             frameworkcode => '',
131             tagfield      => $mss->{'items.itemnumber'}[0]->{tagfield},
132             tagsubfield   => 'X',
133         }
134     )->store;
135
136     $mss = C4::Biblio::GetMarcSubfieldStructure( '', { unsafe => 0 } );
137     my @unlinked_subfields;
138     push @unlinked_subfields, X => 'Something weird';
139     $item->more_subfields_xml( C4::Items::_get_unlinked_subfields_xml( \@unlinked_subfields ) )->store;
140
141     $marc_field = $item->as_marc_field;
142     is( scalar $marc_field->subfield('X'), 'Something weird', 'more_subfield_xml is considered' );
143
144     $schema->storage->txn_rollback;
145 };
146
147 subtest 'pickup_locations' => sub {
148     plan tests => 114;
149
150     $schema->storage->txn_begin;
151
152     my $dbh = C4::Context->dbh;
153
154     # Cleanup database
155     Koha::Holds->search->delete;
156     Koha::Patrons->search->delete;
157     Koha::Items->search->delete;
158     Koha::Libraries->search->delete;
159     $dbh->do('DELETE FROM issues');
160     Koha::CirculationRules->search->delete;
161     Koha::CirculationRules->set_rules(
162         {
163             categorycode => undef,
164             itemtype     => undef,
165             branchcode   => undef,
166             rules        => {
167                 reservesallowed => 25,
168             }
169         }
170     );
171
172     my $root1 = $builder->build_object( { class => 'Koha::Library::Groups', value => { ft_local_hold_group => 1, branchcode => undef } } );
173     my $root2 = $builder->build_object( { class => 'Koha::Library::Groups', value => { ft_local_hold_group => 1, branchcode => undef } } );
174     my $library1 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 1, branchcode => 'TEST1' } } );
175     my $library2 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 1, branchcode => 'TEST2' } } );
176     my $library3 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 0, branchcode => 'TEST3' } } );
177     my $library4 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 1, branchcode => 'TEST4' } } );
178     my $group1_1 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root1->id, branchcode => $library1->branchcode } } );
179     my $group1_2 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root1->id, branchcode => $library2->branchcode } } );
180
181     my $group2_1 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root2->id, branchcode => $library3->branchcode } } );
182     my $group2_2 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root2->id, branchcode => $library4->branchcode } } );
183
184     my $biblioitem  = $builder->build( { source => 'Biblioitem' } );
185
186     my $item1  = Koha::Item->new({
187         biblionumber     => $biblioitem->{biblionumber},
188         biblioitemnumber => $biblioitem->{biblioitemnumber},
189         homebranch       => $library1->branchcode,
190         holdingbranch    => $library2->branchcode,
191         barcode          => '1',
192         itype            => 'test',
193     })->store;
194
195     my $item3  = Koha::Item->new({
196         biblionumber     => $biblioitem->{biblionumber},
197         biblioitemnumber => $biblioitem->{biblioitemnumber},
198         homebranch       => $library3->branchcode,
199         holdingbranch    => $library4->branchcode,
200         barcode          => '3',
201         itype            => 'test',
202     })->store;
203
204     my $patron1 = $builder->build_object( { class => 'Koha::Patrons', value => { branchcode => $library1->branchcode, firstname => '1' } } );
205     my $patron4 = $builder->build_object( { class => 'Koha::Patrons', value => { branchcode => $library4->branchcode, firstname => '4' } } );
206
207     my $results = {
208         "1-1-1-any" => 3,
209         "1-1-1-holdgroup" => 2,
210         "1-1-1-patrongroup" => 2,
211         "1-1-1-homebranch" => 1,
212         "1-1-1-holdingbranch" => 1,
213         "1-1-2-any" => 3,
214         "1-1-2-holdgroup" => 2,
215         "1-1-2-patrongroup" => 2,
216         "1-1-2-homebranch" => 1,
217         "1-1-2-holdingbranch" => 1,
218         "1-1-3-any" => 3,
219         "1-1-3-holdgroup" => 2,
220         "1-1-3-patrongroup" => 2,
221         "1-1-3-homebranch" => 1,
222         "1-1-3-holdingbranch" => 1,
223         "1-4-1-any" => 0,
224         "1-4-1-holdgroup" => 0,
225         "1-4-1-patrongroup" => 0,
226         "1-4-1-homebranch" => 0,
227         "1-4-1-holdingbranch" => 0,
228         "1-4-2-any" => 3,
229         "1-4-2-holdgroup" => 2,
230         "1-4-2-patrongroup" => 1,
231         "1-4-2-homebranch" => 1,
232         "1-4-2-holdingbranch" => 1,
233         "1-4-3-any" => 0,
234         "1-4-3-holdgroup" => 0,
235         "1-4-3-patrongroup" => 0,
236         "1-4-3-homebranch" => 0,
237         "1-4-3-holdingbranch" => 0,
238         "3-1-1-any" => 0,
239         "3-1-1-holdgroup" => 0,
240         "3-1-1-patrongroup" => 0,
241         "3-1-1-homebranch" => 0,
242         "3-1-1-holdingbranch" => 0,
243         "3-1-2-any" => 3,
244         "3-1-2-holdgroup" => 1,
245         "3-1-2-patrongroup" => 2,
246         "3-1-2-homebranch" => 0,
247         "3-1-2-holdingbranch" => 1,
248         "3-1-3-any" => 0,
249         "3-1-3-holdgroup" => 0,
250         "3-1-3-patrongroup" => 0,
251         "3-1-3-homebranch" => 0,
252         "3-1-3-holdingbranch" => 0,
253         "3-4-1-any" => 0,
254         "3-4-1-holdgroup" => 0,
255         "3-4-1-patrongroup" => 0,
256         "3-4-1-homebranch" => 0,
257         "3-4-1-holdingbranch" => 0,
258         "3-4-2-any" => 3,
259         "3-4-2-holdgroup" => 1,
260         "3-4-2-patrongroup" => 1,
261         "3-4-2-homebranch" => 0,
262         "3-4-2-holdingbranch" => 1,
263         "3-4-3-any" => 3,
264         "3-4-3-holdgroup" => 1,
265         "3-4-3-patrongroup" => 1,
266         "3-4-3-homebranch" => 0,
267         "3-4-3-holdingbranch" => 1
268     };
269
270     sub _doTest {
271         my ( $item, $patron, $ha, $hfp, $results ) = @_;
272
273         Koha::CirculationRules->set_rules(
274             {
275                 branchcode => undef,
276                 itemtype   => undef,
277                 rules => {
278                     holdallowed => $ha,
279                     hold_fulfillment_policy => $hfp,
280                     returnbranch => 'any'
281                 }
282             }
283         );
284         my @pl = $item->pickup_locations( { patron => $patron} );
285         my $ha_value=$ha==3?'holdgroup':($ha==2?'any':'homebranch');
286
287         foreach my $pickup_location (@pl) {
288             is( ref($pickup_location), 'Koha::Library', 'Object type is correct' );
289         }
290         ok(
291             scalar(@pl) == $results->{
292                     $item->barcode . '-'
293                   . $patron->firstname . '-'
294                   . $ha . '-'
295                   . $hfp
296             },
297             'item'
298               . $item->barcode
299               . ', patron'
300               . $patron->firstname
301               . ', holdallowed: '
302               . $ha_value
303               . ', hold_fulfillment_policy: '
304               . $hfp
305               . ' should return '
306               . $results->{
307                     $item->barcode . '-'
308                   . $patron->firstname . '-'
309                   . $ha . '-'
310                   . $hfp
311               }
312               . ' but returns '
313               . scalar(@pl)
314         );
315
316     }
317
318
319     foreach my $item ($item1, $item3) {
320         foreach my $patron ($patron1, $patron4) {
321             #holdallowed 1: homebranch, 2: any, 3: holdgroup
322             foreach my $ha (1, 2, 3) {
323                 foreach my $hfp ('any', 'holdgroup', 'patrongroup', 'homebranch', 'holdingbranch') {
324                     _doTest($item, $patron, $ha, $hfp, $results);
325                 }
326             }
327         }
328     }
329
330     $schema->storage->txn_rollback;
331 };