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 $ret = $item_hold->set_pickup_location({ library_id => $library_2->id });
129 is( ref($ret), 'Koha::Hold', 'self is returned' );
131 $item_hold->discard_changes;
132 is( $item_hold->branchcode, $library_2->id, 'Pickup location changed correctly' );
135 { $item_hold->set_pickup_location({ library_id => undef }); }
136 'Koha::Exceptions::MissingParameter',
137 'Exception thrown if missing parameter';
139 is( "$@", 'The library_id parameter is mandatory', 'Exception message is clear' );
141 $schema->storage->txn_rollback;
144 subtest 'is_pickup_location_valid() tests' => sub {
148 $schema->storage->txn_begin;
150 my $mock_biblio = Test::MockModule->new('Koha::Biblio');
151 my $mock_item = Test::MockModule->new('Koha::Item');
153 my $library_1 = $builder->build_object({ class => 'Koha::Libraries' });
154 my $library_2 = $builder->build_object({ class => 'Koha::Libraries' });
155 my $library_3 = $builder->build_object({ class => 'Koha::Libraries' });
157 # let's control what Koha::Biblio->pickup_locations returns, for testing
158 $mock_biblio->mock( 'pickup_locations', sub {
159 return Koha::Libraries->search( { branchcode => [ $library_2->branchcode, $library_3->branchcode ] } );
161 # let's mock what Koha::Item->pickup_locations returns, for testing
162 $mock_item->mock( 'pickup_locations', sub {
163 return Koha::Libraries->search( { branchcode => [ $library_2->branchcode, $library_3->branchcode ] } );
166 my $biblio = $builder->build_sample_biblio;
167 my $item = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
169 # Test biblio-level holds
170 my $biblio_hold = $builder->build_object(
172 class => "Koha::Holds",
174 biblionumber => $biblio->biblionumber,
175 branchcode => $library_3->branchcode,
181 ok( !$biblio_hold->is_pickup_location_valid({ library_id => $library_1->branchcode }), 'Pickup location invalid');
182 ok( $biblio_hold->is_pickup_location_valid({ library_id => $library_2->id }), 'Pickup location valid');
184 # Test item-level holds
185 my $item_hold = $builder->build_object(
187 class => "Koha::Holds",
189 biblionumber => $biblio->biblionumber,
190 branchcode => $library_3->branchcode,
191 itemnumber => $item->itemnumber,
196 ok( !$item_hold->is_pickup_location_valid({ library_id => $library_1->branchcode }), 'Pickup location invalid');
197 ok( $item_hold->is_pickup_location_valid({ library_id => $library_2->id }), 'Pickup location valid' );
199 $schema->storage->txn_rollback;