Bug 35353: Add REST API endpoint to retrieve old holds
[koha.git] / t / db_dependent / Koha / Old / Hold.t
1 #!/usr/bin/perl
2
3 # This file is part of Koha
4 #
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18 use Modern::Perl;
19
20 use Test::More tests => 3;
21 use Test::Exception;
22
23 use Koha::Database;
24
25 use t::lib::Mocks;
26 use t::lib::TestBuilder;
27
28 my $schema  = Koha::Database->new->schema;
29 my $builder = t::lib::TestBuilder->new;
30
31 subtest 'anonymize() tests' => sub {
32
33     plan tests => 8;
34
35     $schema->storage->txn_begin;
36
37     my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
38
39     is( $patron->old_holds->count, 0, 'Patron has no old holds' );
40
41     my $hold_1 = $builder->build_object(
42         {
43             class => 'Koha::Old::Holds',
44             value => { borrowernumber => $patron->id }
45         }
46     );
47     my $hold_2 = $builder->build_object(
48         {
49             class => 'Koha::Old::Holds',
50             value => { borrowernumber => $patron->id }
51         }
52     );
53
54     is( $patron->old_holds->count, 2, 'Patron has 2 completed holds' );
55
56     t::lib::Mocks::mock_preference( 'AnonymousPatron', undef );
57
58     throws_ok
59         { $hold_1->anonymize; }
60         'Koha::Exceptions::SysPref::NotSet',
61         'Exception thrown because AnonymousPatron not set';
62
63     is( $@->syspref, 'AnonymousPatron', 'syspref parameter is correctly passed' );
64     is( $patron->old_holds->count, 2, 'No changes, patron has 2 linked completed holds' );
65
66     is( $hold_1->borrowernumber, $patron->id,
67         'Anonymized hold not linked to patron' );
68     is( $hold_2->borrowernumber, $patron->id,
69         'Not anonymized hold still linked to patron' );
70
71     my $anonymous_patron =
72       $builder->build_object( { class => 'Koha::Patrons' } );
73     t::lib::Mocks::mock_preference( 'AnonymousPatron', $anonymous_patron->id );
74
75     # anonymize second hold
76     $hold_2->anonymize;
77     $hold_2->discard_changes;
78     is( $hold_2->borrowernumber, $anonymous_patron->id,
79         'Anonymized hold linked to anonymouspatron' );
80
81     $schema->storage->txn_rollback;
82 };
83
84 subtest 'biblio() tests' => sub {
85
86     plan tests => 3;
87
88     $schema->storage->txn_begin;
89
90     my $hold_1 = $builder->build_object(
91         {
92             class => 'Koha::Old::Holds',
93             value => { biblionumber => undef }
94         }
95     );
96
97     is( $hold_1->biblio, undef, 'Old hold has no biblionumber, returns undef' );
98
99     my $hold_2 = $builder->build_object(
100         {
101             class => 'Koha::Old::Holds',
102             value => { biblionumber => '' }
103         }
104     );
105
106     is( $hold_1->biblio, undef, 'Old hold has empty biblionumber, returns undef' );
107
108     my $biblio = $builder->build_object( { class => 'Koha::Biblios' } );
109
110     my $hold_3 = $builder->build_object(
111         {
112             class => 'Koha::Old::Holds',
113             value => { biblionumber => $biblio->biblionumber }
114         }
115     );
116
117     is_deeply( $hold_3->biblio->unblessed, $biblio->unblessed, 'Old hold has a biblionumber, returns a biblio object' );
118
119     $schema->storage->txn_rollback;
120 };
121
122 subtest 'item/pickup_library() tests' => sub {
123
124     plan tests => 4;
125
126     $schema->storage->txn_begin;
127
128     my $old_hold = $builder->build_object(
129         {
130             class => 'Koha::Old::Holds',
131         }
132     );
133     is( ref( $old_hold->item ),           'Koha::Item',    '->item should return a Koha::Item object' );
134     is( ref( $old_hold->pickup_library ), 'Koha::Library', '->pickup_library should return a Koha::Library object' );
135
136     $old_hold->item->delete;
137     $old_hold->pickup_library->delete;
138
139     $old_hold = $old_hold->get_from_storage;    # Be on the safe side
140
141     is( $old_hold->item,           undef, 'Item has been deleted, ->item should return undef' );
142     is( $old_hold->pickup_library, undef, 'Library has been deleted, ->pickup_library should return undef' );
143
144     $schema->storage->txn_rollback;
145 };