Bug 27920: Add ability to update patron expiration dates when importing patrons
[koha.git] / t / db_dependent / Koha / Virtualshelves.t
1 #!/usr/bin/perl
2
3 # Copyright 2022 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
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 'disown_or_delete() tests' => sub {
32
33     plan tests => 3;
34
35     subtest 'All set cases' => sub {
36
37         plan tests => 6;
38
39         $schema->storage->txn_begin;
40
41         my $patron_1 = $builder->build_object({ class => 'Koha::Patrons' });
42         my $patron_2 = $builder->build_object({ class => 'Koha::Patrons' });
43         my $patron_3 = $builder->build_object({ class => 'Koha::Patrons' });
44
45         my $public_list = $builder->build_object(
46             {
47                 class => "Koha::Virtualshelves",
48                 value => { owner => $patron_1->id, public => 1 }
49             }
50         );
51
52         my $private_list = $builder->build_object(
53             {
54                 class => "Koha::Virtualshelves",
55                 value => { owner => $patron_1->id, public => 0 }
56             }
57         );
58
59         my $private_list_shared = $builder->build_object(
60             {
61                 class => "Koha::Virtualshelves",
62                 value => { owner => $patron_1->id, public => 0 }
63             }
64         );
65
66         # add share
67         $builder->build_object(
68             {
69                 class => 'Koha::Virtualshelfshares',
70                 value => { shelfnumber => $private_list_shared->id, invitekey => undef, borrowernumber => $patron_3->id }
71             }
72         );
73
74         t::lib::Mocks::mock_preference( 'ListOwnershipUponPatronDeletion', 'transfer' );
75         t::lib::Mocks::mock_preference( 'ListOwnerDesignated', $patron_2->id );
76
77         my $rs = Koha::Virtualshelves->search( { shelfnumber => [ $public_list->id, $private_list->id, $private_list_shared->id ] } );
78
79         my $result = $rs->disown_or_delete;
80         is( ref($result), 'Koha::Virtualshelves', 'Return type is correct' );
81         $rs->reset;
82
83         is( $rs->count, 2, 'The private/non-shared list was deleted' );
84         my $first = $rs->next;
85         is( $first->id, $public_list->id );
86         is( $first->owner, $patron_2->id );
87
88         my $second = $rs->next;
89         is( $second->id, $private_list_shared->id );
90         is( $second->owner, $patron_2->id );
91
92         $schema->storage->txn_rollback;
93     };
94
95     subtest 'Fallback to userenv' => sub {
96
97         plan tests => 7;
98
99         $schema->storage->txn_begin;
100
101         my $patron_1 = $builder->build_object({ class => 'Koha::Patrons' });
102         my $patron_2 = $builder->build_object({ class => 'Koha::Patrons' });
103         my $patron_3 = $builder->build_object({ class => 'Koha::Patrons' });
104
105         my $public_list = $builder->build_object(
106             {
107                 class => "Koha::Virtualshelves",
108                 value => { owner => $patron_1->id, public => 1 }
109             }
110         );
111
112         my $private_list = $builder->build_object(
113             {
114                 class => "Koha::Virtualshelves",
115                 value => { owner => $patron_1->id, public => 0 }
116             }
117         );
118
119         my $private_list_shared = $builder->build_object(
120             {
121                 class => "Koha::Virtualshelves",
122                 value => { owner => $patron_1->id, public => 0 }
123             }
124         );
125
126         # add share
127         $builder->build_object(
128             {
129                 class => 'Koha::Virtualshelfshares',
130                 value => { shelfnumber => $private_list_shared->id, invitekey => undef, borrowernumber => $patron_2->id }
131             }
132         );
133
134         t::lib::Mocks::mock_preference( 'ListOwnershipUponPatronDeletion', 'transfer' );
135         t::lib::Mocks::mock_preference( 'ListOwnerDesignated', undef );
136
137         my $public_list_to_delete = $builder->build_object(
138             {
139                 class => "Koha::Virtualshelves",
140                 value => { owner => $patron_1->id, public => 1 }
141             }
142         );
143
144         my $rs = Koha::Virtualshelves->search({ shelfnumber => $public_list_to_delete->id });
145         my $result = $rs->disown_or_delete;
146         is( ref($result), 'Koha::Virtualshelves', 'Return type is correct' );
147         $rs->reset;
148
149         is( $rs->count, 0, 'ListOwnerDesignated and userenv not set yield deletion' );
150
151         t::lib::Mocks::mock_userenv({ patron => $patron_3 });
152
153         $rs = Koha::Virtualshelves->search( { shelfnumber => [ $public_list->id, $private_list->id, $private_list_shared->id ] } );
154
155         $rs->disown_or_delete;
156         $rs->reset;
157
158         is( $rs->count, 2, 'The private/non-shared list was deleted' );
159         my $first = $rs->next;
160         is( $first->id, $public_list->id );
161         is( $first->owner, $patron_3->id );
162
163         my $second = $rs->next;
164         is( $second->id, $private_list_shared->id );
165         is( $second->owner, $patron_3->id );
166
167         $schema->storage->txn_rollback;
168     };
169
170     subtest 'ListOwnershipUponPatronDeletion set to delete' => sub {
171
172         plan tests => 2;
173
174         $schema->storage->txn_begin;
175
176         my $patron_1 = $builder->build_object({ class => 'Koha::Patrons' });
177         my $patron_2 = $builder->build_object({ class => 'Koha::Patrons' });
178         my $patron_3 = $builder->build_object({ class => 'Koha::Patrons' });
179
180         my $public_list = $builder->build_object(
181             {
182                 class => "Koha::Virtualshelves",
183                 value => { owner => $patron_1->id, public => 1 }
184             }
185         );
186
187         my $private_list = $builder->build_object(
188             {
189                 class => "Koha::Virtualshelves",
190                 value => { owner => $patron_1->id, public => 0 }
191             }
192         );
193
194         my $private_list_shared = $builder->build_object(
195             {
196                 class => "Koha::Virtualshelves",
197                 value => { owner => $patron_1->id, public => 0 }
198             }
199         );
200
201         # add share
202         $builder->build_object(
203             {
204                 class => 'Koha::Virtualshelfshares',
205                 value => { shelfnumber => $private_list_shared->id, invitekey => undef, borrowernumber => $patron_2->id }
206             }
207         );
208
209         t::lib::Mocks::mock_preference( 'ListOwnershipUponPatronDeletion', 'delete' );
210
211         my $rs = Koha::Virtualshelves->search( { shelfnumber => [ $public_list->id, $private_list->id, $private_list_shared->id ] } );
212
213         my $result = $rs->disown_or_delete;
214         is( ref($result), 'Koha::Virtualshelves', 'Return type is correct' );
215         $rs->reset;
216
217         is( $rs->count, 0, 'All lists deleted' );
218
219         $schema->storage->txn_rollback;
220     };
221 };