Bug 25514: Try to fix random failure from REST/Plugin/Objects.t
[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     plan tests => 5;
40
41     $schema->storage->txn_begin;
42
43     my $club = $builder->build_object({ class => 'Koha::Clubs' });
44     my $library = $builder->build_object({ class => 'Koha::Libraries', value => { pickup_location => 1 } });
45     my $item1 = $builder->build_sample_item({ library => $library->branchcode });
46     my $item2 = $builder->build_sample_item({ library => $library->branchcode });
47
48     try {
49         Koha::Club::Hold::add({ club_id => $club->id, biblio_id => $item1->biblionumber, pickup_library_id => $library->branchcode });
50     } catch {
51         my $class = ref $_;
52         ok($class eq 'Koha::Exceptions::ClubHold::NoPatrons', 'Exception thrown when no patron is enrolled in club');
53     };
54
55     my $patron = $builder->build_object({ class => 'Koha::Patrons', value => { branchcode => $library->branchcode } });
56     my $e = $builder->build_object({ class => 'Koha::Club::Enrollments' , value => { club_id => $club->id, borrowernumber => $patron->borrowernumber, date_canceled => undef }} );
57
58     my $club_hold = Koha::Club::Hold::add({
59         club_id => $club->id,
60         biblio_id => $item1->biblionumber,
61         pickup_library_id => $library->branchcode
62     });
63
64     is(blessed($club_hold), 'Koha::Club::Hold', 'add returns a Koha::Club::Hold');
65
66     $e->date_canceled(dt_from_string)->store;
67
68     try {
69         Koha::Club::Hold::add({ club_id => $club->id, biblio_id => $item2->biblionumber, pickup_library_id => $library->branchcode });
70     } catch {
71         my $class = ref $_;
72         ok($class eq 'Koha::Exceptions::ClubHold::NoPatrons', 'Exception thrown when no patron is enrolled in club');
73     };
74
75     my $patron_holds = Koha::Club::Hold::PatronHolds->search({ club_hold_id => $club_hold->id });
76
77     ok($patron_holds->count, "There must be at least one patron_hold");
78
79     my $patron_hold = $patron_holds->next;
80
81     my $hold = Koha::Holds->find($patron_hold->hold_id);
82
83     is($patron_hold->patron_id, $hold->borrowernumber, 'Patron must be the same');
84
85     $schema->storage->txn_rollback;
86 }