Bug 27209: Unit 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 => 2;
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 => 10;
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     $ret = $item_hold->set_pickup_location({ library_id => $library_2->id });
129     is( ref($ret), 'Koha::Hold', 'self is returned' );
130
131     $item_hold->discard_changes;
132     is( $item_hold->branchcode, $library_2->id, 'Pickup location changed correctly' );
133
134     throws_ok
135         { $item_hold->set_pickup_location({ library_id => undef }); }
136         'Koha::Exceptions::MissingParameter',
137         'Exception thrown if missing parameter';
138
139     is( "$@", 'The library_id parameter is mandatory', 'Exception message is clear' );
140
141     $schema->storage->txn_rollback;
142 };