3 # Copyright 2022 Koha Development team
5 # This file is part of Koha
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.
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.
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>.
22 use Test::More tests => 1;
26 use t::lib::TestBuilder;
28 my $schema = Koha::Database->new->schema;
29 my $builder = t::lib::TestBuilder->new;
31 subtest 'disown_or_delete() tests' => sub {
35 subtest 'All set cases' => sub {
39 $schema->storage->txn_begin;
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' });
45 my $public_list = $builder->build_object(
47 class => "Koha::Virtualshelves",
48 value => { owner => $patron_1->id, public => 1 }
52 my $private_list = $builder->build_object(
54 class => "Koha::Virtualshelves",
55 value => { owner => $patron_1->id, public => 0 }
59 my $private_list_shared = $builder->build_object(
61 class => "Koha::Virtualshelves",
62 value => { owner => $patron_1->id, public => 0 }
67 $builder->build_object(
69 class => 'Koha::Virtualshelfshares',
70 value => { shelfnumber => $private_list_shared->id, invitekey => undef, borrowernumber => $patron_3->id }
74 t::lib::Mocks::mock_preference( 'ListOwnershipUponPatronDeletion', 'transfer' );
75 t::lib::Mocks::mock_preference( 'ListOwnerDesignated', $patron_2->id );
77 my $rs = Koha::Virtualshelves->search( { shelfnumber => [ $public_list->id, $private_list->id, $private_list_shared->id ] } );
79 my $result = $rs->disown_or_delete;
80 is( ref($result), 'Koha::Virtualshelves', 'Return type is correct' );
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 );
88 my $second = $rs->next;
89 is( $second->id, $private_list_shared->id );
90 is( $second->owner, $patron_2->id );
92 $schema->storage->txn_rollback;
95 subtest 'Fallback to userenv' => sub {
99 $schema->storage->txn_begin;
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' });
105 my $public_list = $builder->build_object(
107 class => "Koha::Virtualshelves",
108 value => { owner => $patron_1->id, public => 1 }
112 my $private_list = $builder->build_object(
114 class => "Koha::Virtualshelves",
115 value => { owner => $patron_1->id, public => 0 }
119 my $private_list_shared = $builder->build_object(
121 class => "Koha::Virtualshelves",
122 value => { owner => $patron_1->id, public => 0 }
127 $builder->build_object(
129 class => 'Koha::Virtualshelfshares',
130 value => { shelfnumber => $private_list_shared->id, invitekey => undef, borrowernumber => $patron_2->id }
134 t::lib::Mocks::mock_preference( 'ListOwnershipUponPatronDeletion', 'transfer' );
135 t::lib::Mocks::mock_preference( 'ListOwnerDesignated', undef );
137 my $public_list_to_delete = $builder->build_object(
139 class => "Koha::Virtualshelves",
140 value => { owner => $patron_1->id, public => 1 }
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' );
149 is( $rs->count, 0, 'ListOwnerDesignated and userenv not set yield deletion' );
151 t::lib::Mocks::mock_userenv({ patron => $patron_3 });
153 $rs = Koha::Virtualshelves->search( { shelfnumber => [ $public_list->id, $private_list->id, $private_list_shared->id ] } );
155 $rs->disown_or_delete;
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 );
163 my $second = $rs->next;
164 is( $second->id, $private_list_shared->id );
165 is( $second->owner, $patron_3->id );
167 $schema->storage->txn_rollback;
170 subtest 'ListOwnershipUponPatronDeletion set to delete' => sub {
174 $schema->storage->txn_begin;
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' });
180 my $public_list = $builder->build_object(
182 class => "Koha::Virtualshelves",
183 value => { owner => $patron_1->id, public => 1 }
187 my $private_list = $builder->build_object(
189 class => "Koha::Virtualshelves",
190 value => { owner => $patron_1->id, public => 0 }
194 my $private_list_shared = $builder->build_object(
196 class => "Koha::Virtualshelves",
197 value => { owner => $patron_1->id, public => 0 }
202 $builder->build_object(
204 class => 'Koha::Virtualshelfshares',
205 value => { shelfnumber => $private_list_shared->id, invitekey => undef, borrowernumber => $patron_2->id }
209 t::lib::Mocks::mock_preference( 'ListOwnershipUponPatronDeletion', 'delete' );
211 my $rs = Koha::Virtualshelves->search( { shelfnumber => [ $public_list->id, $private_list->id, $private_list_shared->id ] } );
213 my $result = $rs->disown_or_delete;
214 is( ref($result), 'Koha::Virtualshelves', 'Return type is correct' );
217 is( $rs->count, 0, 'All lists deleted' );
219 $schema->storage->txn_rollback;