Bug 34932: Patron.t - Pass borrowernumber of manager to userenv
[koha.git] / t / db_dependent / Koha / Club / Hold.t
1 #!/usr/bin/perl
2
3 # Copyright 2015 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 => 1;
23 use Test::Exception;
24 use Try::Tiny;
25
26 use Koha::Club::Hold;
27 use Koha::Club::Hold::PatronHolds;
28 use Koha::Holds;
29 use Koha::Database;
30 use Koha::DateUtils qw(dt_from_string);
31 use Scalar::Util qw(blessed);
32
33 use t::lib::TestBuilder;
34
35 my $builder = t::lib::TestBuilder->new;
36 my $schema = Koha::Database->new->schema;
37
38 subtest 'add' => sub {
39
40     plan tests => 9;
41
42     $schema->storage->txn_begin;
43
44     my $club = $builder->build_object({ class => 'Koha::Clubs' });
45     my $library = $builder->build_object({ class => 'Koha::Libraries', value => { pickup_location => 1 } });
46     my $item1 = $builder->build_sample_item({ library => $library->branchcode });
47     my $item2 = $builder->build_sample_item({ library => $library->branchcode });
48
49     throws_ok {
50         Koha::Club::Hold::add(
51             {
52                 club_id => $club->id
53             }
54         );
55     }
56     'Koha::Exceptions::MissingParameter',
57       'Exception thrown when biblio_id is passed';
58
59     like( "$@", qr/The biblio_id parameter is mandatory/ );
60
61     throws_ok {
62         Koha::Club::Hold::add(
63             {
64                 biblio_id => $item1->biblionumber
65             }
66         );
67     }
68     'Koha::Exceptions::MissingParameter',
69       'Exception thrown when club_id is passed';
70
71     like( "$@", qr/The club_id parameter is mandatory/ );
72
73     throws_ok {
74         Koha::Club::Hold::add(
75             {
76                 club_id           => $club->id,
77                 biblio_id         => $item1->biblionumber,
78                 pickup_library_id => $library->branchcode
79             }
80         );
81     }
82     'Koha::Exceptions::ClubHold::NoPatrons',
83       'Exception thrown when no patron is enrolled in club';
84
85     my $patron = $builder->build_object(
86         {
87             class => 'Koha::Patrons',
88             value => { branchcode => $library->branchcode }
89         }
90     );
91     my $e = $builder->build_object(
92         {
93             class => 'Koha::Club::Enrollments',
94             value => {
95                 club_id        => $club->id,
96                 borrowernumber => $patron->borrowernumber,
97                 date_canceled  => undef
98             }
99         }
100     );
101
102     my $club_hold = Koha::Club::Hold::add(
103         {
104             club_id           => $club->id,
105             biblio_id         => $item1->biblionumber,
106             pickup_library_id => $library->branchcode
107         }
108     );
109
110     is(blessed($club_hold), 'Koha::Club::Hold', 'add returns a Koha::Club::Hold');
111
112     $e->date_canceled(dt_from_string)->store;
113
114     throws_ok {
115         Koha::Club::Hold::add(
116             {
117                 club_id           => $club->id,
118                 biblio_id         => $item2->biblionumber,
119                 pickup_library_id => $library->branchcode
120             }
121         );
122     }
123     'Koha::Exceptions::ClubHold::NoPatrons',
124       'Exception thrown when no patron is enrolled in club';
125
126     my $patron_holds = Koha::Club::Hold::PatronHolds->search({ club_hold_id => $club_hold->id });
127
128     ok($patron_holds->count, "There must be at least one patron_hold");
129
130     my $patron_hold = $patron_holds->next;
131
132     my $hold = Koha::Holds->find($patron_hold->hold_id);
133
134     is($patron_hold->patron_id, $hold->borrowernumber, 'Patron must be the same');
135
136     $schema->storage->txn_rollback;
137 }