3 # Copyright 2020 Koha Development team
5 # This file is part of Koha
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.
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.
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>.
22 use Test::More tests => 3;
27 use t::lib::TestBuilder;
31 my $schema = Koha::Database->new->schema;
32 my $builder = t::lib::TestBuilder->new;
34 subtest 'patron() tests' => sub {
38 $schema->storage->txn_begin;
40 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
41 my $hold = $builder->build_object(
43 class => 'Koha::Holds',
45 borrowernumber => $patron->borrowernumber
50 my $hold_patron = $hold->patron;
51 is( ref($hold_patron), 'Koha::Patron', 'Right type' );
52 is( $hold_patron->id, $patron->id, 'Right object' );
54 $schema->storage->txn_rollback;
57 subtest 'set_pickup_location() tests' => sub {
61 $schema->storage->txn_begin;
63 my $mock_biblio = Test::MockModule->new('Koha::Biblio');
64 my $mock_item = Test::MockModule->new('Koha::Item');
66 my $library_1 = $builder->build_object({ class => 'Koha::Libraries' });
67 my $library_2 = $builder->build_object({ class => 'Koha::Libraries' });
68 my $library_3 = $builder->build_object({ class => 'Koha::Libraries' });
70 # let's control what Koha::Biblio->pickup_locations returns, for testing
71 $mock_biblio->mock( 'pickup_locations', sub {
72 return Koha::Libraries->search( { branchcode => [ $library_2->branchcode, $library_3->branchcode ] } );
74 # let's mock what Koha::Item->pickup_locations returns, for testing
75 $mock_item->mock( 'pickup_locations', sub {
76 return Koha::Libraries->search( { branchcode => [ $library_2->branchcode, $library_3->branchcode ] } );
79 my $biblio = $builder->build_sample_biblio;
80 my $item = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
82 # Test biblio-level holds
83 my $biblio_hold = $builder->build_object(
85 class => "Koha::Holds",
87 biblionumber => $biblio->biblionumber,
88 branchcode => $library_3->branchcode,
95 { $biblio_hold->set_pickup_location({ library_id => $library_1->branchcode }); }
96 'Koha::Exceptions::Hold::InvalidPickupLocation',
97 'Exception thrown on invalid pickup location';
99 $biblio_hold->discard_changes;
100 is( $biblio_hold->branchcode, $library_3->branchcode, 'branchcode remains untouched' );
102 my $ret = $biblio_hold->set_pickup_location({ library_id => $library_2->id });
103 is( ref($ret), 'Koha::Hold', 'self is returned' );
105 $biblio_hold->discard_changes;
106 is( $biblio_hold->branchcode, $library_2->id, 'Pickup location changed correctly' );
108 # Test item-level holds
109 my $item_hold = $builder->build_object(
111 class => "Koha::Holds",
113 biblionumber => $biblio->biblionumber,
114 branchcode => $library_3->branchcode,
115 itemnumber => $item->itemnumber,
121 { $item_hold->set_pickup_location({ library_id => $library_1->branchcode }); }
122 'Koha::Exceptions::Hold::InvalidPickupLocation',
123 'Exception thrown on invalid pickup location';
125 $item_hold->discard_changes;
126 is( $item_hold->branchcode, $library_3->branchcode, 'branchcode remains untouched' );
128 $item_hold->set_pickup_location({ library_id => $library_1->branchcode, force => 1 });
129 $item_hold->discard_changes;
130 is( $item_hold->branchcode, $library_1->branchcode, 'branchcode changed because of \'force\'' );
132 $ret = $item_hold->set_pickup_location({ library_id => $library_2->id });
133 is( ref($ret), 'Koha::Hold', 'self is returned' );
135 $item_hold->discard_changes;
136 is( $item_hold->branchcode, $library_2->id, 'Pickup location changed correctly' );
139 { $item_hold->set_pickup_location({ library_id => undef }); }
140 'Koha::Exceptions::MissingParameter',
141 'Exception thrown if missing parameter';
143 is( "$@", 'The library_id parameter is mandatory', 'Exception message is clear' );
145 $schema->storage->txn_rollback;
148 subtest 'is_pickup_location_valid() tests' => sub {
152 $schema->storage->txn_begin;
154 my $mock_biblio = Test::MockModule->new('Koha::Biblio');
155 my $mock_item = Test::MockModule->new('Koha::Item');
157 my $library_1 = $builder->build_object({ class => 'Koha::Libraries' });
158 my $library_2 = $builder->build_object({ class => 'Koha::Libraries' });
159 my $library_3 = $builder->build_object({ class => 'Koha::Libraries' });
161 # let's control what Koha::Biblio->pickup_locations returns, for testing
162 $mock_biblio->mock( 'pickup_locations', sub {
163 return Koha::Libraries->search( { branchcode => [ $library_2->branchcode, $library_3->branchcode ] } );
165 # let's mock what Koha::Item->pickup_locations returns, for testing
166 $mock_item->mock( 'pickup_locations', sub {
167 return Koha::Libraries->search( { branchcode => [ $library_2->branchcode, $library_3->branchcode ] } );
170 my $biblio = $builder->build_sample_biblio;
171 my $item = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
173 # Test biblio-level holds
174 my $biblio_hold = $builder->build_object(
176 class => "Koha::Holds",
178 biblionumber => $biblio->biblionumber,
179 branchcode => $library_3->branchcode,
185 ok( !$biblio_hold->is_pickup_location_valid({ library_id => $library_1->branchcode }), 'Pickup location invalid');
186 ok( $biblio_hold->is_pickup_location_valid({ library_id => $library_2->id }), 'Pickup location valid');
188 # Test item-level holds
189 my $item_hold = $builder->build_object(
191 class => "Koha::Holds",
193 biblionumber => $biblio->biblionumber,
194 branchcode => $library_3->branchcode,
195 itemnumber => $item->itemnumber,
200 ok( !$item_hold->is_pickup_location_valid({ library_id => $library_1->branchcode }), 'Pickup location invalid');
201 ok( $item_hold->is_pickup_location_valid({ library_id => $library_2->id }), 'Pickup location valid' );
203 subtest 'pickup_locations() returning ->empty' => sub {
207 $schema->storage->txn_begin;
209 my $library = $builder->build_object({ class => 'Koha::Libraries' });
211 my $mock_item = Test::MockModule->new('Koha::Item');
212 $mock_item->mock( 'pickup_locations', sub { return Koha::Libraries->new->empty; } );
214 my $mock_biblio = Test::MockModule->new('Koha::Biblio');
215 $mock_biblio->mock( 'pickup_locations', sub { return Koha::Libraries->new->empty; } );
217 my $item = $builder->build_sample_item();
218 my $biblio = $item->biblio;
220 # Test biblio-level holds
221 my $biblio_hold = $builder->build_object(
223 class => "Koha::Holds",
225 biblionumber => $biblio->biblionumber,
231 ok( !$biblio_hold->is_pickup_location_valid({ library_id => $library->branchcode }), 'Pickup location invalid');
233 # Test item-level holds
234 my $item_hold = $builder->build_object(
236 class => "Koha::Holds",
238 biblionumber => $biblio->biblionumber,
239 itemnumber => $item->itemnumber,
244 ok( !$item_hold->is_pickup_location_valid({ library_id => $library->branchcode }), 'Pickup location invalid');
246 $schema->storage->txn_rollback;
249 $schema->storage->txn_rollback;