Bug 29804: Regression tests
[koha.git] / t / db_dependent / Koha / Hold.t
1 #!/usr/bin/perl
2
3 # Copyright 2020 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 => 3;
23
24 use Test::Exception;
25 use Test::MockModule;
26
27 use t::lib::TestBuilder;
28
29 use Koha::Libraries;
30
31 my $schema  = Koha::Database->new->schema;
32 my $builder = t::lib::TestBuilder->new;
33
34 subtest 'patron() tests' => sub {
35
36     plan tests => 2;
37
38     $schema->storage->txn_begin;
39
40     my $patron = $builder->build_object({ class => 'Koha::Patrons' });
41     my $hold   = $builder->build_object(
42         {
43             class => 'Koha::Holds',
44             value => {
45                 borrowernumber => $patron->borrowernumber
46             }
47         }
48     );
49
50     my $hold_patron = $hold->patron;
51     is( ref($hold_patron), 'Koha::Patron', 'Right type' );
52     is( $hold_patron->id, $patron->id, 'Right object' );
53
54     $schema->storage->txn_rollback;
55 };
56
57 subtest 'set_pickup_location() tests' => sub {
58
59     plan tests => 11;
60
61     $schema->storage->txn_begin;
62
63     my $mock_biblio = Test::MockModule->new('Koha::Biblio');
64     my $mock_item   = Test::MockModule->new('Koha::Item');
65
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' });
69
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 ] } );
73     });
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 ] } );
77     });
78
79     my $biblio = $builder->build_sample_biblio;
80     my $item   = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
81
82     # Test biblio-level holds
83     my $biblio_hold = $builder->build_object(
84         {
85             class => "Koha::Holds",
86             value => {
87                 biblionumber => $biblio->biblionumber,
88                 branchcode   => $library_3->branchcode,
89                 itemnumber   => undef,
90             }
91         }
92     );
93
94     throws_ok
95         { $biblio_hold->set_pickup_location({ library_id => $library_1->branchcode }); }
96         'Koha::Exceptions::Hold::InvalidPickupLocation',
97         'Exception thrown on invalid pickup location';
98
99     $biblio_hold->discard_changes;
100     is( $biblio_hold->branchcode, $library_3->branchcode, 'branchcode remains untouched' );
101
102     my $ret = $biblio_hold->set_pickup_location({ library_id => $library_2->id });
103     is( ref($ret), 'Koha::Hold', 'self is returned' );
104
105     $biblio_hold->discard_changes;
106     is( $biblio_hold->branchcode, $library_2->id, 'Pickup location changed correctly' );
107
108     # Test item-level holds
109     my $item_hold = $builder->build_object(
110         {
111             class => "Koha::Holds",
112             value => {
113                 biblionumber => $biblio->biblionumber,
114                 branchcode   => $library_3->branchcode,
115                 itemnumber   => $item->itemnumber,
116             }
117         }
118     );
119
120     throws_ok
121         { $item_hold->set_pickup_location({ library_id => $library_1->branchcode }); }
122         'Koha::Exceptions::Hold::InvalidPickupLocation',
123         'Exception thrown on invalid pickup location';
124
125     $item_hold->discard_changes;
126     is( $item_hold->branchcode, $library_3->branchcode, 'branchcode remains untouched' );
127
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\'' );
131
132     $ret = $item_hold->set_pickup_location({ library_id => $library_2->id });
133     is( ref($ret), 'Koha::Hold', 'self is returned' );
134
135     $item_hold->discard_changes;
136     is( $item_hold->branchcode, $library_2->id, 'Pickup location changed correctly' );
137
138     throws_ok
139         { $item_hold->set_pickup_location({ library_id => undef }); }
140         'Koha::Exceptions::MissingParameter',
141         'Exception thrown if missing parameter';
142
143     is( "$@", 'The library_id parameter is mandatory', 'Exception message is clear' );
144
145     $schema->storage->txn_rollback;
146 };
147
148 subtest 'is_pickup_location_valid() tests' => sub {
149
150     plan tests => 5;
151
152     $schema->storage->txn_begin;
153
154     my $mock_biblio = Test::MockModule->new('Koha::Biblio');
155     my $mock_item   = Test::MockModule->new('Koha::Item');
156
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' });
160
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 ] } );
164     });
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 ] } );
168     });
169
170     my $biblio = $builder->build_sample_biblio;
171     my $item   = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
172
173     # Test biblio-level holds
174     my $biblio_hold = $builder->build_object(
175         {
176             class => "Koha::Holds",
177             value => {
178                 biblionumber => $biblio->biblionumber,
179                 branchcode   => $library_3->branchcode,
180                 itemnumber   => undef,
181             }
182         }
183     );
184
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');
187
188     # Test item-level holds
189     my $item_hold = $builder->build_object(
190         {
191             class => "Koha::Holds",
192             value => {
193                 biblionumber => $biblio->biblionumber,
194                 branchcode   => $library_3->branchcode,
195                 itemnumber   => $item->itemnumber,
196             }
197         }
198     );
199
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' );
202
203     subtest 'pickup_locations() returning ->empty' => sub {
204
205         plan tests => 2;
206
207         $schema->storage->txn_begin;
208
209         my $library = $builder->build_object({ class => 'Koha::Libraries' });
210
211         my $mock_item = Test::MockModule->new('Koha::Item');
212         $mock_item->mock( 'pickup_locations', sub { return Koha::Libraries->new->empty; } );
213
214         my $mock_biblio = Test::MockModule->new('Koha::Biblio');
215         $mock_biblio->mock( 'pickup_locations', sub { return Koha::Libraries->new->empty; } );
216
217         my $item   = $builder->build_sample_item();
218         my $biblio = $item->biblio;
219
220         # Test biblio-level holds
221         my $biblio_hold = $builder->build_object(
222             {
223                 class => "Koha::Holds",
224                 value => {
225                     biblionumber => $biblio->biblionumber,
226                     itemnumber   => undef,
227                 }
228             }
229         );
230
231         ok( !$biblio_hold->is_pickup_location_valid({ library_id => $library->branchcode }), 'Pickup location invalid');
232
233         # Test item-level holds
234         my $item_hold = $builder->build_object(
235             {
236                 class => "Koha::Holds",
237                 value => {
238                     biblionumber => $biblio->biblionumber,
239                     itemnumber   => $item->itemnumber,
240                 }
241             }
242         );
243
244         ok( !$item_hold->is_pickup_location_valid({ library_id => $library->branchcode }), 'Pickup location invalid');
245
246         $schema->storage->txn_rollback;
247     };
248
249     $schema->storage->txn_rollback;
250 };